bpo-32492: Tweak _collections._tuplegetter. (GH-11367)

* Replace the docstrings cache with sys.intern().
* Improve tests.
* Unify names of tp_descr_get and tp_descr_set functions.
This commit is contained in:
Serhiy Storchaka 2018-12-31 14:15:16 +02:00 committed by GitHub
parent 5c117dd227
commit 052b2dfdc9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 75 additions and 37 deletions

View file

@ -316,8 +316,6 @@ try:
except ImportError:
_tuplegetter = lambda index, doc: property(_itemgetter(index), doc=doc)
_nt_itemgetters = {}
def namedtuple(typename, field_names, *, rename=False, defaults=None, module=None):
"""Returns a new subclass of tuple with named fields.
@ -456,16 +454,9 @@ def namedtuple(typename, field_names, *, rename=False, defaults=None, module=Non
'_asdict': _asdict,
'__getnewargs__': __getnewargs__,
}
cache = _nt_itemgetters
for index, name in enumerate(field_names):
try:
doc = cache[index]
except KeyError:
doc = f'Alias for field number {index}'
cache[index] = doc
tuplegetter_object = _tuplegetter(index, doc)
class_namespace[name] = tuplegetter_object
doc = _sys.intern(f'Alias for field number {index}')
class_namespace[name] = _tuplegetter(index, doc)
result = type(typename, (tuple,), class_namespace)