Skip to content

Commit 21be735

Browse files
committed
Check for tuple in the __get__ of the new descriptor and don't cache the descriptor itself
1 parent 631ab2c commit 21be735

File tree

2 files changed

+7
-4
lines changed

2 files changed

+7
-4
lines changed

‎Lib/collections/__init__.py‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -459,13 +459,12 @@ def __getnewargs__(self):
459459
cache = _nt_itemgetters
460460
for index, name in enumerate(field_names):
461461
try:
462-
tuplegetter_object, doc = cache[index]
462+
doc = cache[index]
463463
except KeyError:
464464
doc = f'Alias for field number {index}'
465-
tuplegetter_object = _tuplegetter(index, doc=doc)
466-
cache[index] = tuplegetter_object, doc
465+
cache[index] = doc
467466

468-
tuplegetter_object.__doc__ = doc
467+
tuplegetter_object = _tuplegetter(index, doc=doc)
469468
class_namespace[name] = tuplegetter_object
470469

471470
result = type(typename, (tuple,), class_namespace)

‎Modules/_collectionsmodule.c‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2367,6 +2367,10 @@ tuplegetterdescr_get(PyObject *self, PyObject *obj, PyObject *type)
23672367
Py_INCREF(self);
23682368
return self;
23692369
}
2370+
if (!PyTuple_Check(obj)){
2371+
PyErr_SetString(PyExc_TypeError, "_tuplegetter must be used with tuples");
2372+
return NULL;
2373+
}
23702374
result = PyTuple_GetItem(obj, ((_tuplegetterobject*)self)->index);
23712375
Py_XINCREF(result);
23722376
return result;

0 commit comments

Comments
 (0)