Skip to content

Commit 4c3a0a3

Browse files
committed
More on SF bug [#460020] bug or feature: unicode() and subclasses.
tuple(i) repaired to return a true tuple when i is an instance of a tuple subclass. Added PyTuple_CheckExact macro. PySequence_Tuple(): if a tuple-like object isn't exactly a tuple, it's not safe to return the object as-is -- make a new tuple of it instead.
1 parent caaff8d commit 4c3a0a3

File tree

3 files changed

+7
-2
lines changed

3 files changed

+7
-2
lines changed

Include/tupleobject.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ typedef struct {
2727
extern DL_IMPORT(PyTypeObject) PyTuple_Type;
2828

2929
#define PyTuple_Check(op) PyObject_TypeCheck(op, &PyTuple_Type)
30+
#define PyTuple_CheckExact(op) ((op)->ob_type == &PyTuple_Type)
3031

3132
extern DL_IMPORT(PyObject *) PyTuple_New(int size);
3233
extern DL_IMPORT(int) PyTuple_Size(PyObject *);

Lib/test/test_descr.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1416,7 +1416,7 @@ def rev(self):
14161416
verify(v == t)
14171417
a = madtuple((1,2,3,4,5))
14181418
verify(tuple(a) == (1,2,3,4,5))
1419-
#XXX verify(tuple(a).__class__ is tuple)
1419+
verify(tuple(a).__class__ is tuple)
14201420
a = madtuple(())
14211421
verify(tuple(a) == ())
14221422
#XXX verify(tuple(a).__class__ is tuple)

Objects/abstract.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1235,7 +1235,11 @@ PySequence_Tuple(PyObject *v)
12351235
return null_error();
12361236

12371237
/* Special-case the common tuple and list cases, for efficiency. */
1238-
if (PyTuple_Check(v)) {
1238+
if (PyTuple_CheckExact(v)) {
1239+
/* Note that we can't know whether it's safe to return
1240+
a tuple *subclass* instance as-is, hence the restriction
1241+
to exact tuples here. In contrasts, lists always make
1242+
a copy, so there's need for exactness below. */
12391243
Py_INCREF(v);
12401244
return v;
12411245
}

0 commit comments

Comments
 (0)