Skip to content

Conversation

@Aniketsy
Copy link
Contributor

@Aniketsy Aniketsy commented Dec 24, 2025

  • Update SimpleQueue.sizeof() to include the memory used by its internal buffer.
  • add a unit test.

Please let me know if my approach or fix needs any improvements . I’m open to feedback and happy to make changes based on suggestions.
Thankyou !

Copy link
Contributor

@colesbury colesbury left a comment

Choose a reason for hiding this comment

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

A few comments below. Please also add a blurb

Comment on lines 241 to 243
static PyObject *
simplequeue_sizeof(PyObject *op, PyObject *Py_UNUSED(ignored))
{
Copy link
Contributor

Choose a reason for hiding this comment

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

This needs a critical section (a lock for the queue). The simplest way is to use "Argument Clinic" like the other functions.

Something like:

/*[clinic input]
@critical_section
_queue.SimpleQueue.__sizeof__ -> Py_ssize_t

Returns size in memory, in bytes.
[clinic start generated code]*/

static Py_ssize_t
simplequeue_sizeof_impl(simplequeueobject *self)
/*[clinic end generated code: output=3303f008eaa6a0a5 input=9b51620c76fc4507]*/

Then run make clinic to regenerate the bindings.

_QUEUE_SIMPLEQUEUE_PUT_METHODDEF
_QUEUE_SIMPLEQUEUE_PUT_NOWAIT_METHODDEF
_QUEUE_SIMPLEQUEUE_QSIZE_METHODDEF
{"__sizeof__", (PyCFunction)simplequeue_sizeof, METH_NOARGS, NULL},
Copy link
Contributor

Choose a reason for hiding this comment

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

You'll also need to change this to use the Argument Clinic methoddef (like above).


def test_simplequeue_sizeof_reflects_buffer_growth(self):
q = self.type2test()
before = q.__sizeof__()
Copy link
Contributor

Choose a reason for hiding this comment

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

A few changes:

  1. Don't use ctypes (no support for that in wasi)
  2. Use support.calcobjsize
  3. Use sys.getsizeof() instead of calling __sizeof__ directly

@Aniketsy
Copy link
Contributor Author

@colesbury thanks for the feedback. i've applied changes as per suggestions, please let me know if this needs further improvement.

@picnixz
Copy link
Member

picnixz commented Dec 25, 2025

thanks for the feedback. i've applied changes as per suggestions, please let me know if this needs further improvement.

Please make it so that the CI is green before requesting a new review.

@picnixz picnixz changed the title gh-140025: Fix Fix queue.SimpleQueue.__sizeof__() gh-140025: Fix queue.SimpleQueue.__sizeof__() Dec 25, 2025
@Aniketsy
Copy link
Contributor Author

Aniketsy commented Jan 2, 2026

@picnixz ci is now green. @colesbury could you please review now if you get a chance. Thanks!

@colesbury colesbury self-requested a review January 2, 2026 18:09
Copy link
Contributor

@colesbury colesbury left a comment

Choose a reason for hiding this comment

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

Thanks a few comments below

from test.support import gc_collect, bigmemtest
from test.support import import_helper
from test.support import threading_helper
import sys
Copy link
Contributor

Choose a reason for hiding this comment

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

Move import sys up between import random and import threading

Copy link
Contributor Author

Choose a reason for hiding this comment

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

sure, I missed that. I thought running pre-commit run would take care of it.

[clinic start generated code]*/

static Py_ssize_t
_queue_SimpleQueue___sizeof___impl(simplequeueobject *self)
Copy link
Member

Choose a reason for hiding this comment

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

Can you put the definition of this method after the definition of qsize please?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure, I’ll update this.

Aniketsy and others added 4 commits January 3, 2026 11:54
Co-authored-by: Sam Gross <colesbury@gmail.com>
…OX58_.rst

Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
…OX58_.rst

Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
@kumaraditya303 kumaraditya303 enabled auto-merge (squash) January 8, 2026 12:10
@kumaraditya303 kumaraditya303 merged commit f3e069a into python:main Jan 8, 2026
46 checks passed
@bedevere-bot
Copy link

⚠️⚠️⚠️ Buildbot failure ⚠️⚠️⚠️

Hi! The buildbot AMD64 Debian root 3.x (tier-1) has failed when building commit f3e069a.

What do you need to do:

  1. Don't panic.
  2. Check the buildbot page in the devguide if you don't know what the buildbots are or how they work.
  3. Go to the page of the buildbot that failed (https://buildbot.python.org/#/builders/345/builds/13136) and take a look at the build logs.
  4. Check if the failure is related to this commit (f3e069a) or if it is a false positive.
  5. If the failure is related to this commit, please, reflect that on the issue and make a new Pull Request with a fix.

You can take a look at the buildbot page here:

https://buildbot.python.org/#/builders/345/builds/13136

Summary of the results of the build (if available):

==

Click to see traceback logs
Traceback (most recent call last):
  File "/root/buildarea/3.x.angelico-debian-amd64/build/Lib/test/support/__init__.py", line 946, in gc_collect
    gc.collect()
    ~~~~~~~~~~^^
ResourceWarning: unclosed file <_io.FileIO name=13 mode='wb' closefd=True>


Traceback (most recent call last):
  File "/root/buildarea/3.x.angelico-debian-amd64/build/Lib/test/support/__init__.py", line 946, in gc_collect
    gc.collect()
    ~~~~~~~~~~^^
ResourceWarning: unclosed file <_io.FileIO name=11 mode='wb' closefd=True>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants