python-c-api

How to get a char* from a PyObject which points to a string

一世执手 提交于 2019-12-07 11:58:28
问题 How can I get a char* from a PyObject which points to a string. For example, this is the python script, Test.Connect("272.22.20.65", 1234) and this is the C++ code, static PyObject* Connect(PyObject *self, PyObject *args) { PyObject* pIP; PyObject* pPort; if (!PyArg_UnpackTuple(args, "Connect", 2, 2, &pIP, &pPort)) { return NULL; } const char* zIP = GetAsString(pIP); long iPort = PyLong_AsLong(pPort); I want to get that IP address as a char* (GetAsString is a dummy function :D ). Please note

Accessing view of a NumPy array using the C API

℡╲_俬逩灬. 提交于 2019-12-07 08:17:12
问题 In a Python extension module I've written in C++, I use the following snippet of code to convert a NumPy array into an Armadillo array for use in the C++ portion of the code: static arma::mat convertPyArrayToArma(PyArrayObject* pyarr, int nrows, int ncols) { // Check if the dimensions are what I expect. if (!checkPyArrayDimensions(pyarr, nrows, ncols)) throw WrongDimensions(); const std::vector<int> dims = getPyArrayDimensions(pyarr); // Gets the dimensions using the API PyArray_Descr*

Python C-API access String constants

戏子无情 提交于 2019-12-07 06:10:20
问题 I wanted to implement a library I have written for python in C using the C-API of python. In python I can declare "constants" in my module by just stating: RED = "red" # Not really a constant, I know BLUE = "blue" # but suitable, nevertheless def solve(img_h): # Awesome computations return (RED, BLUE)[some_flag] Those constants are then later returned by the functions offered by the module. I have some trouble doing the same thing in C. Here is what I got so far: PyMODINIT_FUNC PyInit_puzzler

How to embed properly using Python for .NET

痞子三分冷 提交于 2019-12-07 05:12:26
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? Looks like my issue was just importing the module without extension (.py). Very stupid mistake, but hope this helps others who start with pythonnet. For one-file

How is __slots__ implemented in Python?

风流意气都作罢 提交于 2019-12-06 20:16:28
问题 How is __slots__ implemented in Python? Is this exposed in the C interface? How do I get __slots__ behaviour when defining a Python class in C via PyTypeObject? 回答1: When creating Python classes, they by default have a __dict__ and you can set any attribute on them. The point of slots is to not create a __dict__ to save space. In the C interface it's the other way around, an extension class has by default no __dict__ , and you would instead explicitly have to add one and add getattr/setattr

“AttributeError: 'module' object has no attribute 'argv'” when using Python.h

拈花ヽ惹草 提交于 2019-12-06 19:16:41
问题 When messing around with Python.h I got this error: AttributeError: 'module' object has no attribute 'argv' C++ code: #include "stdafx.h" #include "C:/Python27/include/Python.h" #include <iostream> using namespace std; int main() { Py_Initialize(); PyRun_SimpleString("import sys\nprint sys.argv[0]"); } Which in Python is: import sys print sys.argv[0] What am I missing? 回答1: Conceptually, sys.argv should contain the arguments that Python was called with (and what it was called under). What

How to extend/reuse Python C Extensions/API implementation?

我是研究僧i 提交于 2019-12-06 15:17:42
问题 The problem is that now, I have to use the Posix C getline function to get the line from the file, only then convert it to a Python Unicode Object using PyUnicode_DecodeUTF8 and cache it using my caching policy algorithm. This process is losing 23% of performance compared to Python builtin for line in file C implementation. If I remove the PyUnicode_DecodeUTF8 call from my code, then, my implementation using the Posix C getline becomes 5% faster than the Python builtin for line in file C

Alternatives of fused type in cython

半城伤御伤魂 提交于 2019-12-06 14:17:52
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. Ignoring ali_m's perfectly valid comment about whether you've actually measured your performance issues.

Embedding CPython: how do you constuct Python callables to wrap C callback pointers?

大兔子大兔子 提交于 2019-12-06 11:52:53
问题 Suppose I am embedding the CPython interpreter into a larger program, written in C. The C component of the program occasionally needs to call functions written in Python, supplying callback functions to them as arguments. Using the CPython extending and embedding APIs, how do I construct a Python "callable" object that wraps a C pointer-to-function, so that I can pass that object to Python code and have the Python code successfully call back into the C code? Note: this is a revised version of

How to redirect stderr in Python? Via Python C API?

…衆ロ難τιáo~ 提交于 2019-12-06 11:39:57
问题 This is a combination of my two recent questions: [1] Python instance method in C [2] How to redirect stderr in Python? I would like to log the output of both stdout and stderr from a python script. The thing I want to ask is, to create a new type according to [1] seems fairly complicated. Does it simplifies the things if there was no need to expose the new type to Python, i.e. it would only exist in C? I mean, when Python prints something it goes to "Objects/fileobject.c" and there in