From: Daniele Varrazzo Date: Wed, 30 Dec 2020 19:30:59 +0000 (+0100) Subject: Added guard for lazy import to avoid the import statement X-Git-Tag: 3.0.dev0~219 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=54cb6475d0225386c0ae01b1b418d704183bd0eb;p=thirdparty%2Fpsycopg.git Added guard for lazy import to avoid the import statement I'm mot so sure it's always so fast to re-import. --- diff --git a/psycopg3/psycopg3/types/network.py b/psycopg3/psycopg3/types/network.py index 85c34bc29..516508a6d 100644 --- a/psycopg3/psycopg3/types/network.py +++ b/psycopg3/psycopg3/types/network.py @@ -18,6 +18,7 @@ Interface = Union["ipaddress.IPv4Interface", "ipaddress.IPv6Interface"] Network = Union["ipaddress.IPv4Network", "ipaddress.IPv6Network"] # These functions will be imported lazily +imported = False ip_address: Callable[[str], Address] ip_interface: Callable[[str], Interface] ip_network: Callable[[str], Network] @@ -50,8 +51,11 @@ class NetworkDumper(Dumper): class _LazyIpaddress(Loader): def __init__(self, oid: int, context: Optional[AdaptContext] = None): super().__init__(oid, context) - global ip_address, ip_interface, ip_network - from ipaddress import ip_address, ip_interface, ip_network + global imported, ip_address, ip_interface, ip_network + if not imported: + from ipaddress import ip_address, ip_interface, ip_network + + imported = True @Loader.text(builtins["inet"].oid) diff --git a/psycopg3/psycopg3/types/uuid.py b/psycopg3/psycopg3/types/uuid.py index 4dc284d83..99fc7c3d4 100644 --- a/psycopg3/psycopg3/types/uuid.py +++ b/psycopg3/psycopg3/types/uuid.py @@ -14,6 +14,7 @@ if TYPE_CHECKING: import uuid # Importing the uuid module is slow, so import it only on request. +imported = False UUID: Callable[..., "uuid.UUID"] @@ -43,8 +44,11 @@ class UUIDLoader(Loader): def __init__(self, oid: int, context: Optional[AdaptContext] = None): super().__init__(oid, context) - global UUID - from uuid import UUID + global imported, UUID + if not imported: + from uuid import UUID + + imported = True def load(self, data: bytes) -> "uuid.UUID": return UUID(data.decode("utf8"))