SWIG Generating C++ Python3 wrapper in Windows causes assertion MSVC 2017










1















Using SWIG to generate a wrapper around a C++ class is causing a strange assertion at run-time:



Assertion failed!

Program: C:Python37python37_d.dll
File: c:_work4sobjectstypeobject.c
Line: 3634

Expression: PyTuple_Check(args)

For information on how your program can cause an assertion
failure, see the Visual C++ documentation on asserts

(Press Retry to debug the application - JIT must be enabled)


If ignore is clicked on the dialogue that pops up, everything seems to work fine.



I've created an example program to try replicate the problem and have experienced the same issue:



main.cpp



#include "testwrapper.h"

#pragma push_macro("slots")
#undef slots
#include "Python.h"
#pragma pop_macro("slots")

#include "SwigModules/generated/swig_runtime.h"

PyObject * ConvertToWrapper(SwigInterface * instance)

swig_type_info * pTypeInfo = SWIG_TypeQuery("SwigInterface *");
PyObject* obj = SWIG_NewPointerObj(instance, pTypeInfo, 0); <- issue occurs here
return obj;


TestWrapper * wrapper = new TestWrapper();

void TestSwig()

Py_Initialize();
PyRun_SimpleString("import test_module");
ConvertToWrapper(wrapper);
Py_Finalize();


int main(int argc, char *argv)

TestSwig();
return 0;



testwrapper.h



#pragma once
#include "swiginterface.h"

class TestWrapper : public SwigInterface

public:
TestWrapper()
virtual ~TestWrapper()
virtual void Test();
;


swiginterface.h



#pragma once

class SwigInterface

public:
virtual ~SwigInterface()
virtual void Test() = 0;
;


test_module.i (swig interface file)



%module test_module

%
#include "../swiginterface.h"
%

%include "../SwigInterface.h"

%inline %
SwigInterface * test;
%


setup_function (python)



from distutils.core import setup, Extension
setup(name="test_module",
py_modules=['test_module'],
ext_modules=[Extension("_test_module",
["test_module.i"],
extra_compile_args=["-DSWIG_TYPE_TABLE=test_module"],
swig_opts=["-c++", "-py3"],
)])


Function in swig_runtime.h where issue occurs:



