python-c-api

Python C api PyImport_importmodule fail when the file has an import statement

夙愿已清 提交于 2019-12-24 12:09:29
问题 I tried to use the Python C api to call a function from python in C++, the test was successful. But if I intend to import a module already importing other module, the Pymodule_findmodule will return Null even though it's there and created a compiled file. Here is my code Py_Initialize(); PySys_SetPath("C:/Users/Mik/Documents/GitHub/youtube-dl"); PyObject * pythonFile = PyImport_ImportModule("test2"); Here is the python filed named test2.py at that directory with a file named test_dl.py and a

Assignment into Python 3.x Buffers with itemsize > 1

僤鯓⒐⒋嵵緔 提交于 2019-12-24 02:23:08
问题 I am trying to expose a buffer of image pixel information (32 bit RGBA) through the Python 3.x buffer interface. After quite a bit of playing around, I was able to get this working like so: int Image_get_buffer(PyObject* self, Py_buffer* view, int flags) { int img_len; void* img_bytes; // Do my image fetch magic get_image_pixel_data(self, &img_bytes, &img_len); // Let python fill my buffer PyBuffer_FillInfo(view, self, img_bytes, img_len, 0, flags); } And in python I can play with it like so:

Python: Generate function stubs from C module

大城市里の小女人 提交于 2019-12-24 01:56:06
问题 I made a Python module in C/C++ with Python C API. I use setuptools.Extension in my setup.py. It creates one .py file which loads a python module from some compiled .pyd file: def __bootstrap__(): global __bootstrap__, __loader__, __file__ import sys, pkg_resources, imp __file__ = pkg_resources.resource_filename(__name__, 'zroya.cp36-win32.pyd') __loader__ = None; del __bootstrap__, __loader__ imp.load_dynamic(__name__,__file__) __bootstrap__() But it does not generate python stubs for IDE

python works in c++ in debug mode, but not in exe file

风流意气都作罢 提交于 2019-12-24 01:54:40
问题 I am working on a Python code embedding in c++ helloworld program, necessary additional include/library directories are properly set up. When I use Local Windows Debugger, it shows "Hello World" correctly. But if I double click project.exe, it says project.exe has stopped working. Does anyone know what kind of configurations or steps to make so that project.exe shows "Hello World" when double clicked?? Code goes like the following: main.cpp #include <iostream> #include <Python.h> #include

embedding python error on initialization

不问归期 提交于 2019-12-23 22:07:41
问题 when im running C code to call python functions, there's error on Py_Initialize() The error is ImportError: No module named site. Ive tried to put Py_SetProgramName(argv[0]) but it doesnt work. The cmd call is cInterfacePython Test.py multiply 3 2 (exe is cInterfacePython) 回答1: I had to muck about a bit with the PATH env-var as well as PYTHONPATH to make things work better when embedding. Py_SetProgramName is not important, it's mostly for internal reference etc... So, I suggest you find

Python C API: Assigning PyObjects to a dictionary causes memory leak

不问归期 提交于 2019-12-23 16:15:24
问题 I am writing a C++ wrapper for Python using the Python C API. In my case I have to make bigger amounts of byte oriented data accessible for the Python script. For this purpose I use the PyByteArray_FromStringAndSize method to produce a Python bytearray (https://docs.python.org/2.7/c-api/bytearray.html). When returning this bytearray directly I have not experienced any problems. When however adding the bytearray into a Python dict, the memory from the bytearray will not be released once the

How to clear a PyListObject?

筅森魡賤 提交于 2019-12-23 15:05:31
问题 I have a question that how to clear a list that's formed by PyList_Append() ? Is there a document about Python/C extention API functions in detail? Thanks. 回答1: IIRC you have to use PyList_SetSlice: PyList_SetSlice(your_list, 0, PyList_Size(your_list), NULL); 回答2: You can use the PySequence_DelSlice function: # The same as: del L[0:len(L)] PySequence_DelSlice(L, 0, PySequence_Length(L)); 来源: https://stackoverflow.com/questions/23489177/how-to-clear-a-pylistobject

Python c-api and unicode strings

若如初见. 提交于 2019-12-23 13:06:23
问题 I need to convert between python objects and c strings of various encodings. Going from a c string to a unicode object was fairly simple using PyUnicode_Decode, however Im not sure how to go the other way //char* can be a wchar_t or any other element size, just make sure it is correctly terminated for its encoding Unicode(const char *str, size_t bytes, const char *encoding="utf-16", const char *errors="strict") :Object(PyUnicode_Decode(str, bytes, encoding, errors)) { //check for any python

Printing a variable in an embedded Python interpreter

China☆狼群 提交于 2019-12-23 12:59:26
问题 I have written a small C program that embeds Python. I'm setting it up correctly using Py_Initialize() and Py_Finalize(), and am able to run scripts either using PyRun_SimpleString or PyRun_SimpleFile. However, I don't know how mimic the behavior of Python's own interpreter when printing variables. Specifically: a = (1, 2, 3) print a Works fine for me: it prints out (1, 2, 3) However: a = (1, 2, 3) a Prints out nothing at all. In Python's own interpreter, this would print out (1, 2, 3) as

Embedding python + numpy code into C++ dll callback

孤人 提交于 2019-12-23 09:40:26
问题 I 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