[pypy-svn] r14644 - in pypy/dist/pypy/rpython: . test

pedronis at codespeak.net pedronis at codespeak.net
Wed Jul 13 22:02:11 CEST 2005


Author: pedronis
Date: Wed Jul 13 22:02:05 2005
New Revision: 14644

Modified:
   pypy/dist/pypy/rpython/llinterp.py
   pypy/dist/pypy/rpython/rbool.py
   pypy/dist/pypy/rpython/rdict.py
   pypy/dist/pypy/rpython/rint.py
   pypy/dist/pypy/rpython/robject.py
   pypy/dist/pypy/rpython/rstr.py
   pypy/dist/pypy/rpython/rtyper.py
   pypy/dist/pypy/rpython/test/test_llinterp.py
   pypy/dist/pypy/rpython/test/test_robject.py
Log:
* added support for operations on pyobjects (ptrs) in llinterp (with some simple tests)
* extended interpret interface with a flag someobjects wich if True will pass to annotation SomeObjects for
 the arguments that are of type Ptr(PyObject) 
* special case dicts with SomeObject keys and values to be just treated as SomeObjects with a test

- fixed problem about pyobject ops returning void instead of pyobjects in robject.
- added _callable to some gencapis so that they can be used when testing with llinterp.
  This is a stop-gap solutuin. We likely want to refactor gencapi using the external function table.




Modified: pypy/dist/pypy/rpython/llinterp.py
==============================================================================
--- pypy/dist/pypy/rpython/llinterp.py	(original)
+++ pypy/dist/pypy/rpython/llinterp.py	Wed Jul 13 22:02:05 2005
@@ -3,7 +3,7 @@
 from pypy.objspace.flow.model import Constant, Variable, last_exception
 from pypy.rpython.rarithmetic import intmask, r_uint, ovfcheck
 import py
-from pypy.rpython.lltype import _ptr, Ptr, Void, typeOf, malloc, cast_pointer
+from pypy.rpython.lltype import _ptr, Ptr, Void, typeOf, malloc, cast_pointer, PyObject, pyobjectptr
 from pypy.rpython.lltype import Array
 import math
 
@@ -28,6 +28,12 @@
         llframe = LLFrame(graph, args, self)
         return llframe.eval()
 
+# implementations of ops from flow.operation
+from pypy.objspace.flow.operation import FunctionByName
+opimpls = FunctionByName.copy()
+opimpls['is_true'] = bool
+
+
 class LLFrame(object):
     def __init__(self, graph, args, llinterpreter):
         self.graph = graph
@@ -289,11 +295,21 @@
         assert type(c) is float
         return math.fmod(b,c)
 
+    # operations on pyobjects!
+    for opname in opimpls.keys():
+        exec py.code.Source("""
+        def op_%(opname)s(self, *pyobjs):
+            for pyo in pyobjs:
+                assert typeOf(pyo) == Ptr(PyObject)
+            func = opimpls[%(opname)r]
+            return pyobjectptr(func(*[pyo._obj.value for pyo in pyobjs]))
+        """ % locals()).compile()
+    del opname
+
+
+
 # __________________________________________________________
 # primitive operations
