Author: Wim Lavrijsen <[email protected]>
Branch: reflex-support
Changeset: r55708:efe5611213bb
Date: 2012-06-15 16:16 -0700
http://bitbucket.org/pypy/pypy/changeset/efe5611213bb/

Log:    add _python_owns flag to set ownership

diff --git a/pypy/module/cppyy/interp_cppyy.py 
b/pypy/module/cppyy/interp_cppyy.py
--- a/pypy/module/cppyy/interp_cppyy.py
+++ b/pypy/module/cppyy/interp_cppyy.py
@@ -2,7 +2,7 @@
 
 from pypy.interpreter.error import OperationError
 from pypy.interpreter.gateway import interp2app, unwrap_spec
-from pypy.interpreter.typedef import TypeDef, interp_attrproperty
+from pypy.interpreter.typedef import TypeDef, GetSetProperty, 
interp_attrproperty
 from pypy.interpreter.baseobjspace import Wrappable, W_Root
 
 from pypy.rpython.lltypesystem import rffi, lltype
@@ -676,6 +676,15 @@
             raise OperationError(self.space.w_ReferenceError,
                                  self.space.wrap("trying to access a NULL 
pointer"))
 
+    # allow user to determine ownership rules on a per object level
+    @unwrap_spec(self='self')
+    def fget_python_owns(self, space):
+        return space.wrap(self.python_owns)
+
+    @unwrap_spec(value=bool)
+    def fset_python_owns(self, space, value):
+        self.python_owns = space.is_true(value)
+
     def get_cppthis(self, calling_scope):
         return self.cppclass.get_cppthis(self, calling_scope)
 
@@ -714,6 +723,7 @@
 W_CPPInstance.typedef = TypeDef(
     'CPPInstance',
     cppclass = interp_attrproperty('cppclass', cls=W_CPPInstance),
+    _python_owns = GetSetProperty(W_CPPInstance.fget_python_owns, 
W_CPPInstance.fset_python_owns),
     __eq__ = interp2app(W_CPPInstance.instance__eq__, unwrap_spec=['self', 
W_Root]),
     __ne__ = interp2app(W_CPPInstance.instance__ne__, unwrap_spec=['self', 
W_Root]),
     __nonzero__ = interp2app(W_CPPInstance.instance__nonzero__, 
unwrap_spec=['self']),
diff --git a/pypy/module/cppyy/test/example01.cxx 
b/pypy/module/cppyy/test/example01.cxx
--- a/pypy/module/cppyy/test/example01.cxx
+++ b/pypy/module/cppyy/test/example01.cxx
@@ -86,6 +86,10 @@
     return count;
 }
 
+void example01::setCount(int value) {
+    count = value;
+}
+
 // instance methods
 int example01::addDataToInt(int a) {
     return m_somedata + a;
diff --git a/pypy/module/cppyy/test/example01.h 
b/pypy/module/cppyy/test/example01.h
--- a/pypy/module/cppyy/test/example01.h
+++ b/pypy/module/cppyy/test/example01.h
@@ -36,6 +36,7 @@
     static payload* staticCyclePayload(payload* p, double d);
     static payload staticCopyCyclePayload(payload* p, double d);
     static int getCount();
+    static void setCount(int);
 
 public:        // instance methods
     int addDataToInt(int a);
diff --git a/pypy/module/cppyy/test/test_cppyy.py 
b/pypy/module/cppyy/test/test_cppyy.py
--- a/pypy/module/cppyy/test/test_cppyy.py
+++ b/pypy/module/cppyy/test/test_cppyy.py
@@ -150,8 +150,30 @@
         gc.collect()
         assert t.get_overload("getCount").call(None) == 0
 
+    def test05a_memory2(self):
+        """Test ownership control."""
+
+        import gc, cppyy
+
+        t = self.example01
+
+        assert t.get_overload("getCount").call(None) == 0
+
+        e1 = t.get_overload(t.type_name).call(None, 7)
+        assert t.get_overload("getCount").call(None) == 1
+        assert e1._python_owns == True
+        e1._python_owns = False
+        e1 = None
+        gc.collect()
+        assert t.get_overload("getCount").call(None) == 1
+
+        # forced fix-up of object count for later tests
+        t.get_overload("setCount").call(None, 0)
+
+
     def test06_method_double(self):
-        """Test passing of a double and returning of double on a method"""
+        """Test passing of a double and returning of double on a method."""
+
         import cppyy
 
         t = self.example01
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to