From a1d151c4c35c80295e519e50c1dfb76d3d064659 Mon Sep 17 00:00:00 2001 From: Hong Chen Date: Thu, 23 May 2024 10:37:27 -0500 Subject: [PATCH] Updated not to always pass disown flag for swig pointer conversion instead base on "own" value of the input object. (#1690) --- include/trick/swig/swig_class_typedef.i | 10 +++++++++- test/SIM_test_ip/RUN_test/unit_test.py | 8 ++++++-- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/include/trick/swig/swig_class_typedef.i b/include/trick/swig/swig_class_typedef.i index de549175..07a15165 100644 --- a/include/trick/swig/swig_class_typedef.i +++ b/include/trick/swig/swig_class_typedef.i @@ -51,7 +51,15 @@ // TYPE * void * temp_ptr ; - if ( SWIG_IsOK(SWIG_ConvertPtr($input, &temp_ptr,$1_descriptor, SWIG_POINTER_DISOWN)) ) { + SwigPyObject *sobj = SWIG_Python_GetSwigThis($input) ; + // isDisown is set to SWIG_POINTER_DISOWN by default + int isDisown = SWIG_POINTER_DISOWN ; + if (sobj) { + // isDisown is re-set based on whether the Python processor owns the input object + isDisown = !sobj->own ; + } + + if ( SWIG_IsOK(SWIG_ConvertPtr($input, &temp_ptr,$1_descriptor, isDisown)) ) { $1 = reinterpret_cast< $1_ltype >(temp_ptr) ; } else if ( SWIG_IsOK(SWIG_ConvertPtr($input, &temp_ptr,SWIG_TypeQuery("_p_swig_ref"), 0)) ) { // Array to pointer assignment diff --git a/test/SIM_test_ip/RUN_test/unit_test.py b/test/SIM_test_ip/RUN_test/unit_test.py index 35ecab8d..746fdbe3 100644 --- a/test/SIM_test_ip/RUN_test/unit_test.py +++ b/test/SIM_test_ip/RUN_test/unit_test.py @@ -2781,12 +2781,16 @@ def main(): # Polymorphic assignments and access test_suite = "polymorphism" - test_so.a = trick.Cat() + #test_so.a = trick.Cat() + # use MM to allocate memory for a pointer declared in S_define + test_so.a = trick.TMM_declare_var_s("Cat") TRICK_EXPECT_EQ( test_so.a.id , 1, test_suite , "single abstract ptr" ) trick.trick_test_add_parent( test_suite , "single abstract ptr" , "1770735610") #test_so.a.speak() #test_so.a[0].speak() - test_so.a = trick.Dog() + #test_so.a = trick.Dog() + # use MM to allocate memory for a pointer declared in S_define + test_so.a = trick.TMM_declare_var_s("Dog") TRICK_EXPECT_EQ( test_so.a.id , 2, test_suite , "single abstract ptr" ) test_so.aarray[0] = trick.Cat()