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,10 +427,7 @@ class MinTimeLoggingCursor(LoggingCursor):
# a dbtype and adapter for Python UUID type
try:
import uuid
class UUID_adapter(object):
class UUID_adapter(object):
"""Adapt Python's uuid.UUID__ type to PostgreSQL's uuid__.
.. __: http://docs.python.org/library/uuid.html
@ -448,8 +445,11 @@ try:
__str__ = getquoted
def register_uuid(oids=None, conn_or_curs=None):
def register_uuid(oids=None, conn_or_curs=None):
"""Create the UUID type and an uuid.UUID adapter."""
import uuid
if not oids:
oid1 = 2950
oid2 = 2951
@ -478,15 +478,6 @@ try:
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