python-c-api

Returning objects to Python from C

╄→гoц情女王★ 提交于 2020-01-01 05:09:07
问题 I've read the documentation for the Python C-API, and even written a few extension modules. However, I'm still a bit unclear on the exact semantics when it comes to returning Python objects from a C function. The limited examples in the Python docs usually show a C function which returns the result of Py_BuildValue . Now, Py_BuildValue returns a New Reference , and transfers ownership of this reference over to the interpreter. So, can I extrapolate from this that it is a general rule that any

Why does PyCXX handle new-style classes in the way it does?

孤街浪徒 提交于 2019-12-31 04:44:05
问题 I'm picking apart some C++ Python wrapper code that allows the consumer to construct custom old style and new style Python classes from C++. The original code comes from PyCXX, with old and new style classes here and here. I have however rewritten the code substantially, and in this question I will reference my own code, as it allows me to present the situation in the greatest clarity that I am able. I think there would be very few individuals capable of understanding the original code

How to improve Python C Extensions file line reading?

落爺英雄遲暮 提交于 2019-12-30 16:39:15
问题 Originally asked on Are there alternative and portable algorithm implementation for reading lines from a file on Windows (Visual Studio Compiler) and Linux? but closed as too abroad, then, I am here trying to reduce its scope with a more concise case usage. My goal is to implement my own file reading module for Python with Python C Extensions with a line caching policy. The purely Python Algorithm implementation without any line caching policy is this: # This takes 1 second to parse 100MB of

How to improve Python C Extensions file line reading?

怎甘沉沦 提交于 2019-12-30 16:39:14
问题 Originally asked on Are there alternative and portable algorithm implementation for reading lines from a file on Windows (Visual Studio Compiler) and Linux? but closed as too abroad, then, I am here trying to reduce its scope with a more concise case usage. My goal is to implement my own file reading module for Python with Python C Extensions with a line caching policy. The purely Python Algorithm implementation without any line caching policy is this: # This takes 1 second to parse 100MB of

reading a global variable of python in c

£可爱£侵袭症+ 提交于 2019-12-30 11:20:52
问题 I am trying to learn how to use the Python/C API correctly - all I actually need to do is to read a global variable (in my case dictionary - but I'm starting with a simple integer variable). Using the discussion: How to access a Python global variable from C? and the source of the answer there: http://bytes.com/topic/python/answers/705918-c-api-embedded-python-how-get-set-named-variables I wrote this little thing: Python code (tryStuff.py): var1 = 1 var2 = ['bla', 'blalba'] var3 = {"3" :

Static Variables in Python C API

一曲冷凌霜 提交于 2019-12-30 07:00:48
问题 How would one expose "static" variables like this class MyClass: X = 1 Y = 2 via the C API? The only variable on the PyTypeObject that looks like it would work is tp_members, but I see no flag in the PyMemberDef to indicate that the member should be per-class, not per-instance. For a bit more clarification, since it may change the answer, I'm trying to expose a C enum to Python such that the enumeration enum MyFlags { Alpha = 0, Beta = 1 }; Can be accessed in Python as: module.MyFlags.Alpha

Python: GIL context - switching

◇◆丶佛笑我妖孽 提交于 2019-12-30 04:43:08
问题 So, I generally have a pretty good understanding of how the Global Interpreter Lock (GIL) in Python works. Essentially, while the interpreter is running, one thread holds the GIL for N ticks (where N can be set using sys.setcheckinterval ), at which point the GIL is released and another thread can acquire the GIL. The also happens if one thread begins an I/O operation. What I'm a bit confused about is how this all works with C extension modules. If you have a C extension module that acquires

How to pass an array from C to an embedded python script

戏子无情 提交于 2019-12-28 11:52:34
问题 I am running to some problems and would like some help. I have a piece code, which is used to embed a python script. This python script contains a function which will expect to receive an array as an argument (in this case I am using numpy array within the python script). I would like to know how can I pass an array from C to the embedded python script as an argument for the function within the script. More specifically can someone show me a simple example of this. 回答1: Really, the best

How to pass an array from C to an embedded python script

耗尽温柔 提交于 2019-12-28 11:52:25
问题 I am running to some problems and would like some help. I have a piece code, which is used to embed a python script. This python script contains a function which will expect to receive an array as an argument (in this case I am using numpy array within the python script). I would like to know how can I pass an array from C to the embedded python script as an argument for the function within the script. More specifically can someone show me a simple example of this. 回答1: Really, the best

error: value of type 'PyObject' (aka '_object') is not contextually convertible to 'bool'

喜夏-厌秋 提交于 2019-12-25 02:55:08
问题 I am passing a python module to C as a PyObject . I want to check to see if this value is NONE in my C code, using this form: int func(PyObject tmp) { if(tmp) { // etc I am getting the following error. How can I convert from a PyObject to boolean value, simillar to the way Python's if function behaves. It is worth noting that when tmp is a boost::python::object variable this command works as expected. ex_program.cpp:72:7: error: value of type 'PyObject' (aka '_object') is not contextually