[Python-Dev] It is really necessary to check that a object has a Py_TPFLAGS_HEAPTYPE flag?

2021年2月08日 19:52:19 -0800

Hi everywhere.
I decided to figure out why it is impossible to change methods of built-in 
types and found 
```
 if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) {
 PyErr_Format(
 PyExc_TypeError,
 "can't set attributes of built-in/extension type '%s'",
 type->tp_name);
 return -1;
 }
```
in function type_setattro. 
If comment this line and try to patch method __eq__ on the list for example the 
code will work as expected
Test:
```
with patch("builtins.list.__eq__", return_value=False):
 assert [] == []
```
Result: `AssertionError` as expected. Is there a real need to disallow patching 
of built-in types or is it just legacy?
_______________________________________________
Python-Dev mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at 
https://mail.python.org/archives/list/[email protected]/message/FYS52CPAN7EMSQF4WF3WM5VNO52TBOIT/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to