Author: Carl Friedrich Bolz-Tereick <[email protected]>
Branch: py3.6
Changeset: r94461:97a56d2df52b
Date: 2018-05-02 22:23 +0200
http://bitbucket.org/pypy/pypy/changeset/97a56d2df52b/

Log:    getting started with __set_name__ support (PEP 487)

diff --git a/pypy/objspace/std/test/test_typeobject.py 
b/pypy/objspace/std/test/test_typeobject.py
--- a/pypy/objspace/std/test/test_typeobject.py
+++ b/pypy/objspace/std/test/test_typeobject.py
@@ -1544,3 +1544,15 @@
 
     def test_type_construct_unicode_surrogate_issue(self):
         raises(ValueError, type, 'A\udcdcb', (), {})
+
+    def test_set_name(self):
+        class Descriptor:
+            def __set_name__(self, owner, name):
+                self.owner = owner
+                self.name = name
+
+        class X:
+            a = Descriptor()
+        assert X.a.owner is X
+        assert X.a.name == "a"
+
diff --git a/pypy/objspace/std/typeobject.py b/pypy/objspace/std/typeobject.py
--- a/pypy/objspace/std/typeobject.py
+++ b/pypy/objspace/std/typeobject.py
@@ -800,6 +800,8 @@
     W_TypeObject.__init__(w_type, space, name, bases_w or [space.w_object],
                           dict_w, is_heaptype=True)
     w_type.ready()
+
+    _set_names(space, w_type)
     return w_type
 
 def _calculate_metaclass(space, w_metaclass, bases_w):
@@ -822,6 +824,13 @@
         raise oefmt(space.w_TypeError, "X is not a type object (%T)", w_type)
     return w_type
 
+def _set_names(space, w_type):
+    for key, w_value in w_type.dict_w.iteritems():
+        w_meth = space.lookup(w_value, '__set_name__')
+        if w_meth is not None:
+            # XXX what happens when the call raises, gets turned into a 
RuntimeError?
+            space.get_and_call_function(w_meth, w_value, w_type, 
space.newtext(key))
+
 
 def descr__init__(space, w_type, __args__):
     if __args__.keywords:
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to