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".

CPython is the default and most widely used implementation of the programming language. It is written in C.

In addition to CPython, there are several other production-quality Python implementations: , a JIT-compiler. , written in Java and , which is written for the Common Language Runtime. There are also several experimental implementations.

CPython is a bytecode interpreter. It has a foreign function interface with several languages including C, in which one must explicitly write bindings in a language other than Python.

See also

1358 questions
12
votes
1 answer

Embedding multiple Python sub-interpreters into a C program

I am writing a C program that spawns multiple C threads, with one Python sub-interpreter per thread. The sub-interpreters do not share any mutable Python variables, they are isolated from each other. (They do have a read-only access to a common…
Alec Matusis
  • 781
  • 1
  • 7
  • 16
12
votes
3 answers

How and when does Python determine the data type of a variable?

I was trying to figure out exactly how Python 3 (using CPython as an interpreter) executes its program. I found out that the steps are: Compilation of Python source code (.py file) by CPython compilator to Python bytecode (.pyc) file. In the case…
PyFox
  • 383
  • 3
  • 11
12
votes
4 answers

In C python, accessing the bytecode evaluation stack

Given a C Python frame pointer, how do I look at arbitrary evaluation stack entries? (Some specific stack entries can be found via locals(), I'm talking about other stack entries.) I asked a broader question like this a while ago: getting the C…
rocky
  • 7,226
  • 3
  • 33
  • 74
12
votes
4 answers

Regex replace is taking time for millions of documents, how to make it faster?

I have documents like: documents = [ "I work on c programing.", "I work on c coding.", ] I have synonym file such as: synonyms = { "c programing": "c programing", "c coding": "c programing" } I want to replace all synonyms for…
Vikash Singh
  • 13,213
  • 8
  • 40
  • 70
12
votes
1 answer

What makes Python3's print function thread safe?

I've seen on various mailing lists and forums that people keep mentioning that the print function in Python 3 is thread safe. From my own testing, I see no reason to doubt that. import threading import time import random def worker(letter): …
Goodies
  • 4,439
  • 3
  • 31
  • 57
12
votes
2 answers

`object in list` behaves different from `object in dict`?

I've got an iterator with some objects in it and I wanted to create a collection of uniqueUsers in which I only list every user once. So playing around a bit I tried it with both a list and a dict: >>> for m in ms: print m.to_user # let's first…
kramer65
  • 50,427
  • 120
  • 308
  • 488
12
votes
6 answers

Can I treat IronPython as a Pythonic replacement to C#?

I do understand that this topic has been covered in some way at StackOverflow but I'm still not able to figure out the exact answer: can I treat IronPython as a Pythonic replacement to C#? I use CPython every day, I love the Zen :) but my current…
Ewan Doe
  • 205
  • 1
  • 8
12
votes
3 answers

Using libspotify .dll/.lib files in MinGW32 compiling pySpotify

Using MinGW32 on a Windows PC I am trying to compile pySpotify. The first error was that libspotify/api.h was missing. I fixed this by copying the appropriate folder from libspotify into C:\MinGW\include. However now dllwrap is now failing with ld…
Metalshark
  • 8,194
  • 7
  • 34
  • 49
12
votes
1 answer

Storing Python objects in a Python list vs. a fixed-length Numpy array

In doing some bioinformatics work, I've been pondering the ramifications of storing object instances in a Numpy array rather than a Python list, but in all the testing I've done the performance was worse in every instance. I am using CPython. Does…
dpyro
  • 1,559
  • 2
  • 13
  • 19
11
votes
2 answers

Why cpython exposes 'PyTuple_SetItem' as C-API if tuple is immutable by design?

Tuple in python is immutable by design, so if we try to mutate a tuple object python emits following TypeError which make sense. >>> a = (1, 2, 3) >>> a[0] = 12 Traceback (most recent call last): File "", line 1, in TypeError:…
Abdul Niyas P M
  • 18,035
  • 2
  • 25
  • 46
11
votes
2 answers

__str__ function of class ported from rust to python using pyo3 doesn't get used in print

I am using the pyo3 rust crate (version 0.11.1) in order to port rust code, into cpython (version 3.8.2) code. I have created a class called my_class and defined the following functions: new, __str__, and __repr__. TL;DR: The __str__ function…
fdsa
  • 113
  • 1
  • 6
11
votes
3 answers

How is ternary operator implemented in Python

I understand that conditional expressions (or ternary operators) are lazy in Python. They represent conditional execution rather than conditional selection. In other words, only one of a or b is evaluated in the following: c = a if condition else…
jpp
  • 159,742
  • 34
  • 281
  • 339
11
votes
1 answer

Retrieving address of native base class with ctypes

I want to be able to pass a certificate to Python's ssl library without requiring a temporary file. It seems that the Python ssl module cannot do that. To work around this problem I want to retrieve the underlying SSL_CTX struct stored in the…
josch
  • 6,716
  • 3
  • 41
  • 49
11
votes
2 answers

Why Do I have to worry about Thread Safety in CPython?

From what I understand, the Global Interpreter Lock allows only a single thread to access the interpreter and execute bytecode. If that's the case, then at any given time, only a single thread will be using the interpreter and its memory. With that…
Anfernee
  • 1,445
  • 1
  • 15
  • 26
11
votes
1 answer

Where does Python store the name binding of function closure?

So recently I understand the concept of function closure. def outer(): somevar = [] assert "somevar" in locals() and not "somevar" in globals() def inner(): assert "somevar" in locals() and not "somevar" in globals() …
FunkySayu
  • 7,641
  • 10
  • 38
  • 61