python-c-api

How can I assert from Python C code?

社会主义新天地 提交于 2019-12-13 13:03:32
问题 I'm writing a Python class in C and I want to put assertions in my debug code. assert.h suits me fine. This only gets put in debug compiles so there's no chance of an assert failure impacting a user of the Python code*. I'm trying to divide my 'library' code (which should be separate to the code linked against Python) so I can use it from other C code. My Python methods are therefore thinnish wrappers around my pure-C code. So I can't do this in my 'library' code: if (black == white) { PyErr

Are there any datetime.tzinfo implementations in C?

為{幸葍}努か 提交于 2019-12-13 08:14:35
问题 I've been working on a Python library that uses a C extension module to do ISO 8601 parsing. Part of that work requires the creation of tzinfo objects, which is by far the slowest part of the parse. Calls out to Python implementations of tzinfo (currently pytz.FixedOffset ) are simply too slow. In Python 3.7, datetime.timezone is finally exposed to the C-API. My code takes adavantage of it, and gets a tremendous performance boost from using a C implementation instead of a Python one. I'd love

Linking C module with Python 3.7. CMake in Windows

流过昼夜 提交于 2019-12-13 04:16:08
问题 I'm trying to create a C library that can be called from Python, so far I've created the proxy C file that exposes the module information and the method table (For simplicity just one method is added get_cycle_count and its implementation irrelevant): static PyMethodDef NESulator_methods[] = { {"NESulator", get_cycle_count, METH_VARARGS, "Retrieves the current cycle count"}, {NULL, NULL, 0, NULL} /* Sentinel */ }; static struct PyModuleDef NESulator_module = { PyModuleDef_HEAD_INIT,

Python C API: Call a Python function with argument(s) passed by reference

那年仲夏 提交于 2019-12-13 00:45:53
问题 I'm writing a python module in C and I need to have it call a Python function with one of the arguments passed by "reference". The end result should be that what the Python function does to the argument gets saved into the original C variable. int *resume; // this int is actually passed in to this body of code PyObject *resumeInt; // which was originally a C callback func for libnids, but PyObject *ret; // I removed/rewrote most of this code for clarity resumeInt = Py_BuildValue("i",-1); ret

Python C api - function overloading

痴心易碎 提交于 2019-12-12 19:41:07
问题 I have a number of C functions that accept different arguments, e.g. foo_i(int a) foo_c(char c) Is it possible to overload these functions in python C api? I tried to use the following methods table: static PyMethodDef test_methods[] = { {"foo", (PyCFunction)foo_i, METH_VARARGS, "int"}, {"foo", (PyCFunction)foo_c, METH_VARARGS, "char"}, {NULL, NULL, 0, NULL} }; But when I invoke foo from python I always end up using the function at the bottom of the table. Any ideas on how to invoke both foo

Pointer-type mismatch with PyArray_SimpleNew

℡╲_俬逩灬. 提交于 2019-12-12 14:26:52
问题 I am creating a module for Python with Numpy using the C API and encounter weird incompatibilities with the output of PyArray_SimpleNew , which I would like to understand. But first a minimal example: # include <Python.h> # include <numpy/arrayobject.h> void foo() { Py_Initialize(); import_array(); npy_intp dims[1] = {42}; PyObject * A = PyArray_SimpleNew(1,dims,NPY_DOUBLE); // Line A Py_Finalize(); } int main() { foo(); return 0; } If I compile this with gcc source.c -lpython2.7 -I/usr

std::bad_alloc after replacing boost:python function wrapper with Python/C API

£可爱£侵袭症+ 提交于 2019-12-12 14:10:15
问题 I had a function in C which I used to extend python, previously using the BOOST_MODULE function to accomplish this. This error came up when transitioning to the python-C API. I am certain that the run_mymodule function runs fine without this wrapper. static PyObject * wrap_run_mymodule(PyObject *, PyObject *args) { char *file1, *file2, *file3; PyObject *tmpp; if(!PyArg_ParseTuple(args, "sssO", &file1, &file2, &file3, &tmpp)) return NULL; return Py_BuildValue("i", run_mymodule(file1, file2,

How to import a file by its full path using C api?

可紊 提交于 2019-12-12 11:56:23
问题 PyObject* PyImport_ImportModule( const char *name) How to specify a full file path instead and a module name? Like PyImport_SomeFunction(const char *path_to_script, const char *name) Thanks, Elias 回答1: Another solution for cases when all *.py file are in one directory: PySys_SetPath("path/with/python/files"); PyObject *pModule = PyImport_ImportModule("filename_without_extension"); 回答2: As pointed out, using the above solution by AlexP, you can't import any modules outside of the specified

Create a Python type from C that implements a __dict__?

这一生的挚爱 提交于 2019-12-12 07:56:48
问题 How is a type created to have a __dict__ as per a "normal" class would have were it defined in Python? Are there any examples of non-dynamic types with __dict__ s? Do types defined via Python's PyTypeObject pass through type_new? There is a tp_dict member of PyTypeObject , but I can find no information on how it's used. There also seems to be something going on in typeobject.c 's type_new but I can't decipher it clearly. Here is some related information I've found: __dict__ in class inherited

PyArg_ParseTuple() on arbitrary tuples

梦想与她 提交于 2019-12-12 05:38:41
问题 I am looking for confirmation on this issue: Can I use PyArg_ParseTuple() on any Python tuple, or just on those passed as argument lists from function calls? I see strong indication for the former, but to my reading the documentation is rather vague on this point, hence my question here. 回答1: The only problem is that the error messages, if an error occurs while parsing the tuple, will be appropriate to a function call. Otherwise, it should work on arbitrary tuples just as well as on argument