-from pypy.objspace.flow.operation import FunctionByName
-opimpls = FunctionByName.copy()
-opimpls['is_true'] = bool
 ops_returning_a_bool = {'gt': True, 'ge': True,
                         'lt': True, 'le': True,
                         'eq': True, 'ne': True,

Modified: pypy/dist/pypy/rpython/rbool.py
==============================================================================
--- pypy/dist/pypy/rpython/rbool.py	(original)
+++ pypy/dist/pypy/rpython/rbool.py	Wed Jul 13 22:02:05 2005
@@ -54,7 +54,9 @@
 class __extend__(pairtype(PyObjRepr, BoolRepr)):
     def convert_from_to((r_from, r_to), v, llops):
         if r_to.lowleveltype == Bool:
-            return llops.gencapicall('PyObject_IsTrue', [v], resulttype=Bool)
+            # xxx put in table
+            return llops.gencapicall('PyObject_IsTrue', [v], resulttype=Bool,
+                                     _callable=lambda pyo: bool(pyo._obj.value))
         return NotImplemented
 
 class __extend__(pairtype(BoolRepr, PyObjRepr)):

Modified: pypy/dist/pypy/rpython/rdict.py
==============================================================================
--- pypy/dist/pypy/rpython/rdict.py	(original)
+++ pypy/dist/pypy/rpython/rdict.py	Wed Jul 13 22:02:05 2005
@@ -4,6 +4,7 @@
 from pypy.rpython import rmodel, lltype, rstr
 from pypy.rpython.rarithmetic import r_uint
 from pypy.rpython import rlist, rconstantdict, remptydict
+from pypy.rpython import robject
 
 # ____________________________________________________________
 #
@@ -25,7 +26,8 @@
 
 class __extend__(annmodel.SomeDict):
     def rtyper_makerepr(self, rtyper):
-        s_key = self.dictdef.dictkey.s_value 
+        s_key = self.dictdef.dictkey.s_value
+        s_value = self.dictdef.dictvalue.s_value
         if isinstance(s_key, annmodel.SomeString): 
             if s_key.can_be_none():
                 raise rmodel.TyperError("cannot make repr of dict with "
@@ -41,6 +43,9 @@
                         rtyper.getrepr(dictvalue.s_value))
         elif isinstance(s_key, annmodel.SomeImpossibleValue):
             return remptydict.EmptyDictRepr()
+        elif (s_key.__class__ is annmodel.SomeObject and s_key.knowntype == object and
+              s_value.__class__ is annmodel.SomeObject and s_value.knowntype == object):
+            return robject.pyobj_repr
         else: 
             raise rmodel.TyperError("cannot make repr of %r" %(self.dictdef,))
 
@@ -284,6 +289,8 @@
 
 def rtype_newdict(hop):
     r_dict = hop.r_result
+    if r_dict == robject.pyobj_repr: # special case: SomeObject: SomeObject dicts!
+        return hop.inputconst(robject.pyobj_repr, {})
     if not isinstance(r_dict, StrDictRepr):
         raise rmodel.TyperError("cannot create non-StrDicts, got %r" %(r_dict,))
     c1 = hop.inputconst(lltype.Void, r_dict.lowleveltype)

Modified: pypy/dist/pypy/rpython/rint.py
==============================================================================
--- pypy/dist/pypy/rpython/rint.py	(original)
+++ pypy/dist/pypy/rpython/rint.py	Wed Jul 13 22:02:05 2005
@@ -2,7 +2,7 @@
 from pypy.annotation import model as annmodel
 from pypy.objspace.flow.objspace import op_appendices
 from pypy.rpython.lltype import Signed, Unsigned, Bool, Float, Void, Char, \
-     UniChar, GcArray, malloc, Array
+     UniChar, GcArray, malloc, Array, pyobjectptr
 from pypy.rpython.rmodel import Repr, TyperError, IntegerRepr, CharRepr, \
      inputconst
 from pypy.rpython.robject import PyObjRepr, pyobj_repr
@@ -374,6 +374,7 @@
             return llops.gencapicall('PyLong_FromUnsignedLong', [v],
                                      resulttype=pyobj_repr)
         if r_from.lowleveltype == Signed:
+            # xxx put in table
             return llops.gencapicall('PyInt_FromLong', [v],
-                                     resulttype=pyobj_repr)
+                                     resulttype=pyobj_repr, _callable = lambda i: pyobjectptr(i))
         return NotImplemented

Modified: pypy/dist/pypy/rpython/robject.py
==============================================================================
--- pypy/dist/pypy/rpython/robject.py	(original)
+++ pypy/dist/pypy/rpython/robject.py	Wed Jul 13 22:02:05 2005
@@ -49,8 +49,11 @@
 def make_operation(opname, cls=PyObjRepr):
     def rtype_op(_, hop):
         vlist = hop.inputargs(*([pyobj_repr]*hop.nb_args))
-        v = hop.genop(opname, vlist, resulttype = pyobj_repr)
-        return hop.llops.convertvar(v, pyobj_repr, hop.r_result)
+        if isinstance(hop.r_result, VoidRepr):
+            hop.genop(opname, vlist)
+        else:
+            v = hop.genop(opname, vlist, resulttype=pyobj_repr)
+            return hop.llops.convertvar(v, pyobj_repr, hop.r_result)
 
     funcname = 'rtype_' + opname
     func = func_with_new_name(rtype_op, funcname)

Modified: pypy/dist/pypy/rpython/rstr.py
==============================================================================
--- pypy/dist/pypy/rpython/rstr.py	(original)
+++ pypy/dist/pypy/rpython/rstr.py	Wed Jul 13 22:02:05 2005
@@ -11,7 +11,7 @@
 from pypy.rpython.rslice import startstop_slice_repr, startonly_slice_repr
 from pypy.rpython.rslice import minusone_slice_repr
 from pypy.rpython.lltype import GcStruct, Signed, Array, Char, Ptr, malloc
-from pypy.rpython.lltype import Bool, Void, GcArray, nullptr, typeOf
+from pypy.rpython.lltype import Bool, Void, GcArray, nullptr, typeOf, pyobjectptr
 from pypy.rpython.rclass import InstanceRepr
 
 
@@ -451,9 +451,11 @@
                               resulttype=Ptr(STR.chars))
         v_size = llops.genop('getarraysize', [v_chars],
                              resulttype=Signed)
