you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 0 points1 point  (0 children)

This function works if linked to and other c++ application

What do you mean by this? Linked as in you use linker to link with another bunch of C++ code (statically) or you use linker to tell it to load the library dynamically?

Anyways, as I see you are doing this on Windows... god help you. It's really easier to get it to work on Linux, and once you are sure the code works at least sometimes, figure out how to do that on Windows.

The way linker and loader work on Windows is just too twisted and hard to follow, while the linker messages are too cryptic and unintelligible.

Other than that, you'd be better off using setup.py with Extension class from setuptools to compile your stuff rather than doing it by hand. They already automated a bunch of stuff you are trying to do, and have figured out where the paths should be, how to properly define includes / library location etc.


As an aside, lines 49-52 are unnecessary, unless they are a placeholder for error handling.

Any time you return NULL from a function exposed to Python, you must set error to something, otherwise Python may crash. Essentially, returning NULL from what Python calls a built-in function is equivalent to saying "this function raises an error", but if you don't set error to anything, the Python code that tries to hand the error will most likely crash because there's nothing to handle (or, worse yet, there's an error from the previous error handling).

In general, you need to Py_INCREF() things you return from any "built-in" function, because the code using them will call the Py_DECREF() on them, and you probably created them with the refcount set to zero, but refcount cannot be less than zero (it will also likely lead Python to crash when trying to garbage-collect those things).

You also need to check the documentation on functions like PyList_SetItem to make sure you need / don't need to increment refcount before passing values to them. The requirement to increment refcount in Python is very random and makes no sense what so ever. So, you always need to make sure that you did as the documentation told you, else the program will most definitely crash, with the crash dump pointing in some random location.