Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5323173

Browse files
committed
Closes #15514: Correct __sizeof__ support for cpu_set
1 parent 077ef45 commit 5323173

3 files changed

Lines changed: 24 additions & 0 deletions

File tree

Lib/test/test_posix.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1008,6 +1008,12 @@ def test_cpu_set_bitwise(self):
10081008
self.assertIs(b, l)
10091009
self.assertEqual(l.count(), 3)
10101010

1011+
@requires_sched_affinity
1012+
@support.cpython_only
1013+
def test_cpu_set_sizeof(self):
1014+
self.assertGreater(sys.getsizeof(posix.cpu_set(1000)),
1015+
sys.getsizeof(posix.cpu_set(1)))
1016+
10111017
def test_rtld_constants(self):
10121018
# check presence of major RTLD_* constants
10131019
posix.RTLD_LAZY

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,9 @@ Library
373373
- Issue #15487: Add a __sizeof__ implementation for buffered I/O objects.
374374
Patch by Serhiy Storchaka.
375375

376+
- Issue #15514: Correct __sizeof__ support for cpu_set.
377+
Patch by Serhiy Storchaka.
378+
376379
- Issue #15187: Bugfix: remove temporary directories test_shutil was leaving
377380
behind.
378381

Modules/posixmodule.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5772,6 +5772,20 @@ cpu_set_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
57725772
return (PyObject *)make_new_cpu_set(type, size);
57735773
}
57745774

5775+
PyDoc_STRVAR(cpu_set_sizeof_doc,
5776+
"cpu_set.__sizeof__() -> int\n\n\
5777+
Returns size in memory, in bytes.");
5778+
5779+
static PyObject *
5780+
cpu_set_sizeof(Py_cpu_set *set, PyObject *noargs)
5781+
{
5782+
Py_ssize_t res = 0;
5783+
5784+
res = sizeof(Py_cpu_set);
5785+
res += set->size;
5786+
return PyLong_FromSsize_t(res);
5787+
}
5788+
57755789
static PyObject *
57765790
cpu_set_repr(Py_cpu_set *set)
57775791
{
@@ -5959,6 +5973,7 @@ static PyMethodDef cpu_set_methods[] = {
59595973
{"isset", (PyCFunction)cpu_set_isset, METH_VARARGS, cpu_set_isset_doc},
59605974
{"set", (PyCFunction)cpu_set_set, METH_VARARGS, cpu_set_set_doc},
59615975
{"zero", (PyCFunction)cpu_set_zero, METH_NOARGS, cpu_set_zero_doc},
5976+
{"__sizeof__", (PyCFunction)cpu_set_sizeof, METH_NOARGS, cpu_set_sizeof_doc},
59625977
{NULL, NULL} /* sentinel */
59635978
};
59645979

0 commit comments

Comments
 (0)