cpython

Why is the float * int multiplication faster than int * float in CPython?

Why is the float * int multiplication faster than int * float in CPython? Question: Basically, the expression 0.4 * a is consistently, and surprisingly, significantly faster than a * 0.4. a being an integer. And I have no idea why. I speculated that it is a case of a LOAD_CONST LOAD_FAST bytecode pair being …

Total answers: 1

How to convert python code into a simple c haeder?

How to convert python code into a simple c haeder? Question: I’m Working on a program that takes in text from the user and then implements functionalities in the backend, kind of like an interpreter, I have the parser working amazingly in python but some of the backend capabilities I feel would do great on …

Total answers: 1

XOR encryption with a PyObject

XOR encryption with a PyObject Question: I’m having trouble encrypting bytes from a PyObject using XOR. For now I only managed to print the bytes as an encoded string (with PyUnicode_AsEncodedString): Here’s what I tried (taken from this SO answer) PyObject* repr = PyObject_Repr(wf.str); // wf.str is a PyObject * PyObject* str = PyUnicode_AsEncodedString(repr, "utf-8", …

Total answers: 2

compile official doc "Embedding Python in Another Application" example failed

compile official doc "Embedding Python in Another Application" example failed Question: I am trying to compile and run the example from https://docs.python.org/3/extending/embedding.html#very-high-level-embedding , but failed. My environment is Ubuntu 20.04.2 LTS, with system shipped python3.8(statically built), libpython3-dev and libpython3.8-dev packages installed. What I’ve tried: main.c : #define PY_SSIZE_T_CLEAN #include <Python.h> int main(int argc, char *argv[]) …

Total answers: 2

MacOS M1 system is detected as ARM by Python package even though I'm using Rosetta

MacOS M1 system is detected as ARM by Python package even though I'm using Rosetta Question: I’m on a Macbook with M1 (Apple ARM architecture) and I’ve tried running the following Python code using the layoutparser library, which indirectly uses pycocotools: import layoutparser as lp lp.Detectron2LayoutModel() And I’ve received the error: […] ImportError: dlopen([…]/.venv/lib/python3.9/site-packages/pycocotools/_mask.cpython-39-darwin.so, 0x0002): …

Total answers: 1

Why don't Python sets preserve insertion order?

Why don't Python sets preserve insertion order? Question: I was surprised to discover recently that while dicts are guaranteed to preserve insertion order in Python 3.7+, sets are not: >>> d = {‘a’: 1, ‘b’: 2, ‘c’: 3} >>> d {‘a’: 1, ‘b’: 2, ‘c’: 3} >>> d[‘d’] = 4 >>> d {‘a’: 1, ‘b’: …

Total answers: 2

What causes [*a] to overallocate?

What causes [*a] to overallocate? Question: Apparently list(a) doesn’t overallocate, [x for x in a] overallocates at some points, and [*a] overallocates all the time? Here are sizes n from 0 to 12 and the resulting sizes in bytes for the three methods: 0 56 56 56 1 64 88 88 2 72 88 96 …

Total answers: 3

Running Python code in parallel from Rust with rust-cpython

Running Python code in parallel from Rust with rust-cpython Question: I’m trying to speed up a data pipeline using Rust. The pipeline contains bits of Python code that I don’t want to modify, so I’m trying to run them as-is from Rust using rust-cpython and multiple threads. However, the performance is not what I expected, …

Total answers: 2

Possible to get "value of address" in python?

Possible to get "value of address" in python? Question: In the following, I can see that when adding an integer in python, it adds the integer, assigns that resultant value to a new memory address and then sets the variable to point to that memory address: >>> a=100 >>> id(a) 4304852448 >>> a+=5 >>> id(a) …

Total answers: 2

embed python environment in c++ application

embed python environment in c++ application Question: Using the c-python api I try to embed python 3.6 into a c++ application. However instead of using the system install i’d like to use a virtual environment. I didn’t find any documentation on the way to do that. Some related documentation mention py_SetProgramName or py_SetPythonHome Also when …

Total answers: 2