cpython

What is the `ExceptionTable` in the output of `dis`?

What is the `ExceptionTable` in the output of `dis`? Question: 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 1 LOAD_NAME 0 (range) PUSH_NULL LOAD_CONST 0 (10) CALL 1 GET_ITER LOAD_FAST_AND_CLEAR 0 (i) SWAP …

Total answers: 1

Where is the giant cpython switch case statement moved?

Where is the giant cpython switch case statement moved? Question: Not sure if it’s off topic but don’t know any better place to ask. In cpython there was a very giant switch case statement for executing each opcode. This switch case previously was placed in the _PyEval_EvalFrameDefault function. Here is the link. The switch case …

Total answers: 1

sys.getrefcount() returning very large reference counts

sys.getrefcount() returning very large reference counts Question: In CPython 3.11, the following code returns very large reference counts for some objects. It seems to follow pre-cached objects like integers -5 to 256, but CPython 3.10 does not: Python 3.11.2 (tags/v3.11.2:878ead1, Feb 7 2023, 16:38:35) [MSC v.1934 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" …

Total answers: 1

Why is b.pop(0) over 200 times slower than del b[0] for bytearray?

Why is b.pop(0) over 200 times slower than del b[0] for bytearray? Question: Letting them compete three times (a million pops/dels each time): from timeit import timeit for _ in range(3): t1 = timeit(‘b.pop(0)’, ‘b = bytearray(1000000)’) t2 = timeit(‘del b[0]’, ‘b = bytearray(1000000)’) print(t1 / t2) Time ratios (Try it online!): 274.6037053753368 219.38099365582403 252.08691226683823 …

Total answers: 3

Sphinx: Autodocs for classes inheriting from base classes implemented in C

Sphinx: Autodocs for classes inheriting from base classes implemented in C Question: I am running into an error/warning when inheriting from collections.deque. Sphinx complains: utils.py:docstring of utils.Test.count:1: WARNING: py:class reference target not found: integer — return number of occurrences of value For this example, utils.py only contains from collections import deque class Test(deque): pass My …

Total answers: 1

Does a bitwise "and" operation prepend zeros to the binary representation?

Does a bitwise "and" operation prepend zeros to the binary representation? Question: When I use bitwise and operator(&) with the number of 1 to find out if a number x is odd or even (x & 1), does the interpreter change the binary representation of 1 according to the binary representation of x? For example: …

Total answers: 1

Why PyList_Append is called each time a list is evaluated?

Why PyList_Append is called each time a list is evaluated? Question: I’m working with CPython3.11.0a3+. I added a break point at PyList_Append and modified the function to stop when the newitem is a dict. The original function: int PyList_Append(PyObject *op, PyObject *newitem) { if (PyList_Check(op) && (newitem != NULL)) return app1((PyListObject *)op, newitem); PyErr_BadInternalCall(); return …

Total answers: 1

How are small sets stored in memory?

How are small sets stored in memory? Question: If we look at the resize behavior for sets under 50k elements: >>> import sys >>> s = set() >>> seen = {} >>> for i in range(50_000): … size = sys.getsizeof(s) … if size not in seen: … seen[size] = len(s) … print(f"{size=} {len(s)=}") … s.add(i) …

Total answers: 1

Why sum function is slower if the start argument is an instance of custom class?

Why sum function is slower if the 'start' argument is an instance of custom class? Question: I was playing around with sum function and observed the following behaviour. case 1: source = """ class A: def __init__(self, a): self.a = a def __add__(self, other): return self.a + other; sum([*range(10000)], start=A(10)) """ import timeit print(timeit.timeit(stmt=source)) As …

Total answers: 2