Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
13 changes: 13 additions & 0 deletions Lib/test/test_bytes.py
Original file line number Diff line number Diff line change
Expand Up @@ -898,6 +898,19 @@ def test_memory_leak_gh_140939(self):
b = self.type2test(b'%*b')
b % (_testcapi.PY_SSIZE_T_MAX, b'abc')

def test_mod_bytes_lookup_error(self):
# An error raised while looking up __bytes__ propagates.
class BadBytes:
@property
def __bytes__(self):
raise ZeroDivisionError
def __buffer__(self, flags):
return memoryview(b'abc')

b = self.type2test(b'%b')
with self.assertRaises(ZeroDivisionError):
b % BadBytes()

def test_imod(self):
b = self.type2test(b'hello, %b!')
orig = b
Expand Down
11 changes: 11 additions & 0 deletions Lib/test/test_weakref.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,17 @@ def __getattr__(self, x):
self.assertIn('MyConfig', repr(obj_weakref))
self.assertIn('MyConfig', str(obj_weakref))

def test_repr_name_lookup_error(self):
# An error raised while looking up __name__ propagates.
class BadName:
@property
def __name__(self):
raise ZeroDivisionError

obj = BadName()
with self.assertRaises(ZeroDivisionError):
repr(weakref.ref(obj))

def test_basic_callback(self):
self.check_basic_callback(C)
self.check_basic_callback(create_function)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Report the correct exception if looking up :meth:`~object.__bytes__` raises
an exception while performing %-formatting of :class:`bytes`.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Report the correct exception if looking up ``__name__`` raises an exception
when calling :func:`repr` on a :class:`weakref.ref`.
3 changes: 3 additions & 0 deletions Objects/bytesobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -652,6 +652,9 @@ format_obj(PyObject *v, Py_ssize_t argidx, PyObject *key,
*plen = PyBytes_GET_SIZE(result);
return result;
}
else if (PyErr_Occurred()) {
return NULL;
}
/* does it support buffer protocol? */
if (PyObject_CheckBuffer(v)) {
/* maybe we can avoid making a copy of the buffer object here? */
Expand Down
4 changes: 4 additions & 0 deletions Objects/weakrefobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,10 @@ weakref_repr(PyObject *self)
}

PyObject *name = _PyObject_LookupSpecial(obj, &_Py_ID(__name__));
if (name == NULL && PyErr_Occurred()) {
Py_DECREF(obj);
return NULL;
}
PyObject *repr;
if (name == NULL || !PyUnicode_Check(name)) {
repr = PyUnicode_FromFormat(
Expand Down
Loading