Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 19 additions & 6 deletions mypyc/lib-rt/vecs/librt_vecs.c
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,9 @@ PyTypeObject VecGenericAliasType = {
static PyObject *extract_optional_item(PyObject *item) {
PyObject *args = PyObject_GetAttrString(item, "__args__");
if (args == NULL) {
PyErr_Clear();
if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_AttributeError)) {
PyErr_Clear();
}
return NULL;
}
if (!PyTuple_CheckExact(args))
Expand All @@ -264,9 +266,11 @@ static PyObject *extract_optional_item(PyObject *item) {
PyObject *item0 = PyTuple_GET_ITEM(args, 0);
PyObject *item1 = PyTuple_GET_ITEM(args, 1);
if (item0 == (PyObject *)Py_None->ob_type) {
Py_INCREF(item1);
Py_DECREF(args);
return item1;
} else if (item1 == (PyObject *)Py_None->ob_type) {
Py_INCREF(item0);
Py_DECREF(args);
return item0;
}
Expand Down Expand Up @@ -300,9 +304,14 @@ static PyObject *vec_class_getitem(PyObject *type, PyObject *item)
size_t depth = 0;
size_t item_type;
int optional = 0;
PyObject *optional_item = NULL;
if (!PyObject_TypeCheck(item, &PyType_Type)) {
PyObject *it = extract_optional_item(item);
if (it == NULL && PyErr_Occurred()) {
return NULL;
}
if (it != NULL) {
optional_item = it;
optional = 1;
item = it;
// Check if this is a specialized value type that doesn't support None
Expand All @@ -314,20 +323,20 @@ static PyObject *vec_class_getitem(PyObject *type, PyObject *item)
item == (PyObject *)&PyBool_Type) {
PyErr_SetString(PyExc_TypeError,
"vec does not support optional value types (use vec[object] instead)");
return NULL;
goto error;
}
}
if (item->ob_type == &VecGenericAliasType) {
if (optional) {
PyErr_SetString(PyExc_TypeError, "optional type not expected in vec[...]");
return NULL;
goto error;
}
VecGenericAlias *p = (VecGenericAlias *)item;
item_type = p->item_type;
depth = p->depth + 1;
} else if (!PyObject_TypeCheck(item, &PyType_Type)) {
PyErr_SetString(PyExc_TypeError, "type object expected in vec[...]");
return NULL;
goto error;
} else {
item_type = (size_t)item | optional;
}
Expand Down Expand Up @@ -357,17 +366,21 @@ static PyObject *vec_class_getitem(PyObject *type, PyObject *item)
if (item == (PyObject *)&PyLong_Type) {
PyErr_Format(PyExc_ValueError, "unsupported type in vec[%s] (use i64, i32, i16, or u8)",
((PyTypeObject *)item)->tp_name);
return NULL;
goto error;
}
VecGenericAlias *p;
p = PyObject_GC_New(VecGenericAlias, &VecGenericAliasType);
if (p == NULL)
return NULL;
goto error;
Py_INCREF(item);
p->item_type = item_type;
p->depth = depth;
PyObject_GC_Track(p);
Py_XDECREF(optional_item);
return (PyObject *)p;
error:
Py_XDECREF(optional_item);
return NULL;
}
}

Expand Down
17 changes: 17 additions & 0 deletions mypyc/test-data/run-vecs-misc-interp.test
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,23 @@ ITEM_TYPES = [i64, u8, i16, i32, float, bool]

# Common tests for all item types

class BadArgs:
@property
def __args__(self) -> Any:
raise RuntimeError("optional lookup failed")

class FakeOptional:
@property
def __args__(self) -> Any:
return (object(), type(None))

def test_optional_lookup_error() -> None:
with assertRaises(RuntimeError, "optional lookup failed"):
vec[BadArgs()]

with assertRaises(TypeError, "type object expected"):
vec[FakeOptional()]

def test_bare_vec_cannot_be_constructed() -> None:
with assertRaises(TypeError):
vec()
Expand Down
Loading