python / python/cpython

`queue.SimpleQueue.__sizeof__()` ignores the underlying data structure

Open
#140,025 3 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

extension-modules type-bug
Dominant language
Python
Stars
77.2k
Forks
35.9k
PR merge metrics
PR metrics pending

Description

Bug report

Bug description:

queue.SimpleQueue (C level) uses a list on Python < 3.13[1][2][3][4], a ring buffer on Python >= 3.13[5][6]. However, it does not implement its own __sizeof__() method, and as a result, only the size of the simplequeueobject structure itself (basicsize) is taken as the size of the object, while the size of the underlying structure is ignored.

>>> from queue import SimpleQueue
>>> q = SimpleQueue()
>>> q.__sizeof__()
72  # 56 on Python < 3.13
>>> for _ in range(1_000):
...     q.put(object())
...     
>>> q.__sizeof__()
72  # 56 on Python < 3.13

Expected (should be, on Python >= 3.13):

>>> from queue import SimpleQueue
>>> q = SimpleQueue()
>>> q.__sizeof__()
136  # == 72 + 8*8 == sizeof(simplequeueobject) + sizeof(PyObject *)*INITIAL_RING_BUF_CAPACITY
>>> for _ in range(1_000):
...     q.put(object())
...     
>>> q.__sizeof__()
8264  # == 72 + 8*1024 == sizeof(simplequeueobject) + sizeof(PyObject *)*pow(2, ceil(log2(1000)))
CPython versions tested on:

3.9, 3.10, 3.11, 3.12, 3.13, 3.14

Operating systems tested on:

Linux

Linked PRs
  • gh-140086
  • gh-143137
  • gh-143277

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in Modules/_queuemodule.c, focusing on the SimpleQueue implementations linked in the report and their size accounting. Run the supplied reproductions across the relevant Python versions, then verify that reported sizes include the underlying list or ring buffer and match the stated expected values.

Written by the indexing model from the issue text.

Assessment

Tech stack
c, python
Domain
devtools
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.