10000 gh-99845: _PySys_GetSizeOf() uses size_t by vstinner · Pull Request #99903 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-99845: _PySys_GetSizeOf() uses size_t #99903

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
gh-99845: _PySys_GetSizeOf() uses size_t
The _PySys_GetSizeOf() function now uses an unsigned type (size_t) to
compute the size, rather than a signed type (Py_ssize_t).

* _PySys_GetSizeOf() n
8000
ow checks for integer overflow when adding the
  header size.
* Reformat also _PySys_GetSizeOf().
  • Loading branch information
vstinner committed Nov 30, 2022
commit 42c0291e1dfc45387dc55611c836b524a381d21a
14 changes: 8 additions & 6 deletions Lib/test/test_sys.py
Original file line number Diff line number Diff line change
Expand Up @@ -1297,17 +1297,19 @@ def __sizeof__(self):
self.assertRaises(TypeError, sys.getsizeof, FloatSizeof())
self.assertIs(sys.getsizeof(FloatSizeof(), sentinel), sentinel)

# size_t maximum
header_size = self.gc_headsize * 2
umaxsize = (sys.maxsize * 2 + 1) - header_size

class OverflowSizeof(int):
def __sizeof__(self):
return int(self)
self.assertEqual(sys.getsizeof(OverflowSizeof(sys.maxsize)),
sys.maxsize + self.gc_headsize*2)
self.assertEqual(sys.getsizeof(OverflowSizeof(umaxsize)),
umaxsize + header_size)
with self.assertRaises(OverflowError):
sys.getsizeof(OverflowSizeof(sys.maxsize + 1))
with self.assertRaises(ValueError):
sys.getsizeof(OverflowSizeof(-1))
sys.getsizeof(OverflowSizeof(umaxsize + 1))
with self.assertRaises((ValueError, OverflowError)):
sys.getsizeof(OverflowSizeof(-sys.maxsize - 1))
sys.getsizeof(OverflowSizeof(-1))

def test_default(self):
size = test.support.calcvobjsize
Expand Down
33 changes: 15 additions & 18 deletions Python/sysmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -1741,44 +1741,41 @@ sys_set_int_max_str_digits_impl(PyObject *module, int maxdigits)
size_t
_PySys_GetSizeOf(PyObject *o)
{
PyObject *res = NULL;
PyObject *method;
Py_ssize_t size;
PyThreadState *tstate = _PyThreadState_GET();

/* Make sure the type is initialized. float gets initialized late */
if (PyType_Ready(Py_TYPE(o)) < 0) {
return (size_t)-1;
}

method = _PyObject_LookupSpecial(o, &_Py_ID(__sizeof__));
PyObject *method = _PyObject_LookupSpecial(o, &_Py_ID(__sizeof__));
PyThreadState *tstate = _PyThreadState_GET();
if (method == NULL) {
if (!_PyErr_Occurred(tstate)) {
_PyErr_Format(tstate, PyExc_TypeError,
"Type %.100s doesn't define __sizeof__",
Py_TYPE(o)->tp_name);
}
}
else {
res = _PyObject_CallNoArgs(method);
Py_DECREF(method);
return (size_t)-1;
}

if (res == NULL)
PyObject *res = _PyObject_CallNoArgs(method);
Py_DECREF(method);
if (res == NULL) {
return (size_t)-1;
}

size = PyLong_AsSsize_t(res);
size_t size = PyLong_AsSize_t(res);
Py_DECREF(res);
if (size == -1 && _PyErr_Occurred(tstate))
if (size == (size_t)-1 && _PyErr_Occurred(tstate)) {
return (size_t)-1;
}

if (size < 0) {
_PyErr_SetString(tstate, PyExc_ValueError,
"__sizeof__() should return >= 0");
size_t header_size = _PyType_PreHeaderSize(Py_TYPE(o));
if (size > SIZE_MAX - header_size) {
PyErr_SetString(PyExc_OverflowError,
"size greater than size_t maximum");
return (size_t)-1;
}

return (size_t)size + _PyType_PreHeaderSize(Py_TYPE(o));
return header_size + size;
}

static PyObject *
Expand Down
0