[pypy-svn] r21122 - in pypy/dist/pypy/interpreter: . test

adim at codespeak.net adim at codespeak.net
Tue Dec 13 11:20:22 CET 2005


Author: adim
Date: Tue Dec 13 11:20:21 2005
New Revision: 21122

Modified:
   pypy/dist/pypy/interpreter/baseobjspace.py
   pypy/dist/pypy/interpreter/test/test_objspace.py
Log:
(adim, arigo)

A helper space.interp_w() with proper type checking.


Modified: pypy/dist/pypy/interpreter/baseobjspace.py
==============================================================================
--- pypy/dist/pypy/interpreter/baseobjspace.py	(original)
+++ pypy/dist/pypy/interpreter/baseobjspace.py	Tue Dec 13 11:20:21 2005
@@ -390,12 +390,29 @@
     def interpclass_w(space, w_obj):
         """
          If w_obj is a wrapped internal interpreter class instance unwrap to it,
-         otherwise return None
+         otherwise return None.  (Can be overridden in specific spaces; you
+	 should generally use the helper space.interp_w() instead.)
         """
         if isinstance(w_obj, Wrappable):
             return w_obj
         return None
 
+    def interp_w(self, RequiredClass, w_obj, can_be_None=False):
+        """
+	 Unwrap w_obj, checking that it is an instance of the required internal
+	 interpreter class (a subclass of Wrappable).
+	"""
+	if can_be_None and self.is_w(w_obj, self.w_None):
+	    return None
+	obj = self.interpclass_w(w_obj)
+	if not isinstance(obj, RequiredClass):   # or obj is None
+	    msg = "expected a %s, got %s instead" % (
+	        RequiredClass.typedef.name,
+		w_obj.getclass(self).getname(self, '?'))
+	    raise OperationError(self.w_TypeError, self.wrap(msg))
+	return obj
+    interp_w._annspecialcase_ = 'specialize:arg1'
+
     def unpackiterable(self, w_iterable, expected_length=-1):
         """Unpack an iterable object into a real (interpreter-level) list.
         Raise a real (subclass of) ValueError if the length is wrong."""

Modified: pypy/dist/pypy/interpreter/test/test_objspace.py
==============================================================================
--- pypy/dist/pypy/interpreter/test/test_objspace.py	(original)
+++ pypy/dist/pypy/interpreter/test/test_objspace.py	Tue Dec 13 11:20:21 2005
@@ -1,5 +1,7 @@
 import autopath
 from py.test import raises
+from pypy.interpreter.function import Function
+from pypy.interpreter.pycode import PyCode
 
 # this test isn't so much to test that the objspace interface *works*
 # -- it's more to test that it's *there*
@@ -87,6 +89,17 @@
         assert not self.space.exception_match(self.space.w_ValueError,
                                                self.space.w_LookupError)
 
+    def test_interp_w(self):
+        w = self.space.wrap
+	w_bltinfunction = self.space.builtin.get('len')
+	res = self.space.interp_w(Function, w_bltinfunction)
+	assert res is w_bltinfunction   # with the std objspace only
+	self.space.raises_w(self.space.w_TypeError, self.space.interp_w, PyCode, w_bltinfunction)
+	self.space.raises_w(self.space.w_TypeError, self.space.interp_w, Function, w(42))
+	self.space.raises_w(self.space.w_TypeError, self.space.interp_w, Function, w(None))
+	res = self.space.interp_w(Function, w(None), can_be_None=True)
+	assert res is None
+
 class TestModuleMinimal: 
     def test_sys_exists(self):
         assert self.space.sys 



More information about the Pypy-commit mailing list