python-c-api

Python extension - construct and inspect large integers efficiently

我的未来我决定 提交于 2019-12-08 15:41:05
问题 I have a native library for which a natural interface would involve passing potentially large numbers. I anticipate about half being < 32 bits; another quarter < 64 bits; the next eighth < 128 bits - and so on, without a fixed length limit. PyLong_FromUnsignedLongLong() and PyLong_AsUnsignedLongLong() would be suitable if I could constrain values to fit in a single register. PyLong_FromString() overcomes this - but at the undesirable expense of requiring an intermediate representation.

How to clear a PyList object that contains objects?

一世执手 提交于 2019-12-08 10:40:16
问题 I am trying to clear a Python list I pass to a C function using ctypes. The calls I found at this link seem to work sometimes, but when the list I pass in contains other lists or classes as elements I get a segfault. I have a file foo.py that looks like this: import ctypes _libpractice=ctypes.CDLL('./_practice.so', mode=ctypes.RTLD_GLOBAL) _libpractice.update.argtypes = [ctypes.py_object] _libpractice.update.restype = ctypes.py_object def c_update(my_list): return _libpractice.update(my_list)

Integrating C and Python: ValueError: module functions cannot set METH_CLASS or METH_STATIC

浪尽此生 提交于 2019-12-08 05:13:02
问题 I am making my first venture into integrating C and Python 2.7.3. For starters, I'm just trying to write a C module for Python that can do basic addition. (It is called npfind because once I figure this out, I want to write a find method for numpy) npfind.h: #include <math.h> extern int add(int a, int b); npfind.c: #include "npfind.h" int add(int a, int b) { return a + b; } pynpfind.c: #include "Python.h" #include "npfind.h" static char* py_add_doc = "Adds two numbers."; static PyObject* py

How to embed properly using Python for .NET

*爱你&永不变心* 提交于 2019-12-08 05:04:41
问题 When I try to use PythonEngine.ImportModule(mymodulename) some of the optional modules in dependencies are attempted to be loaded (not required for module use without embedding). This results in return null from this method because some of these optional dependencies are not required and hence not available. What is the proper method to use in this PythonNET API for loading user-written module which depends on multiple other modules? 回答1: Looks like my issue was just importing the module

Allocating a dynamic array in a dynamically allocated struct (struct of arrays)

感情迁移 提交于 2019-12-08 03:58:31
问题 This question is really about how to use variable-length types in the Python/C API (PyObject_NewVar, PyObject_VAR_HEAD, PyTypeObject.tp_basicsize and .tp_itemsize , but I can ask this question without bothering with the details of the API. Just assume I need to use an array inside a struct . I can create a list data structure in one of two ways. (I'll just talk about char lists for now, but it doesn't matter.) The first uses a pointer and requires two allocations . Ignoring #include s and

Large POST data is corrupted when using Django/PyISAPIe/IIS

旧巷老猫 提交于 2019-12-08 03:04:20
问题 I'm running into a problem with large POST data (>16384 bytes) when using Django 1.2.3, PyISAPIe v1.1.0-rc4, and IIS 7.5. For example, when submitting approx. 60kB of form data using POST, the following happens: The first 16kB block of POST data are correct The next 16kB block is a repeat of the first block The next 16kB is another repeat of the first block The rest (<16kB) is correct again The interesting part is that when using content-type="multipart/form-data" , it works fine. Using this

C Python API Extensions is ignoring open(errors=“ignore”) and keeps throwing the encoding exception anyways

不羁的心 提交于 2019-12-08 02:18:13
问题 Given a file /myfiles/file_with_invalid_encoding.txt with invalid UTF8 as: parse this correctly Føö»BÃ¥r also parse this correctly I am using the builtin Python open function from the C API as follows the minimal example (excluding C Python setup boilerplate): const char* filepath = "/myfiles/file_with_invalid_encoding.txt"; PyObject* iomodule = PyImport_ImportModule( "builtins" ); if( iomodule == NULL ) { PyErr_PrintEx(100); return; } PyObject* openfunction = PyObject_GetAttrString(

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

Alternatives of fused type in cython

可紊 提交于 2019-12-07 23:10:30
问题 I am working on rewriting a python module originally written in C using python-C api to Cython.The module also uses NumPy. A major challenge of the project is to maintain the current speed of module and also it should work for all Numpy data types. I am thinking to use fused data type to make it generic but I am worried because of its bottleneck effect on performance. Are there any other technique that can be used instead of fused type which I can use to achieve both speed and generic code.

Why and where python interned strings when executing `a = 'python'` while the source code does not show that?

独自空忆成欢 提交于 2019-12-07 12:10:59
问题 I am trying to learn the intern mechanism of python using in the implementation of string object. But in both PyObject *PyString_FromString(const char *str) and PyObject *PyString_FromStringAndSize(const char *str, Py_ssize_t size) python interned strings only when its size is 0 or 1. PyObject * PyString_FromString(const char *str) { fprintf(stdout, "creating %s\n", str);------------[1] //... //creating... /* share short strings */ if (size == 0) { PyObject *t = (PyObject *)op; PyString