[Python-checkins] cpython (3.3): do safety checks on __qualname__ assignment

benjamin.peterson python-checkins at python.org
Wed Oct 31 05:08:14 CET 2012


http://hg.python.org/cpython/rev/c21c894eefe1
changeset:   80078:c21c894eefe1
branch:      3.3
user:        Benjamin Peterson <benjamin at python.org>
date:        Wed Oct 31 00:01:15 2012 -0400
summary:
  do safety checks on __qualname__ assignment

files:
  Lib/test/test_descr.py |  8 ++++++++
  Misc/NEWS              |  3 +++
  Objects/typeobject.c   |  2 ++
  3 files changed, 13 insertions(+), 0 deletions(-)


diff --git a/Lib/test/test_descr.py b/Lib/test/test_descr.py
--- a/Lib/test/test_descr.py
+++ b/Lib/test/test_descr.py
@@ -4502,6 +4502,14 @@
         self.assertEqual(float.real.__qualname__, 'float.real')
         self.assertEqual(int.__add__.__qualname__, 'int.__add__')
 
+        class X:
+            pass
+        with self.assertRaises(TypeError):
+            del X.__qualname__
+
+        self.assertRaises(TypeError, type.__dict__['__qualname__'].__set__,
+                          str, 'Oink')
+
     def test_qualname_dict(self):
         ns = {'__qualname__': 'some.name'}
         tp = type('Foo', (), ns)
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -12,6 +12,9 @@
 Core and Builtins
 -----------------
 
+- Fix segfaults on setting __qualname__ on builtin types and attempting to
+  delete it on any type.
+
 - Issue #16271: Fix strange bugs that resulted from __qualname__ appearing in a
   class's __dict__ and on type.
 
diff --git a/Objects/typeobject.c b/Objects/typeobject.c
--- a/Objects/typeobject.c
+++ b/Objects/typeobject.c
@@ -311,6 +311,8 @@
 {
     PyHeapTypeObject* et;
 
+    if (!check_set_special_type_attr(type, value, "__qualname__"))
+        return -1;
     if (!PyUnicode_Check(value)) {
         PyErr_Format(PyExc_TypeError,
                      "can only assign string to %s.__qualname__, not '%s'",

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list