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

arigo at codespeak.net arigo at codespeak.net
Tue Aug 30 16:27:16 CEST 2005


Author: arigo
Date: Tue Aug 30 16:27:13 2005
New Revision: 17085

Modified:
   pypy/dist/pypy/rpython/rclass.py
   pypy/dist/pypy/rpython/test/test_rclass.py
Log:
We use in descroperation.py  "if type(descr) is Function"  where 'descr' can
sometimes be None.  This would crash after translation.  Added a test and, for
now, let the low-level type() of a NULL instance return a NULL pointer.

 --This line, and those below, will be ignored--

M    rpython/test/test_rclass.py
M    rpython/rclass.py


Modified: pypy/dist/pypy/rpython/rclass.py
==============================================================================
--- pypy/dist/pypy/rpython/rclass.py	(original)
+++ pypy/dist/pypy/rpython/rclass.py	Tue Aug 30 16:27:13 2005
@@ -558,8 +558,12 @@
         return vptr
 
     def rtype_type(self, hop):
-        vinst, = hop.inputargs(self)
-        return self.getfield(vinst, '__class__', hop.llops)
+        instance_repr = self.common_repr()
+        vinst, = hop.inputargs(instance_repr)
+        if hop.args_s[0].can_be_none():
+            return hop.gendirectcall(ll_inst_type, vinst)
+        else:
+            return instance_repr.getfield(vinst, '__class__', hop.llops)
 
     def rtype_hash(self, hop):
         if self.classdef is None:
@@ -700,3 +704,10 @@
     if cached == 0:
        cached = ins.hash_cache = id(ins)
     return cached
+
+def ll_inst_type(obj):
+    if obj:
+        return obj.typeptr
+    else:
+        # type(None) -> NULL  (for now)
+        return nullptr(typeOf(obj).TO.typeptr.TO)

Modified: pypy/dist/pypy/rpython/test/test_rclass.py
==============================================================================
--- pypy/dist/pypy/rpython/test/test_rclass.py	(original)
+++ pypy/dist/pypy/rpython/test/test_rclass.py	Tue Aug 30 16:27:13 2005
@@ -300,3 +300,24 @@
     assert res.item0 == True
     assert res.item1 == intmask(hash(c)+hash(d))
     
+def test_type():
+    class A:
+        pass
+    class B(A):
+        pass
+    def g(a):
+        return type(a)
+    def f(i):
+        if i > 0:
+            a = A()
+        elif i < 0:
+            a = B()
+        else:
+            a = None
+        return g(a) is A    # should type(None) work?  returns None for now
+    res = interpret(f, [1])
+    assert res is True
+    res = interpret(f, [-1])
+    assert res is False
+    res = interpret(f, [0])
+    assert res is False



More information about the Pypy-commit mailing list