python-c-extension

Python: Usage of PyDateTime_FromTimestamp

坚强是说给别人听的谎言 提交于 2019-12-08 00:33:21
问题 I'm working on a python c-extension and want to create an instance of python datetime object with a unix timestamp. On the documentation site ( http://docs.python.org/c-api/datetime.html ) I found the function PyDateTime_FromTimestamp() which returns a new reference based on an input parameter. The description is as follows: Create and return a new datetime.datetime object given an argument tuple suitable for passing to datetime.datetime.fromtimestamp(). I tried out to call the function with

Argument passing in Python module written in C

最后都变了- 提交于 2019-12-06 15:24:29
问题 I have been getting my feet wet with writing Python modules in C. I've started with just a simple example of computing the norm of two points. The code looks like this, _norm.c #include <Python.h> #include "norm.h" static char module_docstring[] = "This module provides an interface for computing the norm using C."; static char norm_docstring[] = "Calculate the norm between two points"; static char norm2_docstring[] = "Calculate the square norm between two points. For efficiency."; static

Python: Usage of PyDateTime_FromTimestamp

故事扮演 提交于 2019-12-06 09:34:19
I'm working on a python c-extension and want to create an instance of python datetime object with a unix timestamp. On the documentation site ( http://docs.python.org/c-api/datetime.html ) I found the function PyDateTime_FromTimestamp() which returns a new reference based on an input parameter. The description is as follows: Create and return a new datetime.datetime object given an argument tuple suitable for passing to datetime.datetime.fromtimestamp(). I tried out to call the function with a PyFloat_Object but the function always returns NULL (even if I simply put in 0). Does somebody have

When is PyEval_InitThreads meant to be called? [duplicate]

六月ゝ 毕业季﹏ 提交于 2019-12-06 02:43:22
问题 This question already has answers here : PyEval_InitThreads in Python 3: How/when to call it? (the saga continues ad nauseam) (7 answers) Closed 6 years ago . I'm a bit confused about when I'm supposed to call PyEval_InitThreads . In general, I understand that PyEval_InitThreads must be called whenever a non-Python thread (i.e. a thread that is spawned within an extension module) is used. However, I'm confused if PyEval_InitThreads is for C programs which embed the Python interpreter, or

How to have PyPI package install header files for C extension with distutils/setuptools?

主宰稳场 提交于 2019-12-06 01:07:32
We have a package (rebound) up on PyPI that includes a C extension . The relevant part of the setup.py file looks like this (simplified): libreboundmodule = Extension('librebound', sources = [ 'src/rebound.c'], include_dirs = ['src'],) Additional libraries need access to rebound.h, but when one runs pip install rebound it doesn't install rebound.h anywhere. How can we get distutils/setuptools to install rebound.h somewhere along with all the python modules? We're hoping that we can have pip install rebound do all the work so the user doesn't have to run any additional commands. 来源: https:/

segmentation fault - core dump in python C-extension

江枫思渺然 提交于 2019-12-05 19:45:53
I am writing a c-extension for python. As you can see below, the aim of the code is to calculate the euclidean-dist of two vectors. the first param n is the dimension of the vectors, the second , the third param is the two list of float. I call the function in python like this: import cutil cutil.c_euclidean_dist(2,[1.0,1,0],[0,0]) and it worked well, return the right result. but if i do it for more than 100 times(the dimension is 1*1000), it will cause segmentation fault - core dump: #!/usr/bin/env python #coding:utf-8 import cutil import science import time a = [] b = [] d = 0.0 for x in

PyList_SetItem vs. PyList_SETITEM

强颜欢笑 提交于 2019-12-05 05:33:09
From what I can tell, the difference between PyList_SetItem and PyList_SETITEM is that PyList_SetItem will lower the reference count of the list item it overwrites and PyList_SETITEM does not. Is there any reason why I shouldn't just use PyList_SetItem all the time? Or would I get into trouble if I used PyList_SetItem to initialize an index position in a list? PyList_SET_ITEM is an unsafe macro that basically sticks an object into the list's internal pointer array without any bound checks. If anything non- NULL is in the i th position of the list, a reference leak will occur. PyList_SET_ITEM

How to use C extensions in python to get around GIL

落爺英雄遲暮 提交于 2019-12-05 01:17:22
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? 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 this pipeline by using cPickle on sys.stdin and sys.stdout . Without too much work, this can often lead to

Argument passing in Python module written in C

霸气de小男生 提交于 2019-12-04 21:40:19
I have been getting my feet wet with writing Python modules in C. I've started with just a simple example of computing the norm of two points. The code looks like this, _norm.c #include <Python.h> #include "norm.h" static char module_docstring[] = "This module provides an interface for computing the norm using C."; static char norm_docstring[] = "Calculate the norm between two points"; static char norm2_docstring[] = "Calculate the square norm between two points. For efficiency."; static PyObject *norm_norm(PyObject *self, PyObject *args); static PyObject *norm_norm2(PyObject *self, PyObject

Passing 3-dimensional numpy array to C

社会主义新天地 提交于 2019-12-04 08:31:42
问题 I'm writing a C extension to my Python program for speed purposes, and running into some very strange behaviour trying to pass in a 3-dimensional numpy array. It works with a 2-dimensional array, but I'm sure I'm screwing something up with the pointers trying to get it to work with the 3rd dimension. But here's the weird part. If I just pass in a 3-D array, it crashes with a Bus Error . If (in Python) I create my variable as a 2D array first, and then overwrite it with a 3D array, it works