-
-
Notifications
You must be signed in to change notification settings - Fork 32.1k
Improved error message for metaclass incompatibility #134864
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Metaclass incompatibility error message now more clearly explains the issue including the name of the offending baseclass and the conflicting metaclasses. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Metaclass incompatibility error message now more clearly explains the issue | ||
including the name of the offending baseclass and the conflicting metaclasses. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4105,13 +4105,14 @@ PyType_SUPPORTS_WEAKREFS(PyTypeObject *type) | |
|
||
/* Determine the most derived metatype. */ | ||
PyTypeObject * | ||
_PyType_CalculateMetaclass(PyTypeObject *metatype, PyObject *bases) | ||
_PyType_CalculateMetaclass(PyTypeObject *metatype, | ||
PyObject *bases, | ||
PyObject *name) | ||
{ | ||
Py_ssize_t i, nbases; | ||
PyTypeObject *winner; | ||
PyObject *tmp; | ||
PyTypeObject *tmptype; | ||
|
||
/* Determine the proper metatype to deal with this, | ||
and check for metatype conflicts while we're at it. | ||
Note that if some other metatype wins to contract, | ||
|
@@ -4129,11 +4130,43 @@ _PyType_CalculateMetaclass(PyTypeObject *metatype, PyObject *bases) | |
continue; | ||
} | ||
/* else: */ | ||
PyErr_SetString(PyExc_TypeError, | ||
"metaclass conflict: " | ||
"the metaclass of a derived class " | ||
"must be a (non-strict) subclass " | ||
"of the metaclasses of all its bases"); | ||
PyObject *declared_meta_name = NULL; | ||
PyObject *base_class_name = NULL; | ||
PyObject *base_meta_name = NULL; | ||
PyObject *format_result = NULL; | ||
|
||
declared_meta_name = PyObject_GetAttrString((PyObject *)winner, "__name__"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
PyObject *first_attr = PyObject_GetAttrString(whatever, "first_attr");
if (first_attr == NULL) {
return NULL;
}
PyObject *second_attr = PyObject_GetAttrString(whatever, "second_attr");
if (second_attr == NULL) {
Py_DECREF(first_attr);
return NULL;
}
/* ... */
Py_DECREF(first_attr);
Py_DECREF(second_attr); |
||
base_class_name = PyObject_GetAttrString(tmp, "__name__"); | ||
base_meta_name = PyObject_GetAttrString((PyObject *)tmptype, "__name__"); | ||
|
||
if (declared_meta_name && base_class_name && base_meta_name) { | ||
format_result = PyUnicode_FromFormat( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can use |
||
"Metaclass conflict while defining class: '%U'\n" | ||
"- Declared metaclass: '%U'\n" | ||
"- Incompatible base class: '%U'\n" | ||
"- That base is derived from metaclass: '%U'\n" | ||
"All base classes must be based on classes derived from the same " | ||
"metaclass or a subclass thereof.", | ||
name, | ||
declared_meta_name, | ||
base_class_name, | ||
base_meta_name | ||
); | ||
if (format_result) { | ||
PyErr_SetObject(PyExc_TypeError, format_result); | ||
Py_DECREF(format_result); | ||
} | ||
} else { | ||
/* fallback */ | ||
PyErr_SetString(PyExc_TypeError, | ||
"metaclass conflict: " | ||
"the metaclass of a derived class must be a (non-strict) " | ||
"subclass of the metaclasses of all its bases"); | ||
} | ||
|
||
Py_XDECREF(declared_meta_name); | ||
Py_XDECREF(base_class_name); | ||
Py_XDECREF(base_meta_name); | ||
return NULL; | ||
} | ||
return winner; | ||
|
@@ -4917,7 +4950,7 @@ type_new_get_bases(type_new_ctx *ctx, PyObject **type) | |
|
||
// Search the bases for the proper metatype to deal with this | ||
PyTypeObject *winner; | ||
winner = _PyType_CalculateMetaclass(ctx->metatype, ctx->bases); | ||
winner = _PyType_CalculateMetaclass(ctx->metatype, ctx->bases, ctx->name); | ||
if (winner == NULL) { | ||
return -1; | ||
} | ||
|
@@ -5333,10 +5366,19 @@ PyType_FromMetaclass( | |
if (!metaclass) { | ||
metaclass = &PyType_Type; | ||
} | ||
metaclass = _PyType_CalculateMetaclass(metaclass, bases); | ||
/* Get resolve the name from 'spec' */ | ||
PyObject *name = NULL; | ||
if (spec && spec->name) { | ||
name = PyUnicode_FromString(spec->name); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think we have to manually convert this to a string. |
||
} | ||
|
||
metaclass = _PyType_CalculateMetaclass(metaclass, bases, name); | ||
if (metaclass == NULL) { | ||
goto finally; | ||
} | ||
/* Remove name again */ | ||
Py_XDECREF(name); | ||
|
||
if (!PyType_Check(metaclass)) { | ||
PyErr_Format(PyExc_TypeError, | ||
"Metaclass '%R' is not a subclass of 'type'.", | ||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -169,7 +169,7 @@ builtin___build_class__(PyObject *self, PyObject *const *args, Py_ssize_t nargs, | |||||
/* meta is really a class, so check for a more derived | ||||||
metaclass, or possible metaclass conflicts: */ | ||||||
winner = (PyObject *)_PyType_CalculateMetaclass((PyTypeObject *)meta, | ||||||
bases); | ||||||
bases,name); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
if (winner == NULL) { | ||||||
goto error; | ||||||
} | ||||||
|
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 don't need this file, you can go ahead and delete it.