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