Skip to content

Commit 5b9d005

Browse files
Remove @deleter from setters which only reject the deletion
They raised TypeError for deletion, and the generated code now raises AttributeError.
1 parent 68a34fa commit 5b9d005

12 files changed

Lines changed: 98 additions & 113 deletions

File tree

Lib/test/test_ctypes/test_delattr.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ class X(Structure):
88

99
class TestCase(unittest.TestCase):
1010
def test_simple(self):
11-
with self.assertRaises(TypeError):
11+
with self.assertRaises(AttributeError):
1212
del c_int(42).value
1313

1414
def test_chararray(self):
1515
chararray = (c_char * 5)()
16-
with self.assertRaises(TypeError):
16+
with self.assertRaises(AttributeError):
1717
del chararray.value
1818

1919
def test_struct(self):

Lib/test/test_exceptions.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -683,17 +683,17 @@ def test_invalid_setattr(self):
683683
self.assertRaisesRegex(TE, msg, setattr, exc, '__context__', 1)
684684

685685
def test_invalid_delattr(self):
686-
TE = TypeError
686+
AE = AttributeError
687687
try:
688688
raise IndexError(4)
689689
except Exception as e:
690690
exc = e
691691

692-
msg = "may not be deleted"
693-
self.assertRaisesRegex(TE, msg, delattr, exc, 'args')
694-
self.assertRaisesRegex(TE, msg, delattr, exc, '__traceback__')
695-
self.assertRaisesRegex(TE, msg, delattr, exc, '__cause__')
696-
self.assertRaisesRegex(TE, msg, delattr, exc, '__context__')
692+
msg = "cannot be deleted"
693+
self.assertRaisesRegex(AE, msg, delattr, exc, 'args')
694+
self.assertRaisesRegex(AE, msg, delattr, exc, '__traceback__')
695+
self.assertRaisesRegex(AE, msg, delattr, exc, '__cause__')
696+
self.assertRaisesRegex(AE, msg, delattr, exc, '__context__')
697697

698698
def testNoneClearsTracebackAttr(self):
699699
try:

Lib/test/test_funcattrs.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ def not_generic(): pass
229229
for func in (not_generic, lambda_):
230230
with self.subTest(func=func):
231231
self.assertEqual(func.__type_params__, ())
232-
with self.assertRaises(TypeError):
232+
with self.assertRaises(AttributeError):
233233
del func.__type_params__
234234
with self.assertRaises(TypeError):
235235
func.__type_params__ = 42

Lib/test/test_raise.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ def test_attrs(self):
257257
self.assertIs(tb.tb_next.tb_next, None)
258258

259259
# Invalid assignments
260-
with self.assertRaises(TypeError):
260+
with self.assertRaises(AttributeError):
261261
del tb.tb_next
262262

263263
with self.assertRaises(TypeError):

Modules/_ctypes/_ctypes.c

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1533,22 +1533,15 @@ _ctypes_PyCArrayType_Type_value_get_impl(CDataObject *self)
15331533
/*[clinic input]
15341534
@critical_section
15351535
@setter
1536-
@deleter
15371536
_ctypes.PyCArrayType_Type.value
1538-
value: object(subclass_of='&PyBytes_Type', type='PyBytesObject *') = NULL
1537+
value: object(subclass_of='&PyBytes_Type', type='PyBytesObject *')
15391538
[clinic start generated code]*/
15401539

