mirror of
https://github.com/psycopg/psycopg2.git
synced 2025-02-12 07:10:33 +03:00
lazy import for uuid module
Attached patch moves uuid import from inside try-except to register_uuid function. Reason: uuid module import is *very* heavy. It goes into OS searching for various .dll/.so libraries, lauches 'ldconfig' and so on... With this patch, 200x python -c 'import psycopg2.extras' goes from 22s to 7s. (plain 'import psycopg2' is 6s) -- marko
This commit is contained in:
parent
f8a5dabdc1
commit
cb1d163f4f
|
@ -427,65 +427,56 @@ class MinTimeLoggingCursor(LoggingCursor):
|
||||||
|
|
||||||
# a dbtype and adapter for Python UUID type
|
# a dbtype and adapter for Python UUID type
|
||||||
|
|
||||||
try:
|
class UUID_adapter(object):
|
||||||
|
"""Adapt Python's uuid.UUID__ type to PostgreSQL's uuid__.
|
||||||
|
|
||||||
|
.. __: http://docs.python.org/library/uuid.html
|
||||||
|
.. __: http://www.postgresql.org/docs/8.4/static/datatype-uuid.html
|
||||||
|
"""
|
||||||
|
|
||||||
|
def __init__(self, uuid):
|
||||||
|
self._uuid = uuid
|
||||||
|
|
||||||
|
def prepare(self, conn):
|
||||||
|
pass
|
||||||
|
|
||||||
|
def getquoted(self):
|
||||||
|
return "'"+str(self._uuid)+"'::uuid"
|
||||||
|
|
||||||
|
__str__ = getquoted
|
||||||
|
|
||||||
|
def register_uuid(oids=None, conn_or_curs=None):
|
||||||
|
"""Create the UUID type and an uuid.UUID adapter."""
|
||||||
|
|
||||||
import uuid
|
import uuid
|
||||||
|
|
||||||
class UUID_adapter(object):
|
if not oids:
|
||||||
"""Adapt Python's uuid.UUID__ type to PostgreSQL's uuid__.
|
oid1 = 2950
|
||||||
|
oid2 = 2951
|
||||||
|
elif type(oids) == list:
|
||||||
|
oid1, oid2 = oids
|
||||||
|
else:
|
||||||
|
oid1 = oids
|
||||||
|
oid2 = 2951
|
||||||
|
|
||||||
.. __: http://docs.python.org/library/uuid.html
|
def parseUUIDARRAY(data, cursor):
|
||||||
.. __: http://www.postgresql.org/docs/8.4/static/datatype-uuid.html
|
if data is None:
|
||||||
"""
|
return None
|
||||||
|
elif data == '{}':
|
||||||
def __init__(self, uuid):
|
return []
|
||||||
self._uuid = uuid
|
|
||||||
|
|
||||||
def prepare(self, conn):
|
|
||||||
pass
|
|
||||||
|
|
||||||
def getquoted(self):
|
|
||||||
return "'"+str(self._uuid)+"'::uuid"
|
|
||||||
|
|
||||||
__str__ = getquoted
|
|
||||||
|
|
||||||
def register_uuid(oids=None, conn_or_curs=None):
|
|
||||||
"""Create the UUID type and an uuid.UUID adapter."""
|
|
||||||
if not oids:
|
|
||||||
oid1 = 2950
|
|
||||||
oid2 = 2951
|
|
||||||
elif type(oids) == list:
|
|
||||||
oid1, oid2 = oids
|
|
||||||
else:
|
else:
|
||||||
oid1 = oids
|
return [((len(x) > 0 and x != 'NULL') and uuid.UUID(x) or None)
|
||||||
oid2 = 2951
|
for x in data[1:-1].split(',')]
|
||||||
|
|
||||||
def parseUUIDARRAY(data, cursor):
|
_ext.UUID = _ext.new_type((oid1, ), "UUID",
|
||||||
if data is None:
|
lambda data, cursor: data and uuid.UUID(data) or None)
|
||||||
return None
|
_ext.UUIDARRAY = _ext.new_type((oid2,), "UUID[]", parseUUIDARRAY)
|
||||||
elif data == '{}':
|
|
||||||
return []
|
|
||||||
else:
|
|
||||||
return [((len(x) > 0 and x != 'NULL') and uuid.UUID(x) or None)
|
|
||||||
for x in data[1:-1].split(',')]
|
|
||||||
|
|
||||||
_ext.UUID = _ext.new_type((oid1, ), "UUID",
|
_ext.register_type(_ext.UUID, conn_or_curs)
|
||||||
lambda data, cursor: data and uuid.UUID(data) or None)
|
_ext.register_type(_ext.UUIDARRAY, conn_or_curs)
|
||||||
_ext.UUIDARRAY = _ext.new_type((oid2,), "UUID[]", parseUUIDARRAY)
|
_ext.register_adapter(uuid.UUID, UUID_adapter)
|
||||||
|
|
||||||
_ext.register_type(_ext.UUID, conn_or_curs)
|
return _ext.UUID
|
||||||
_ext.register_type(_ext.UUIDARRAY, conn_or_curs)
|
|
||||||
_ext.register_adapter(uuid.UUID, UUID_adapter)
|
|
||||||
|
|
||||||
return _ext.UUID
|
|
||||||
|
|
||||||
except ImportError, e:
|
|
||||||
def register_uuid(oid=None):
|
|
||||||
"""Create the UUID type and an uuid.UUID adapter.
|
|
||||||
|
|
||||||
This is a fake function that will always raise an error because the
|
|
||||||
import of the uuid module failed.
|
|
||||||
"""
|
|
||||||
raise e
|
|
||||||
|
|
||||||
|
|
||||||
# a type, dbtype and adapter for PostgreSQL inet type
|
# a type, dbtype and adapter for PostgreSQL inet type
|
||||||
|
|
Loading…
Reference in New Issue
Block a user