[pypy-svn] r44482 - in pypy/dist/pypy: config rpython/ootypesystem rpython/ootypesystem/test

antocuni at codespeak.net antocuni at codespeak.net
Sun Jun 24 16:35:59 CEST 2007


Author: antocuni
Date: Sun Jun 24 16:35:58 2007
New Revision: 44482

Modified:
   pypy/dist/pypy/config/translationoption.py
   pypy/dist/pypy/rpython/ootypesystem/rclass.py
   pypy/dist/pypy/rpython/ootypesystem/rpbc.py
   pypy/dist/pypy/rpython/ootypesystem/test/test_oortype.py
Log:
new translation option that makes ootype not to mangle the names of
the attributes. Might be useful for a general purpose
rpython-compiler.



Modified: pypy/dist/pypy/config/translationoption.py
==============================================================================
--- pypy/dist/pypy/config/translationoption.py	(original)
+++ pypy/dist/pypy/config/translationoption.py	Sun Jun 24 16:35:58 2007
@@ -104,6 +104,11 @@
                   "hintannotate", "timeshift"],
                  default=None, cmdline="--fork-before"),
 
+    # options for ootype
+    OptionDescription("ootype", "Object Oriented Typesystem options", [
+        BoolOption("mangle", "Mangle names of class members", default=True),
+    ]),
+
     OptionDescription("backendopt", "Backend Optimization Options", [
         # control inlining
         BoolOption("inline", "Do basic inlining and malloc removal",

Modified: pypy/dist/pypy/rpython/ootypesystem/rclass.py
==============================================================================
--- pypy/dist/pypy/rpython/ootypesystem/rclass.py	(original)
+++ pypy/dist/pypy/rpython/ootypesystem/rclass.py	Sun Jun 24 16:35:58 2007
@@ -137,15 +137,23 @@
 
 # ____________________________________________________________
 
-def mangle(name):
+def mangle(name, config):
     # XXX temporary: for now it looks like a good idea to mangle names
     # systematically to trap bugs related to a confusion between mangled
     # and non-mangled names
-    return 'o' + name
-
-def unmangle(mangled):
-    assert mangled.startswith('o')
-    return mangled[1:]
+    if config.translation.ootype.mangle:
+        return 'o' + name
+    else:
+        not_allowed = ('_hash_cache_', 'meta', 'class_')
+        assert name not in not_allowed, "%s is a reserved name" % name
+        return name
+
+def unmangle(mangled, config):
+    if config.translation.ootype.mangle:
+        assert mangled.startswith('o')
+        return mangled[1:]
+    else:
+        return mangled
 
 class InstanceRepr(AbstractInstanceRepr):
     def __init__(self, rtyper, classdef, gcflavor='ignored'):
@@ -194,7 +202,7 @@
         selfattrs = self.classdef.attrs
 
         for name, attrdef in selfattrs.iteritems():
-            mangled = mangle(name)            
+            mangled = mangle(name, self.rtyper.getconfig())
             if not attrdef.readonly:
                 repr = self.rtyper.getrepr(attrdef.s_value)
                 allfields[mangled] = repr
@@ -226,7 +234,7 @@
                 s_meth = self.classdef.classdesc.s_get_value(self.classdef,
                         meth_name)
                 if isinstance(s_meth, annmodel.SomePBC):
-                    mangled = mangle(meth_name)
+                    mangled = mangle(meth_name, self.rtyper.getconfig())
                     allmethods[mangled] = meth_name, s_meth
                 # else: it's the __init__ of a builtin exception
             
@@ -279,7 +287,7 @@
             for name, attrdef in classdef.attrs.iteritems():
                 if not attrdef.readonly:
                     continue
-                mangled = mangle(name)
+                mangled = mangle(name, self.rtyper.getconfig())
                 if mangled in allclassattributes:
                     selfdesc = self.classdef.classdesc
                     if name not in selfattrs:
@@ -297,7 +305,7 @@
                     # a non-method class attribute
                     if not attrdef.s_value.is_constant():
                         classattributes[mangled] = attrdef.s_value, value
-        
+
         ootype.addMethods(self.lowleveltype, methods)
         
         self.allfields = allfields
@@ -344,7 +352,7 @@
                     # about it
                     repr = self.rtyper.getrepr(attrdef.s_value)
                     oot = repr.lowleveltype
-                    mangled = mangle(name)
+                    mangled = mangle(name, self.rtyper.getconfig())
                     value = self.classdef.classdesc.read_attribute(name)
                     default = repr.convert_desc_or_const(value)
                     overridden_defaults[mangled] = oot, default
@@ -375,7 +383,7 @@
         v_inst, _ = hop.inputargs(self, ootype.Void)
         s_inst = hop.args_s[0]
         attr = hop.args_s[1].const
-        mangled = mangle(attr)
+        mangled = mangle(attr, self.rtyper.getconfig())
         v_attr = hop.inputconst(ootype.Void, mangled)
         if mangled in self.allfields:
             # regular instance attributes
@@ -409,7 +417,7 @@
 
     def rtype_setattr(self, hop):
         attr = hop.args_s[1].const
-        mangled = mangle(attr)
+        mangled = mangle(attr, self.rtyper.getconfig())
         self.lowleveltype._check_field(mangled)
         r_value = self.allfields[mangled]
         v_inst, _, v_newval = hop.inputargs(self, ootype.Void, r_value)
@@ -420,7 +428,7 @@
         # this method emulates behaviour from the corresponding
         # lltypesystem one. It is referenced in some obscure corners
         # like rtyping of OSError.
-        mangled_name = mangle(attr)
+        mangled_name = mangle(attr, self.rtyper.getconfig())
         cname = inputconst(ootype.Void, mangled_name)
         llops.genop('oosetfield', [vinst, cname, vvalue])
 
@@ -476,7 +484,7 @@
             elif mangled == '_hash_cache_': # hash() support
                 llattrvalue = hash(value)
             else:
-                name = unmangle(mangled)
+                name = unmangle(mangled, self.rtyper.getconfig())
                 try:
                     attrvalue = getattr(value, name)
                 except AttributeError:

Modified: pypy/dist/pypy/rpython/ootypesystem/rpbc.py
==============================================================================
--- pypy/dist/pypy/rpython/ootypesystem/rpbc.py	(original)
+++ pypy/dist/pypy/rpython/ootypesystem/rpbc.py	Sun Jun 24 16:35:58 2007
@@ -149,7 +149,7 @@
 
     def _get_method_name(self, opname, s_pbc, args_s):
         shape, index, callfamily = self._get_shape_index_callfamily(opname, s_pbc, args_s)
-        mangled = mangle(self.methodname)
+        mangled = mangle(self.methodname, self.rtyper.getconfig())
         row = self.concretetable[shape, index]
         derived_mangled = row_method_name(mangled, row.attrname)
         return derived_mangled

Modified: pypy/dist/pypy/rpython/ootypesystem/test/test_oortype.py
==============================================================================
--- pypy/dist/pypy/rpython/ootypesystem/test/test_oortype.py	(original)
+++ pypy/dist/pypy/rpython/ootypesystem/test/test_oortype.py	Sun Jun 24 16:35:58 2007
@@ -1,3 +1,4 @@
+import py
 from pypy import conftest
 from pypy.rpython.ootypesystem.ootype import *
 from pypy.rpython.ootypesystem import ootype
@@ -10,8 +11,9 @@
 from pypy.rlib.objectmodel import r_dict
 from pypy.rpython.ootypesystem import ooregistry # side effects
 
-def gengraph(f, args=[], viewBefore=False, viewAfter=False):
+def gengraph(f, args=[], viewBefore=False, viewAfter=False, mangle=True):
     t = TranslationContext()
+    t.config.translation.ootype.mangle = mangle
     t.buildannotator().build_types(f, args)
     if viewBefore or conftest.option.view:
         t.view()
@@ -268,3 +270,21 @@
         d['x'] = 42
         return d['x']
     assert interpret(oof, [], type_system='ootype') == 42
+
+def test_not_mangle_attrs():
+    class Foo:
+        def __init__(self):
+            self.x = 42
+    def fn():
+        return Foo()
+
+    graph = gengraph(fn, mangle=False)
+    FOO = graph.getreturnvar().concretetype
+    assert FOO._fields.keys() == ['x']
+
+    class Bar:
+        def __init__(self):
+            self.meta = 42
+    def fn():
+        return Bar()
+    py.test.raises(AssertionError, gengraph, fn, mangle=False)



More information about the Pypy-commit mailing list