summaryrefslogtreecommitdiff
path: root/Lib/python
diff options
context:
space:
mode:
authorWilliam S Fulton <wsf@fultondesigns.co.uk>2022-07-16 13:14:40 +0100
committerWilliam S Fulton <wsf@fultondesigns.co.uk>2022-07-17 15:20:20 +0100
commitc3c061cac8fbae8c65caefe462a3bd879fc6f547 (patch)
treee676e2bbfe3889c3cdc94127936cabeed88daba1 /Lib/python
parentc737bd57131c02eed9573d13f30662397890ba0b (diff)
downloadswig-c3c061cac8fbae8c65caefe462a3bd879fc6f547.tar.gz
Add Python support for std::unique_ptr inputs
Equivalent to Java/C# implementation.
Diffstat (limited to 'Lib/python')
-rw-r--r--Lib/python/pyrun.swg17
-rw-r--r--Lib/python/std_unique_ptr.i13
2 files changed, 25 insertions, 5 deletions
diff --git a/Lib/python/pyrun.swg b/Lib/python/pyrun.swg
index 935885934..ec6e3d5a8 100644
--- a/Lib/python/pyrun.swg
+++ b/Lib/python/pyrun.swg
@@ -1357,12 +1357,19 @@ SWIG_Python_ConvertPtrAndOwn(PyObject *obj, void **ptr, swig_type_info *ty, int
}
}
if (sobj) {
- if (own)
- *own = *own | sobj->own;
- if (flags & SWIG_POINTER_DISOWN) {
- sobj->own = 0;
+ if (((flags & SWIG_POINTER_RELEASE) == SWIG_POINTER_RELEASE) && !sobj->own) {
+ res = SWIG_ERROR_RELEASE_NOT_OWNED;
+ } else {
+ if (own)
+ *own = *own | sobj->own;
+ if (flags & SWIG_POINTER_DISOWN) {
+ sobj->own = 0;
+ }
+ if (flags & SWIG_POINTER_CLEAR) {
+ sobj->ptr = 0;
+ }
+ res = SWIG_OK;
}
- res = SWIG_OK;
} else {
if (implicit_conv) {
SwigPyClientData *data = ty ? (SwigPyClientData *) ty->clientdata : 0;
diff --git a/Lib/python/std_unique_ptr.i b/Lib/python/std_unique_ptr.i
index 331817f76..bec8bf968 100644
--- a/Lib/python/std_unique_ptr.i
+++ b/Lib/python/std_unique_ptr.i
@@ -8,9 +8,22 @@
* ----------------------------------------------------------------------------- */
%define %unique_ptr(TYPE)
+%typemap(in, noblock=1) std::unique_ptr< TYPE > (void *argp = 0, int res = 0) {
+ res = SWIG_ConvertPtr($input, &argp, $descriptor(TYPE *), SWIG_POINTER_RELEASE | %convertptr_flags);
+ if (!SWIG_IsOK(res)) {
+ if (res == SWIG_ERROR_RELEASE_NOT_OWNED) {
+ %releasenotowned_fail(res, "TYPE *", $symname, $argnum);
+ } else {
+ %argument_fail(res, "TYPE *", $symname, $argnum);
+ }
+ }
+ $1.reset((TYPE *)argp);
+}
+
%typemap (out) std::unique_ptr< TYPE > %{
%set_output(SWIG_NewPointerObj($1.release(), $descriptor(TYPE *), SWIG_POINTER_OWN | %newpointer_flags));
%}
+
%template() std::unique_ptr< TYPE >;
%enddef