Questions tagged [cpython]
The reference implementation of the Python programming language. Use this tag for questions specific to this implementation, general Python questions should just be tagged with "python".
1,362
questions
2
votes
1
answer
44
views
How much overhead does `python -X importtime` add?
Does anyone know how much slower running python -X importtime [module or file] is vs. python [module or file]?
I'm wondering if it's a bad idea to run python -X importtime for production code. The ...
-1
votes
0
answers
73
views
What is happening "under the hood" for any() in python?
I've developed a habit of using any() for avoiding "elif hell" and I've been wondering whether it's impacting my code. Particularly when used in for loops.
After testing it out, for the most ...
0
votes
0
answers
29
views
OpenMP First function call is slow
A parallelized function in C is slow on the first call. The subsequent call exectues ~5x faster.
The number of calculations is exactly the same.
The heavy lifting is done by this function which has ...
2
votes
1
answer
204
views
What is the `ExceptionTable` in the output of `dis`?
In python3.13, when I try to disassemble [i for i in range(10)], the result is as below:
>>> import dis
>>>
>>> dis.dis('[i for i in range(10)]')
0 RESUME ...
0
votes
1
answer
94
views
Python parallel threads in 3.13 GIL-less environment
I was working on a python program and I was attempting to implement some parallel processing to improve the runtime for a cpu-bound task that don't write to the same memory location. I've had some ...
1
vote
0
answers
29
views
Python interning [duplicate]
Let's assume we have file foo.py
a = 257
b = 257
print(a is b)
When I launch it from the terminal (python3 ./foo.py) the output is True:
(.venv) user@group:~/development/playground$ python3 ./foo.py
...
2
votes
0
answers
63
views
What does RESUME opcode actually do?
The documentation is not very informative (at least for me):
opcode:: RESUME (context)
A no-op. Performs internal tracing, debugging and optimization
checks.
The context oparand consists of two parts....
37
votes
2
answers
2k
views
Why list comprehensions create a function internally?
This is disassembly of a list comprehension in python 3.10:
Python 3.10.12 (main, Jun 11 2023, 05:26:28) [GCC 11.4.0] on linux
Type "help", "copyright", "credits" or &...
2
votes
2
answers
114
views
compile CPython extension written in Zig on Windows
Zig is able to import C libraries and therefore it can be used to write a CPython extension and compile it. This is potentially really useful for me.
Here's my simple.zig Python extension
const py = @...
0
votes
0
answers
13
views
Non-exist directory and Unable to find vcvarsall.bat
I'm trying to run the project in github, everything has been good untill I download the requirements through
pip install requirements.txt
I got this error and some question want to ask, please look ...
0
votes
2
answers
30
views
"ImportError: cannot import name 'base_impl'" while running standalone python zipapp executable
I am running oracle linux 7.9 with python 3.6.8. I created a standalone python zipapp executable following this tutorial. When I run the resulting executable I get the following error(1m is just an ...
0
votes
0
answers
31
views
How to embed Python interpreter in C program that is compiled against another Python version
I have a Python 3.8 embedded in C program (compiled and linked against 3.8). How can I make possible that this C code embed Python 3.10 and 3.11 without changing targeting 3.8 while compiling?
I was ...
0
votes
0
answers
82
views
SciPy fails to build from source on Mac under CPython 3.9/PyPy on Intel & M1 Macs
I have virtualenvs on both an Intel Mac running Catalina and an M1 Mac running Ventura and a friend has tried on an Intel Mac running Monterey.
I am following the build from source instructions from ...
0
votes
0
answers
31
views
set breakpoint in Python via the CPython API by line number without changing the passed code
I'm developing a Python IDE for iOS, and any IDE has a Debug, so I need to implement it. Reading various sources, I found out that Python has a breakpoint() function, but it needs to be embedded in ...
1
vote
2
answers
3k
views
AttributeError: module 'virtualenv.create.via_global_ref.builtin.cpython.mac_os' has no attribute 'CPython3macOsBrew'
I am running Ubuntu 20.04.6 LTS and wanted to create a project with python3.10, so I installed the python using the deadsnakes PPA, it works fine
$ python3.10 Python 3.10.13 (main, Aug 25 2023, 13:20:...
-1
votes
1
answer
140
views
Cannot install fbprophet
c:\users\gmmke\appdata\local\programs\python\python310\lib\site-packages (23.2.1)
when I install 'fbphrophet' using pip I get the following error;
error: subprocess-exited-with-error
python setup....
0
votes
0
answers
27
views
Where does it says in the Python CAPI that if `__new__` does not return the class or an instance of it, `__init__` will not be called?
In the Python's documentation, it is said that if the __new__ method of the class does not return either the class itself or an instance of it, the corresponding __init__ method will not be called.
...
0
votes
0
answers
172
views
How to call a Python function from Rust?
I have a function defined in a python module:
def myfunc(arg1, arg2):
print(arg1, arg2)
I'm trying to call it from Rust using cpython:
use cpython::{Python, PyResult, PyObject, PyTuple, ...
0
votes
1
answer
65
views
I want to use Python Module in C++ Project, Module is PIL, But an import error occurs
I want to use (from PIL import Image), but I can't this. My Python module name is Temp.py, Temp.py is TestCode.
I use Windows 10, and visual studio 2022, Python is Visual Code. version is 3.11.4
...
0
votes
0
answers
26
views
Setuptools, skbuild and Pybind11 : wrong library location
I made a python interface to a fairly large C++ project, using Pybind11 : https://pypi.org/project/cytosim/
I wrote a setup.py file that does compile the library (provided some libraries are installed)...
2
votes
0
answers
104
views
Reference count after deleting a literal
I am new to python and while I was counting the references to the number "3" in the context of embedding Python in C++ I found out a behavior that I was not expecting at all. Can someone ...
0
votes
0
answers
52
views
Building wheel package with a CPython built from sources on Windows
I'm building Python 3.12 from sources on Windows through the scipt PCbuild\build.bat -p x64.
The result gets built in the amd64 folder and python.exe works perfectly fine.
My problem comes when I try ...
1
vote
0
answers
46
views
Pickling of enum with functional API throws error
I want to create a enum from a list of variables and then pickle it. I use the functional constructor. This should be done inside a class that is loaded in different sub-classes.
I got it to work for ...
0
votes
1
answer
50
views
How do I make the Python Interpreter 'forgets' a variable in Cython?
Say I have this Cython class, like so:
cdef class MyClass:
cdef readonly object _value
def __cinit__(self, value):
self._value = value
cpdef forget_self(self):
# what ...
3
votes
0
answers
34
views
normal Python function, C extension function, ctypes C function, what are the differences?
From my understanding and question:
normal Python function is compiled into byte code, and be executed in a forever loop
C extension function is written in C by user and integrated into interpreter, ...
0
votes
0
answers
172
views
Debugging a .so file, compiled by cython
I have a .so file that has some functions in it (about inferencing model of deep learning) The .pyx file accidently got deleted during compile.
I imported it in my python code regularly. I want to ...
3
votes
1
answer
193
views
What is the specification on how value comparisons have to be implemented?
I can't seem to find the actual language specification on how value comparisons have to be done by a complying implementation (e.g. CPython). Consider the simple case
a == b
The reference says that ...
0
votes
1
answer
133
views
Python 3.11.4 launcher.wixproj cannot detect v143 buildtools
I tried to build Python 3.11.4 with Visual Studio 2022 (v143) and I get following error at the end of compilation.
Rest of the project binaries are built using v143 successfully.
I used following ...
0
votes
1
answer
59
views
Files with bytecode are really created only for imported modules?
In my system I have created a Python application that is composed by more than one file. The directory structure of the application is the following:
application_directory
|- file_a.py
|- file_b....
0
votes
0
answers
12
views
Issue with import pyximport in Sagemaker jupyter notebook
I manually install CPython again but for some reason, a file in my program is not able to import pyximport in a Sagemaker notebook instance .ipynb file
Any ideas what's going on?
0
votes
0
answers
46
views
Does os.urandom() always use win32_urandom on Windows?
In the answer to this question, we see that os.urandom() calls the function _PyOS_URandom() from Python/bootstrap_hash.c. In line 531 of this document, I found that this function calls the function ...
0
votes
0
answers
60
views
Using py_limited_api=cp36 for Python 3.8+ wheel
Let's say I have Python 3.6 on my machine and I can't upgrade it for some particular reason.
I want to build a library for Python >= 3.8 on that machine using setuptools. That library also uses the ...
-6
votes
1
answer
125
views
Calling random in secrets module
I am trying to understand how the secrets module of Python works and which modules it calls when generating random numbers. I don’t have a lot of programming experience, so please stay with me.
In the ...
0
votes
0
answers
24
views
why cpython has list_reverse and PyList_Reverse
I am recently reading CPython source code.
In CPython3.11, I found that in listobject implementation.
There are two methods that does the same thing
One is first in clinic check for signature named ...
0
votes
0
answers
525
views
SetuptoolsDeprecationWarning: setup.py install is deprecated. !! - How to build Python/C++ package locally and distribution wheel
I have Python/C++ project, where I am using some C++ functions wrapped in Python and provided in Python as a library. The following shows the directory structure:
.
├── examples
│ └── test_file1.py
├...
0
votes
0
answers
34
views
Thread reports dead after KeyboardInterrupt in thread.join()
The code:
from threading import Thread
from time import sleep
def f() -> None:
while True:
sleep(0.4)
print("Thread is running.")
thread = Thread(target=f, daemon=...
0
votes
0
answers
47
views
Checking CPython configuration options
I switched to Python 3.11.4 and got some problem when importing some modules. So, I decided to try different configuration options in order to find the right combination(s). The problem seems to be ...
0
votes
0
answers
42
views
Importing `CPython/Objects/genobject.c` into Cython
In this question and this reference, there are examples of importing c-header files into Cython, but there is no answer to how can one import a .c file content into Cython.
The .c file which I am ...
-1
votes
2
answers
147
views
Embed python code in C++ using cpython: cannot import numpy
I'm developing a C++ application (using CMake) that embeds a Python script. The application is responsible for initializing the data, passing it to the Python script, and retrieving the result.
...
1
vote
0
answers
34
views
Open python2.7 and python3.9 interpreters at the same time in c++ embedded python
I have C++ project. I need to run python modules from c++ code through C Python API. Some of these modules support python2.7 and other support python3.9. Is it possible to open 2 interpreters with ...
0
votes
0
answers
29
views
Open another version of interpreter after Py_Finalize
Can I close interpreter of python 2.7 with Py_Finalize and open interpreter with Python3.9 with Py_Initialize after that?
I need at first run some code with Python2.7, but then with Python3.5
I try do ...
0
votes
0
answers
37
views
Cpython consists of C code or binary machine code
What is the meaning and the significance of Cpython is the implementation of python.
It is the interpreter, which used C language to create the Cpython program. It was than compiled to machine code, ...
0
votes
0
answers
36
views
Getting a pointer to a instance attribute in python using Cython
Reference: https://edcjones.tripod.com/refcount.html
Returning a PyObject* from a function requires the function to first increment the strong reference count of PyObject*.
Consider this:
class A:
...
0
votes
2
answers
221
views
Running multiple processes in each iteration of loop in Python
I have two functions func_1 and func_2. They both take an array of integers as input.
I have a loop that creates arrays of length i in the ith iteration.
I have two predefined lists list_1 and list_2 ...
2
votes
0
answers
68
views
Persisting TemporaryFile in Python without rewriting it | Call linkstat in python on fd
I have a library that provides me with a TemporaryFile object which I would like to persist on the file system it was stored, without rewriting it: The file could be quite huge and performance is ...
-1
votes
1
answer
71
views
Python Cpp API And Immutable Strings
I'm currently working on a project using this open source library (nanobind)
and am trying to understand how python enforces immutability. Lets say I have some memory allocation in C with the ...
1
vote
0
answers
73
views
Create a Python string from a native pointer without char buffer copy
Is it possible?
I'd like to have a lot of strings stored in a PyTorch/NumPy tensor (e.g. in fixed-size UTF-32 4-byte character) - could even be mmap'd from disk, and then to manipulate them with ...
0
votes
0
answers
31
views
how to understand Python object? [duplicate]
python 3.8 cpython on windows 10
case 1:
print(id('sss'))
print(id('sss'))
print(id('sss'))
print(id('sss'))
print(id('sss'))
print(id('sss'))
print(id('sss'))
print(id('sss'))
print(id('sss'))
print(...
0
votes
0
answers
47
views
PyPy slower than Cpython using tornado
I have an account service for testing pypy and cpython using jmeter:
import tornado.ioloop
import tornado.web
import uuid
class Account(object):
def init(self):
self.accounts = {}
...
5
votes
5
answers
767
views
How to create python C++ extension with submodule that can be imported
I'm creating a C++ extension for python. It creates a module parent that contains a sub-module child. The child has one method hello(). It works fine if I call it as
import parent
parent.child.hello()
...