15411540
static int
15421541
_ctypes_PyCArrayType_Type_value_set_impl(CDataObject *self,
15431542
PyBytesObject *value)
1544-
/*[clinic end generated code: output=21cbd436230dc33e input=d35228bbfb2f5228]*/
1543+
/*[clinic end generated code: output=21cbd436230dc33e input=47d40501c4ecae23]*/
15451544
{
1546-
if (value == NULL) {
1547-
PyErr_SetString(PyExc_TypeError,
1548-
"can't delete attribute");
1549-
return -1;
1550-
}
1551-
15521545
Py_ssize_t size = PyBytes_GET_SIZE(value);
15531546
if (size > self->b_size) {
15541547
PyErr_SetString(PyExc_ValueError,
@@ -5388,22 +5381,15 @@ class _ctypes.Simple "CDataObject *" "clinic_state()->Simple_Type"
53885381
/*[clinic input]
53895382
@critical_section
53905383
@setter
5391-
@deleter
53925384
_ctypes.Simple.value
53935385
[clinic start generated code]*/
53945386

53955387
static int
53965388
_ctypes_Simple_value_set_impl(CDataObject *self, PyObject *value)
5397-
/*[clinic end generated code: output=f267186118939863 input=4e6c1143d17c2c3f]*/
5389+
/*[clinic end generated code: output=f267186118939863 input=977af9dc9e71e857]*/
53985390
{
53995391
PyObject *result;
54005392

5401-
if (value == NULL) {
5402-
PyErr_SetString(PyExc_TypeError,
5403-
"can't delete attribute");
5404-
return -1;
5405-
}
5406-
54075393
ctypes_state *st = get_module_state_by_def(Py_TYPE(Py_TYPE(self)));
54085394
StgInfo *info;
54095395
if (PyStgInfo_FromObject(st, (PyObject *)self, &info) < 0) {

Modules/_ctypes/clinic/_ctypes.c.h

Lines changed: 19 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Objects/clinic/exceptions.c.h

Lines changed: 29 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Objects/clinic/funcobject.c.h

Lines changed: 19 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Objects/exceptions.c

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -346,19 +346,14 @@ BaseException_args_get_impl(PyBaseExceptionObject *self)
346346
/*[clinic input]
347347
@critical_section
348348
@setter
349-
@deleter
350349
BaseException.args
351350
[clinic start generated code]*/
352351

353352
static int
354353
BaseException_args_set_impl(PyBaseExceptionObject *self, PyObject *value)
355-
/*[clinic end generated code: output=331137e11d8f9e80 input=177ad350c8b45219]*/
354+
/*[clinic end generated code: output=331137e11d8f9e80 input=2400047ea5970a84]*/
356355
{
357356
PyObject *seq;
358-
if (value == NULL) {
359-
PyErr_SetString(PyExc_TypeError, "args may not be deleted");
360-
return -1;
361-
}
362357
seq = PySequence_Tuple(value);
363358
if (!seq)
364359
return -1;
@@ -386,19 +381,14 @@ BaseException___traceback___get_impl(PyBaseExceptionObject *self)
386381
/*[clinic input]
387382
@critical_section
388383
@setter
389-
@deleter
390384
BaseException.__traceback__
391385
[clinic start generated code]*/
392386

393387
static int
394388
BaseException___traceback___set_impl(PyBaseExceptionObject *self,
395389
PyObject *value)
396-
/*[clinic end generated code: output=a82c86d9f29f48f0 input=53a1df586023d786]*/
390+
/*[clinic end generated code: output=a82c86d9f29f48f0 input=12676035676badad]*/
397391
{
398-
if (value == NULL) {
399-
PyErr_SetString(PyExc_TypeError, "__traceback__ may not be deleted");
400-
return -1;
401-
}
402392
if (PyTraceBack_Check(value)) {
403393
Py_XSETREF(self->traceback, Py_NewRef(value));
404394
}
@@ -432,19 +422,15 @@ BaseException___context___get_impl(PyBaseExceptionObject *self)
432422
/*[clinic input]
433423
@critical_section
434424
@setter
435-
@deleter
436425
BaseException.__context__
437426
[clinic start generated code]*/
438427

439428
static int
440429
BaseException___context___set_impl(PyBaseExceptionObject *self,
441430
PyObject *value)
442-
/*[clinic end generated code: output=b4cb52dcca1da3bd input=fe79e7c0a0854004]*/
431+
/*[clinic end generated code: output=b4cb52dcca1da3bd input=c0971adf47fa1858]*/
443432
{
444-
if (value == NULL) {
445-
PyErr_SetString(PyExc_TypeError, "__context__ may not be deleted");
446-
return -1;
447-
} else if (value == Py_None) {
433+
if (value == Py_None) {
448434
value = NULL;
449435
} else if (!PyExceptionInstance_Check(value)) {
450436
PyErr_SetString(PyExc_TypeError, "exception context must be None "
@@ -476,19 +462,15 @@ BaseException___cause___get_impl(PyBaseExceptionObject *self)
476462
/*[clinic input]
477463
@critical_section
478464
@setter
479-
@deleter
480465
BaseException.__cause__
481466
[clinic start generated code]*/
482467

483468
static int
484469
BaseException___cause___set_impl(PyBaseExceptionObject *self,
485470
PyObject *value)
486-
/*[clinic end generated code: output=6161315398aaf541 input=3fdd9a0d1674abc9]*/
471+
/*[clinic end generated code: output=6161315398aaf541 input=e1b403c0bde3f62a]*/
487472
{
488-
if (value == NULL) {
489-
PyErr_SetString(PyExc_TypeError, "__cause__ may not be deleted");
490-
return -1;
491-
} else if (value == Py_None) {
473+
if (value == Py_None) {
492474
value = NULL;
493475
} else if (!PyExceptionInstance_Check(value)) {
494476
PyErr_SetString(PyExc_TypeError, "exception cause must be None "

0 commit comments

Comments
 (0)