[pypy-svn] r10201 - in pypy/dist/pypy: interpreter lib objspace/std translator

tismer at codespeak.net tismer at codespeak.net
Thu Mar 31 19:33:52 CEST 2005


Author: tismer
Date: Thu Mar 31 19:33:52 2005
New Revision: 10201

Modified:
   pypy/dist/pypy/interpreter/gateway.py
   pypy/dist/pypy/lib/_formatting.py
   pypy/dist/pypy/objspace/std/stringobject.py
   pypy/dist/pypy/translator/geninterplevel.py
Log:
little changes to _formatting to make it RPythonic,
hack to avoid collision with py.magic in flowing,
finally succeeded with flowing all of stringobject.
_formatting and _float_formatting are directly
compiled in, no worries about *these* any longer!

To do: cache the compiling (will do on my way home)

Modified: pypy/dist/pypy/interpreter/gateway.py
==============================================================================
--- pypy/dist/pypy/interpreter/gateway.py	(original)
+++ pypy/dist/pypy/interpreter/gateway.py	Thu Mar 31 19:33:52 2005
@@ -488,7 +488,7 @@
     hidden_applevel = True
     NOT_RPYTHON_ATTRIBUTES = ['code']
 
-    def __init__(self, source, filename=None):
+    def __init__(self, source, filename=None, *args, **kwds):
         "NOT_RPYTHON"
         if filename is None: 
             self.code = py.code.Source(source).compile()

Modified: pypy/dist/pypy/lib/_formatting.py
==============================================================================
--- pypy/dist/pypy/lib/_formatting.py	(original)
+++ pypy/dist/pypy/lib/_formatting.py	Thu Mar 31 19:33:52 2005
@@ -127,7 +127,7 @@
                 sign = ''
         return v, sign
 
-    def numeric_postprocess(self, r, sign,prefix=""):
+    def numeric_postprocess(self, r, sign, prefix=""):
         assert self.char in 'iduoxXeEfFgG'
         padchar = ' '
         if self.flags.f_zero:
@@ -162,6 +162,7 @@
 
 
 def funcFormatter(*funcs):
+    """NOT_RPYTHON"""
     class _F(Formatter):
         def format(self):
             r = self.value
@@ -344,6 +345,7 @@
     '%':funcFormatter(lambda x:'%'),
     }
 
+del funcFormatter # don't irritate flow space
 
 class FmtIter(object):
     def __init__(self, fmt):

Modified: pypy/dist/pypy/objspace/std/stringobject.py
==============================================================================
--- pypy/dist/pypy/objspace/std/stringobject.py	(original)
+++ pypy/dist/pypy/objspace/std/stringobject.py	Thu Mar 31 19:33:52 2005
@@ -1021,7 +1021,7 @@
                 return _formatting.format(format, (values,), values)
             else:
                 return _formatting.format(format, (values,), None)
-''')## XXX not yet, error in pow:   , do_imports=('_formatting', '_float_formatting'))
+''', do_imports=True)
 
 str_translate__String_ANY_ANY = app.interphook('str_translate__String_ANY_ANY') 
 str_decode__String_ANY_ANY = app.interphook('str_decode__String_ANY_ANY') 

Modified: pypy/dist/pypy/translator/geninterplevel.py
==============================================================================
--- pypy/dist/pypy/translator/geninterplevel.py	(original)
+++ pypy/dist/pypy/translator/geninterplevel.py	Thu Mar 31 19:33:52 2005
@@ -18,6 +18,7 @@
 
 from __future__ import generators
 import autopath, os, sys, exceptions, inspect, types
+import cPickle as pickle, __builtin__
 from pypy.objspace.flow.model import Variable, Constant, SpaceOperation
 from pypy.objspace.flow.model import FunctionGraph, Block, Link
 from pypy.objspace.flow.model import last_exception, last_exc_value
@@ -46,16 +47,36 @@
 
 def eval_helper(self, typename, expr):
     name = self.uniquename("gtype_%s" % typename)
-    bltinsname = self.nameof('__builtins__')
+    unique = self.uniquenameofprebuilt("eval_helper", eval_helper)
     self.initcode.append1(
-        'def eval_helper(expr):\n'
-        '    import types\n'
-        '    dic = space.newdict([(%s, space.w_builtins)])\n'
+        'def %s(expr):\n'
+        '    dic = space.newdict([])\n'
         '    space.exec_("import types", dic, dic)\n'
-        '    return space.eval(expr, dic, dic)' % bltinsname)
-    self.initcode.append1('%s = eval_helper(%r)' % (name, expr))
+        '    return space.eval(expr, dic, dic)' % unique)
+    self.initcode.append1('%s = %s(%r)' % (name, unique, expr))
     return name
 
+def unpickle_helper(self, name, value):
+    unique = self.uniquenameofprebuilt("unpickle_helper", unpickle_helper)
+    self.initcode.append1(
+        'def %s(value):\n'
+        '    dic = space.newdict([])\n'
+        '    space.exec_("import cPickle as pickle", dic, dic)\n'
+        '    return space.eval("pickle.loads(%%r)" %% value, dic, dic)' % unique)
+    self.initcode.append1('%s = %s(%r)' % (
+        name, unique, pickle.dumps(value, 2)) )
+
+# hey, for longs we can do even easier:
+def long_helper(self, name, value):
+    unique = self.uniquenameofprebuilt("long_helper", long_helper)
+    self.initcode.append1(
+        'def %s(value):\n'
+        '    dic = space.newdict([])\n'
+        '    space.exec_("", dic, dic) # init __builtins__\n'
+        '    return space.eval("long(%%r, 16)" %% value, dic, dic)' % unique)
+    self.initcode.append1('%s = %s(%r)' % (
+        name, unique, hex(value)[2:-1] ) )
+
 class GenRpy:
     def __init__(self, translator, entrypoint=None, modname=None, moddict=None):
         self.translator = translator
