From c1b4d1c254cb6d1d50c55e550b9c7f7210c85ec5 Mon Sep 17 00:00:00 2001 From: Andrew Svetlov Date: Sat, 3 Nov 2012 15:56:05 +0200 Subject: Issue #7317: Display full tracebacks when an error occurs asynchronously. Patch by Alon Horev with update by Alexey Kachayev. --- Python/errors.c | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) (limited to 'Python/errors.c') diff --git a/Python/errors.c b/Python/errors.c index 626b16e46f..a2d1a82ddf 100644 --- a/Python/errors.c +++ b/Python/errors.c @@ -798,7 +798,12 @@ PyErr_WriteUnraisable(PyObject *obj) PyErr_Fetch(&t, &v, &tb); f = PySys_GetObject("stderr"); if (f != NULL && f != Py_None) { - PyFile_WriteString("Exception ", f); + if (obj) { + PyFile_WriteString("Exception ignored in: ", f); + PyFile_WriteObject(obj, f, 0); + PyFile_WriteString("\n", f); + } + PyTraceBack_Print(tb, f); if (t) { PyObject* moduleName; char* className; @@ -828,15 +833,11 @@ PyErr_WriteUnraisable(PyObject *obj) PyFile_WriteString(className, f); if (v && v != Py_None) { PyFile_WriteString(": ", f); - PyFile_WriteObject(v, f, 0); + PyFile_WriteObject(v, f, Py_PRINT_RAW); } + PyFile_WriteString("\n", f); Py_XDECREF(moduleName); } - if (obj) { - PyFile_WriteString(" in ", f); - PyFile_WriteObject(obj, f, 0); - } - PyFile_WriteString(" ignored\n", f); PyErr_Clear(); /* Just in case */ } Py_XDECREF(t); -- cgit v1.2.1 From 4c3208fadf4fea67e916d8d8c7a04a28c4ad92f9 Mon Sep 17 00:00:00 2001 From: Andrew Svetlov Date: Wed, 19 Dec 2012 14:33:35 +0200 Subject: Issue #16719: Get rid of WindowsError. Use OSError instead Patch by Serhiy Storchaka. --- Python/errors.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'Python/errors.c') diff --git a/Python/errors.c b/Python/errors.c index a2d1a82ddf..1f955b54f0 100644 --- a/Python/errors.c +++ b/Python/errors.c @@ -588,7 +588,7 @@ PyObject *PyErr_SetExcFromWindowsErr(PyObject *exc, int ierr) PyObject *PyErr_SetFromWindowsErr(int ierr) { - return PyErr_SetExcFromWindowsErrWithFilename(PyExc_WindowsError, + return PyErr_SetExcFromWindowsErrWithFilename(PyExc_OSError, ierr, NULL); } PyObject *PyErr_SetFromWindowsErrWithFilename( @@ -597,7 +597,7 @@ PyObject *PyErr_SetFromWindowsErrWithFilename( { PyObject *name = filename ? PyUnicode_DecodeFSDefault(filename) : NULL; PyObject *result = PyErr_SetExcFromWindowsErrWithFilenameObject( - PyExc_WindowsError, + PyExc_OSError, ierr, name); Py_XDECREF(name); return result; @@ -611,7 +611,7 @@ PyObject *PyErr_SetFromWindowsErrWithUnicodeFilename( PyUnicode_FromUnicode(filename, wcslen(filename)) : NULL; PyObject *result = PyErr_SetExcFromWindowsErrWithFilenameObject( - PyExc_WindowsError, + PyExc_OSError, ierr, name); Py_XDECREF(name); return result; -- cgit v1.2.1 From 21d4e9534aceac9c2753b70bffe4f75ff38ec525 Mon Sep 17 00:00:00 2001 From: Brett Cannon Date: Wed, 12 Jun 2013 23:29:18 -0400 Subject: Issue #15767: Touch up ModuleNotFoundError usage by import. Forgot to raise ModuleNotFoundError when None is found in sys.modules. This led to introducing the C function PyErr_SetImportErrorSubclass() to make setting ModuleNotFoundError easier. Also updated the reference docs to mention ModuleNotFoundError appropriately. Updated the docs for ModuleNotFoundError to mention the None in sys.modules case. Lastly, it was noticed that PyErr_SetImportError() was not setting an exception when returning None in one case. That issue is now fixed. --- Python/errors.c | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) (limited to 'Python/errors.c') diff --git a/Python/errors.c b/Python/errors.c index 1f955b54f0..89021aadd6 100644 --- a/Python/errors.c +++ b/Python/errors.c @@ -619,12 +619,25 @@ PyObject *PyErr_SetFromWindowsErrWithUnicodeFilename( #endif /* MS_WINDOWS */ PyObject * -PyErr_SetImportError(PyObject *msg, PyObject *name, PyObject *path) +PyErr_SetImportErrorSubclass(PyObject *exception, PyObject *msg, + PyObject *name, PyObject *path) { + int issubclass; PyObject *args, *kwargs, *error; - if (msg == NULL) + issubclass = PyObject_IsSubclass(exception, PyExc_ImportError); + if (issubclass < 0) { + return NULL; + } + else if (!issubclass) { + PyErr_SetString(PyExc_TypeError, "expected a subclass of ImportError"); + return NULL; + } + + if (msg == NULL) { + PyErr_SetString(PyExc_TypeError, "expected a message argument"); return NULL; + } args = PyTuple_New(1); if (args == NULL) @@ -649,7 +662,7 @@ PyErr_SetImportError(PyObject *msg, PyObject *name, PyObject *path) PyDict_SetItemString(kwargs, "name", name); PyDict_SetItemString(kwargs, "path", path); - error = PyObject_Call(PyExc_ImportError, args, kwargs); + error = PyObject_Call(exception, args, kwargs); if (error != NULL) { PyErr_SetObject((PyObject *)Py_TYPE(error), error); Py_DECREF(error); @@ -661,6 +674,12 @@ PyErr_SetImportError(PyObject *msg, PyObject *name, PyObject *path) return NULL; } +PyObject * +PyErr_SetImportError(PyObject *msg, PyObject *name, PyObject *path) +{ + return PyErr_SetImportErrorSubclass(PyExc_ImportError, msg, name, path); +} + void _PyErr_BadInternalCall(const char *filename, int lineno) { -- cgit v1.2.1 From af5e4f4219618da2bfcb02b52ac7c7ac54b7542a Mon Sep 17 00:00:00 2001 From: Brett Cannon Date: Thu, 4 Jul 2013 17:48:16 -0400 Subject: Issue #15767: Revert 3a50025f1900 for ModuleNotFoundError --- Python/errors.c | 25 +++---------------------- 1 file changed, 3 insertions(+), 22 deletions(-) (limited to 'Python/errors.c') diff --git a/Python/errors.c b/Python/errors.c index 89021aadd6..1f955b54f0 100644 --- a/Python/errors.c +++ b/Python/errors.c @@ -619,25 +619,12 @@ PyObject *PyErr_SetFromWindowsErrWithUnicodeFilename( #endif /* MS_WINDOWS */ PyObject * -PyErr_SetImportErrorSubclass(PyObject *exception, PyObject *msg, - PyObject *name, PyObject *path) +PyErr_SetImportError(PyObject *msg, PyObject *name, PyObject *path) { - int issubclass; PyObject *args, *kwargs, *error; - issubclass = PyObject_IsSubclass(exception, PyExc_ImportError); - if (issubclass < 0) { - return NULL; - } - else if (!issubclass) { - PyErr_SetString(PyExc_TypeError, "expected a subclass of ImportError"); - return NULL; - } - - if (msg == NULL) { - PyErr_SetString(PyExc_TypeError, "expected a message argument"); + if (msg == NULL) return NULL; - } args = PyTuple_New(1); if (args == NULL) @@ -662,7 +649,7 @@ PyErr_SetImportErrorSubclass(PyObject *exception, PyObject *msg, PyDict_SetItemString(kwargs, "name", name); PyDict_SetItemString(kwargs, "path", path); - error = PyObject_Call(exception, args, kwargs); + error = PyObject_Call(PyExc_ImportError, args, kwargs); if (error != NULL) { PyErr_SetObject((PyObject *)Py_TYPE(error), error); Py_DECREF(error); @@ -674,12 +661,6 @@ PyErr_SetImportErrorSubclass(PyObject *exception, PyObject *msg, return NULL; } -PyObject * -PyErr_SetImportError(PyObject *msg, PyObject *name, PyObject *path) -{ - return PyErr_SetImportErrorSubclass(PyExc_ImportError, msg, name, path); -} - void _PyErr_BadInternalCall(const char *filename, int lineno) { -- cgit v1.2.1 From 0f0e064b4acc5c926afa935dcdb38cfe79337e31 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Fri, 12 Jul 2013 00:37:30 +0200 Subject: Issue #18408: errors.c: in debug mode, calling PyErr_BadInternalCall() now fails with an assertion error --- Python/errors.c | 1 + 1 file changed, 1 insertion(+) (limited to 'Python/errors.c') diff --git a/Python/errors.c b/Python/errors.c index 1f955b54f0..34445b65ea 100644 --- a/Python/errors.c +++ b/Python/errors.c @@ -675,6 +675,7 @@ _PyErr_BadInternalCall(const char *filename, int lineno) void PyErr_BadInternalCall(void) { + assert(0 && "bad argument to internal function"); PyErr_Format(PyExc_SystemError, "bad argument to internal function"); } -- cgit v1.2.1 From 0302b1098edae221ddc2b4a4fe34b05e1a38958c Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Wed, 17 Jul 2013 00:44:53 +0200 Subject: Issue #18408: Fix PyErr_NormalizeException(), handle PyObject_IsSubclass() failure PyObject_IsSubclass() can fail and raise a new exception! --- Python/errors.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) (limited to 'Python/errors.c') diff --git a/Python/errors.c b/Python/errors.c index 34445b65ea..53dd9a9650 100644 --- a/Python/errors.c +++ b/Python/errors.c @@ -227,12 +227,21 @@ PyErr_NormalizeException(PyObject **exc, PyObject **val, PyObject **tb) value will be an instance. */ if (PyExceptionClass_Check(type)) { + int is_subclass; + if (inclass) { + is_subclass = PyObject_IsSubclass(inclass, type); + if (is_subclass < 0) + goto finally; + } + else + is_subclass = 0; + /* if the value was not an instance, or is not an instance whose class is (or is derived from) type, then use the value as an argument to instantiation of the type class. */ - if (!inclass || !PyObject_IsSubclass(inclass, type)) { + if (!inclass || !is_subclass) { PyObject *args, *res; if (value == Py_None) -- cgit v1.2.1 From 352ce458dc0b97db04c87fd82f793797d65c820c Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Wed, 17 Jul 2013 21:54:25 +0200 Subject: Issue #18408: Fix PyErr_SetImportError(), handle PyDict_SetItemString() failure --- Python/errors.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) (limited to 'Python/errors.c') diff --git a/Python/errors.c b/Python/errors.c index 53dd9a9650..c693b78cac 100644 --- a/Python/errors.c +++ b/Python/errors.c @@ -655,8 +655,11 @@ PyErr_SetImportError(PyObject *msg, PyObject *name, PyObject *path) Py_INCREF(msg); PyTuple_SET_ITEM(args, 0, msg); - PyDict_SetItemString(kwargs, "name", name); - PyDict_SetItemString(kwargs, "path", path); + + if (PyDict_SetItemString(kwargs, "name", name) < 0) + return NULL; + if (PyDict_SetItemString(kwargs, "path", path) < 0) + return NULL; error = PyObject_Call(PyExc_ImportError, args, kwargs); if (error != NULL) { -- cgit v1.2.1 From b951431ae7602c52c518a55e31b823714110b04d Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Thu, 18 Jul 2013 01:41:08 +0200 Subject: Issue #18408: PyEval_EvalFrameEx() and PyEval_CallObjectWithKeywords() now fail with an assertion error if they are called with an exception set (PyErr_Occurred()). If these functions are called with an exception set, the exception may be cleared and so the caller looses its exception. Add also assertions to PyEval_CallObjectWithKeywords() and call_function() to check if the function succeed with no exception set, or the function failed with an exception set. --- Python/errors.c | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'Python/errors.c') diff --git a/Python/errors.c b/Python/errors.c index c693b78cac..b0f8b18939 100644 --- a/Python/errors.c +++ b/Python/errors.c @@ -71,6 +71,11 @@ PyErr_SetObject(PyObject *exception, PyObject *value) if (value == NULL || !PyExceptionInstance_Check(value)) { /* We must normalize the value right now */ PyObject *args, *fixed_value; +#ifdef Py_DEBUG + /* in debug mode, PyEval_EvalFrameEx() fails with an assertion + error if an exception is set when it is called */ + PyErr_Clear(); +#endif if (value == NULL || value == Py_None) args = PyTuple_New(0); else if (PyTuple_Check(value)) { @@ -707,6 +712,12 @@ PyErr_Format(PyObject *exception, const char *format, ...) va_start(vargs); #endif +#ifdef Py_DEBUG + /* in debug mode, PyEval_EvalFrameEx() fails with an assertion error + if an exception is set when it is called */ + PyErr_Clear(); +#endif + string = PyUnicode_FromFormatV(format, vargs); PyErr_SetObject(exception, string); Py_XDECREF(string); -- cgit v1.2.1 From 964a3316f3209cfab36719a2f544b74d2694e3dc Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Mon, 22 Jul 2013 22:28:37 +0200 Subject: Issue #18520: PyErr_NoMemory() now fails with a fatal error if it is called before PyExc_MemoryError has been initialized by _PyExc_Init() --- Python/errors.c | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'Python/errors.c') diff --git a/Python/errors.c b/Python/errors.c index b0f8b18939..8c0c01849c 100644 --- a/Python/errors.c +++ b/Python/errors.c @@ -380,6 +380,12 @@ PyErr_BadArgument(void) PyObject * PyErr_NoMemory(void) { + if (Py_TYPE(PyExc_MemoryError) == NULL) { + /* PyErr_NoMemory() has been called before PyExc_MemoryError has been + initialized by _PyExc_Init() */ + Py_FatalError("Out of memory and PyExc_MemoryError is not " + "initialized yet"); + } PyErr_SetNone(PyExc_MemoryError); return NULL; } -- cgit v1.2.1 From 69e213e962f615d59241c086f30f6bec5e66fcd3 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Mon, 26 Aug 2013 14:04:10 +0200 Subject: Issue #18664, #18408: Rewrite PyErr_WriteUnraisable() to handle errors * Catch PyFile_WriteString() and PyFile_WriteObject() errors * Clear the current exception on _PyObject_GetAttrId() failure * Use PyUnicode_CompareWithASCIIString() and PyFile_WriteObject() instead of _PyUnicode_AsString() and strcmp() to avoid Unicode encoding error. stderr has a more tolerant error handler than utf-8/strict. --- Python/errors.c | 102 ++++++++++++++++++++++++++++++++++---------------------- 1 file changed, 62 insertions(+), 40 deletions(-) (limited to 'Python/errors.c') diff --git a/Python/errors.c b/Python/errors.c index 8c0c01849c..2d5eb6c21e 100644 --- a/Python/errors.c +++ b/Python/errors.c @@ -825,54 +825,76 @@ PyErr_WriteUnraisable(PyObject *obj) { _Py_IDENTIFIER(__module__); PyObject *f, *t, *v, *tb; + PyObject *moduleName = NULL; + char* className; + PyErr_Fetch(&t, &v, &tb); + f = PySys_GetObject("stderr"); - if (f != NULL && f != Py_None) { - if (obj) { - PyFile_WriteString("Exception ignored in: ", f); - PyFile_WriteObject(obj, f, 0); - PyFile_WriteString("\n", f); - } - PyTraceBack_Print(tb, f); - if (t) { - PyObject* moduleName; - char* className; - assert(PyExceptionClass_Check(t)); - className = PyExceptionClass_Name(t); - if (className != NULL) { - char *dot = strrchr(className, '.'); - if (dot != NULL) - className = dot+1; - } + if (f == NULL || f == Py_None) + goto done; + + if (obj) { + if (PyFile_WriteString("Exception ignored in: ", f) < 0) + goto done; + if (PyFile_WriteObject(obj, f, 0) < 0) + goto done; + if (PyFile_WriteString("\n", f) < 0) + goto done; + } - moduleName = _PyObject_GetAttrId(t, &PyId___module__); - if (moduleName == NULL) - PyFile_WriteString("", f); - else { - char* modstr = _PyUnicode_AsString(moduleName); - if (modstr && - strcmp(modstr, "builtins") != 0) - { - PyFile_WriteString(modstr, f); - PyFile_WriteString(".", f); - } - } - if (className == NULL) - PyFile_WriteString("", f); - else - PyFile_WriteString(className, f); - if (v && v != Py_None) { - PyFile_WriteString(": ", f); - PyFile_WriteObject(v, f, Py_PRINT_RAW); - } - PyFile_WriteString("\n", f); - Py_XDECREF(moduleName); + if (PyTraceBack_Print(tb, f) < 0) + goto done; + + if (!t) + goto done; + + assert(PyExceptionClass_Check(t)); + className = PyExceptionClass_Name(t); + if (className != NULL) { + char *dot = strrchr(className, '.'); + if (dot != NULL) + className = dot+1; + } + + moduleName = _PyObject_GetAttrId(t, &PyId___module__); + if (moduleName == NULL) { + PyErr_Clear(); + if (PyFile_WriteString("", f) < 0) + goto done; + } + else { + if (PyUnicode_CompareWithASCIIString(moduleName, "builtins") != 0) { + if (PyFile_WriteObject(moduleName, f, Py_PRINT_RAW) < 0) + goto done; + if (PyFile_WriteString(".", f) < 0) + goto done; } - PyErr_Clear(); /* Just in case */ } + if (className == NULL) { + if (PyFile_WriteString("", f) < 0) + goto done; + } + else { + if (PyFile_WriteString(className, f) < 0) + goto done; + } + + if (v && v != Py_None) { + if (PyFile_WriteString(": ", f) < 0) + goto done; + if (PyFile_WriteObject(v, f, Py_PRINT_RAW) < 0) + goto done; + } + if (PyFile_WriteString("\n", f) < 0) + goto done; + +done: + Py_XDECREF(moduleName); Py_XDECREF(t); Py_XDECREF(v); Py_XDECREF(tb); + PyErr_Clear(); /* Just in case */ } extern PyObject *PyModule_GetWarningsModule(void); -- cgit v1.2.1 From c36bfc1c99c6dffff8cb89c723001902d721ff97 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Mon, 26 Aug 2013 22:28:21 +0200 Subject: Close #11619: The parser and the import machinery do not encode Unicode filenames anymore on Windows. --- Python/errors.c | 56 +++++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 41 insertions(+), 15 deletions(-) (limited to 'Python/errors.c') diff --git a/Python/errors.c b/Python/errors.c index 2d5eb6c21e..7985eab536 100644 --- a/Python/errors.c +++ b/Python/errors.c @@ -901,7 +901,8 @@ extern PyObject *PyModule_GetWarningsModule(void); void -PyErr_SyntaxLocation(const char *filename, int lineno) { +PyErr_SyntaxLocation(const char *filename, int lineno) +{ PyErr_SyntaxLocationEx(filename, lineno, -1); } @@ -911,7 +912,7 @@ PyErr_SyntaxLocation(const char *filename, int lineno) { to make printing of exceptions believe it is a syntax error. */ void -PyErr_SyntaxLocationEx(const char *filename, int lineno, int col_offset) +PyErr_SyntaxLocationObject(PyObject *filename, int lineno, int col_offset) { PyObject *exc, *v, *tb, *tmp; _Py_IDENTIFIER(filename); @@ -945,16 +946,10 @@ PyErr_SyntaxLocationEx(const char *filename, int lineno, int col_offset) } } if (filename != NULL) { - tmp = PyUnicode_DecodeFSDefault(filename); - if (tmp == NULL) + if (_PyObject_SetAttrId(v, &PyId_filename, filename)) PyErr_Clear(); - else { - if (_PyObject_SetAttrId(v, &PyId_filename, tmp)) - PyErr_Clear(); - Py_DECREF(tmp); - } - tmp = PyErr_ProgramText(filename, lineno); + tmp = PyErr_ProgramTextObject(filename, lineno); if (tmp) { if (_PyObject_SetAttrId(v, &PyId_text, tmp)) PyErr_Clear(); @@ -984,6 +979,21 @@ PyErr_SyntaxLocationEx(const char *filename, int lineno, int col_offset) PyErr_Restore(exc, v, tb); } +void +PyErr_SyntaxLocationEx(const char *filename, int lineno, int col_offset) +{ + PyObject *fileobj; + if (filename != NULL) { + fileobj = PyUnicode_DecodeFSDefault(filename); + if (fileobj == NULL) + PyErr_Clear(); + } + else + fileobj = NULL; + PyErr_SyntaxLocationObject(fileobj, lineno, col_offset); + Py_XDECREF(fileobj); +} + /* Attempt to load the line of text that the exception refers to. If it fails, it will return NULL but will not set an exception. @@ -991,15 +1001,11 @@ PyErr_SyntaxLocationEx(const char *filename, int lineno, int col_offset) functionality in tb_displayline() in traceback.c. */ PyObject * -PyErr_ProgramText(const char *filename, int lineno) +err_programtext(FILE *fp, int lineno) { - FILE *fp; int i; char linebuf[1000]; - if (filename == NULL || *filename == '\0' || lineno <= 0) - return NULL; - fp = fopen(filename, "r" PY_STDIOTEXTMODE); if (fp == NULL) return NULL; for (i = 0; i < lineno; i++) { @@ -1030,6 +1036,26 @@ PyErr_ProgramText(const char *filename, int lineno) return NULL; } +PyObject * +PyErr_ProgramText(const char *filename, int lineno) +{ + FILE *fp; + if (filename == NULL || *filename == '\0' || lineno <= 0) + return NULL; + fp = fopen(filename, "r" PY_STDIOTEXTMODE); + return err_programtext(fp, lineno); +} + +PyObject * +PyErr_ProgramTextObject(PyObject *filename, int lineno) +{ + FILE *fp; + if (filename == NULL || lineno <= 0) + return NULL; + fp = _Py_fopen(filename, "r" PY_STDIOTEXTMODE); + return err_programtext(fp, lineno); +} + #ifdef __cplusplus } #endif -- cgit v1.2.1 From e5883d6d95989121670eca012dd5bde1721747e7 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Wed, 28 Aug 2013 00:53:59 +0200 Subject: Issue #18571: Implementation of the PEP 446: file descriptors and file handles are now created non-inheritable; add functions os.get/set_inheritable(), os.get/set_handle_inheritable() and socket.socket.get/set_inheritable(). --- Python/errors.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'Python/errors.c') diff --git a/Python/errors.c b/Python/errors.c index 7985eab536..93b1120191 100644 --- a/Python/errors.c +++ b/Python/errors.c @@ -1042,7 +1042,7 @@ PyErr_ProgramText(const char *filename, int lineno) FILE *fp; if (filename == NULL || *filename == '\0' || lineno <= 0) return NULL; - fp = fopen(filename, "r" PY_STDIOTEXTMODE); + fp = _Py_fopen(filename, "r" PY_STDIOTEXTMODE); return err_programtext(fp, lineno); } @@ -1052,7 +1052,7 @@ PyErr_ProgramTextObject(PyObject *filename, int lineno) FILE *fp; if (filename == NULL || lineno <= 0) return NULL; - fp = _Py_fopen(filename, "r" PY_STDIOTEXTMODE); + fp = _Py_fopen_obj(filename, "r" PY_STDIOTEXTMODE); return err_programtext(fp, lineno); } -- cgit v1.2.1 From 81ee0af7749af3994faad79de5e3092e5fb8ea49 Mon Sep 17 00:00:00 2001 From: Raymond Hettinger Date: Mon, 2 Sep 2013 15:59:26 -0700 Subject: Factor-out the common code for setting a KeyError. --- Python/errors.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'Python/errors.c') diff --git a/Python/errors.c b/Python/errors.c index 93b1120191..b67448090f 100644 --- a/Python/errors.c +++ b/Python/errors.c @@ -117,6 +117,20 @@ PyErr_SetObject(PyObject *exception, PyObject *value) PyErr_Restore(exception, value, tb); } +/* Set a key error with the specified argument, wrapping it in a + * tuple automatically so that tuple keys are not unpacked as the + * exception arguments. */ +void +_PyErr_SetKeyError(PyObject *arg) +{ + PyObject *tup; + tup = PyTuple_Pack(1, arg); + if (!tup) + return; /* caller will expect error to be set anyway */ + PyErr_SetObject(PyExc_KeyError, tup); + Py_DECREF(tup); +} + void PyErr_SetNone(PyObject *exception) { -- cgit v1.2.1 From 337c5c534d5987d149dbc2d8058a93270184ff8c Mon Sep 17 00:00:00 2001 From: Antoine Pitrou Date: Sat, 12 Oct 2013 22:41:17 +0200 Subject: Don't export internal symbols ("make smelly") --- Python/errors.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'Python/errors.c') diff --git a/Python/errors.c b/Python/errors.c index b67448090f..1832b5b602 100644 --- a/Python/errors.c +++ b/Python/errors.c @@ -1014,7 +1014,7 @@ PyErr_SyntaxLocationEx(const char *filename, int lineno, int col_offset) XXX The functionality of this function is quite similar to the functionality in tb_displayline() in traceback.c. */ -PyObject * +static PyObject * err_programtext(FILE *fp, int lineno) { int i; -- cgit v1.2.1