[pypy-svn] r12296 - in pypy/dist/pypy/objspace/std: . test

pedronis at codespeak.net pedronis at codespeak.net
Sun May 15 16:47:44 CEST 2005


Author: pedronis
Date: Sun May 15 16:47:44 2005
New Revision: 12296

Modified:
   pypy/dist/pypy/objspace/std/test/test_typeobject.py
   pypy/dist/pypy/objspace/std/typeobject.py
Log:
name mangling (code from compiler/misc.py) for slots and test



Modified: pypy/dist/pypy/objspace/std/test/test_typeobject.py
==============================================================================
--- pypy/dist/pypy/objspace/std/test/test_typeobject.py	(original)
+++ pypy/dist/pypy/objspace/std/test/test_typeobject.py	Sun May 15 16:47:44 2005
@@ -277,6 +277,25 @@
         raises(TypeError, A.__dict__['x'].__set__, z, 1)
         raises(TypeError, A.__dict__['x'].__delete__, z)
 
+    def test_slot_mangling(self):
+        class A(object):
+            __slots__ = ('x', '__x','__xxx__','__','__dict__')
+        a = A()
+        assert '__dict__' in A.__dict__
+        assert '__' in A.__dict__
+        assert '__xxx__' in A.__dict__
+        assert 'x' in A.__dict__
+        assert '_A__x' in A.__dict__
+        a.x = 1
+        a._A__x = 2
+        a.__xxx__ = 3
+        a.__ = 4
+        assert a.x == 1
+        assert a._A__x == 2
+        assert a.__xxx__ == 3
+        assert a.__ == 4
+        assert a.__dict__ == {}
+
     def test_repr(self):
         globals()['__name__'] = 'a'
         class A(object):

Modified: pypy/dist/pypy/objspace/std/typeobject.py
==============================================================================
--- pypy/dist/pypy/objspace/std/typeobject.py	(original)
+++ pypy/dist/pypy/objspace/std/typeobject.py	Sun May 15 16:47:44 2005
@@ -7,6 +7,31 @@
 
 from copy_reg import _HEAPTYPE
 
+# from compiler/misc.py
+
+MANGLE_LEN = 256 # magic constant from compile.c
+
+def _mangle(name, klass):
+    if not name.startswith('__'):
+        return name
+    if len(name) + 2 >= MANGLE_LEN:
+        return name
+    if name.endswith('__'):
+        return name
+    try:
+        i = 0
+        while klass[i] == '_':
+            i = i + 1
+    except IndexError:
+        return name
+    klass = klass[i:]
+
+    tlen = len(klass) + len(name)
+    if tlen > MANGLE_LEN:
+        klass = klass[:MANGLE_LEN-tlen]
+
+    return "_%s%s" % (klass, name)
+
 class W_TypeObject(W_Object):
     from pypy.objspace.std.typetype import type_typedef as typedef
 
@@ -109,6 +134,7 @@
                         wantdict = True
                     else:
                         # create member
+                        slot_name = _mangle(slot_name, name)
                         w_self.dict_w[slot_name] = space.wrap(Member(nslots, slot_name, w_self))
                         nslots += 1
 



More information about the Pypy-commit mailing list