Skip to content

Conversation

@ayushm98
Copy link

@ayushm98 ayushm98 commented Dec 29, 2025

Fixes #140025

Summary

The __sizeof__() method for queue.SimpleQueue now properly accounts for the dynamically allocated internal buffer.

Problem

Previously, SimpleQueue.__sizeof__() only returned the basic object size, ignoring the underlying RingBuf data structure that holds the queued items.

Solution

Implemented a custom __sizeof__() method that includes:

  • Base object size
  • Size of the dynamically allocated items array: items_cap * sizeof(PyObject*)

Example

import queue
q = queue.SimpleQueue()
initial_size = q.__sizeof__()

# Add many items to grow the buffer
for i in range(1000):
    q.put(i)

# Now __sizeof__() correctly reflects the larger buffer
larger_size = q.__sizeof__()
assert larger_size > initial_size

Changes

  • Added simplequeue_sizeof() function in Modules/_queuemodule.c
  • Added __sizeof__ to simplequeue_methods array

Fixes python#140025

The __sizeof__() method for queue.SimpleQueue previously ignored
the underlying RingBuf data structure, only returning the basic
object size.

Now properly accounts for the dynamically allocated items array
in the RingBuf, which grows with the queue capacity:
- Returns base object size + (items_cap * sizeof(PyObject*))

This gives accurate memory usage for SimpleQueue instances.
@bedevere-app
Copy link

bedevere-app bot commented Dec 29, 2025

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

@python-cla-bot
Copy link

The following commit authors need to sign the Contributor License Agreement:

CLA not signed

@StanFromIreland StanFromIreland changed the title Fix queue.SimpleQueue.__sizeof__() to account for buffer gh-140025: Fix queue.SimpleQueue.__sizeof__() to account for buffer Dec 30, 2025
Copy link
Member

@StanFromIreland StanFromIreland left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You need to add tests, a blurb and sign the CLA.

@picnixz
Copy link
Member

picnixz commented Dec 30, 2025

There is already an open PR: #143137.

@picnixz picnixz closed this Dec 30, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

3 participants