python-c-extension

In Python how can one tell if a module comes from a C extension?

你。 提交于 2019-12-12 07:14:08
问题 What is the correct or most robust way to tell from Python if an imported module comes from a C extension as opposed to a pure Python module? This is useful, for example, if a Python package has a module with both a pure Python implementation and a C implementation, and you want to be able to tell at runtime which one is being used. One idea is to examine the file extension of module.__file__ , but I'm not sure all the file extensions one should check for and if this approach is necessarily

Can I develop C extensions for Python 2.7 through 3.1 on Windows *without* installing Visual Studio 2008?

ぃ、小莉子 提交于 2019-12-12 05:00:28
问题 I'm aware that Python extensions on Windows typically have to be built with the same version of Visual Studio used to compile Python itself, and I'm further aware that Python 2.7 through 3.1 are built using Visual Studio 2008. But the machine I'm on already has VS 2013 installed, and, as I've discovered countless times, one way to rapidly mess up your Windows development environments is to install Visual Studio in any order than from oldest to newest. Besides which, install VS2008 on a brand

Obtaining address of custom data-type in C from Python using ctypes

蓝咒 提交于 2019-12-12 00:50:54
问题 I have a vector struct in C with the following fields, struct vector { unsigned char* data; unsigned long size; unsigned long elemsize; unsigned long capacity; }; and there are a few functions which correspondingly act on vector instances, such as: struct vector* vector_new(unsigned long elemsize); void vector_delete(struct vector* vec); void vector_push_back(struct vector* vec, void* value, unsigned long elemsize); void vector_reserve(struct vector* vec, unsigned long cap); ... and so on

Return list of new custom-class objects in python C API

允我心安 提交于 2019-12-11 01:53:16
问题 I need to create a new list via the python C API containing new copies of objects of a Quaternion class I've written (in C++). [Actually, I'd really like a numpy array, but any sort of sequence would do.] But I'm getting seg faults with everything I've tried. I'm terrible with pointers, so that's not a big surprise. I'm thinking maybe I need to give python ownership of the objects I create with new . The best I've gotten so far appears below. Am I not supposed to copy-construct the Quaternion

Passing a C struct to a Python function

左心房为你撑大大i 提交于 2019-12-10 17:35:47
问题 I need a simple way to pass a C struct to a Python function. I have embedded Python into a game server, and I intend to write game logic in Python. I've scoured Google and mailing lists and found nothing useful. I have a complex structure in C (with pointers to other relatively complex structures) and have found no reasonable way of doing this. I have this struct: struct client { int state; int sockfd; struct sockaddr_in *addr; struct epoll_event *epollev; struct buffer *in_buffer; struct

How can I set log level used by distutils when using pip?

瘦欲@ 提交于 2019-12-10 14:53:35
问题 I'm trying to find out what's the reason of error: Unable to find vcvarsall.bat after pip install greenlet . I'd like to set log level used by distutils so that debug messages like log.debug("Unable to find productdir in registry") would get printed. After looking at def parse_command_line(self): I thought pip install --install-option="-vv" greenlet should work but it doesn't (verbosity is still 1). How can I do this? 回答1: The correct way is to use the --global-option="-vv" switch for pip

How to use C extensions in python to get around GIL

守給你的承諾、 提交于 2019-12-10 01:47:54
问题 I want to run a cpu intensive program in Python across multiple cores and am trying to figure out how to write C extensions to do this. Are there any code samples or tutorials on this? 回答1: You can already break a Python program into multiple processes. The OS will already allocate your processes across all the cores. Do this. python part1.py | python part2.py | python part3.py | ... etc. The OS will assure that part uses as many resources as possible. You can trivially pass information along

How does PyArg_ParseTupleAndKeywords work?

◇◆丶佛笑我妖孽 提交于 2019-12-09 02:20:09
问题 I've been trying to learn how to write C-extensions for Python and want to be sure I understand how PyArg_ParseTupleAndKeywords works. I believe that the first argument is a PyObject pointer that points to an array of the arguments being passed into the C-extension function in the order they were passed. The second argument is a list of keywords that were passed, the positions at which they were passed and, very likely, some sort of indicator flag telling at which position the keywords begin

C array to PyArray

左心房为你撑大大i 提交于 2019-12-08 16:37:20
问题 I'm writing a Python C-Extension without using Cython. I want to allocate a double array in C, use it in an internal function (that happens to be in Fortran) and return it. I point out that the C-Fortran interface works perfectly in C. static PyObject * Py_drecur(PyObject *self, PyObject *args) { // INPUT int n; int ipoly; double al; double be; if (!PyArg_ParseTuple(args, "iidd", &n, &ipoly, &al, &be)) return NULL; // OUTPUT int nd = 1; npy_intp dims[] = {n}; double a[n]; double b[n]; int

PyArg_ParseTuple causing segmentation fault

荒凉一梦 提交于 2019-12-08 13:17:15
问题 I'm trying to call a c function from my extension and have narrowed the problem down to this test case. #import "Python.h" ... // Called from python with test_method(0, 0, 'TEST') static PyObject* test_method(PyObject *args) { int ok, x, y, size; const char *s; // this causes Segmentation fault //ok = PyArg_ParseTuple(args, "iis#", &x, &y, &s, &size); // also segfaults //if(ok) PyErr_SetString(PyExc_SystemError, 'Exception'); // this does not cause segfault but fills the variables with