Commits

wlav committed efe5611

add _python_owns flag to set ownership

  • Participants
  • Parent commits 4d9e469
  • Branches reflex-support

Comments (0)

Files changed (4)

File pypy/module/cppyy/interp_cppyy.py

 
 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
             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)
 
 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']),

File pypy/module/cppyy/test/example01.cxx

     return count;
 }
 
+void example01::setCount(int value) {
+    count = value;
+}
+
 // instance methods
 int example01::addDataToInt(int a) {
     return m_somedata + a;

File pypy/module/cppyy/test/example01.h

     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);

File pypy/module/cppyy/test/test_cppyy.py

         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