{"id":2751,"date":"2022-08-30T15:27:28","date_gmt":"2022-08-30T15:27:28","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/02\/08\/problem-about-python-c-api-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:27:28","modified_gmt":"2022-08-30T15:27:28","slug":"problem-about-python-c-api-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/problem-about-python-c-api-collection-of-common-programming-errors\/","title":{"rendered":"problem about python-c-api-Collection of common programming errors"},"content":{"rendered":"<ul>\n<li>\n<img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/1dabc41adce7bd60b63fdae878f62e43?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nMo Zo<br \/>\npython c cython python-c-api python-c-extension<br \/>\nSo, the python docs for writing extension says this:&#8221;We want to expose our instancevariables as attributes. There are anumber of ways to do that. Thesimplest way is to define memberdefinitions:static PyMemberDef Noddy_members[] = {{&#8220;first&#8221;, T_OBJECT_EX, offsetof(Noddy, first), 0,&#8221;first name&#8221;},{&#8220;last&#8221;, T_OBJECT_EX, offsetof(Noddy, last), 0,&#8221;last name&#8221;},{&#8220;number&#8221;, T_INT, offsetof(Noddy, number), 0,&#8221;noddy number&#8221;},{NULL} \/* Sentinel *\/ };and put the definitions in thetp_members slot:Noddy_members,<\/li>\n<li>\n<img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/5f2c9e05fe39d0daf10a734dbb1f3cab?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\ndizgam<br \/>\npython python-3.x python-c-api<br \/>\nI m learning python c api functions and keen to learn python 3.1 stable version. Found a same kind of issue recently and triedPyModule_AddIntConstant(PyObject *module, const char *name, long value) Runtime error occurred for this function call.Is there something wrong with the function in python 3.1?<\/li>\n<li>\n<img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/4b8ce2e5dff7e913642c84213353f6a3?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nAmro<br \/>\nc++ python boost-python python-c-api python-embedding<br \/>\nI have a C++ application dynamically linked to the Python interpreter. I want to be able to import python modules from a particular directory. I want to modify the PYTHONPATH for my process so that sys.path will include the paths that I added to the PYTHONPATH. That seems to be the way it works according to this documentation:http:\/\/docs.python.org\/c-api\/intro.html#embedding-pythonHowever, when I print sys.path from Python-land it has the original contents of PYTHONPATH and not the one I set.<\/li>\n<li>\n<img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/7d22cf74f5f87fedb26aeef5fa94f9a3?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\ni30817<br \/>\nc++ python c python-c-api<br \/>\nEdit done: I am wrapping a custom python dll with the purpose of removing the custom code eventually and using a recent python instead of the ancient version that is being used now. One of the functions initializes a python extension, and callsPyObject* _PyObject_New(PyTypeObject *type)In my wrapper the (c) object produced by this call is crashing on PyErr_Print() (yes i&#8217;ve wrapped it too).I&#8217;d like to experiment with replacing the Repr or Str calls to something of my choosing to see if those are<\/li>\n<li>\n<img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/4483780c8f47da569cb58ce6de78e794?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nuser763305<br \/>\nc++ python numpy python-c-api python-embedding<br \/>\nI am new of python embedding. I am trying to embed python + numpy code inside a C++ callback function (inside a dll)the problem i am facing is the following. if i have:Py_Initialize(); \/\/ some python glue \/\/ python invocation Py_Finalize();everything works fine.but if i have:Py_Initialize(); _import_array(); \/\/to initialize numpy C-API \/\/ some python glue + numpy array object creation \/\/ python invocation via PyObject_CallObject() Py_Finalize();this crashes at the second time it reaches _import_<\/li>\n<li>\n<img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/d4a6fedba56c0767d6e8613007fb188e?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nJason Sundram<br \/>\nc++ python segmentation-fault python-c-api pycairo<br \/>\nI am running OSX Lion and trying to import the python module for goocanvas, using python2.7.I managed to successfully compile pygoocanvas-0.14.1, but when I try to import goocanvas through the python2.7 console, I get a segfault. After some debugging, I&#8217;m led to this code:DL_EXPORT (void) initgoocanvas (void) {PyObject *m, *d;fprintf(stderr,&#8221;init&lt;&lt; \\n&#8221;);\/\/ Pycairo_IMPORT; \/\/ XXX removed, it expands to the line below, anywaysPycairo_CAPI = (Pycairo_CAPI_t*) PyCObject_Import(&#8220;cairo&#8221;, &#8220;CAP<\/li>\n<li>\n<img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/e9b2f000a6ae6b518d1a0976980b3cbb?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nAdam<br \/>\npython python-c-api<br \/>\nI have a C++ library which uses Python callbacks. The callback, i.e. PyObject*, is stored in an object of class UnaryFunction, and the constructor Py_INCREFs it. The destuctor Py_XDECREFs it. That&#8217;s the problem. The interpreter segfaults on that DECREF.My solution is to just not DECREF it, but that seems wrong. What is the proper way to INC\/DEC the reference count of a function, and more importantly, why does the interpreter try to GC a function body when there are other live references to it?Ed<\/li>\n<li>\n<img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/4483780c8f47da569cb58ce6de78e794?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nuser763305<br \/>\npython python-c-api<br \/>\nHaving some trouble with this code. Trying to return a tuple of tuples (coordinates) from a C++ module Im writing. It looks right to me, the dirty list contains two Coords so len is 2, the x and y values of the items in the list are 0,0 and 0,1 respectively. First time Im attempting this so I might very well have misunderstood the docs or something. Any hints?PyObject* getDirty() {int len = dirty.size();PyObject* tuple = PyTuple_New(len);int count = 0;for (std::list&lt;Coord&gt;::iterator i = di<\/li>\n<li>\n<img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/4b8ce2e5dff7e913642c84213353f6a3?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nAmro<br \/>\npython c python-c-api python-embedding<br \/>\nI am having problems using the Embedded Python for C as per the Documentation &#8211; Whenever I try using imported modules I get an :Unhandled exception at 0x1e089e85 in PythonIncl.exe: 0xC0000005:Access violation reading location 0x00000004.The error occurs in the PyObject_GetAttrString() method and the documentation isn&#8217;t much help. I have also tried using tutorials as in an Example from IBM, but always get the same access violation.The following is the example code from one of the tutorials which<\/li>\n<li>\n<img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/4bfad9b7290f680e143a32833103bcb0?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nJ.T. Davies<br \/>\nc++ python-3.3 python-c-api python-embedding<br \/>\nI am attempting to emulate the Python\/C API&#8217;s PyRun_InteractiveLoop() function, but from a different input system used by my employer. The Python FAQ (http:\/\/docs.python.org\/3\/faq\/extending.html#how-do-i-tell-incomplete-input-from-invalid-input) has the following code, used to check if a given character array is a complete Python block:#include &lt;Python.h&gt; #include &lt;node.h&gt; #include &lt;errcode.h&gt; #include &lt;grammar.h&gt; #include &lt;parsetok.h&gt; #include &lt;compile.h&gt;int<\/li>\n<li>\n<img decoding=\"async\" src=\"http:\/\/i.stack.imgur.com\/gWrKl.jpg?s=32&amp;g=1\" \/><br \/>\nepicbrew<br \/>\nc++ python c callback python-c-api<br \/>\nI am trying to call methods in a python class from C++. The C++ method from which this is called is a C++ callback.Within this method when I am trying to call python method, it was giving segmentation fault.I have saved an instance of python function in a global variable like pFunc = PyDict_GetItemString(pDict, &#8220;PlxMsgWrapper&#8221;); (pFunc is global variable of type PyObject*)where PlxMsgWrapper is a python method, which will be used in the callback.In the callback, the arguments are created as PyOb<\/li>\n<li>\n<img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/3d86a4582f6a7cda40556f1fa3baccf8?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nPeterG<br \/>\npython c python-c-api python-embedding<br \/>\nIm trying to call python functions from C code, and i followed a sample from hereI also have the correct include file directries, library directries, and linked the python32.lib (im using python 32) however the error was that python\/C APIs such as PyString_FromString, PyInt_FromLong, PyInt_AsLong are undefined (error in the debugger)this is strange because im also using other APIs, but they&#8217;re all fine&#8230; whats the problem here??int main(int argc, char *argv[]) {PyObject *pName, *pModule, *pDict<\/li>\n<li>\n<img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/039d0df2ad0bdee4c506943c79bf70c3?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nMichael<br \/>\npython distutils python-c-api<br \/>\nI&#8217;m writing a Python extension in C that requires the CoreFoundation framework (among other things). This compiles fine with:gcc -o foo foo.c -framework CoreFoundation -framework Python(&#8220;-framework&#8221; is an Apple-only gcc extension, but that&#8217;s okay because I&#8217;m using their specific framework anyway)How do I tell setup.py to pass this flag to gcc?I tried this, but it doesn&#8217;t seem to work (it compiles, but then complains of undefined symbols when I try to run it):from distutils.core import setup, Ext<\/li>\n<li>\n<img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/741a1b0507432bfb522d1a284ce34ba9?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nFigus<br \/>\npython c gcc python-c-api python-c-extension<br \/>\nI&#8217;m starting the study of Python\/C API and I make the first code to test some functions, I write this:file: test.c#include &#8220;Python.h&#8221;int main() {PyObject* none = Py_BuildValue(&#8220;&#8221;); }I compile with command:gcc -I\/usr\/include\/python2.7 test.cI&#8217;ve the error undefined reference to `Py_BuildValue&#8217;After I run:gcc -I\/usr\/include\/python2.7 &#8211;shared -fPIC hashmem.cthis compile without errors, but when I run the compiled file I&#8217;ve aSegmentation fault (core dumped)How do I set the gcc parameters?I&#8217;ve ubunt<\/li>\n<li>\n<img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/eec2e174897771bff4bf95039074b57e?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nMark<br \/>\nmingw python-imaging-library python-c-api<br \/>\nI&#8217;m trying to compile Python Imaging Library on Windows 7 64-bit by running pip install pilI&#8217;ve set my compiler to MinGW. It looks like it&#8217;s going fine until it&#8217;s time to compile _imaging.c. It fails on this monster gcc call: C:\\MinGW\\bin\\gcc.exe -shared -s build\\temp.win-amd64-2.7\\Release\\_imaging.o build\\temp.win-amd64-2.7\\Release\\decode.o build\\temp.win-amd64-2.7\\Release\\encode.o build\\temp.win-amd64-2.7\\Release\\map.o build\\temp.win-amd64-2.7\\Release\\display.o build\\temp.win-amd64-2.7\\Relea<\/li>\n<\/ul>\n<p id=\"rop\"><small>Originally posted 2014-02-08 15:56:40. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>Mo Zo python c cython python-c-api python-c-extension So, the python docs for writing extension says this:&#8221;We want to expose our instancevariables as attributes. There are anumber of ways to do that. Thesimplest way is to define memberdefinitions:static PyMemberDef Noddy_members[] = {{&#8220;first&#8221;, T_OBJECT_EX, offsetof(Noddy, first), 0,&#8221;first name&#8221;},{&#8220;last&#8221;, T_OBJECT_EX, offsetof(Noddy, last), 0,&#8221;last name&#8221;},{&#8220;number&#8221;, T_INT, offsetof(Noddy, number), 0,&#8221;noddy [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-2751","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/2751","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=2751"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/2751\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=2751"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=2751"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=2751"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}