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:
Marko Kreen 2011-06-28 18:12:43 +03:00 committed by Federico Di Gregorio
parent f8a5dabdc1
commit cb1d163f4f

View File

@ -427,65 +427,56 @@ class MinTimeLoggingCursor(LoggingCursor):
# 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
class UUID_adapter(object):
"""Adapt Python's uuid.UUID__ type to PostgreSQL's uuid__.
if not oids:
oid1 = 2950
oid2 = 2951
elif type(oids) == list:
oid1, oid2 = oids
else:
oid1 = oids
oid2 = 2951
.. __: 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."""
if not oids:
oid1 = 2950
oid2 = 2951
elif type(oids) == list:
oid1, oid2 = oids
def parseUUIDARRAY(data, cursor):
if data is None:
return None
elif data == '{}':
return []
else:
oid1 = oids
oid2 = 2951
return [((len(x) > 0 and x != 'NULL') and uuid.UUID(x) or None)
for x in data[1:-1].split(',')]
def parseUUIDARRAY(data, cursor):
if data is None:
return None
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",
lambda data, cursor: data and uuid.UUID(data) or None)
_ext.UUIDARRAY = _ext.new_type((oid2,), "UUID[]", parseUUIDARRAY)
_ext.UUID = _ext.new_type((oid1, ), "UUID",
lambda data, cursor: data and uuid.UUID(data) or None)
_ext.UUIDARRAY = _ext.new_type((oid2,), "UUID[]", parseUUIDARRAY)
_ext.register_type(_ext.UUID, conn_or_curs)
_ext.register_type(_ext.UUIDARRAY, conn_or_curs)
_ext.register_adapter(uuid.UUID, UUID_adapter)
_ext.register_type(_ext.UUID, conn_or_curs)
_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
return _ext.UUID
# a type, dbtype and adapter for PostgreSQL inet type