]> git.ipfire.org Git - thirdparty/psycopg.git/commitdiff
Added guard for lazy import to avoid the import statement
authorDaniele Varrazzo <daniele.varrazzo@gmail.com>
Wed, 30 Dec 2020 19:30:59 +0000 (20:30 +0100)
committerDaniele Varrazzo <daniele.varrazzo@gmail.com>
Fri, 8 Jan 2021 01:26:53 +0000 (02:26 +0100)
I'm mot so sure it's always so fast to re-import.

psycopg3/psycopg3/types/network.py
psycopg3/psycopg3/types/uuid.py

index 85c34bc299e422bb60cc318dc7bb22ff8dbcf3e8..516508a6dc1bd25ae4e453c00e6192f12a46df1a 100644 (file)
@@ -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)
index 4dc284d838949ea0ee191eff6985fd64509e870a..99fc7c3d49a72bd66c1deea31c0cac5005ea1570 100644 (file)
@@ -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"))