Skip to content

Commit

Permalink
1.6.1dev: use psycopg2.extensions.libpq_version() to retrieve the c…
Browse files Browse the repository at this point in the history
…lient's version if available (refs #13803)

git-svn-id: http://trac.edgewall.org/intertrac/log:/branches/1.6-stable@17862 af82e41b-90c4-0310-8c96-b1721e28e2e2
  • Loading branch information
jomae committed Oct 27, 2024
1 parent abf33d3 commit ff9f244
Showing 1 changed file with 38 additions and 37 deletions.
75 changes: 38 additions & 37 deletions trac/db/postgres_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,32 +44,7 @@
register_type(UNICODE)
register_adapter(Markup, lambda markup: QuotedString(str(markup)))
register_adapter(type(empty), lambda empty: AsIs("''"))
psycopg2_version = get_pkginfo(psycopg).get('version',
psycopg.__version__)
_libpq_pathname = None
if not hasattr(psycopg, 'libpq_version'):
# search path of libpq only if it is dynamically linked
_f = _match = None
try:
with open(psycopg._psycopg.__file__, 'rb') as _f:
if os.name != 'nt':
_match = re.search(
r'''
\0(
(?:/[^/\0]+)*/?
libpq\.(?:so\.[0-9]+|[0-9]+\.dylib)
)\0
'''.encode('utf-8'),
_f.read(), re.VERBOSE)
if _match:
_libpq_pathname = _match.group(1).decode('utf-8')
else:
if re.search(r'\0libpq\.dll\0'.encode('utf-8'), _f.read(),
re.IGNORECASE):
_libpq_pathname = find_library('libpq')
except AttributeError:
pass
del _f, _match
psycopg2_version = get_pkginfo(psycopg).get('version', psycopg.__version__)

_like_escape_re = re.compile(r'([/_%])')

Expand All @@ -94,6 +69,36 @@ def quote(value):
for name, value in dsn.items() if value)


def _get_client_version():
if hasattr(psycopg2.extensions, 'libpq_version'): # psycopg2 2.7+
return psycopg2.extensions.libpq_version()

if hasattr(psycopg, 'libpq_version'):
return psycopg.libpq_version()

# search path of libpq only if it is dynamically linked
libpq_path = None
with open(psycopg._psycopg.__file__, 'rb') as f:
data = f.read()
if os.name != 'nt':
match = re.search(
br'''
\0(
(?:/[^/\0]+)*/?
libpq\.(?:so\.[0-9]+|[0-9]+\.dylib)
)\0
''',
data, re.VERBOSE)
if match:
libpq_path = str(match.group(1), 'utf-8')
else:
if re.search(br'\0libpq\.dll\0', data, re.IGNORECASE):
libpq_path = find_library('libpq')
if libpq_path:
lib = ctypes.CDLL(libpq_path)
return lib.PQlibVersion()


def _quote(identifier):
return '"%s"' % identifier.replace('"', '""')

Expand Down Expand Up @@ -291,17 +296,13 @@ def get_system_info(self):

@lazy
def _client_version(self):
version = None
if hasattr(psycopg, 'libpq_version'):
version = psycopg.libpq_version()
elif _libpq_pathname:
try:
lib = ctypes.CDLL(_libpq_pathname)
version = lib.PQlibVersion()
except Exception as e:
self.log.warning("Exception caught while retrieving libpq's "
"version%s",
exception_to_unicode(e, traceback=True))
try:
version = _get_client_version()
except Exception as e:
self.log.warning("Exception caught while retrieving libpq's "
"version%s",
exception_to_unicode(e, traceback=True))
version = None
return _version_tuple(version)

def _pgdump_version(self):
Expand Down

0 comments on commit ff9f244

Please sign in to comment.