summaryrefslogtreecommitdiffstats
path: root/Python/errors.c
diff options
context:
space:
mode:
authorBrett Cannon <brett@python.org>2012-04-13 00:24:54 (GMT)
committerBrett Cannon <brett@python.org>2012-04-13 00:24:54 (GMT)
commit79ec55e980d7b205bbc78d44e0892d0ef37d3abb (patch)
tree7fca6e94007fd848ec36ba029aee3b1ec2a8785c /Python/errors.c
parentf50b38a11fa951582b7f1656685201269f265784 (diff)
downloadcpython-79ec55e980d7b205bbc78d44e0892d0ef37d3abb.zip
cpython-79ec55e980d7b205bbc78d44e0892d0ef37d3abb.tar.gz
cpython-79ec55e980d7b205bbc78d44e0892d0ef37d3abb.tar.bz2
Issue #1559549: Add 'name' and 'path' attributes to ImportError.
Currently import does not use these attributes as they are planned for use by importlib (which will be another commit). Thanks to Filip GruszczyƄski for the initial patch and Brian Curtin for refining it.
Diffstat (limited to 'Python/errors.c')
-rw-r--r--Python/errors.c47
1 files changed, 47 insertions, 0 deletions
diff --git a/Python/errors.c b/Python/errors.c
index 31fa9e2..345a345 100644
--- a/Python/errors.c
+++ b/Python/errors.c
@@ -585,6 +585,53 @@ PyObject *PyErr_SetFromWindowsErrWithUnicodeFilename(
}
#endif /* MS_WINDOWS */
+PyObject *
+PyErr_SetExcWithArgsKwargs(PyObject *exc, PyObject *args, PyObject *kwargs)
+{
+ PyObject *val;
+
+ /* args must at least be an empty tuple */
+ if (args == NULL)
+ args = PyTuple_New(0);
+
+ val = PyObject_Call(exc, args, kwargs);
+ if (val != NULL) {
+ PyErr_SetObject((PyObject *) Py_TYPE(val), val);
+ Py_DECREF(val);
+ }
+
+ return NULL;
+}
+
+PyObject *
+PyErr_SetFromImportErrorWithNameAndPath(PyObject *msg,
+ PyObject *name, PyObject *path)
+{
+ PyObject *args = PyTuple_New(1);
+ PyObject *kwargs = PyDict_New();
+ PyObject *result;
+
+ if (path == NULL)
+ path = Py_None;
+
+ PyTuple_SetItem(args, 0, msg);
+ PyDict_SetItemString(kwargs, "name", name);
+ PyDict_SetItemString(kwargs, "path", path);
+
+ result = PyErr_SetExcWithArgsKwargs(PyExc_ImportError, args, kwargs);
+
+ Py_DECREF(args);
+ Py_DECREF(kwargs);
+
+ return result;
+}
+
+PyObject *
+PyErr_SetFromImportErrorWithName(PyObject *msg, PyObject *name)
+{
+ return PyErr_SetFromImportErrorWithNameAndPath(msg, name, NULL);
+}
+
void
_PyErr_BadInternalCall(const char *filename, int lineno)
{