Use psycopg2cffi module instead of psycopg2 if running on pypy

The psycopg2 package isn't available for PyPy.  This commit adds a check
if the runtime is PyPy, and if it is uses psycopg2cffi module in favor
of psycopg2. This is almost a drop-in replacement, except for one place
where an additional cast to string is required.
This commit is contained in:
Vincent Breitmoser 2018-01-06 18:13:56 +01:00
parent 9fbe70a7dc
commit 89de934981
2 changed files with 8 additions and 2 deletions

View File

@ -376,7 +376,7 @@ class SQLBaseStore(object):
Returns: Returns:
A list of dicts where the key is the column header. A list of dicts where the key is the column header.
""" """
col_headers = list(intern(column[0]) for column in cursor.description) col_headers = list(intern(str(column[0])) for column in cursor.description)
results = list( results = list(
dict(zip(col_headers, row)) for row in cursor dict(zip(col_headers, row)) for row in cursor
) )

View File

@ -18,6 +18,7 @@ from .postgres import PostgresEngine
from .sqlite3 import Sqlite3Engine from .sqlite3 import Sqlite3Engine
import importlib import importlib
import platform
SUPPORTED_MODULE = { SUPPORTED_MODULE = {
@ -31,7 +32,12 @@ def create_engine(database_config):
engine_class = SUPPORTED_MODULE.get(name, None) engine_class = SUPPORTED_MODULE.get(name, None)
if engine_class: if engine_class:
module = importlib.import_module(name) needs_pypy_hack = (name == "psycopg2" and
platform.python_implementation() == "PyPy")
if needs_pypy_hack:
module = importlib.import_module("psycopg2cffi")
else:
module = importlib.import_module(name)
return engine_class(module, database_config) return engine_class(module, database_config)
raise RuntimeError( raise RuntimeError(