mirror of https://github.com/python/cpython.git
bpo-44483: Fix crash in union object with bad ``__module__`` (GH-26848)
This commit is contained in:
parent
bc6c12c72a
commit
adfa1ba398
|
@ -747,6 +747,15 @@ def __eq__(self, other):
|
||||||
with self.assertRaises(TypeError):
|
with self.assertRaises(TypeError):
|
||||||
issubclass(list, type_)
|
issubclass(list, type_)
|
||||||
|
|
||||||
|
def test_or_type_operator_with_bad_module(self):
|
||||||
|
class TypeVar:
|
||||||
|
@property
|
||||||
|
def __module__(self):
|
||||||
|
1 / 0
|
||||||
|
# Crashes in Issue44483
|
||||||
|
with self.assertRaises(ZeroDivisionError):
|
||||||
|
str | TypeVar()
|
||||||
|
|
||||||
def test_ellipsis_type(self):
|
def test_ellipsis_type(self):
|
||||||
self.assertIsInstance(Ellipsis, types.EllipsisType)
|
self.assertIsInstance(Ellipsis, types.EllipsisType)
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,2 @@
|
||||||
|
Fix a crash in ``types.Union`` objects when creating a union of an object
|
||||||
|
with bad ``__module__`` field.
|
|
@ -284,6 +284,16 @@ is_new_type(PyObject *obj)
|
||||||
return is_typing_module(obj);
|
return is_typing_module(obj);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Emulates short-circuiting behavior of the ``||`` operator
|
||||||
|
// while also checking negative values.
|
||||||
|
#define CHECK_RES(res) { \
|
||||||
|
int result = res; \
|
||||||
|
if (result) { \
|
||||||
|
return result; \
|
||||||
|
} \
|
||||||
|
}
|
||||||
|
|
||||||
|
// Returns 1 on true, 0 on false, and -1 on error.
|
||||||
static int
|
static int
|
||||||
is_unionable(PyObject *obj)
|
is_unionable(PyObject *obj)
|
||||||
{
|
{
|
||||||
|
@ -291,10 +301,11 @@ is_unionable(PyObject *obj)
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
PyTypeObject *type = Py_TYPE(obj);
|
PyTypeObject *type = Py_TYPE(obj);
|
||||||
|
CHECK_RES(is_typevar(obj));
|
||||||
|
CHECK_RES(is_new_type(obj));
|
||||||
|
CHECK_RES(is_special_form(obj));
|
||||||
return (
|
return (
|
||||||
is_typevar(obj) ||
|
// The following checks never fail.
|
||||||
is_new_type(obj) ||
|
|
||||||
is_special_form(obj) ||
|
|
||||||
PyType_Check(obj) ||
|
PyType_Check(obj) ||
|
||||||
PyObject_TypeCheck(obj, &Py_GenericAliasType) ||
|
PyObject_TypeCheck(obj, &Py_GenericAliasType) ||
|
||||||
type == &_Py_UnionType);
|
type == &_Py_UnionType);
|
||||||
|
|
Loading…
Reference in New Issue