+        # xxx put in table        
         return llops.gencapicall('PyString_FromLLCharArrayAndSize',
                                  [v_chars, v_size],
-                                 resulttype=pyobj_repr)
+                                 resulttype=pyobj_repr,
+                                 _callable= lambda chars, sz: pyobjectptr(''.join(chars)))
 
 # ____________________________________________________________
 #

Modified: pypy/dist/pypy/rpython/rtyper.py
==============================================================================
--- pypy/dist/pypy/rpython/rtyper.py	(original)
+++ pypy/dist/pypy/rpython/rtyper.py	Wed Jul 13 22:02:05 2005
@@ -586,7 +586,7 @@
         return self.genop('direct_call', [cf]+list(args_v), resulttype)
 
     def gencapicall(self, cfnname, args_v, resulttype=None, **flags):
-        return self.genexternalcall(cfnname, args_v, resulttype=resulttype, external="C")
+        return self.genexternalcall(cfnname, args_v, resulttype=resulttype, external="C", **flags)
 
 # _______________________________________________________________________
 # this has the side-effect of registering the unary and binary operations

Modified: pypy/dist/pypy/rpython/test/test_llinterp.py
==============================================================================
--- pypy/dist/pypy/rpython/test/test_llinterp.py	(original)
+++ pypy/dist/pypy/rpython/test/test_llinterp.py	Wed Jul 13 22:02:05 2005
@@ -1,6 +1,6 @@
 
 import py
-from pypy.rpython.lltype import typeOf,pyobjectptr
+from pypy.rpython.lltype import typeOf, pyobjectptr, Ptr, PyObject
 from pypy.rpython.rtyper import RPythonTyper
 from pypy.rpython.llinterp import LLInterpreter, LLException,log
 from pypy.translator.translator import Translator
@@ -52,12 +52,19 @@
 
 _lastinterpreted = []
 _tcache = {}
-def interpret(func, values, view=False, viewbefore=False, policy=None):
-    key = (func,) + tuple([typeOf(x) for x in values])
+def interpret(func, values, view=False, viewbefore=False, policy=None, someobjects=False):
+    key = (func,) + tuple([typeOf(x) for x in values])+ (someobjects,)
     try: 
         (t, interp) = _tcache[key]
-    except KeyError: 
-        t, typer = gengraph(func, [lltype_to_annotation(typeOf(x)) 
+    except KeyError:
+        def annotation(x):
+            T = typeOf(x)
+            if T == Ptr(PyObject) and someobjects:
+                return object
+            else:
+                return lltype_to_annotation(T)
+        
+        t, typer = gengraph(func, [annotation(x)
                       for x in values], viewbefore, policy)
         interp = LLInterpreter(t.flowgraphs, typer)
         _tcache[key] = (t, interp)
@@ -231,6 +238,21 @@
         return [l1,l2,l3]
     res = interpret(f,[])
     assert len(res.items) == 3
+
+def test_obj_obj_add():
+    def f(x,y):
+        return x+y
+    _1L = pyobjectptr(1L)
+    _2L = pyobjectptr(2L)
+    res = interpret(f, [_1L, _2L], someobjects=True)
+    assert res._obj.value == 3L
+
+def test_obj_obj_is():
+    def f(x,y):
+        return x is y
+    o = pyobjectptr(object())
+    res = interpret(f, [o, o], someobjects=True)
+    assert res is True
     
 #__________________________________________________________________
 #

Modified: pypy/dist/pypy/rpython/test/test_robject.py
==============================================================================
--- pypy/dist/pypy/rpython/test/test_robject.py	(original)
+++ pypy/dist/pypy/rpython/test/test_robject.py	Wed Jul 13 22:02:05 2005
@@ -1,16 +1,22 @@
 from pypy.translator.translator import Translator
 from pypy.rpython.lltype import *
+from pypy.rpython.test.test_llinterp import interpret
 
 
-def rtype(fn, argtypes=[]):
-    t = Translator(fn)
-    t.annotate(argtypes)
-    t.specialize()
-    t.checkgraphs()
-    return t
-
-
-def test_set_del_item():
-    def dummyfn(obj):
+def test_simple():
+    def fn(obj):
         return obj + 1
-    rtype(dummyfn, [object])
+    _1L = pyobjectptr(1L)
+    res = interpret(fn, [_1L], someobjects=True)
+    assert res._obj.value == 2L
+
+def test_obj_obj_dict():
+    def f(i, c):
+        d = {}
+        d[1] = 'a'
+        d['a'] = i
+        d['ab'] = c
+        return d
+    res = interpret(f, [1, 'c'])
+    print res
+    



More information about the Pypy-commit mailing list