SWIGRUNTIME PyObject* 
SWIG_Python_NewShadowInstance(SwigPyClientData *data, PyObject *swig_this)
{
#if (PY_VERSION_HEX >= 0x02020000)
PyObject *inst = 0;
PyObject *newraw = data->newraw;
if (newraw)
inst = PyObject_Call(newraw, data->newargs, NULL);
if (inst)
#if !defined(SWIG_PYTHON_SLOW_GETSET_THIS)
PyObject **dictptr = _PyObject_GetDictPtr(inst);
if (dictptr != NULL)
PyObject *dict = *dictptr;
if (dict == NULL)
dict = PyDict_New();
*dictptr = dict;
PyDict_SetItem(dict, SWIG_This(), swig_this);


#else
PyObject *key = SWIG_This();
PyObject_SetAttr(inst, key, swig_this);
#endif

else
#if PY_VERSION_HEX >= 0x03000000
inst = ((PyTypeObject*) data->newargs)->tp_new((PyTypeObject*) data->newargs, Py_None, Py_None); //<----- HERE
if (inst)
PyObject_SetAttr(inst, SWIG_This(), swig_this);
Py_TYPE(inst)->tp_flags &= ~Py_TPFLAGS_VALID_VERSION_TAG;

#else
PyObject *dict = PyDict_New();
if (dict)
PyDict_SetItem(dict, SWIG_This(), swig_this);
inst = PyInstance_NewRaw(data->newargs, dict);
Py_DECREF(dict);

#endif

return inst;


I've tried this with multiple versions of SWIG and all result the same. It loads the module no problem, both into the C++ code and into an external python interpreter. How do I go about debugging / fixing this issue?










share|improve this question
























  • Debug the call that fails? I suppose there is some Python somewhere? Where is it?

    – Matthieu Brucher
    Nov 14 '18 at 13:24











  • No python at this point, it's for passing a pointer into python so it can be used in the embedded interpreter. The code works on Linux / Mac and did using previous version of windows.

    – robby987
    Nov 14 '18 at 13:29











  • Both the instance and pTypeInfo pointers are ok. The last argument is for flags, which should be set to 0

    – robby987
    Nov 14 '18 at 13:30











  • On Windows, I #undef _DEBUG before including Python.h and explicitly link to the release version of Python37. Your errors may be related to debug/release linking of python libraries

    – Jens Munk
    Dec 19 '18 at 12:59















1















Using SWIG to generate a wrapper around a C++ class is causing a strange assertion at run-time:



Assertion failed!

Program: C:Python37python37_d.dll
File: c:_work4sobjectstypeobject.c
Line: 3634

Expression: PyTuple_Check(args)

For information on how your program can cause an assertion
failure, see the Visual C++ documentation on asserts

(Press Retry to debug the application - JIT must be enabled)


If ignore is clicked on the dialogue that pops up, everything seems to work fine.



I've created an example program to try replicate the problem and have experienced the same issue:



main.cpp



#include "testwrapper.h"

#pragma push_macro("slots")
#undef slots
#include "Python.h"
#pragma pop_macro("slots")

#include "SwigModules/generated/swig_runtime.h"

PyObject * ConvertToWrapper(SwigInterface * instance)

swig_type_info * pTypeInfo = SWIG_TypeQuery("SwigInterface *");
PyObject* obj = SWIG_NewPointerObj(instance, pTypeInfo, 0); <- issue occurs here
return obj;


TestWrapper * wrapper = new TestWrapper();

void TestSwig()

Py_Initialize();
PyRun_SimpleString("import test_module");
ConvertToWrapper(wrapper);
Py_Finalize();


int main(int argc, char *argv)

TestSwig();
return 0;



testwrapper.h



#pragma once
#include "swiginterface.h"

class TestWrapper : public SwigInterface

public:
TestWrapper()
virtual ~TestWrapper()
virtual void Test();
;


swiginterface.h



#pragma once

class SwigInterface

public:
virtual ~SwigInterface()
virtual void Test() = 0;
;


test_module.i (swig interface file)



%module test_module

%
#include "../swiginterface.h"
%

%include "../SwigInterface.h"

%inline %
SwigInterface * test;
%


setup_function (python)



from distutils.core import setup, Extension
setup(name="test_module",
py_modules=['test_module'],
ext_modules=[Extension("_test_module",
["test_module.i"],
extra_compile_args=["-DSWIG_TYPE_TABLE=test_module"],
swig_opts=["-c++", "-py3"],
)])


Function in swig_runtime.h where issue occurs:



SWIGRUNTIME PyObject* 
SWIG_Python_NewShadowInstance(SwigPyClientData *data, PyObject *swig_this)
{
#if (PY_VERSION_HEX >= 0x02020000)
PyObject *inst = 0;
PyObject *newraw = data->newraw;
if (newraw)
inst = PyObject_Call(newraw, data->newargs, NULL);
if (inst)
#if !defined(SWIG_PYTHON_SLOW_GETSET_THIS)
PyObject **dictptr = _PyObject_GetDictPtr(inst);
if (dictptr != NULL)
PyObject *dict = *dictptr;
if (dict == NULL)
dict = PyDict_New();
*dictptr = dict;
PyDict_SetItem(dict, SWIG_This(), swig_this);


#else
PyObject *key = SWIG_This();
PyObject_SetAttr(inst, key, swig_this);
#endif

else
#if PY_VERSION_HEX >= 0x03000000
inst = ((PyTypeObject*) data->newargs)->tp_new((PyTypeObject*) data->newargs, Py_None, Py_None); //<----- HERE
if (inst)
PyObject_SetAttr(inst, SWIG_This(), swig_this);
Py_TYPE(inst)->tp_flags &= ~Py_TPFLAGS_VALID_VERSION_TAG;

#else
PyObject *dict = PyDict_New();
if (dict)
PyDict_SetItem(dict, SWIG_This(), swig_this);
inst = PyInstance_NewRaw(data->newargs, dict);
Py_DECREF(dict);

#endif

return inst;


I've tried this with multiple versions of SWIG and all result the same. It loads the module no problem, both into the C++ code and into an external python interpreter. How do I go about debugging / fixing this issue?










share|improve this question
























  • Debug the call that fails? I suppose there is some Python somewhere? Where is it?

    – Matthieu Brucher
    Nov 14 '18 at 13:24











  • No python at this point, it's for passing a pointer into python so it can be used in the embedded interpreter. The code works on Linux / Mac and did using previous version of windows.

    – robby987
    Nov 14 '18 at 13:29











  • Both the instance and pTypeInfo pointers are ok. The last argument is for flags, which should be set to 0

    – robby987
    Nov 14 '18 at 13:30











  • On Windows, I #undef _DEBUG before including Python.h and explicitly link to the release version of Python37. Your errors may be related to debug/release linking of python libraries

    – Jens Munk
    Dec 19 '18 at 12:59













1












1








1


1






Using SWIG to generate a wrapper around a C++ class is causing a strange assertion at run-time:



Assertion failed!

Program: C:Python37python37_d.dll
File: c:_work4sobjectstypeobject.c
Line: 3634

Expression: PyTuple_Check(args)

For information on how your program can cause an assertion
failure, see the Visual C++ documentation on asserts

(Press Retry to debug the application - JIT must be enabled)


If ignore is clicked on the dialogue that pops up, everything seems to work fine.



I've created an example program to try replicate the problem and have experienced the same issue:



main.cpp



#include "testwrapper.h"

#pragma push_macro("slots")
#undef slots
#include "Python.h"
#pragma pop_macro("slots")

#include "SwigModules/generated/swig_runtime.h"

PyObject * ConvertToWrapper(SwigInterface * instance)

swig_type_info * pTypeInfo = SWIG_TypeQuery("SwigInterface *");
PyObject* obj = SWIG_NewPointerObj(instance, pTypeInfo, 0); <- issue occurs here
return obj;


TestWrapper * wrapper = new TestWrapper();

void TestSwig()

Py_Initialize();
PyRun_SimpleString("import test_module");
ConvertToWrapper(wrapper);
Py_Finalize();


int main(int argc, char *argv)

TestSwig();
return 0;



testwrapper.h



#pragma once
#include "swiginterface.h"

class TestWrapper : public SwigInterface

public:
TestWrapper()
virtual ~TestWrapper()
virtual void Test();
;


swiginterface.h



#pragma once

class SwigInterface

public:
virtual ~SwigInterface()
virtual void Test() = 0;
;


test_module.i (swig interface file)



%module test_module

%
#include "../swiginterface.h"
%

%include "../SwigInterface.h"

%inline %
SwigInterface * test;
%


setup_function (python)



from distutils.core import setup, Extension
setup(name="test_module",
py_modules=['test_module'],
ext_modules=[Extension("_test_module",
["test_module.i"],
extra_compile_args=["-DSWIG_TYPE_TABLE=test_module"],
swig_opts=["-c++", "-py3"],
)])


Function in swig_runtime.h where issue occurs:



SWIGRUNTIME PyObject* 
SWIG_Python_NewShadowInstance(SwigPyClientData *data, PyObject *swig_this)
{
#if (PY_VERSION_HEX >= 0x02020000)
PyObject *inst = 0;
PyObject *newraw = data->newraw;
if (newraw)
inst = PyObject_Call(newraw, data->newargs, NULL);
if (inst)
#if !defined(SWIG_PYTHON_SLOW_GETSET_THIS)
PyObject **dictptr = _PyObject_GetDictPtr(inst);
if (dictptr != NULL)
PyObject *dict = *dictptr;
if (dict == NULL)
dict = PyDict_New();
*dictptr = dict;
PyDict_SetItem(dict, SWIG_This(), swig_this);


#else
PyObject *key = SWIG_This();
PyObject_SetAttr(inst, key, swig_this);
#endif

else
#if PY_VERSION_HEX >= 0x03000000
inst = ((PyTypeObject*) data->newargs)->tp_new((PyTypeObject*) data->newargs, Py_None, Py_None); //<----- HERE
if (inst)
PyObject_SetAttr(inst, SWIG_This(), swig_this);
Py_TYPE(inst)->tp_flags &= ~Py_TPFLAGS_VALID_VERSION_TAG;

#else
PyObject *dict = PyDict_New();
if (dict)
PyDict_SetItem(dict, SWIG_This(), swig_this);
inst = PyInstance_NewRaw(data->newargs, dict);
Py_DECREF(dict);

#endif

return inst;


I've tried this with multiple versions of SWIG and all result the same. It loads the module no problem, both into the C++ code and into an external python interpreter. How do I go about debugging / fixing this issue?










share|improve this question
















Using SWIG to generate a wrapper around a C++ class is causing a strange assertion at run-time:



Assertion failed!

Program: C:Python37python37_d.dll
File: c:_work4sobjectstypeobject.c
Line: 3634

Expression: PyTuple_Check(args)

For information on how your program can cause an assertion
failure, see the Visual C++ documentation on asserts

(Press Retry to debug the application - JIT must be enabled)


If ignore is clicked on the dialogue that pops up, everything seems to work fine.



I've created an example program to try replicate the problem and have experienced the same issue:



main.cpp



#include "testwrapper.h"

#pragma push_macro("slots")
#undef slots
#include "Python.h"
#pragma pop_macro("slots")

#include "SwigModules/generated/swig_runtime.h"

PyObject * ConvertToWrapper(SwigInterface * instance)

swig_type_info * pTypeInfo = SWIG_TypeQuery("SwigInterface *");
PyObject* obj = SWIG_NewPointerObj(instance, pTypeInfo, 0); <- issue occurs here
return obj;


TestWrapper * wrapper = new TestWrapper();

void TestSwig()

Py_Initialize();
PyRun_SimpleString("import test_module");
ConvertToWrapper(wrapper);
Py_Finalize();


int main(int argc, char *argv)

TestSwig();
return 0;



testwrapper.h



#pragma once
#include "swiginterface.h"

class TestWrapper : public SwigInterface

public:
TestWrapper()
virtual ~TestWrapper()
virtual void Test();
;


swiginterface.h



#pragma once

class SwigInterface

public:
virtual ~SwigInterface()
virtual void Test() = 0;
;


test_module.i (swig interface file)



%module test_module

%
#include "../swiginterface.h"
%

%include "../SwigInterface.h"

%inline %
SwigInterface * test;
%


setup_function (python)



from distutils.core import setup, Extension
setup(name="test_module",
py_modules=['test_module'],
ext_modules=[Extension("_test_module",
["test_module.i"],
extra_compile_args=["-DSWIG_TYPE_TABLE=test_module"],
swig_opts=["-c++", "-py3"],
)])


Function in swig_runtime.h where issue occurs:



SWIGRUNTIME PyObject* 
SWIG_Python_NewShadowInstance(SwigPyClientData *data, PyObject *swig_this)
{
#if (PY_VERSION_HEX >= 0x02020000)
PyObject *inst = 0;
PyObject *newraw = data->newraw;
if (newraw)
inst = PyObject_Call(newraw, data->newargs, NULL);
if (inst)
#if !defined(SWIG_PYTHON_SLOW_GETSET_THIS)
PyObject **dictptr = _PyObject_GetDictPtr(inst);
if (dictptr != NULL)
PyObject *dict = *dictptr;
if (dict == NULL)
dict = PyDict_New();
*dictptr = dict;
PyDict_SetItem(dict, SWIG_This(), swig_this);


#else
PyObject *key = SWIG_This();
PyObject_SetAttr(inst, key, swig_this);
#endif

else
#if PY_VERSION_HEX >= 0x03000000
inst = ((PyTypeObject*) data->newargs)->tp_new((PyTypeObject*) data->newargs, Py_None, Py_None); //<----- HERE
if (inst)
PyObject_SetAttr(inst, SWIG_This(), swig_this);
Py_TYPE(inst)->tp_flags &= ~Py_TPFLAGS_VALID_VERSION_TAG;

#else
PyObject *dict = PyDict_New();
if (dict)
PyDict_SetItem(dict, SWIG_This(), swig_this);
inst = PyInstance_NewRaw(data->newargs, dict);
Py_DECREF(dict);

#endif

return inst;


I've tried this with multiple versions of SWIG and all result the same. It loads the module no problem, both into the C++ code and into an external python interpreter. How do I go about debugging / fixing this issue?







python c++ swig






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 14 '18 at 14:25







robby987

















asked Nov 14 '18 at 13:16









robby987robby987

4321518




4321518












  • Debug the call that fails? I suppose there is some Python somewhere? Where is it?

    – Matthieu Brucher
    Nov 14 '18 at 13:24











  • No python at this point, it's for passing a pointer into python so it can be used in the embedded interpreter. The code works on Linux / Mac and did using previous version of windows.

    – robby987
    Nov 14 '18 at 13:29











  • Both the instance and pTypeInfo pointers are ok. The last argument is for flags, which should be set to 0

    – robby987
    Nov 14 '18 at 13:30











  • On Windows, I #undef _DEBUG before including Python.h and explicitly link to the release version of Python37. Your errors may be related to debug/release linking of python libraries

    – Jens Munk
    Dec 19 '18 at 12:59

















  • Debug the call that fails? I suppose there is some Python somewhere? Where is it?

    – Matthieu Brucher
    Nov 14 '18 at 13:24











  • No python at this point, it's for passing a pointer into python so it can be used in the embedded interpreter. The code works on Linux / Mac and did using previous version of windows.

    – robby987
    Nov 14 '18 at 13:29











  • Both the instance and pTypeInfo pointers are ok. The last argument is for flags, which should be set to 0

    – robby987
    Nov 14 '18 at 13:30











  • On Windows, I #undef _DEBUG before including Python.h and explicitly link to the release version of Python37. Your errors may be related to debug/release linking of python libraries

    – Jens Munk
    Dec 19 '18 at 12:59
















Debug the call that fails? I suppose there is some Python somewhere? Where is it?

– Matthieu Brucher
Nov 14 '18 at 13:24





Debug the call that fails? I suppose there is some Python somewhere? Where is it?

– Matthieu Brucher
Nov 14 '18 at 13:24













No python at this point, it's for passing a pointer into python so it can be used in the embedded interpreter. The code works on Linux / Mac and did using previous version of windows.

– robby987
Nov 14 '18 at 13:29





No python at this point, it's for passing a pointer into python so it can be used in the embedded interpreter. The code works on Linux / Mac and did using previous version of windows.

– robby987
Nov 14 '18 at 13:29













Both the instance and pTypeInfo pointers are ok. The last argument is for flags, which should be set to 0

– robby987
Nov 14 '18 at 13:30





Both the instance and pTypeInfo pointers are ok. The last argument is for flags, which should be set to 0

– robby987
Nov 14 '18 at 13:30













On Windows, I #undef _DEBUG before including Python.h and explicitly link to the release version of Python37. Your errors may be related to debug/release linking of python libraries

– Jens Munk
Dec 19 '18 at 12:59





On Windows, I #undef _DEBUG before including Python.h and explicitly link to the release version of Python37. Your errors may be related to debug/release linking of python libraries

– Jens Munk
Dec 19 '18 at 12:59












1 Answer
1






active

oldest

votes


















0














To resolve this issue, I needed to downgrade from Python3.7 to Python3.6






share|improve this answer






















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53301146%2fswig-generating-c-python3-wrapper-in-windows-causes-assertion-msvc-2017%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    To resolve this issue, I needed to downgrade from Python3.7 to Python3.6






    share|improve this answer



























      0














      To resolve this issue, I needed to downgrade from Python3.7 to Python3.6






      share|improve this answer

























        0












        0








        0







        To resolve this issue, I needed to downgrade from Python3.7 to Python3.6






        share|improve this answer













        To resolve this issue, I needed to downgrade from Python3.7 to Python3.6







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 15 '18 at 19:31









        robby987robby987

        4321518




        4321518





























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53301146%2fswig-generating-c-python3-wrapper-in-windows-causes-assertion-msvc-2017%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            How to read a connectionString WITH PROVIDER in .NET Core?

            Node.js Script on GitHub Pages or Amazon S3

            Museum of Modern and Contemporary Art of Trento and Rovereto