python-c-api

How to pass const char* from python to c function

寵の児 提交于 2020-03-19 05:32:11
问题 I am using ctypes in Python to open a file for writing in C++. My C++ code: extern "C" { void openfile(const char *filename) { cout<<"File to open for writing = " <<filename<<endl; FILE *fp = fopen(filename,"w"); fprintf(fp,"writing into file"); fclose(fp); } } My Python code: >>> import ctypes >>> lib = ctypes.cdll.LoadLibrary('/in/vrtime/mahesh/blue/rnd/software/test/test.so') >>> outfile = "myfirstfile.txt" >>> lib.openfile(outfile) File to open for writing = m I am getting the file name

A full and minimal example for a class (not method) with Python C Extension?

狂风中的少年 提交于 2020-02-27 04:02:27
问题 Everywhere, I can easily find an example about writing a method with Python C Extensions and use it in Python. Like this one: Python 3 extension example $ python3 >>> import hello >>> hello.hello_world() Hello, world! >>> hello.hello('world') Hello, world! How to do write a hello word full featured Python class (not just a module method)? I think this How to wrap a C++ object using pure Python Extension API (python3)? question has an example, but it does not seem minimal as he is using (or

f2py linking quadmath libraries? Use ctypes for fortran wrapper instead?

十年热恋 提交于 2020-01-25 08:00:09
问题 Update 11/23/2019: This started out as a question about why I could not get f2py to work for a simple fortran wrapper. My "answer" (below) is to use ctypes instead. Original post: I have spent the last three days trying to use f2py to interface fortran to python. I am working on windows using both cygwin and mingw. This post is about using cygwin, but I'm concerned about conflicts between the two. Here is the source multxy.f90: subroutine multxy(x,y,z) integer, parameter :: flt = selected

Python C API - Is it thread safe?

陌路散爱 提交于 2020-01-24 10:56:25
问题 I have a C extension that is called from my multithreaded Python application. I use a static variable i somewhere in a C function, and I have a few i++ statements later on that can be run from different Python threads (that variable is only used in my C code though, I don't yield it to Python). For some reason I haven't met any race condition so far, but I wonder if it's just luck... I don't have any thread-related C code (no Py_BEGIN_ALLOW_THREADS or anything). I know that the GIL only

Multiple Inheritance in Python C API

 ̄綄美尐妖づ 提交于 2020-01-23 02:42:27
问题 How do I create a type using the Python C API that inherits from multiple other types? The Python documentation includes an example of a type that inherits from one other type, but there is no example or mention of multiple inheritance I could find. 回答1: The C API does not support multiple inheritance. You'd have to call PyType_Type yourself, simulating a standard Python class statement. This is documented under the C API section on specifying a base type for an extension type: PyTypeObject*

Python multi-thread multi-interpreter C API

放肆的年华 提交于 2020-01-22 05:19:24
问题 I'm playing around with the C API for Python, but it is quite difficult to understand some corner cases. I could test it, but it seems a bug-prone and time consuming. So I come here to see if somebody has already done this. The question is, which is the correct way to manage a multi-thread with sub-interpreters, with no direct relation between threads and sub-interpreters? Py_Initialize(); PyEval_InitThreads(); /* <-- needed? */ _main = PyEval_SaveThread(); /* <-- acquire lock? does it matter

Embedded Python: Multiple Sub-Interpreters not working

99封情书 提交于 2020-01-16 19:35:08
问题 I'm trying to understand sub-interpreters and GIL. But my experiment is failing often(The same code rarely works). Gathering info from SO questions and few sites, I have the following code which spawns 2 non-python threads. Each of these threads are given a python sub-interpreter. I want to release GIL inside these threads and call a DLL function in C++(though this example does not detail that. Here I just write to stdout). Basically I want to see concurrency in the execution(Non-Python DLL

“No module named '_<module>'” when importing a SWIG module with embedded Python

你离开我真会死。 提交于 2020-01-15 10:40:06
问题 I'm trying to use SWIG with embedded Python 3.5.2. The following is built as a Windows console app. It fails initializing the Python side SWIG module "arpy.py" when it tries to import the C++ side SWIG module "_arpy". My (probably incorrect) understanding it that the C++ side "_arpy" module should already be loaded by the SWIG module init function called from main() but this doesn't seem to be the case. arpy.i: %module arpy %{ #include "arpy.h" %} %include <windows.i> int test(); arpy.h:

How to call a python function by name from the C-API?

岁酱吖の 提交于 2020-01-15 03:29:26
问题 From the c-api, I would like to call a python function by name. I would then be calling the function with a list of python objects as arguments. It is not clear to me in the Python documentation how I would get a "Callable" object from the main python interpreter. Any help appreciated in: Getting the address from the function Calling the function with my PythonObject's as arguments. I'm using Python 2.x series for my development. 回答1: Basically, you use the Python C API to get the module the

In Python, why is a module implemented in C faster than a pure Python module, and how do I write one?

孤者浪人 提交于 2020-01-12 07:12:48
问题 The python documentation states, that the reason cPickle is faster than Pickle is, that the former is implemented in C. What does that mean exactly? I am making a module for advanced mathematics in Python, and some calculations take a significant amount of time. Does that mean that if my program is implemented in C it can be made much faster? I wish to import this module from other Python programs, just the way I can import cPickle. Can you explain how to do implement a Python module in C?