@@ -100,7 +121,6 @@
 
         # catching all builtins in advance, to avoid problems
         # with modified builtins
-        import __builtin__
         
         class bltinstub:
             def __init__(self, name):
@@ -294,6 +314,17 @@
         self.globaldecl.append('# global object %s' % (name,))
         return name
 
+    def uniquenameofprebuilt(self, basename, obj):
+        # identifying an object and giving it a name,
+        # without the attempt to render it.
+        key = Constant(obj).key
+        try:
+            txt = self.rpynames[key]
+        except KeyError:
+            self.rpynames[key] = txt = self.uniquename(basename)
+        return txt
+            
+
     def nameof_NotImplementedType(self, value):
         return "space.w_NotImplemented"
 
@@ -334,9 +365,6 @@
         return name
 
     def nameof_long(self, value):
-        # allow short longs only, meaning they
-        # must fit into a machine word.
-        assert (sys.maxint*2+1)&value==value, "your literal long is too long"
         # assume we want them in hex most of the time
         if value < 256L:
             s = "%dL" % value
@@ -349,7 +377,12 @@
             # the prefix  before the initial '_'
             name = 'glong_minus_%d' % abs(value)
         name = self.uniquename(name)
-        self.initcode.append1('%s = space.wrap(%s) # XXX implement long!' % (name, s))
+        # allow literally short longs only, meaning they
+        # must fit into a machine word.
+        if (sys.maxint*2+1)&value == value:
+            self.initcode.append1('%s = space.wrap(%s) # XXX implement long!' % (name, s))
+        else:
+            long_helper(self, name, value)
         return name
 
     def nameof_float(self, value):
@@ -524,8 +557,6 @@
                 # be lazy
                 return "(space.sys.get(space.str_w(%s)))" % self.nameof(func.__name__)                
             else:
-                print ("WARNING: accessing builtin modules different from sys or __builtin__"
-                       " is likely producing non-sense: %s %s" % (module.__name__, func.__name__))
                 name = self.uniquename('gbltin_' + func.__name__)
                 self.initcode.append1('%s = space.getattr(%s, %s)' % (
                     name, self.nameof(module), self.nameof(func.__name__)))
@@ -538,6 +569,7 @@
 
     def nameof_classobj(self, cls):
         printable_name = cls.__name__
+        gaga = "ssertion" in printable_name
         if cls.__doc__ and cls.__doc__.lstrip().startswith('NOT_RPYTHON'):
             #raise Exception, "%r should never be reached" % (cls,)
             print "skipped class", printable_name
@@ -547,7 +579,9 @@
         name = self.uniquename('gcls_' + cls.__name__)
 
         if issubclass(cls, Exception):
-            if cls.__module__ == 'exceptions':
+            # if cls.__module__ == 'exceptions':
+            # don't rely on this, py.magic redefines AssertionError
+            if getattr(__builtin__,cls.__name__) is cls:
                 # exception are defined on the space
                 return 'space.w_%s' % cls.__name__
 
@@ -558,6 +592,11 @@
             # metaclass = 'space.w_classobj'
 
         basenames = [self.nameof(base) for base in cls.__bases__]
+        if gaga:
+            print cls
+            print cls.__module__
+            print type(cls)
+            1/0
         def initclassobj():
             content = cls.__dict__.items()
             content.sort()
@@ -1427,7 +1466,7 @@
         pass
 
 def translate_as_module(sourcetext, filename=None, modname="app2interpexec",
-                        do_imports=None, tmpname=None):
+                        do_imports=False, tmpname=None):
     """ compile sourcetext as a module, translating to interp level.
     The result is the init function that creates the wrapped module dict.
     This init function needs a space as argument.
@@ -1449,24 +1488,15 @@
         code = compile(sourcetext, filename, 'exec') 
     dic = {'__name__': modname}
     exec code in dic
-    print do_imports
-    if do_imports:
-        # add lib folder to path
-        hold = sys.path
-        sys.path.insert(0, os.path.join(pypy.__path__[0], "lib"))
-        for modname in do_imports:
-            print 100*modname
-            mod = __import__(modname)
-            try: del mod.__builtins__
-            except:pass
-            dic.update(mod.__dict__)
-        sys.path = hold
     del dic['__builtins__']
     entrypoint = dic
     t = Translator(None, verbose=False, simplifying=True,
                    builtins_can_raise_exceptions=True,
-                   do_imports_immediately=False)
+                   do_imports_immediately=do_imports)
+    hold = sys.path
+    sys.path.insert(0, os.path.join(pypy.__path__[0], "lib"))
     gen = GenRpy(t, entrypoint, modname, dic)
+    sys.path = hold
     if tmpname:
         _file = file
     else:



More information about the Pypy-commit mailing list