-
-
Notifications
You must be signed in to change notification settings - Fork 32k
bpo-46417: Add _PyType_CAST() macro #30760
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
In debug mode, the macro makes sure that its argument is a type using an assertion.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see you left some ordinary type casts. Was that deliberate, or did you miss some _PyType_CAST
s?
if (!PyType_Check(ob)) { | ||
PyErr_Format(PyExc_TypeError, | ||
"%s.__bases__ must be tuple of classes, not '%s'", | ||
type->tp_name, Py_TYPE(ob)->tp_name); | ||
return -1; | ||
} | ||
PyTypeObject *base = (PyTypeObject*)ob; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use _PyType_CAST
here as well?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We just checked PyType_Check() at runtime (even if assertions are removed by the compiler), using the macro is redundant (it would call PyType_Check() twice in debug mode).
return -1; | ||
} | ||
PyTypeObject *base = (PyTypeObject*)obj; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ditto
if (!PyType_Check(base_proto)) { | ||
PyErr_SetString( | ||
PyExc_TypeError, | ||
"bases must be types"); | ||
return NULL; | ||
} | ||
base_i = (PyTypeObject *)base_proto; | ||
PyTypeObject *base_i = (PyTypeObject *)base_proto; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ditto
@@ -4793,12 +4780,13 @@ object_set_class(PyObject *self, PyObject *value, void *closure) | |||
Py_TYPE(value)->tp_name); | |||
return -1; | |||
} | |||
PyTypeObject *newto = (PyTypeObject *)value; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ditto
PyObject *arg0, *res; | ||
|
||
if (self == NULL || !PyType_Check(self)) { | ||
PyErr_Format(PyExc_SystemError, | ||
"__new__() called with non-type 'self'"); | ||
return NULL; | ||
} | ||
type = (PyTypeObject *)self; | ||
PyTypeObject *type = (PyTypeObject *)self; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ditto
@@ -7086,7 +7077,8 @@ tp_new_wrapper(PyObject *self, PyObject *args, PyObject *kwds) | |||
Py_TYPE(arg0)->tp_name); | |||
return NULL; | |||
} | |||
subtype = (PyTypeObject *)arg0; | |||
PyTypeObject *subtype = (PyTypeObject *)arg0; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ditto
if ((PyObject *)subclass == Py_None) | ||
PyObject *obj = PyWeakref_GET_OBJECT(ref); | ||
assert(obj != NULL); | ||
if (obj == Py_None) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if (obj == Py_None) { | |
if (Py_IsNone(obj)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Trust me, the temptation to refactor everything in this old giant C file is very high :-D But then it leads to PR which is impossible to review :-( I prefer to leave this test unchanged.
I left unchanged the raw type casts which are just after a PyType_Check() test. Locally, I have a local Git branch to cleanup way more casts:
I prefer to create a separated PR for these ones :-) |
Ok, I assumed you had a good reason for it :) |
In debug mode, the macro makes sure that its argument is a type using
an assertion.
https://bugs.python.org/issue46417