From a20f0d58919ff8f3cb7d9b9df906f18be7536ee6 Mon Sep 17 00:00:00 2001 From: Matthias Koefferlein Date: Mon, 31 Aug 2020 23:55:26 +0200 Subject: [PATCH] Also provide 'void to self' return values for Python. --- src/pya/pya/pyaModule.cc | 15 +++++++++++++++ testdata/python/dbPolygonTest.py | 11 +++++++++++ 2 files changed, 26 insertions(+) diff --git a/src/pya/pya/pyaModule.cc b/src/pya/pya/pyaModule.cc index 31d3961d3..5c16ce6e3 100644 --- a/src/pya/pya/pyaModule.cc +++ b/src/pya/pya/pyaModule.cc @@ -686,6 +686,15 @@ property_name_from_id (int mid, PyObject *self) return cls_decl->name () + "." + mt->property_name (mid); } +static gsi::ArgType create_void_type () +{ + gsi::ArgType at; + at.init (); + return at; +} + +static gsi::ArgType s_void_type = create_void_type (); + static PyObject * get_return_value (PYAObjectBase *self, gsi::SerialArgs &retlist, const gsi::MethodBase *meth, tl::Heap &heap) { @@ -696,6 +705,12 @@ get_return_value (PYAObjectBase *self, gsi::SerialArgs &retlist, const gsi::Meth gsi::IterAdaptorAbstractBase *iter = (gsi::IterAdaptorAbstractBase *) retlist.read (heap); ret = (PyObject *) PYAIteratorObject::create (self ? self->py_object () : 0, iter, &meth->ret_type ()); + } else if (meth->ret_type () == s_void_type && self != 0) { + + // simple, yet magical :) + ret = self->py_object (); + Py_INCREF (ret); + } else { ret = pop_arg (meth->ret_type (), retlist, self, heap).release (); diff --git a/testdata/python/dbPolygonTest.py b/testdata/python/dbPolygonTest.py index f98597d07..64cf8e471 100644 --- a/testdata/python/dbPolygonTest.py +++ b/testdata/python/dbPolygonTest.py @@ -728,6 +728,17 @@ class DBPolygonTests(unittest.TestCase): self.assertEqual(str(p1), "(21,42;21,62;41,62;41,42)") self.assertEqual(str(pp), "(21,42;21,62;41,62;41,42)") + def test_voidMethodsReturnSelf(self): + + hull = [ pya.Point(0, 0), pya.Point(6000, 0), + pya.Point(6000, 3000), pya.Point(0, 3000) ] + hole1 = [ pya.Point(1000, 1000), pya.Point(2000, 1000), + pya.Point(2000, 2000), pya.Point(1000, 2000) ] + hole2 = [ pya.Point(3000, 1000), pya.Point(4000, 1000), + pya.Point(4000, 2000), pya.Point(3000, 2000) ] + poly = pya.Polygon(hull).insert_hole(hole1).insert_hole(hole2) + self.assertEqual(str(poly), "(0,0;0,3000;6000,3000;6000,0/1000,1000;2000,1000;2000,2000;1000,2000/3000,1000;4000,1000;4000,2000;3000,2000)") + # run unit tests if __name__ == '__main__': suite = unittest.TestLoader().loadTestsFromTestCase(DBPolygonTests)