summaryrefslogtreecommitdiff
path: root/Lib/importlib/_collections.py
diff options
context:
space:
mode:
authorJason R. Coombs <jaraco@jaraco.com>2021-04-24 10:13:51 -0400
committerGitHub <noreply@github.com>2021-04-24 10:13:51 -0400
commitc6ca368867bd68d44f333df840aa85d425a51410 (patch)
tree74312cafb5adb28257beed73ffdcb3bbc285807c /Lib/importlib/_collections.py
parentce9a0643496ba802ea97a3da20eace3a1117ea48 (diff)
downloadcpython-git-c6ca368867bd68d44f333df840aa85d425a51410.tar.gz
bpo-43780: Sync with importlib_metadata 3.10 (GH-25297)
* bpo-43780: Sync with importlib_metadata 3.10. * Add blurb * Apply changes from importlib_metadata 3.10.1.
Diffstat (limited to 'Lib/importlib/_collections.py')
-rw-r--r--Lib/importlib/_collections.py30
1 files changed, 30 insertions, 0 deletions
diff --git a/Lib/importlib/_collections.py b/Lib/importlib/_collections.py
new file mode 100644
index 0000000000..cf0954e1a3
--- /dev/null
+++ b/Lib/importlib/_collections.py
@@ -0,0 +1,30 @@
+import collections
+
+
+# from jaraco.collections 3.3
+class FreezableDefaultDict(collections.defaultdict):
+ """
+ Often it is desirable to prevent the mutation of
+ a default dict after its initial construction, such
+ as to prevent mutation during iteration.
+
+ >>> dd = FreezableDefaultDict(list)
+ >>> dd[0].append('1')
+ >>> dd.freeze()
+ >>> dd[1]
+ []
+ >>> len(dd)
+ 1
+ """
+
+ def __missing__(self, key):
+ return getattr(self, '_frozen', super().__missing__)(key)
+
+ def freeze(self):
+ self._frozen = lambda key: self.default_factory()
+
+
+class Pair(collections.namedtuple('Pair', 'name value')):
+ @classmethod
+ def parse(cls, text):
+ return cls(*map(str.strip, text.split("=", 1)))