bpo-35504: Fix a SystemError when delete the characters_written attribute of an OSError. (GH-11172)
diff --git a/Objects/exceptions.c b/Objects/exceptions.c
index 05578d4..002a602 100644
--- a/Objects/exceptions.c
+++ b/Objects/exceptions.c
@@ -1196,6 +1196,14 @@
static int
OSError_written_set(PyOSErrorObject *self, PyObject *arg, void *context)
{
+ if (arg == NULL) {
+ if (self->written == -1) {
+ PyErr_SetString(PyExc_AttributeError, "characters_written");
+ return -1;
+ }
+ self->written = -1;
+ return 0;
+ }
Py_ssize_t n;
n = PyNumber_AsSsize_t(arg, PyExc_ValueError);
if (n == -1 && PyErr_Occurred())