python-c-api

Python C API How to pass array of structs from C to Python

限于喜欢 提交于 2019-12-03 06:12:00
For a python module I'm creating, I want to pass to the python user an array of structs like this: struct tcpstat { inet_prefix local; inet_prefix remote; int lport; int rport; int state; int rq, wq; int timer; int timeout; int retrs; unsigned ino; int probes; unsigned uid; int refcnt; unsigned long long sk; int rto, ato, qack, cwnd, ssthresh; }; I thought that Py_BuildValues was the function I was looking for. But seems like it isn't. Looking in the Python documentation I found the Buffer Protocol . But is the first time I'm developing a python module and the official documentation didn't

How to tidy/fix PyCXX's creation of new-style Python extension-class?

若如初见. 提交于 2019-12-02 09:45:54
问题 I've nearly finished rewriting a C++ Python wrapper (PyCXX). The original allows old and new style extension classes, but also allows one to derive from the new-style classes: import test // ok a = test.new_style_class(); // also ok class Derived( test.new_style_class() ): def __init__( self ): test_funcmapper.new_style_class.__init__( self ) def derived_func( self ): print( 'derived_func' ) super().func_noargs() def func_noargs( self ): print( 'derived func_noargs' ) d = Derived() The code

AssertionError (3.X only) when calling Py_Finalize with threads

跟風遠走 提交于 2019-12-02 04:41:17
问题 I'm getting an error output when I call the C-API's Py_Finalize() from a different C-thread than I made a python call on. The error I'm seeing is: Exception ignored in: <module 'threading' from 'C:\\Python34-32\\Lib\\threading.py'> Traceback (most recent call last): File "C:\Python34-32\Lib\threading.py", line 1289, in _shutdown assert tlock.locked() AssertionError: This only happens in Python 3.X (tested with 3.4.2), in Python 2.7 the exact same code doesn't have any issues. Here's a minimal

How to tidy/fix PyCXX's creation of new-style Python extension-class?

风格不统一 提交于 2019-12-02 04:20:33
I've nearly finished rewriting a C++ Python wrapper (PyCXX). The original allows old and new style extension classes, but also allows one to derive from the new-style classes: import test // ok a = test.new_style_class(); // also ok class Derived( test.new_style_class() ): def __init__( self ): test_funcmapper.new_style_class.__init__( self ) def derived_func( self ): print( 'derived_func' ) super().func_noargs() def func_noargs( self ): print( 'derived func_noargs' ) d = Derived() The code is convoluted, and appears to contain errors ( Why does PyCXX handle new-style classes in the way it

PyDict_SetItemString segfaults

耗尽温柔 提交于 2019-12-02 02:09:12
I am trying to write a simple C extension for Python3, and it segfaults when I try to add a string to a dictionary. Here is my code: #include <stdio.h> #include <Python.h> int main() { PyObject* dict_p = PyDict_New(); char *val = "idjewijjd"; PyObject* val_p = PyUnicode_FromString(val); const char *key = "dhsjdshj"; for (int j=0; j<8; j++) { printf("%d\n", PyUnicode_READ_CHAR(val_p,j)); } int r = PyDict_SetItemString(dict_p, key, val_p); return 0; } I compile it like this gcc t.c $(python3-config --includes --libs) and run it. I get the following output: $ ./a.out 105 100 106 101 119 105 106

How to improve Python C Extensions file line reading?

跟風遠走 提交于 2019-12-01 14:44:47
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 log data with open('myfile', 'r', errors='replace') as myfile: for line in myfile: if 'word' in line:

How to use setuptools packages and ext_modules with the same name?

青春壹個敷衍的年華 提交于 2019-12-01 12:32:15
I got the following file structure for my Python C Extension project: . ├── setup.py ├── source ├── cppimplementation │ └── fastfile.cpp └── fastfilepackage ├── __init__.py └── version.py And I use the following setup.py file: from setuptools import setup, Extension setup( name= 'fastfilepackage', version= '0.1.1', package_dir = { '': 'source', }, packages = [ 'fastfilepackage', ], ext_modules= [ Extension( 'fastfilepackage', [ 'source/cppimplementation/fastfile.cpp', ] ) ], ) I install them with: $ pip3 --version pip 19.1.1 (python 3.6) $ python3 --version Python 3.6.7 $ pip3 list Package

PyObject segfault on function call

耗尽温柔 提交于 2019-12-01 11:40:13
I'm trying to use Python to open a dialog to accept input into my C++ application. Here is a very minimal representation of what I am trying to do: #include <iostream> #include <Python.h> int main() { /* Begin Python Ititialization - only needs to be done once. */ PyObject *ip_module_name = NULL; PyObject *ip_module = NULL; PyObject *ip_module_contents = NULL; PyObject *ip_module_getip_func = NULL; Py_Initialize(); PyEval_InitThreads(); ip_module_name = PyString_FromString( "get_ip" ); ip_module = PyImport_Import( ip_module_name ); ip_module_contents = PyModule_GetDict( ip_module ); ip_module

How do I properly use Python's C API and exceptions?

空扰寡人 提交于 2019-12-01 03:01:33
if I do something like >>> x = int(1,2,3,4,5) I immediately get a fatal error (one that would end program execution if it was in a pre-written script) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: int() takes at most 2 arguments (5 given) and x remains undefined: >>> x Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'x' is not defined How would I go about implementing that in Python's C API? I found some documentation for it, but I am not sure that I know how to use it correctly. Here is what I have been trying: Print:

Python instance method in C

独自空忆成欢 提交于 2019-12-01 00:43:44
Consider the following Python (3.x) code: class Foo(object): def bar(self): pass foo = Foo() How to write the same functionality in C? I mean, how do I create an object with a method in C? And then create an instance from it? Edit : Oh, sorry! I meant the same functionality via Python C API. How to create a Python method via its C API? Something like: PyObject *Foo = ?????; PyMethod??? *bar = ????; Here's a simple class (adapted from http://nedbatchelder.com/text/whirlext.html for 3.x): #include "Python.h" #include "structmember.h" // The CountDict type. typedef struct { PyObject_HEAD PyObject