[pypy-commit] pypy reflex-support: o) allow user-defined pythonizations
wlav
noreply at buildbot.pypy.org
Wed Mar 14 14:26:59 CET 2012
Author: Wim Lavrijsen <WLavrijsen at lbl.gov>
Branch: reflex-support
Changeset: r53551:46f3dbe3b682
Date: 2012-03-12 08:47 -0700
http://bitbucket.org/pypy/pypy/changeset/46f3dbe3b682/
Log: o) allow user-defined pythonizations o) fix file access bits
diff --git a/pypy/module/cppyy/__init__.py b/pypy/module/cppyy/__init__.py
--- a/pypy/module/cppyy/__init__.py
+++ b/pypy/module/cppyy/__init__.py
@@ -16,4 +16,5 @@
appleveldefs = {
'gbl' : 'pythonify.gbl',
'load_reflection_info' : 'pythonify.load_reflection_info',
+ 'add_pythonization' : 'pythonify.add_pythonization',
}
diff --git a/pypy/module/cppyy/pythonify.py b/pypy/module/cppyy/pythonify.py
--- a/pypy/module/cppyy/pythonify.py
+++ b/pypy/module/cppyy/pythonify.py
@@ -285,8 +285,14 @@
return scope
+_pythonizations = {}
def _pythonize(pyclass):
+ try:
+ _pythonizations[pyclass.__name__](pyclass)
+ except KeyError:
+ pass
+
# map size -> __len__ (generally true for STL)
if hasattr(pyclass, 'size') and \
not hasattr(pyclass,'__len__') and callable(pyclass.size):
@@ -338,3 +344,10 @@
# mostly for the benefit of the CINT backend, which treats std as special
gbl.std = make_cppnamespace(None, "std", None, False)
+
+# user-defined pythonizations interface
+_pythonizations = {}
+def add_pythonization(class_name, callback):
+ if not callable(callback):
+ raise TypeError("given '%s' object is not callable" % str(callback))
+ _pythonizations[class_name] = callback
diff --git a/pypy/module/cppyy/test/test_advancedcpp.py b/pypy/module/cppyy/test/test_advancedcpp.py
old mode 100755
new mode 100644
diff --git a/pypy/module/cppyy/test/test_datatypes.py b/pypy/module/cppyy/test/test_datatypes.py
old mode 100755
new mode 100644
diff --git a/pypy/module/cppyy/test/test_operators.py b/pypy/module/cppyy/test/test_operators.py
old mode 100755
new mode 100644
diff --git a/pypy/module/cppyy/test/test_pythonify.py b/pypy/module/cppyy/test/test_pythonify.py
old mode 100755
new mode 100644
--- a/pypy/module/cppyy/test/test_pythonify.py
+++ b/pypy/module/cppyy/test/test_pythonify.py
@@ -308,3 +308,40 @@
assert hasattr(z, 'myint')
assert z.gime_z_(z)
+
+
+class AppTestPYTHONIFY_UI:
+ def setup_class(cls):
+ cls.space = space
+ env = os.environ
+ cls.w_test_dct = space.wrap(test_dct)
+ cls.w_example01 = cls.space.appexec([], """():
+ import cppyy
+ return cppyy.load_reflection_info(%r)""" % (test_dct, ))
+
+ def test01_pythonizations(self):
+ """Test addition of user-defined pythonizations"""
+
+ import cppyy
+
+ def example01_pythonize(pyclass):
+ assert pyclass.__name__ == 'example01'
+ def getitem(self, idx):
+ return self.addDataToInt(idx)
+ pyclass.__getitem__ = getitem
+
+ cppyy.add_pythonization('example01', example01_pythonize)
+
+ e = cppyy.gbl.example01(1)
+
+ assert e[0] == 1
+ assert e[1] == 2
+ assert e[5] == 6
+
+ def test02_fragile_pythonizations(self):
+ """Test pythonizations error reporting"""
+
+ import cppyy
+
+ example01_pythonize = 1
+ raises(TypeError, cppyy.add_pythonization, 'example01', example01_pythonize)
More information about the pypy-commit
mailing list