]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Speed-up lazy heapq import in collections (gh-127538)
authorRaymond Hettinger <rhettinger@users.noreply.github.com>
Tue, 3 Dec 2024 02:45:36 +0000 (20:45 -0600)
committerGitHub <noreply@github.com>
Tue, 3 Dec 2024 02:45:36 +0000 (20:45 -0600)
Lib/collections/__init__.py

index d688141f9b183d388b454367e92556a3fb2db8d1..78229ac54b80dac71e5ffee4bcee452ec32e8626 100644 (file)
@@ -59,6 +59,8 @@ try:
 except ImportError:
     pass
 
+heapq = None  # Lazily imported
+
 
 ################################################################################
 ### OrderedDict
@@ -633,7 +635,10 @@ class Counter(dict):
             return sorted(self.items(), key=_itemgetter(1), reverse=True)
 
         # Lazy import to speedup Python startup time
-        import heapq
+        global heapq
+        if heapq is None:
+            import heapq
+
         return heapq.nlargest(n, self.items(), key=_itemgetter(1))
 
     def elements(self):