FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-99845: _PySys_GetSizeOf() uses size_t by vstinner · Pull Request #99903 · python/cpython · GitHub

/ cpython Public
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .c  (1) .py  (1) All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
24 changes: 18 additions & 6 deletions Lib/test/test_sys.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -1269,6 +1269,15 @@ def setUp(self):

check_sizeof = test.support.check_sizeof

def test_sizeof_method(self):
class Sizeof(int):
def __sizeof__(self):
return int(self)

header_size = self.gc_headsize * 2
self.assertEqual(sys.getsizeof(Sizeof(0)), header_size)
self.assertEqual(sys.getsizeof(Sizeof(100)), 100 + header_size)

def test_gc_head_size(self):
# Check that the gc header size is added to objects tracked by the gc.
vsize = test.support.calcvobjsize
Expand Down Expand Up @@ -1297,17 +1306,20 @@ 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
maxsize = (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(maxsize)),
maxsize + header_size)
with self.assertRaises(OverflowError):
sys.getsizeof(OverflowSizeof(maxsize + 1))
with self.assertRaises(OverflowError):
sys.getsizeof(OverflowSizeof(sys.maxsize + 1))
with self.assertRaises(ValueError):
sys.getsizeof(OverflowSizeof(-1))
with self.assertRaises((ValueError, OverflowError)):
sys.getsizeof(OverflowSizeof(-sys.maxsize - 1))

def test_default(self):
size = test.support.calcvobjsize
Expand Down
33 changes: 15 additions & 18 deletions Python/sysmodule.c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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,
"__sizeof__() 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

Back | FazBrowse Home | New Git URL