python-extensions

How to build a Python C Extension so I can import it from a module

岁酱吖の 提交于 2019-11-28 06:27:19
I have a Python project with many sub-modules that I package up with distutils. I would like to build some Python extensions in C to live in some of these sub-modules but I don't understand how to get the Python extension to live in a submodule. What follows is the simplest example of what I'm looking for: Here is my Python extension c_extension.c : #include <Python.h> static PyObject * get_answer(PyObject *self, PyObject *args) { return Py_BuildValue("i", 42); } static PyMethodDef Methods[] = { {"get_answer", get_answer, METH_VARARGS, "The meaning of life."}, {NULL, NULL, 0, NULL} };

compile libdnet for python 2.7

三世轮回 提交于 2019-11-28 05:44:35
问题 I'm trying to use scapy on win32 python2.7 I've manage to compile all the other dependencies expect this one can some help in the goal of reaching this executable ? "dnet-1.12.win32-py2.7.exe" (I promise to update the this question too and the scapy manual, Running Scapy on Windows with Python 2.7) Update: I've managed to compile it with mingw32 I'm using vs2005, and I have to make some fixes to libdnet to actually work (look like last time they compiled it on windows it was with vs6.0 I'll

Running Cython in Windows x64 - fatal error C1083: Cannot open include file: 'basetsd.h': No such file or directory

放肆的年华 提交于 2019-11-27 07:16:43
I have been trying to install Cython for Python 2.7 on my Window 7 system. In particular, I prefer everything in 64 bits. (In case you wonder, I need Cython because Cython is one of the components I need for another package for some specialized numerical analysis. And x64 is potentially an advantage for storage of large data sets.) So I downloaded the x64 Python 2.7 from the official website. Got Cython from Christoph Gohlke . The amd64 version for Python 2.7 of course. Before I installed, I added Python 2.7 into the registry with the .reg file found in Joe DF's answer here . Afterwards, I

Create an object using Python's C API

一曲冷凌霜 提交于 2019-11-27 07:04:35
Say I have my object layout defined as: typedef struct { PyObject_HEAD // Other stuff... } pyfoo; ...and my type definition: static PyTypeObject pyfoo_T = { PyObject_HEAD_INIT(NULL) // ... pyfoo_new, }; How do I create a new instance of pyfoo somewhere within my C extension? Call PyObject_New() , followed by PyObject_Init() . EDIT: The best way is to call the class object, just like in Python itself: /* Pass two arguments, a string and an int. */ PyObject *argList = Py_BuildValue("si", "hello", 42); /* Call the class object. */ PyObject *obj = PyObject_CallObject((PyObject *) &pyfoo_T, argList

pylint 1.4 reports E1101(no-member) on all C extensions

时光怂恿深爱的人放手 提交于 2019-11-27 04:10:17
We've been long-time fans of pylint . Its static analysis has become a critical part of all our python projects and has saved tons of time chasing obscure bugs. But after upgrading from 1.3 -> 1.4, almost all compiled c extensions result in E1101(no-member) errors. Projects that previously run perfectly clean through pylint 1.3 now complain about almost every C extension member with E1101. We've been forced to disable E1101 errors, but this materially detracts from the usefulness of pylint . For example, this perfectly valid use of the lxml package r"""valid.py: demonstrate pylint 1.4 error"""

Running Cython in Windows x64 - fatal error C1083: Cannot open include file: &#39;basetsd.h&#39;: No such file or directory

十年热恋 提交于 2019-11-26 13:07:09
问题 I have been trying to install Cython for Python 2.7 on my Window 7 system. In particular, I prefer everything in 64 bits. (In case you wonder, I need Cython because Cython is one of the components I need for another package for some specialized numerical analysis. And x64 is potentially an advantage for storage of large data sets.) So I downloaded the x64 Python 2.7 from the official website. Got Cython from Christoph Gohlke. The amd64 version for Python 2.7 of course. Before I installed, I

Create an object using Python&#39;s C API

独自空忆成欢 提交于 2019-11-26 13:01:35
问题 Say I have my object layout defined as: typedef struct { PyObject_HEAD // Other stuff... } pyfoo; ...and my type definition: static PyTypeObject pyfoo_T = { PyObject_HEAD_INIT(NULL) // ... pyfoo_new, }; How do I create a new instance of pyfoo somewhere within my C extension? 回答1: Call PyObject_New(), followed by PyObject_Init(). EDIT: The best way is to call the class object, just like in Python itself: /* Pass two arguments, a string and an int. */ PyObject *argList = Py_BuildValue("si",

pylint 1.4 reports E1101(no-member) on all C extensions

南楼画角 提交于 2019-11-26 11:05:39
问题 We\'ve been long-time fans of pylint . Its static analysis has become a critical part of all our python projects and has saved tons of time chasing obscure bugs. But after upgrading from 1.3 -> 1.4, almost all compiled c extensions result in E1101(no-member) errors. Projects that previously run perfectly clean through pylint 1.3 now complain about almost every C extension member with E1101. We\'ve been forced to disable E1101 errors, but this materially detracts from the usefulness of pylint