Commit 71436f02 authored by Žiga Seilnacht's avatar Žiga Seilnacht

Patch #1680015: Don't modify __slots__ tuple if it contains an unicode

name. Remove a reference leak that happened if the name could not be
converted to string. Will backport.
parent f66b6039
...@@ -1226,6 +1226,29 @@ def slots(): ...@@ -1226,6 +1226,29 @@ def slots():
class C(object): class C(object):
__slots__ = ["a", "a_b", "_a", "A0123456789Z"] __slots__ = ["a", "a_b", "_a", "A0123456789Z"]
# Test unicode slot names
try:
unichr
except NameError:
pass
else:
# _unicode_to_string used to modify slots in certain circumstances
slots = (unicode("foo"), unicode("bar"))
class C(object):
__slots__ = slots
x = C()
x.foo = 5
vereq(x.foo, 5)
veris(type(slots[0]), unicode)
# this used to leak references
try:
class C(object):
__slots__ = [unichr(128)]
except (TypeError, UnicodeEncodeError):
pass
else:
raise TestFailed, "[unichr(128)] slots not caught"
# Test leaks # Test leaks
class Counted(object): class Counted(object):
counter = 0 # counts the number of instances alive counter = 0 # counts the number of instances alive
......
...@@ -12,6 +12,9 @@ What's New in Python 2.6 alpha 1? ...@@ -12,6 +12,9 @@ What's New in Python 2.6 alpha 1?
Core and builtins Core and builtins
----------------- -----------------
- Patch #1680015: Don't modify __slots__ tuple if it contains an unicode
name.
- Patch #1444529: the builtin compile() now accepts keyword arguments. - Patch #1444529: the builtin compile() now accepts keyword arguments.
- Bug #1678647: write a newline after printing an exception in any - Bug #1678647: write a newline after printing an exception in any
......
...@@ -1654,29 +1654,33 @@ valid_identifier(PyObject *s) ...@@ -1654,29 +1654,33 @@ valid_identifier(PyObject *s)
static PyObject * static PyObject *
_unicode_to_string(PyObject *slots, Py_ssize_t nslots) _unicode_to_string(PyObject *slots, Py_ssize_t nslots)
{ {
PyObject *tmp = slots; PyObject *tmp = NULL;
PyObject *o, *o1; PyObject *slot_name, *new_name;
Py_ssize_t i; Py_ssize_t i;
ssizessizeargfunc copy = slots->ob_type->tp_as_sequence->sq_slice;
for (i = 0; i < nslots; i++) { for (i = 0; i < nslots; i++) {
if (PyUnicode_Check(o = PyTuple_GET_ITEM(tmp, i))) { if (PyUnicode_Check(slot_name = PyTuple_GET_ITEM(slots, i))) {
if (tmp == slots) { if (tmp == NULL) {
tmp = copy(slots, 0, PyTuple_GET_SIZE(slots)); tmp = PySequence_List(slots);
if (tmp == NULL) if (tmp == NULL)
return NULL; return NULL;
} }
o1 = _PyUnicode_AsDefaultEncodedString new_name = _PyUnicode_AsDefaultEncodedString(slot_name,
(o, NULL); NULL);
if (o1 == NULL) { if (new_name == NULL) {
Py_DECREF(tmp); Py_DECREF(tmp);
return 0; return NULL;
} }
Py_INCREF(o1); Py_INCREF(new_name);
Py_DECREF(o); PyList_SET_ITEM(tmp, i, new_name);
PyTuple_SET_ITEM(tmp, i, o1); Py_DECREF(slot_name);
} }
} }
return tmp; if (tmp != NULL) {
slots = PyList_AsTuple(tmp);
Py_DECREF(tmp);
}
return slots;
} }
#endif #endif
...@@ -1804,12 +1808,12 @@ type_new(PyTypeObject *metatype, PyObject *args, PyObject *kwds) ...@@ -1804,12 +1808,12 @@ type_new(PyTypeObject *metatype, PyObject *args, PyObject *kwds)
#ifdef Py_USING_UNICODE #ifdef Py_USING_UNICODE
tmp = _unicode_to_string(slots, nslots); tmp = _unicode_to_string(slots, nslots);
if (tmp == NULL)
goto bad_slots;
if (tmp != slots) { if (tmp != slots) {
Py_DECREF(slots); Py_DECREF(slots);
slots = tmp; slots = tmp;
} }
if (!tmp)
return NULL;
#endif #endif
/* Check for valid slot names and two special cases */ /* Check for valid slot names and two special cases */
for (i = 0; i < nslots; i++) { for (i = 0; i < nslots; i++) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment