Skip to content

Commit ff9f244

Browse files
author
jomae
committed
1.6.1dev: use psycopg2.extensions.libpq_version() to retrieve the client'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
1 parent abf33d3 commit ff9f244

File tree

1 file changed

+38
-37
lines changed

1 file changed

+38
-37
lines changed

trac/db/postgres_backend.py

Lines changed: 38 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -44,32 +44,7 @@
4444
register_type(UNICODE)
4545
register_adapter(Markup, lambda markup: QuotedString(str(markup)))
4646
register_adapter(type(empty), lambda empty: AsIs("''"))
47-
psycopg2_version = get_pkginfo(psycopg).get('version',
48-
psycopg.__version__)
49-
_libpq_pathname = None
50-
if not hasattr(psycopg, 'libpq_version'):
51-
# search path of libpq only if it is dynamically linked
52-
_f = _match = None
53-
try:
54-
with open(psycopg._psycopg.__file__, 'rb') as _f:
55-
if os.name != 'nt':
56-
_match = re.search(
57-
r'''
58-
\0(
59-
(?:/[^/\0]+)*/?
60-
libpq\.(?:so\.[0-9]+|[0-9]+\.dylib)
61-
)\0
62-
'''.encode('utf-8'),
63-
_f.read(), re.VERBOSE)
64-
if _match:
65-
_libpq_pathname = _match.group(1).decode('utf-8')
66-
else:
67-
if re.search(r'\0libpq\.dll\0'.encode('utf-8'), _f.read(),
68-
re.IGNORECASE):
69-
_libpq_pathname = find_library('libpq')
70-
except AttributeError:
71-
pass
72-
del _f, _match
47+
psycopg2_version = get_pkginfo(psycopg).get('version', psycopg.__version__)
7348

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

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

9671

72+
def _get_client_version():
73+
if hasattr(psycopg2.extensions, 'libpq_version'): # psycopg2 2.7+
74+
return psycopg2.extensions.libpq_version()
75+
76+
if hasattr(psycopg, 'libpq_version'):
77+
return psycopg.libpq_version()
78+
79+
# search path of libpq only if it is dynamically linked
80+
libpq_path = None
81+
with open(psycopg._psycopg.__file__, 'rb') as f:
82+
data = f.read()
83+
if os.name != 'nt':
84+
match = re.search(
85+
br'''
86+
\0(
87+
(?:/[^/\0]+)*/?
88+
libpq\.(?:so\.[0-9]+|[0-9]+\.dylib)
89+
)\0
90+
''',
91+
data, re.VERBOSE)
92+
if match:
93+
libpq_path = str(match.group(1), 'utf-8')
94+
else:
95+
if re.search(br'\0libpq\.dll\0', data, re.IGNORECASE):
96+
libpq_path = find_library('libpq')
97+
if libpq_path:
98+
lib = ctypes.CDLL(libpq_path)
99+
return lib.PQlibVersion()
100+
101+
97102
def _quote(identifier):
98103
return '"%s"' % identifier.replace('"', '""')
99104

@@ -291,17 +296,13 @@ def get_system_info(self):
291296

292297
@lazy
293298
def _client_version(self):
294-
version = None
295-
if hasattr(psycopg, 'libpq_version'):
296-
version = psycopg.libpq_version()
297-
elif _libpq_pathname:
298-
try:
299-
lib = ctypes.CDLL(_libpq_pathname)
300-
version = lib.PQlibVersion()
301-
except Exception as e:
302-
self.log.warning("Exception caught while retrieving libpq's "
303-
"version%s",
304-
exception_to_unicode(e, traceback=True))
299+
try:
300+
version = _get_client_version()
301+
except Exception as e:
302+
self.log.warning("Exception caught while retrieving libpq's "
303+
"version%s",
304+
exception_to_unicode(e, traceback=True))
305+
version = None
305306
return _version_tuple(version)
306307

307308
def _pgdump_version(self):

0 commit comments

Comments
 (0)