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
11
votes
2 answers

Python string with space and without space at the end and immutability

I learnt that in some immutable classes, __new__ may return an existing instance - this is what the int, str and tuple types sometimes do for small values. But why do the following two snippets differ in the behavior? With a space at the end: >>> a…
James Sapam
  • 16,036
  • 12
  • 50
  • 73
11
votes
4 answers

Does Slicing `a` (e.g. `a[1:] == a[:-1]`) create copies of the `a`?

A friend of mine showed me the following Python code: a[1:] == a[:-1] Which returns True iff all the items in a are identical. I argued the code is hard to understand from first sight, and moreover - it is inefficient in memory usage, because two…
Adam Matan
  • 128,757
  • 147
  • 397
  • 562
10
votes
4 answers

Python list.clear() time and space complexity?

I am writing a blogpost on Python list.clear() method where I also want to mention about the time and space complexity of the underlying algorithm. I expected the time complexity to be O(N), iterate over the elements and free the memory? But, I…
Pankaj Mishra
  • 550
  • 6
  • 18
10
votes
2 answers

Python source code for math exponent function?

When I do math computation in Python which library are we using. E.g. >>> 2**0.5 1.4142135623730951 How can I find the source code that was used? Is this just the math.pow() function? Unfortunately, inspect.getsource(pow) returns a kind of…
john mangual
  • 7,718
  • 13
  • 56
  • 95
10
votes
2 answers

Python 3.5 vs. 3.6 what made "map" slower compared to comprehensions

I sometimes used map if there was a function/method that was written in C to get a bit extra performance. However I recently revisited some of my benchmarks and noticed that the relative performance (compared to a similar list comprehension)…
MSeifert
  • 145,886
  • 38
  • 333
  • 352
10
votes
3 answers

CPython is bytecode interpreter?

I don't really get the concept of "bytecode interpreter" in the context of CPython. Can someone shed some light over the whole picture? Does it mean that CPython will compile and execute pyc file (bytecode file?). Then what compile py file to pyc…
huy
  • 4,782
  • 6
  • 36
  • 42
10
votes
2 answers

PyPy cpyext: any documentation? how to use? PyThreadState_Get error?

I have read (here) that PyPy has support for CPython extension modules via cpyext. I haven't found any cpyext documentation. Is there any? How do I use it? From the source code (e.g. here), I figured out that to load my leveldb.so module, I probably…
Albert
  • 65,406
  • 61
  • 242
  • 386
10
votes
1 answer

Calling Python code from a C thread

I'm very confused as to how exactly I can ensure thread-safety when calling Python code from a C (or C++) thread. The Python documentation seems to be saying that the usual idiom to do so is: PyGILState_STATE gstate; gstate =…
Channel72
  • 24,139
  • 32
  • 108
  • 180
9
votes
1 answer

Is `0 is 0` always `True` in Python?

Python 3.8 (or CPython 3.8?) added the warning SyntaxWarning: "is" with a literal. Did you mean "=="? for the code 0 is 0. I understand the warning, and I know the difference between is and ==. However, I also know that CPython caches the object…
Albert
  • 65,406
  • 61
  • 242
  • 386
9
votes
2 answers

Correct setting of Python home and sys.prefix in an embedded environment

Current situation I embedded Python into my C++ application. The application is installed into the common Application locations on Windows, macOS (C:\Program Files, /Applications/, ...) which require admin privileges to make modifications to these…
Daniel Stephens
  • 2,371
  • 8
  • 34
  • 86
9
votes
1 answer

Why does float.__repr__ return a different representation compared to the equivalent formatting option?

To see how repr(x) works for float in CPython, I checked the source code for float_repr: buf = PyOS_double_to_string(PyFloat_AS_DOUBLE(v), 'r', 0, Py_DTSF_ADD_DOT_0, …
a_guest
  • 34,165
  • 12
  • 64
  • 118
9
votes
1 answer

Why is `len(l) != 0` faster than `bool(l)` in CPython?

I was doing some experimentation about operations' speed on list. For this I defined two list: l_short = [] and l_long = list(range(10**7)). The idea is to compare bool(l) with len(l) != 0 In an if contest, the following implementation is faster by…
BlueSheepToken
  • 5,751
  • 3
  • 17
  • 42
9
votes
1 answer

Debug the CPython opcode stack

CPython 3.7 introduced the ability to step through individual opcodes in a debugger. However, I can't figure out how to read variables out of the bytecode stack. For example, when debugging def f(a, b, c): return a * b + c f(2, 3, 4) I want to…
crusaderky
  • 2,552
  • 3
  • 20
  • 28
9
votes
2 answers

Why does CPython have a "POP_BLOCK" opcode?

What's the purpose of keeping track of blocks in Python bytecode? The documentation here mentions: ... Per frame, there is a stack of blocks, denoting nested loops, try statements, and such. But they don't actually seem necessary to actually…
math4tots
  • 8,540
  • 14
  • 58
  • 95
9
votes
1 answer

Debug python dump file in windbg

one of my python.exe (v3.5.2) process hang in remote Windows Server. I cannot attach it from remote site so I create a full memory dump and download it back to analyze. however, windbg doesn't have the tool like py-bt in gdb. what I can do is…
SILENCE
  • 225
  • 3
  • 8