Skip to content

Commit e2af34f

Browse files
bpo-35504: Fix a SystemError when delete the characters_written attribute of an OSError. (GH-11172)
1 parent fae9587 commit e2af34f

File tree

3 files changed

+14
-0
lines changed

3 files changed

+14
-0
lines changed

Lib/test/test_exception_hierarchy.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,10 +150,15 @@ def test_blockingioerror(self):
150150
e = BlockingIOError(*args[:n])
151151
with self.assertRaises(AttributeError):
152152
e.characters_written
153+
with self.assertRaises(AttributeError):
154+
del e.characters_written
153155
e = BlockingIOError("a", "b", 3)
154156
self.assertEqual(e.characters_written, 3)
155157
e.characters_written = 5
156158
self.assertEqual(e.characters_written, 5)
159+
del e.characters_written
160+
with self.assertRaises(AttributeError):
161+
e.characters_written
157162

158163

159164
class ExplicitSubclassingTest(unittest.TestCase):
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fixed a SystemError when delete the characters_written attribute of an OSError.

Objects/exceptions.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1196,6 +1196,14 @@ OSError_written_get(PyOSErrorObject *self, void *context)
11961196
static int
11971197
OSError_written_set(PyOSErrorObject *self, PyObject *arg, void *context)
11981198
{
1199+
if (arg == NULL) {
1200+
if (self->written == -1) {
1201+
PyErr_SetString(PyExc_AttributeError, "characters_written");
1202+
return -1;
1203+
}
1204+
self->written = -1;
1205+
return 0;
1206+
}
11991207
Py_ssize_t n;
12001208
n = PyNumber_AsSsize_t(arg, PyExc_ValueError);
12011209
if (n == -1 && PyErr_Occurred())

0 commit comments

Comments
 (0)