annotate Python/errors.c @ 62453:e9fb042e46fb legacy-trunk

Fix misindents in compile.c (for Benjamin). Of course, whoever used the wrong indentation rules needs to be spanked.
author Antoine Pitrou <solipsis@pitrou.net>
date Tue, 22 Jun 2010 21:42:05 +0000
parents fd25030223b0
children b36cb4602e21
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
145
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 90
diff changeset
1
5370
1ea1f019268d Quickly renamed. Also removed the long comment explaining why this is
Guido van Rossum <guido@python.org>
parents: 5318
diff changeset
2 /* Error handling */
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3
5370
1ea1f019268d Quickly renamed. Also removed the long comment explaining why this is
Guido van Rossum <guido@python.org>
parents: 5318
diff changeset
4 #include "Python.h"
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 66
diff changeset
5
2502
d5978238e3ba changes in NT tests
Guido van Rossum <guido@python.org>
parents: 2376
diff changeset
6 #ifndef __STDC__
5318
2c4b802a3b5d Keep Microsoft VC happy.
Guido van Rossum <guido@python.org>
parents: 5312
diff changeset
7 #ifndef MS_WINDOWS
13447
a89f7f268de4 Nuke all remaining occurrences of Py_PROTO and Py_FPROTO.
Tim Peters <tim.peters@gmail.com>
parents: 13256
diff changeset
8 extern char *strerror(int);
2502
d5978238e3ba changes in NT tests
Guido van Rossum <guido@python.org>
parents: 2376
diff changeset
9 #endif
5318
2c4b802a3b5d Keep Microsoft VC happy.
Guido van Rossum <guido@python.org>
parents: 5312
diff changeset
10 #endif
59
433f52d1d958 Added prototype for strerror.
Guido van Rossum <guido@python.org>
parents: 37
diff changeset
11
24071
8142f8d668a0 Patch #569753: Remove support for WIN16.
Martin v. Löwis <martin@v.loewis.de>
parents: 23029
diff changeset
12 #ifdef MS_WINDOWS
10638
df8a1c281ad7 Patch by Christian Tismer for Win32, to use FormatMessage() instead of
Guido van Rossum <guido@python.org>
parents: 9671
diff changeset
13 #include "windows.h"
df8a1c281ad7 Patch by Christian Tismer for Win32, to use FormatMessage() instead of
Guido van Rossum <guido@python.org>
parents: 9671
diff changeset
14 #include "winbase.h"
df8a1c281ad7 Patch by Christian Tismer for Win32, to use FormatMessage() instead of
Guido van Rossum <guido@python.org>
parents: 9671
diff changeset
15 #endif
df8a1c281ad7 Patch by Christian Tismer for Win32, to use FormatMessage() instead of
Guido van Rossum <guido@python.org>
parents: 9671
diff changeset
16
14335
951eb3af3494 code part of patch #100895 by Fredrik Lundh
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 14330
diff changeset
17 #include <ctype.h>
951eb3af3494 code part of patch #100895 by Fredrik Lundh
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 14330
diff changeset
18
37655
2f3f2e32dfd9 spread the extern "C" { } magic pixie dust around. Python itself builds now
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36751
diff changeset
19 #ifdef __cplusplus
2f3f2e32dfd9 spread the extern "C" { } magic pixie dust around. Python itself builds now
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36751
diff changeset
20 extern "C" {
2f3f2e32dfd9 spread the extern "C" { } magic pixie dust around. Python itself builds now
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36751
diff changeset
21 #endif
2f3f2e32dfd9 spread the extern "C" { } magic pixie dust around. Python itself builds now
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36751
diff changeset
22
2f3f2e32dfd9 spread the extern "C" { } magic pixie dust around. Python itself builds now
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36751
diff changeset
23
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
24 void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13578
diff changeset
25 PyErr_Restore(PyObject *type, PyObject *value, PyObject *traceback)
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2019
diff changeset
26 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
27 PyThreadState *tstate = PyThreadState_GET();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
28 PyObject *oldtype, *oldvalue, *oldtraceback;
5405
b7871ca930ad Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents: 5370
diff changeset
29
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
30 if (traceback != NULL && !PyTraceBack_Check(traceback)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
31 /* XXX Should never happen -- fatal error instead? */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
32 /* Well, it could be None. */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
33 Py_DECREF(traceback);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
34 traceback = NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
35 }
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2019
diff changeset
36
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
37 /* Save these in locals to safeguard against recursive
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
38 invocation through Py_XDECREF */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
39 oldtype = tstate->curexc_type;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
40 oldvalue = tstate->curexc_value;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
41 oldtraceback = tstate->curexc_traceback;
5405
b7871ca930ad Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents: 5370
diff changeset
42
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
43 tstate->curexc_type = type;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
44 tstate->curexc_value = value;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
45 tstate->curexc_traceback = traceback;
5405
b7871ca930ad Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents: 5370
diff changeset
46
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
47 Py_XDECREF(oldtype);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
48 Py_XDECREF(oldvalue);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
49 Py_XDECREF(oldtraceback);
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2019
diff changeset
50 }
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2019
diff changeset
51
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2019
diff changeset
52 void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13578
diff changeset
53 PyErr_SetObject(PyObject *exception, PyObject *value)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
54 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
55 Py_XINCREF(exception);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
56 Py_XINCREF(value);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
57 PyErr_Restore(exception, value, (PyObject *)NULL);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
58 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
59
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
60 void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13578
diff changeset
61 PyErr_SetNone(PyObject *exception)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
62 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
63 PyErr_SetObject(exception, (PyObject *)NULL);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
64 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
65
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
66 void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13578
diff changeset
67 PyErr_SetString(PyObject *exception, const char *string)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
68 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
69 PyObject *value = PyString_FromString(string);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
70 PyErr_SetObject(exception, value);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
71 Py_XDECREF(value);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
72 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
73
1832
79e420805c67 err_clear: clear interpreter stack trace
Guido van Rossum <guido@python.org>
parents: 1319
diff changeset
74
5370
1ea1f019268d Quickly renamed. Also removed the long comment explaining why this is
Guido van Rossum <guido@python.org>
parents: 5318
diff changeset
75 PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13578
diff changeset
76 PyErr_Occurred(void)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
77 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
78 PyThreadState *tstate = PyThreadState_GET();
5405
b7871ca930ad Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents: 5370
diff changeset
79
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
80 return tstate->curexc_type;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
81 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
82
6146
a2a1df0ca808 Three new C API functions:
Barry Warsaw <barry@python.org>
parents: 5979
diff changeset
83
a2a1df0ca808 Three new C API functions:
Barry Warsaw <barry@python.org>
parents: 5979
diff changeset
84 int
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13578
diff changeset
85 PyErr_GivenExceptionMatches(PyObject *err, PyObject *exc)
6146
a2a1df0ca808 Three new C API functions:
Barry Warsaw <barry@python.org>
parents: 5979
diff changeset
86 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
87 if (err == NULL || exc == NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
88 /* maybe caused by "import exceptions" that failed early on */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
89 return 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
90 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
91 if (PyTuple_Check(exc)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
92 Py_ssize_t i, n;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
93 n = PyTuple_Size(exc);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
94 for (i = 0; i < n; i++) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
95 /* Test recursively */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
96 if (PyErr_GivenExceptionMatches(
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
97 err, PyTuple_GET_ITEM(exc, i)))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
98 {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
99 return 1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
100 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
101 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
102 return 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
103 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
104 /* err might be an instance, so check its class. */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
105 if (PyExceptionInstance_Check(err))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
106 err = PyExceptionInstance_Class(err);
6146
a2a1df0ca808 Three new C API functions:
Barry Warsaw <barry@python.org>
parents: 5979
diff changeset
107
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
108 if (PyExceptionClass_Check(err) && PyExceptionClass_Check(exc)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
109 int res = 0, reclimit;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
110 PyObject *exception, *value, *tb;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
111 PyErr_Fetch(&exception, &value, &tb);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
112 /* Temporarily bump the recursion limit, so that in the most
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
113 common case PyObject_IsSubclass will not raise a recursion
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
114 error we have to ignore anyway. */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
115 reclimit = Py_GetRecursionLimit();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
116 Py_SetRecursionLimit(reclimit + 5);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
117 res = PyObject_IsSubclass(err, exc);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
118 Py_SetRecursionLimit(reclimit);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
119 /* This function must not fail, so print the error here */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
120 if (res == -1) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
121 PyErr_WriteUnraisable(err);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
122 res = 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
123 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
124 PyErr_Restore(exception, value, tb);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
125 return res;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
126 }
6146
a2a1df0ca808 Three new C API functions:
Barry Warsaw <barry@python.org>
parents: 5979
diff changeset
127
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
128 return err == exc;
6146
a2a1df0ca808 Three new C API functions:
Barry Warsaw <barry@python.org>
parents: 5979
diff changeset
129 }
10638
df8a1c281ad7 Patch by Christian Tismer for Win32, to use FormatMessage() instead of
Guido van Rossum <guido@python.org>
parents: 9671
diff changeset
130
6146
a2a1df0ca808 Three new C API functions:
Barry Warsaw <barry@python.org>
parents: 5979
diff changeset
131
a2a1df0ca808 Three new C API functions:
Barry Warsaw <barry@python.org>
parents: 5979
diff changeset
132 int
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13578
diff changeset
133 PyErr_ExceptionMatches(PyObject *exc)
6146
a2a1df0ca808 Three new C API functions:
Barry Warsaw <barry@python.org>
parents: 5979
diff changeset
134 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
135 return PyErr_GivenExceptionMatches(PyErr_Occurred(), exc);
6146
a2a1df0ca808 Three new C API functions:
Barry Warsaw <barry@python.org>
parents: 5979
diff changeset
136 }
a2a1df0ca808 Three new C API functions:
Barry Warsaw <barry@python.org>
parents: 5979
diff changeset
137
a2a1df0ca808 Three new C API functions:
Barry Warsaw <barry@python.org>
parents: 5979
diff changeset
138
a2a1df0ca808 Three new C API functions:
Barry Warsaw <barry@python.org>
parents: 5979
diff changeset
139 /* Used in many places to normalize a raised exception, including in
a2a1df0ca808 Three new C API functions:
Barry Warsaw <barry@python.org>
parents: 5979
diff changeset
140 eval_code2(), do_raise(), and PyErr_Print()
a2a1df0ca808 Three new C API functions:
Barry Warsaw <barry@python.org>
parents: 5979
diff changeset
141 */
a2a1df0ca808 Three new C API functions:
Barry Warsaw <barry@python.org>
parents: 5979
diff changeset
142 void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13578
diff changeset
143 PyErr_NormalizeException(PyObject **exc, PyObject **val, PyObject **tb)
6146
a2a1df0ca808 Three new C API functions:
Barry Warsaw <barry@python.org>
parents: 5979
diff changeset
144 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
145 PyObject *type = *exc;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
146 PyObject *value = *val;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
147 PyObject *inclass = NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
148 PyObject *initial_tb = NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
149 PyThreadState *tstate = NULL;
6146
a2a1df0ca808 Three new C API functions:
Barry Warsaw <barry@python.org>
parents: 5979
diff changeset
150
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
151 if (type == NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
152 /* There was no exception, so nothing to do. */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
153 return;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
154 }
13883
15ad4088a700 Avoid dumping core when PyErr_NormalizeException() is called without
Guido van Rossum <guido@python.org>
parents: 13658
diff changeset
155
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
156 /* If PyErr_SetNone() was used, the value will have been actually
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
157 set to NULL.
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
158 */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
159 if (!value) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
160 value = Py_None;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
161 Py_INCREF(value);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
162 }
6146
a2a1df0ca808 Three new C API functions:
Barry Warsaw <barry@python.org>
parents: 5979
diff changeset
163
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
164 if (PyExceptionInstance_Check(value))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
165 inclass = PyExceptionInstance_Class(value);
6146
a2a1df0ca808 Three new C API functions:
Barry Warsaw <barry@python.org>
parents: 5979
diff changeset
166
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
167 /* Normalize the exception so that if the type is a class, the
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
168 value will be an instance.
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
169 */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
170 if (PyExceptionClass_Check(type)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
171 /* if the value was not an instance, or is not an instance
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
172 whose class is (or is derived from) type, then use the
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
173 value as an argument to instantiation of the type
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
174 class.
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
175 */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
176 if (!inclass || !PyObject_IsSubclass(inclass, type)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
177 PyObject *args, *res;
6146
a2a1df0ca808 Three new C API functions:
Barry Warsaw <barry@python.org>
parents: 5979
diff changeset
178
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
179 if (value == Py_None)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
180 args = PyTuple_New(0);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
181 else if (PyTuple_Check(value)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
182 Py_INCREF(value);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
183 args = value;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
184 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
185 else
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
186 args = PyTuple_Pack(1, value);
6146
a2a1df0ca808 Three new C API functions:
Barry Warsaw <barry@python.org>
parents: 5979
diff changeset
187
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
188 if (args == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
189 goto finally;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
190 res = PyEval_CallObject(type, args);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
191 Py_DECREF(args);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
192 if (res == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
193 goto finally;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
194 Py_DECREF(value);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
195 value = res;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
196 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
197 /* if the class of the instance doesn't exactly match the
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
198 class of the type, believe the instance
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
199 */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
200 else if (inclass != type) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
201 Py_DECREF(type);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
202 type = inclass;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
203 Py_INCREF(type);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
204 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
205 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
206 *exc = type;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
207 *val = value;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
208 return;
6146
a2a1df0ca808 Three new C API functions:
Barry Warsaw <barry@python.org>
parents: 5979
diff changeset
209 finally:
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
210 Py_DECREF(type);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
211 Py_DECREF(value);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
212 /* If the new exception doesn't set a traceback and the old
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
213 exception had a traceback, use the old traceback for the
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
214 new exception. It's better than nothing.
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
215 */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
216 initial_tb = *tb;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
217 PyErr_Fetch(exc, val, tb);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
218 if (initial_tb != NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
219 if (*tb == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
220 *tb = initial_tb;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
221 else
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
222 Py_DECREF(initial_tb);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
223 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
224 /* normalize recursively */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
225 tstate = PyThreadState_GET();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
226 if (++tstate->recursion_depth > Py_GetRecursionLimit()) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
227 --tstate->recursion_depth;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
228 /* throw away the old exception... */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
229 Py_DECREF(*exc);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
230 Py_DECREF(*val);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
231 /* ... and use the recursion error instead */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
232 *exc = PyExc_RuntimeError;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
233 *val = PyExc_RecursionErrorInst;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
234 Py_INCREF(*exc);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
235 Py_INCREF(*val);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
236 /* just keeping the old traceback */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
237 return;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
238 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
239 PyErr_NormalizeException(exc, val, tb);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
240 --tstate->recursion_depth;
6146
a2a1df0ca808 Three new C API functions:
Barry Warsaw <barry@python.org>
parents: 5979
diff changeset
241 }
a2a1df0ca808 Three new C API functions:
Barry Warsaw <barry@python.org>
parents: 5979
diff changeset
242
a2a1df0ca808 Three new C API functions:
Barry Warsaw <barry@python.org>
parents: 5979
diff changeset
243
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
244 void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13578
diff changeset
245 PyErr_Fetch(PyObject **p_type, PyObject **p_value, PyObject **p_traceback)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
246 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
247 PyThreadState *tstate = PyThreadState_GET();
5405
b7871ca930ad Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents: 5370
diff changeset
248
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
249 *p_type = tstate->curexc_type;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
250 *p_value = tstate->curexc_value;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
251 *p_traceback = tstate->curexc_traceback;
5405
b7871ca930ad Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents: 5370
diff changeset
252
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
253 tstate->curexc_type = NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
254 tstate->curexc_value = NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
255 tstate->curexc_traceback = NULL;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
256 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
257
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
258 void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13578
diff changeset
259 PyErr_Clear(void)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
260 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
261 PyErr_Restore(NULL, NULL, NULL);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
262 }
9
33701db4ea55 Adde dconvenience functions.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
263
33701db4ea55 Adde dconvenience functions.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
264 /* Convenience functions to set a type error exception and return 0 */
33701db4ea55 Adde dconvenience functions.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
265
33701db4ea55 Adde dconvenience functions.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
266 int
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13578
diff changeset
267 PyErr_BadArgument(void)
9
33701db4ea55 Adde dconvenience functions.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
268 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
269 PyErr_SetString(PyExc_TypeError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
270 "bad argument type for built-in operation");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
271 return 0;
9
33701db4ea55 Adde dconvenience functions.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
272 }
33701db4ea55 Adde dconvenience functions.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
273
5370
1ea1f019268d Quickly renamed. Also removed the long comment explaining why this is
Guido van Rossum <guido@python.org>
parents: 5318
diff changeset
274 PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13578
diff changeset
275 PyErr_NoMemory(void)
9
33701db4ea55 Adde dconvenience functions.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
276 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
277 if (PyErr_ExceptionMatches(PyExc_MemoryError))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
278 /* already current */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
279 return NULL;
14066
25eea88047e9 Do not set a MemoryError exception over another MemoryError exception,
Vladimir Marangozov <vladimir.marangozov@t-online.de>
parents: 13883
diff changeset
280
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
281 /* raise the pre-allocated instance if it still exists */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
282 if (PyExc_MemoryErrorInst)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
283 PyErr_SetObject(PyExc_MemoryError, PyExc_MemoryErrorInst);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
284 else
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
285 /* this will probably fail since there's no memory and hee,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
286 hee, we have to instantiate this class
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
287 */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
288 PyErr_SetNone(PyExc_MemoryError);
6190
b5757e87eb32 PyErr_NoMemory(): If the pre-instantiated memory exception is non-null
Barry Warsaw <barry@python.org>
parents: 6146
diff changeset
289
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
290 return NULL;
9
33701db4ea55 Adde dconvenience functions.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
291 }
33701db4ea55 Adde dconvenience functions.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
292
5370
1ea1f019268d Quickly renamed. Also removed the long comment explaining why this is
Guido van Rossum <guido@python.org>
parents: 5318
diff changeset
293 PyObject *
25535
861065256487 Patch 594001: PEP 277 - Unicode file name support for Windows NT.
Mark Hammond <mhammond@skippinet.com.au>
parents: 24423
diff changeset
294 PyErr_SetFromErrnoWithFilenameObject(PyObject *exc, PyObject *filenameObject)
9
33701db4ea55 Adde dconvenience functions.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
295 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
296 PyObject *v;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
297 char *s;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
298 int i = errno;
22205
f4487350c537 Patch #494045: patches errno and stat to cope on plan9.
Martin v. Löwis <martin@v.loewis.de>
parents: 22121
diff changeset
299 #ifdef PLAN9
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
300 char errbuf[ERRMAX];
22205
f4487350c537 Patch #494045: patches errno and stat to cope on plan9.
Martin v. Löwis <martin@v.loewis.de>
parents: 22121
diff changeset
301 #endif
24071
8142f8d668a0 Patch #569753: Remove support for WIN16.
Martin v. Löwis <martin@v.loewis.de>
parents: 23029
diff changeset
302 #ifdef MS_WINDOWS
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
303 char *s_buf = NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
304 char s_small_buf[28]; /* Room for "Windows Error 0xFFFFFFFF" */
11801
fe960ad3db65 Patch by Mark Hammond:
Guido van Rossum <guido@python.org>
parents: 10638
diff changeset
305 #endif
2376
c31eaed7798b rename macstrerror to PyMac_StrError; no EINTR test in CW
Guido van Rossum <guido@python.org>
parents: 2190
diff changeset
306 #ifdef EINTR
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
307 if (i == EINTR && PyErr_CheckSignals())
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
308 return NULL;
2376
c31eaed7798b rename macstrerror to PyMac_StrError; no EINTR test in CW
Guido van Rossum <guido@python.org>
parents: 2190
diff changeset
309 #endif
22205
f4487350c537 Patch #494045: patches errno and stat to cope on plan9.
Martin v. Löwis <martin@v.loewis.de>
parents: 22121
diff changeset
310 #ifdef PLAN9
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
311 rerrstr(errbuf, sizeof errbuf);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
312 s = errbuf;
22205
f4487350c537 Patch #494045: patches errno and stat to cope on plan9.
Martin v. Löwis <martin@v.loewis.de>
parents: 22121
diff changeset
313 #else
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
314 if (i == 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
315 s = "Error"; /* Sometimes errno didn't get set */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
316 else
24071
8142f8d668a0 Patch #569753: Remove support for WIN16.
Martin v. Löwis <martin@v.loewis.de>
parents: 23029
diff changeset
317 #ifndef MS_WINDOWS
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
318 s = strerror(i);
10638
df8a1c281ad7 Patch by Christian Tismer for Win32, to use FormatMessage() instead of
Guido van Rossum <guido@python.org>
parents: 9671
diff changeset
319 #else
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
320 {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
321 /* Note that the Win32 errors do not lineup with the
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
322 errno error. So if the error is in the MSVC error
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
323 table, we use it, otherwise we assume it really _is_
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
324 a Win32 error code
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
325 */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
326 if (i > 0 && i < _sys_nerr) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
327 s = _sys_errlist[i];
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
328 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
329 else {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
330 int len = FormatMessage(
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
331 FORMAT_MESSAGE_ALLOCATE_BUFFER |
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
332 FORMAT_MESSAGE_FROM_SYSTEM |
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
333 FORMAT_MESSAGE_IGNORE_INSERTS,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
334 NULL, /* no message source */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
335 i,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
336 MAKELANGID(LANG_NEUTRAL,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
337 SUBLANG_DEFAULT),
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
338 /* Default language */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
339 (LPTSTR) &s_buf,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
340 0, /* size not used */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
341 NULL); /* no args */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
342 if (len==0) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
343 /* Only ever seen this in out-of-mem
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
344 situations */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
345 sprintf(s_small_buf, "Windows Error 0x%X", i);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
346 s = s_small_buf;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
347 s_buf = NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
348 } else {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
349 s = s_buf;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
350 /* remove trailing cr/lf and dots */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
351 while (len > 0 && (s[len-1] <= ' ' || s[len-1] == '.'))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
352 s[--len] = '\0';
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
353 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
354 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
355 }
22205
f4487350c537 Patch #494045: patches errno and stat to cope on plan9.
Martin v. Löwis <martin@v.loewis.de>
parents: 22121
diff changeset
356 #endif /* Unix/Windows */
f4487350c537 Patch #494045: patches errno and stat to cope on plan9.
Martin v. Löwis <martin@v.loewis.de>
parents: 22121
diff changeset
357 #endif /* PLAN 9*/
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
358 if (filenameObject != NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
359 v = Py_BuildValue("(isO)", i, s, filenameObject);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
360 else
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
361 v = Py_BuildValue("(is)", i, s);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
362 if (v != NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
363 PyErr_SetObject(exc, v);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
364 Py_DECREF(v);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
365 }
24071
8142f8d668a0 Patch #569753: Remove support for WIN16.
Martin v. Löwis <martin@v.loewis.de>
parents: 23029
diff changeset
366 #ifdef MS_WINDOWS
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
367 LocalFree(s_buf);
10638
df8a1c281ad7 Patch by Christian Tismer for Win32, to use FormatMessage() instead of
Guido van Rossum <guido@python.org>
parents: 9671
diff changeset
368 #endif
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
369 return NULL;
9
33701db4ea55 Adde dconvenience functions.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
370 }
10638
df8a1c281ad7 Patch by Christian Tismer for Win32, to use FormatMessage() instead of
Guido van Rossum <guido@python.org>
parents: 9671
diff changeset
371
8706
ca1c669bb041 PyErr_SetFromErrnoWithFilename(): New function which supports setting
Barry Warsaw <barry@python.org>
parents: 6789
diff changeset
372
ca1c669bb041 PyErr_SetFromErrnoWithFilename(): New function which supports setting
Barry Warsaw <barry@python.org>
parents: 6789
diff changeset
373 PyObject *
54721
921e569d1215 Add const qualifier to PyErr_SetFromErrnoWithFilename and to
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 54126
diff changeset
374 PyErr_SetFromErrnoWithFilename(PyObject *exc, const char *filename)
25535
861065256487 Patch 594001: PEP 277 - Unicode file name support for Windows NT.
Mark Hammond <mhammond@skippinet.com.au>
parents: 24423
diff changeset
375 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
376 PyObject *name = filename ? PyString_FromString(filename) : NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
377 PyObject *result = PyErr_SetFromErrnoWithFilenameObject(exc, name);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
378 Py_XDECREF(name);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
379 return result;
25535
861065256487 Patch 594001: PEP 277 - Unicode file name support for Windows NT.
Mark Hammond <mhammond@skippinet.com.au>
parents: 24423
diff changeset
380 }
861065256487 Patch 594001: PEP 277 - Unicode file name support for Windows NT.
Mark Hammond <mhammond@skippinet.com.au>
parents: 24423
diff changeset
381
54126
d64d47ee54a1 Issue #3527: Removed Py_WIN_WIDE_FILENAMES which is not used any more.
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 52738
diff changeset
382 #ifdef MS_WINDOWS
25535
861065256487 Patch 594001: PEP 277 - Unicode file name support for Windows NT.
Mark Hammond <mhammond@skippinet.com.au>
parents: 24423
diff changeset
383 PyObject *
54721
921e569d1215 Add const qualifier to PyErr_SetFromErrnoWithFilename and to
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 54126
diff changeset
384 PyErr_SetFromErrnoWithUnicodeFilename(PyObject *exc, const Py_UNICODE *filename)
25535
861065256487 Patch 594001: PEP 277 - Unicode file name support for Windows NT.
Mark Hammond <mhammond@skippinet.com.au>
parents: 24423
diff changeset
385 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
386 PyObject *name = filename ?
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
387 PyUnicode_FromUnicode(filename, wcslen(filename)) :
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
388 NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
389 PyObject *result = PyErr_SetFromErrnoWithFilenameObject(exc, name);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
390 Py_XDECREF(name);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
391 return result;
25535
861065256487 Patch 594001: PEP 277 - Unicode file name support for Windows NT.
Mark Hammond <mhammond@skippinet.com.au>
parents: 24423
diff changeset
392 }
54126
d64d47ee54a1 Issue #3527: Removed Py_WIN_WIDE_FILENAMES which is not used any more.
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 52738
diff changeset
393 #endif /* MS_WINDOWS */
25535
861065256487 Patch 594001: PEP 277 - Unicode file name support for Windows NT.
Mark Hammond <mhammond@skippinet.com.au>
parents: 24423
diff changeset
394
861065256487 Patch 594001: PEP 277 - Unicode file name support for Windows NT.
Mark Hammond <mhammond@skippinet.com.au>
parents: 24423
diff changeset
395 PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13578
diff changeset
396 PyErr_SetFromErrno(PyObject *exc)
8706
ca1c669bb041 PyErr_SetFromErrnoWithFilename(): New function which supports setting
Barry Warsaw <barry@python.org>
parents: 6789
diff changeset
397 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
398 return PyErr_SetFromErrnoWithFilenameObject(exc, NULL);
8706
ca1c669bb041 PyErr_SetFromErrnoWithFilename(): New function which supports setting
Barry Warsaw <barry@python.org>
parents: 6789
diff changeset
399 }
22
2d3eb717a00d Added err_badcall().
Guido van Rossum <guido@python.org>
parents: 9
diff changeset
400
36751
8e04de2480e4 PEP 352 implementation. Creates a new base class, BaseException, which has an
Brett Cannon <bcannon@gmail.com>
parents: 36737
diff changeset
401 #ifdef MS_WINDOWS
11801
fe960ad3db65 Patch by Mark Hammond:
Guido van Rossum <guido@python.org>
parents: 10638
diff changeset
402 /* Windows specific error code handling */
25535
861065256487 Patch 594001: PEP 277 - Unicode file name support for Windows NT.
Mark Hammond <mhammond@skippinet.com.au>
parents: 24423
diff changeset
403 PyObject *PyErr_SetExcFromWindowsErrWithFilenameObject(
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
404 PyObject *exc,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
405 int ierr,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
406 PyObject *filenameObject)
11801
fe960ad3db65 Patch by Mark Hammond:
Guido van Rossum <guido@python.org>
parents: 10638
diff changeset
407 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
408 int len;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
409 char *s;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
410 char *s_buf = NULL; /* Free via LocalFree */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
411 char s_small_buf[28]; /* Room for "Windows Error 0xFFFFFFFF" */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
412 PyObject *v;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
413 DWORD err = (DWORD)ierr;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
414 if (err==0) err = GetLastError();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
415 len = FormatMessage(
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
416 /* Error API error */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
417 FORMAT_MESSAGE_ALLOCATE_BUFFER |
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
418 FORMAT_MESSAGE_FROM_SYSTEM |
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
419 FORMAT_MESSAGE_IGNORE_INSERTS,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
420 NULL, /* no message source */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
421 err,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
422 MAKELANGID(LANG_NEUTRAL,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
423 SUBLANG_DEFAULT), /* Default language */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
424 (LPTSTR) &s_buf,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
425 0, /* size not used */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
426 NULL); /* no args */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
427 if (len==0) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
428 /* Only seen this in out of mem situations */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
429 sprintf(s_small_buf, "Windows Error 0x%X", err);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
430 s = s_small_buf;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
431 s_buf = NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
432 } else {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
433 s = s_buf;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
434 /* remove trailing cr/lf and dots */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
435 while (len > 0 && (s[len-1] <= ' ' || s[len-1] == '.'))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
436 s[--len] = '\0';
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
437 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
438 if (filenameObject != NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
439 v = Py_BuildValue("(isO)", err, s, filenameObject);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
440 else
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
441 v = Py_BuildValue("(is)", err, s);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
442 if (v != NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
443 PyErr_SetObject(exc, v);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
444 Py_DECREF(v);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
445 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
446 LocalFree(s_buf);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
447 return NULL;
11801
fe960ad3db65 Patch by Mark Hammond:
Guido van Rossum <guido@python.org>
parents: 10638
diff changeset
448 }
fe960ad3db65 Patch by Mark Hammond:
Guido van Rossum <guido@python.org>
parents: 10638
diff changeset
449
25535
861065256487 Patch 594001: PEP 277 - Unicode file name support for Windows NT.
Mark Hammond <mhammond@skippinet.com.au>
parents: 24423
diff changeset
450 PyObject *PyErr_SetExcFromWindowsErrWithFilename(
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
451 PyObject *exc,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
452 int ierr,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
453 const char *filename)
25535
861065256487 Patch 594001: PEP 277 - Unicode file name support for Windows NT.
Mark Hammond <mhammond@skippinet.com.au>
parents: 24423
diff changeset
454 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
455 PyObject *name = filename ? PyString_FromString(filename) : NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
456 PyObject *ret = PyErr_SetExcFromWindowsErrWithFilenameObject(exc,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
457 ierr,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
458 name);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
459 Py_XDECREF(name);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
460 return ret;
25535
861065256487 Patch 594001: PEP 277 - Unicode file name support for Windows NT.
Mark Hammond <mhammond@skippinet.com.au>
parents: 24423
diff changeset
461 }
861065256487 Patch 594001: PEP 277 - Unicode file name support for Windows NT.
Mark Hammond <mhammond@skippinet.com.au>
parents: 24423
diff changeset
462
861065256487 Patch 594001: PEP 277 - Unicode file name support for Windows NT.
Mark Hammond <mhammond@skippinet.com.au>
parents: 24423
diff changeset
463 PyObject *PyErr_SetExcFromWindowsErrWithUnicodeFilename(
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
464 PyObject *exc,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
465 int ierr,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
466 const Py_UNICODE *filename)
25535
861065256487 Patch 594001: PEP 277 - Unicode file name support for Windows NT.
Mark Hammond <mhammond@skippinet.com.au>
parents: 24423
diff changeset
467 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
468 PyObject *name = filename ?
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
469 PyUnicode_FromUnicode(filename, wcslen(filename)) :
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
470 NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
471 PyObject *ret = PyErr_SetExcFromWindowsErrWithFilenameObject(exc,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
472 ierr,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
473 name);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
474 Py_XDECREF(name);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
475 return ret;
25535
861065256487 Patch 594001: PEP 277 - Unicode file name support for Windows NT.
Mark Hammond <mhammond@skippinet.com.au>
parents: 24423
diff changeset
476 }
861065256487 Patch 594001: PEP 277 - Unicode file name support for Windows NT.
Mark Hammond <mhammond@skippinet.com.au>
parents: 24423
diff changeset
477
24423
3db261a93a5b New functions for extension writers on Windows:
Thomas Heller <theller@ctypes.org>
parents: 24071
diff changeset
478 PyObject *PyErr_SetExcFromWindowsErr(PyObject *exc, int ierr)
3db261a93a5b New functions for extension writers on Windows:
Thomas Heller <theller@ctypes.org>
parents: 24071
diff changeset
479 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
480 return PyErr_SetExcFromWindowsErrWithFilename(exc, ierr, NULL);
24423
3db261a93a5b New functions for extension writers on Windows:
Thomas Heller <theller@ctypes.org>
parents: 24071
diff changeset
481 }
3db261a93a5b New functions for extension writers on Windows:
Thomas Heller <theller@ctypes.org>
parents: 24071
diff changeset
482
11801
fe960ad3db65 Patch by Mark Hammond:
Guido van Rossum <guido@python.org>
parents: 10638
diff changeset
483 PyObject *PyErr_SetFromWindowsErr(int ierr)
fe960ad3db65 Patch by Mark Hammond:
Guido van Rossum <guido@python.org>
parents: 10638
diff changeset
484 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
485 return PyErr_SetExcFromWindowsErrWithFilename(PyExc_WindowsError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
486 ierr, NULL);
24423
3db261a93a5b New functions for extension writers on Windows:
Thomas Heller <theller@ctypes.org>
parents: 24071
diff changeset
487 }
3db261a93a5b New functions for extension writers on Windows:
Thomas Heller <theller@ctypes.org>
parents: 24071
diff changeset
488 PyObject *PyErr_SetFromWindowsErrWithFilename(
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
489 int ierr,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
490 const char *filename)
24423
3db261a93a5b New functions for extension writers on Windows:
Thomas Heller <theller@ctypes.org>
parents: 24071
diff changeset
491 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
492 PyObject *name = filename ? PyString_FromString(filename) : NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
493 PyObject *result = PyErr_SetExcFromWindowsErrWithFilenameObject(
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
494 PyExc_WindowsError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
495 ierr, name);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
496 Py_XDECREF(name);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
497 return result;
11801
fe960ad3db65 Patch by Mark Hammond:
Guido van Rossum <guido@python.org>
parents: 10638
diff changeset
498 }
25535
861065256487 Patch 594001: PEP 277 - Unicode file name support for Windows NT.
Mark Hammond <mhammond@skippinet.com.au>
parents: 24423
diff changeset
499
861065256487 Patch 594001: PEP 277 - Unicode file name support for Windows NT.
Mark Hammond <mhammond@skippinet.com.au>
parents: 24423
diff changeset
500 PyObject *PyErr_SetFromWindowsErrWithUnicodeFilename(
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
501 int ierr,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
502 const Py_UNICODE *filename)
25535
861065256487 Patch 594001: PEP 277 - Unicode file name support for Windows NT.
Mark Hammond <mhammond@skippinet.com.au>
parents: 24423
diff changeset
503 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
504 PyObject *name = filename ?
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
505 PyUnicode_FromUnicode(filename, wcslen(filename)) :
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
506 NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
507 PyObject *result = PyErr_SetExcFromWindowsErrWithFilenameObject(
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
508 PyExc_WindowsError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
509 ierr, name);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
510 Py_XDECREF(name);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
511 return result;
25535
861065256487 Patch 594001: PEP 277 - Unicode file name support for Windows NT.
Mark Hammond <mhammond@skippinet.com.au>
parents: 24423
diff changeset
512 }
11801
fe960ad3db65 Patch by Mark Hammond:
Guido van Rossum <guido@python.org>
parents: 10638
diff changeset
513 #endif /* MS_WINDOWS */
fe960ad3db65 Patch by Mark Hammond:
Guido van Rossum <guido@python.org>
parents: 10638
diff changeset
514
22
2d3eb717a00d Added err_badcall().
Guido van Rossum <guido@python.org>
parents: 9
diff changeset
515 void
14170
019265b3a856 Improve the exceptions raised by PyErr_BadInternalCall(); adding the
Fred Drake <fdrake@acm.org>
parents: 14066
diff changeset
516 _PyErr_BadInternalCall(char *filename, int lineno)
019265b3a856 Improve the exceptions raised by PyErr_BadInternalCall(); adding the
Fred Drake <fdrake@acm.org>
parents: 14066
diff changeset
517 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
518 PyErr_Format(PyExc_SystemError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
519 "%s:%d: bad argument to internal function",
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
520 filename, lineno);
14170
019265b3a856 Improve the exceptions raised by PyErr_BadInternalCall(); adding the
Fred Drake <fdrake@acm.org>
parents: 14066
diff changeset
521 }
019265b3a856 Improve the exceptions raised by PyErr_BadInternalCall(); adding the
Fred Drake <fdrake@acm.org>
parents: 14066
diff changeset
522
019265b3a856 Improve the exceptions raised by PyErr_BadInternalCall(); adding the
Fred Drake <fdrake@acm.org>
parents: 14066
diff changeset
523 /* Remove the preprocessor macro for PyErr_BadInternalCall() so that we can
019265b3a856 Improve the exceptions raised by PyErr_BadInternalCall(); adding the
Fred Drake <fdrake@acm.org>
parents: 14066
diff changeset
524 export the entry point for existing object code: */
019265b3a856 Improve the exceptions raised by PyErr_BadInternalCall(); adding the
Fred Drake <fdrake@acm.org>
parents: 14066
diff changeset
525 #undef PyErr_BadInternalCall
019265b3a856 Improve the exceptions raised by PyErr_BadInternalCall(); adding the
Fred Drake <fdrake@acm.org>
parents: 14066
diff changeset
526 void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13578
diff changeset
527 PyErr_BadInternalCall(void)
22
2d3eb717a00d Added err_badcall().
Guido van Rossum <guido@python.org>
parents: 9
diff changeset
528 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
529 PyErr_Format(PyExc_SystemError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
530 "bad argument to internal function");
22
2d3eb717a00d Added err_badcall().
Guido van Rossum <guido@python.org>
parents: 9
diff changeset
531 }
14170
019265b3a856 Improve the exceptions raised by PyErr_BadInternalCall(); adding the
Fred Drake <fdrake@acm.org>
parents: 14066
diff changeset
532 #define PyErr_BadInternalCall() _PyErr_BadInternalCall(__FILE__, __LINE__)
019265b3a856 Improve the exceptions raised by PyErr_BadInternalCall(); adding the
Fred Drake <fdrake@acm.org>
parents: 14066
diff changeset
533
5105
00fd6857f76b Added convenience function PyErr_Format(exception, formatstring, ...) -> NULL.
Guido van Rossum <guido@python.org>
parents: 4654
diff changeset
534
00fd6857f76b Added convenience function PyErr_Format(exception, formatstring, ...) -> NULL.
Guido van Rossum <guido@python.org>
parents: 4654
diff changeset
535
00fd6857f76b Added convenience function PyErr_Format(exception, formatstring, ...) -> NULL.
Guido van Rossum <guido@python.org>
parents: 4654
diff changeset
536 PyObject *
00fd6857f76b Added convenience function PyErr_Format(exception, formatstring, ...) -> NULL.
Guido van Rossum <guido@python.org>
parents: 4654
diff changeset
537 PyErr_Format(PyObject *exception, const char *format, ...)
00fd6857f76b Added convenience function PyErr_Format(exception, formatstring, ...) -> NULL.
Guido van Rossum <guido@python.org>
parents: 4654
diff changeset
538 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
539 va_list vargs;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
540 PyObject* string;
14335
951eb3af3494 code part of patch #100895 by Fredrik Lundh
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 14330
diff changeset
541
951eb3af3494 code part of patch #100895 by Fredrik Lundh
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 14330
diff changeset
542 #ifdef HAVE_STDARG_PROTOTYPES
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
543 va_start(vargs, format);
14335
951eb3af3494 code part of patch #100895 by Fredrik Lundh
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 14330
diff changeset
544 #else
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
545 va_start(vargs);
14335
951eb3af3494 code part of patch #100895 by Fredrik Lundh
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 14330
diff changeset
546 #endif
5105
00fd6857f76b Added convenience function PyErr_Format(exception, formatstring, ...) -> NULL.
Guido van Rossum <guido@python.org>
parents: 4654
diff changeset
547
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
548 string = PyString_FromFormatV(format, vargs);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
549 PyErr_SetObject(exception, string);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
550 Py_XDECREF(string);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
551 va_end(vargs);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
552 return NULL;
5105
00fd6857f76b Added convenience function PyErr_Format(exception, formatstring, ...) -> NULL.
Guido van Rossum <guido@python.org>
parents: 4654
diff changeset
553 }
6319
b7f0b98f5325 New API PyErr_NewException(name, base, dict) to create simple new exceptions.
Guido van Rossum <guido@python.org>
parents: 6190
diff changeset
554
b7f0b98f5325 New API PyErr_NewException(name, base, dict) to create simple new exceptions.
Guido van Rossum <guido@python.org>
parents: 6190
diff changeset
555
38246
ee2139935a1d PyErr_NewException now accepts a tuple of base classes as its
Georg Brandl <georg@python.org>
parents: 37779
diff changeset
556
6319
b7f0b98f5325 New API PyErr_NewException(name, base, dict) to create simple new exceptions.
Guido van Rossum <guido@python.org>
parents: 6190
diff changeset
557 PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13578
diff changeset
558 PyErr_NewException(char *name, PyObject *base, PyObject *dict)
6319
b7f0b98f5325 New API PyErr_NewException(name, base, dict) to create simple new exceptions.
Guido van Rossum <guido@python.org>
parents: 6190
diff changeset
559 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
560 char *dot;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
561 PyObject *modulename = NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
562 PyObject *classname = NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
563 PyObject *mydict = NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
564 PyObject *bases = NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
565 PyObject *result = NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
566 dot = strrchr(name, '.');
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
567 if (dot == NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
568 PyErr_SetString(PyExc_SystemError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
569 "PyErr_NewException: name must be module.class");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
570 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
571 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
572 if (base == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
573 base = PyExc_Exception;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
574 if (dict == NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
575 dict = mydict = PyDict_New();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
576 if (dict == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
577 goto failure;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
578 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
579 if (PyDict_GetItemString(dict, "__module__") == NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
580 modulename = PyString_FromStringAndSize(name,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
581 (Py_ssize_t)(dot-name));
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
582 if (modulename == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
583 goto failure;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
584 if (PyDict_SetItemString(dict, "__module__", modulename) != 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
585 goto failure;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
586 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
587 if (PyTuple_Check(base)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
588 bases = base;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
589 /* INCREF as we create a new ref in the else branch */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
590 Py_INCREF(bases);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
591 } else {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
592 bases = PyTuple_Pack(1, base);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
593 if (bases == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
594 goto failure;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
595 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
596 /* Create a real new-style class. */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
597 result = PyObject_CallFunction((PyObject *)&PyType_Type, "sOO",
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
598 dot+1, bases, dict);
6319
b7f0b98f5325 New API PyErr_NewException(name, base, dict) to create simple new exceptions.
Guido van Rossum <guido@python.org>
parents: 6190
diff changeset
599 failure:
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
600 Py_XDECREF(bases);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
601 Py_XDECREF(mydict);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
602 Py_XDECREF(classname);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
603 Py_XDECREF(modulename);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
604 return result;
6319
b7f0b98f5325 New API PyErr_NewException(name, base, dict) to create simple new exceptions.
Guido van Rossum <guido@python.org>
parents: 6190
diff changeset
605 }
14330
6dee1985c681 refactor __del__ exception handler into PyErr_WriteUnraisable
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 14170
diff changeset
606
57961
cf39c5e462f9 #7033: add new API function PyErr_NewExceptionWithDoc, for easily giving new exceptions a docstring.
Georg Brandl <georg@python.org>
parents: 54721
diff changeset
607
cf39c5e462f9 #7033: add new API function PyErr_NewExceptionWithDoc, for easily giving new exceptions a docstring.
Georg Brandl <georg@python.org>
parents: 54721
diff changeset
608 /* Create an exception with docstring */
cf39c5e462f9 #7033: add new API function PyErr_NewExceptionWithDoc, for easily giving new exceptions a docstring.
Georg Brandl <georg@python.org>
parents: 54721
diff changeset
609 PyObject *
cf39c5e462f9 #7033: add new API function PyErr_NewExceptionWithDoc, for easily giving new exceptions a docstring.
Georg Brandl <georg@python.org>
parents: 54721
diff changeset
610 PyErr_NewExceptionWithDoc(char *name, char *doc, PyObject *base, PyObject *dict)
cf39c5e462f9 #7033: add new API function PyErr_NewExceptionWithDoc, for easily giving new exceptions a docstring.
Georg Brandl <georg@python.org>
parents: 54721
diff changeset
611 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
612 int result;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
613 PyObject *ret = NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
614 PyObject *mydict = NULL; /* points to the dict only if we create it */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
615 PyObject *docobj;
57961
cf39c5e462f9 #7033: add new API function PyErr_NewExceptionWithDoc, for easily giving new exceptions a docstring.
Georg Brandl <georg@python.org>
parents: 54721
diff changeset
616
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
617 if (dict == NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
618 dict = mydict = PyDict_New();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
619 if (dict == NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
620 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
621 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
622 }
57961
cf39c5e462f9 #7033: add new API function PyErr_NewExceptionWithDoc, for easily giving new exceptions a docstring.
Georg Brandl <georg@python.org>
parents: 54721
diff changeset
623
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
624 if (doc != NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
625 docobj = PyString_FromString(doc);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
626 if (docobj == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
627 goto failure;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
628 result = PyDict_SetItemString(dict, "__doc__", docobj);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
629 Py_DECREF(docobj);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
630 if (result < 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
631 goto failure;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
632 }
57961
cf39c5e462f9 #7033: add new API function PyErr_NewExceptionWithDoc, for easily giving new exceptions a docstring.
Georg Brandl <georg@python.org>
parents: 54721
diff changeset
633
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
634 ret = PyErr_NewException(name, base, dict);
57961
cf39c5e462f9 #7033: add new API function PyErr_NewExceptionWithDoc, for easily giving new exceptions a docstring.
Georg Brandl <georg@python.org>
parents: 54721
diff changeset
635 failure:
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
636 Py_XDECREF(mydict);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
637 return ret;
57961
cf39c5e462f9 #7033: add new API function PyErr_NewExceptionWithDoc, for easily giving new exceptions a docstring.
Georg Brandl <georg@python.org>
parents: 54721
diff changeset
638 }
cf39c5e462f9 #7033: add new API function PyErr_NewExceptionWithDoc, for easily giving new exceptions a docstring.
Georg Brandl <georg@python.org>
parents: 54721
diff changeset
639
cf39c5e462f9 #7033: add new API function PyErr_NewExceptionWithDoc, for easily giving new exceptions a docstring.
Georg Brandl <georg@python.org>
parents: 54721
diff changeset
640
14330
6dee1985c681 refactor __del__ exception handler into PyErr_WriteUnraisable
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 14170
diff changeset
641 /* Call when an exception has occurred but there is no way for Python
6dee1985c681 refactor __del__ exception handler into PyErr_WriteUnraisable
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 14170
diff changeset
642 to handle it. Examples: exception in __del__ or during GC. */
6dee1985c681 refactor __del__ exception handler into PyErr_WriteUnraisable
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 14170
diff changeset
643 void
6dee1985c681 refactor __del__ exception handler into PyErr_WriteUnraisable
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 14170
diff changeset
644 PyErr_WriteUnraisable(PyObject *obj)
6dee1985c681 refactor __del__ exception handler into PyErr_WriteUnraisable
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 14170
diff changeset
645 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
646 PyObject *f, *t, *v, *tb;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
647 PyErr_Fetch(&t, &v, &tb);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
648 f = PySys_GetObject("stderr");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
649 if (f != NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
650 PyFile_WriteString("Exception ", f);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
651 if (t) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
652 PyObject* moduleName;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
653 char* className;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
654 assert(PyExceptionClass_Check(t));
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
655 className = PyExceptionClass_Name(t);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
656 if (className != NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
657 char *dot = strrchr(className, '.');
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
658 if (dot != NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
659 className = dot+1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
660 }
38455
1a23706139ca PyErr_Display(), PyErr_WriteUnraisable(): Coverity found a cut-and-paste
Tim Peters <tim.peters@gmail.com>
parents: 38431
diff changeset
661
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
662 moduleName = PyObject_GetAttrString(t, "__module__");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
663 if (moduleName == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
664 PyFile_WriteString("<unknown>", f);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
665 else {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
666 char* modstr = PyString_AsString(moduleName);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
667 if (modstr &&
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
668 strcmp(modstr, "exceptions") != 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
669 {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
670 PyFile_WriteString(modstr, f);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
671 PyFile_WriteString(".", f);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
672 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
673 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
674 if (className == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
675 PyFile_WriteString("<unknown>", f);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
676 else
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
677 PyFile_WriteString(className, f);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
678 if (v && v != Py_None) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
679 PyFile_WriteString(": ", f);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
680 PyFile_WriteObject(v, f, 0);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
681 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
682 Py_XDECREF(moduleName);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
683 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
684 PyFile_WriteString(" in ", f);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
685 PyFile_WriteObject(obj, f, 0);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
686 PyFile_WriteString(" ignored\n", f);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
687 PyErr_Clear(); /* Just in case */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
688 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
689 Py_XDECREF(t);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
690 Py_XDECREF(v);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
691 Py_XDECREF(tb);
14330
6dee1985c681 refactor __del__ exception handler into PyErr_WriteUnraisable
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 14170
diff changeset
692 }
15591
36286632ff3a Add PyErr_Warn().
Guido van Rossum <guido@python.org>
parents: 15318
diff changeset
693
30781
74747f62f286 Made function declaration a proper C prototype
Armin Rigo <arigo@tunes.org>
parents: 30683
diff changeset
694 extern PyObject *PyModule_GetWarningsModule(void);
15591
36286632ff3a Add PyErr_Warn().
Guido van Rossum <guido@python.org>
parents: 15318
diff changeset
695
16850
2d4da6cfee61 Add PyErr_WarnExplicit(), which calls warnings.warn_explicit(), with
Guido van Rossum <guido@python.org>
parents: 16838
diff changeset
696
22121
d4406fbfcb4e Patch #50002: Display line information for bad \x escapes:
Martin v. Löwis <martin@v.loewis.de>
parents: 20221
diff changeset
697 /* Set file and line information for the current exception.
d4406fbfcb4e Patch #50002: Display line information for bad \x escapes:
Martin v. Löwis <martin@v.loewis.de>
parents: 20221
diff changeset
698 If the exception is not a SyntaxError, also sets additional attributes
d4406fbfcb4e Patch #50002: Display line information for bad \x escapes:
Martin v. Löwis <martin@v.loewis.de>
parents: 20221
diff changeset
699 to make printing of exceptions believe it is a syntax error. */
16850
2d4da6cfee61 Add PyErr_WarnExplicit(), which calls warnings.warn_explicit(), with
Guido van Rossum <guido@python.org>
parents: 16838
diff changeset
700
16838
09f6c1372056 Improve SyntaxErrors for bad future statements. Set file and location
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 15591
diff changeset
701 void
26601
cc05c2c8660e Constify filenames and scripts. Fixes #651362.
Martin v. Löwis <martin@v.loewis.de>
parents: 25544
diff changeset
702 PyErr_SyntaxLocation(const char *filename, int lineno)
16838
09f6c1372056 Improve SyntaxErrors for bad future statements. Set file and location
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 15591
diff changeset
703 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
704 PyObject *exc, *v, *tb, *tmp;
16838
09f6c1372056 Improve SyntaxErrors for bad future statements. Set file and location
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 15591
diff changeset
705
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
706 /* add attributes for the line number and filename for the error */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
707 PyErr_Fetch(&exc, &v, &tb);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
708 PyErr_NormalizeException(&exc, &v, &tb);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
709 /* XXX check that it is, indeed, a syntax error. It might not
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
710 * be, though. */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
711 tmp = PyInt_FromLong(lineno);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
712 if (tmp == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
713 PyErr_Clear();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
714 else {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
715 if (PyObject_SetAttrString(v, "lineno", tmp))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
716 PyErr_Clear();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
717 Py_DECREF(tmp);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
718 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
719 if (filename != NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
720 tmp = PyString_FromString(filename);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
721 if (tmp == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
722 PyErr_Clear();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
723 else {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
724 if (PyObject_SetAttrString(v, "filename", tmp))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
725 PyErr_Clear();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
726 Py_DECREF(tmp);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
727 }
16838
09f6c1372056 Improve SyntaxErrors for bad future statements. Set file and location
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 15591
diff changeset
728
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
729 tmp = PyErr_ProgramText(filename, lineno);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
730 if (tmp) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
731 if (PyObject_SetAttrString(v, "text", tmp))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
732 PyErr_Clear();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
733 Py_DECREF(tmp);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
734 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
735 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
736 if (PyObject_SetAttrString(v, "offset", Py_None)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
737 PyErr_Clear();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
738 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
739 if (exc != PyExc_SyntaxError) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
740 if (!PyObject_HasAttrString(v, "msg")) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
741 tmp = PyObject_Str(v);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
742 if (tmp) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
743 if (PyObject_SetAttrString(v, "msg", tmp))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
744 PyErr_Clear();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
745 Py_DECREF(tmp);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
746 } else {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
747 PyErr_Clear();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
748 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
749 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
750 if (!PyObject_HasAttrString(v, "print_file_and_line")) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
751 if (PyObject_SetAttrString(v, "print_file_and_line",
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
752 Py_None))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
753 PyErr_Clear();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
754 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
755 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
756 PyErr_Restore(exc, v, tb);
16838
09f6c1372056 Improve SyntaxErrors for bad future statements. Set file and location
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 15591
diff changeset
757 }
09f6c1372056 Improve SyntaxErrors for bad future statements. Set file and location
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 15591
diff changeset
758
09f6c1372056 Improve SyntaxErrors for bad future statements. Set file and location
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 15591
diff changeset
759 /* com_fetch_program_text will attempt to load the line of text that
09f6c1372056 Improve SyntaxErrors for bad future statements. Set file and location
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 15591
diff changeset
760 the exception refers to. If it fails, it will return NULL but will
36751
8e04de2480e4 PEP 352 implementation. Creates a new base class, BaseException, which has an
Brett Cannon <bcannon@gmail.com>
parents: 36737
diff changeset
761 not set an exception.
16838
09f6c1372056 Improve SyntaxErrors for bad future statements. Set file and location
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 15591
diff changeset
762
09f6c1372056 Improve SyntaxErrors for bad future statements. Set file and location
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 15591
diff changeset
763 XXX The functionality of this function is quite similar to the
09f6c1372056 Improve SyntaxErrors for bad future statements. Set file and location
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 15591
diff changeset
764 functionality in tb_displayline() in traceback.c.
09f6c1372056 Improve SyntaxErrors for bad future statements. Set file and location
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 15591
diff changeset
765 */
09f6c1372056 Improve SyntaxErrors for bad future statements. Set file and location
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 15591
diff changeset
766
09f6c1372056 Improve SyntaxErrors for bad future statements. Set file and location
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 15591
diff changeset
767 PyObject *
26601
cc05c2c8660e Constify filenames and scripts. Fixes #651362.
Martin v. Löwis <martin@v.loewis.de>
parents: 25544
diff changeset
768 PyErr_ProgramText(const char *filename, int lineno)
16838
09f6c1372056 Improve SyntaxErrors for bad future statements. Set file and location
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 15591
diff changeset
769 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
770 FILE *fp;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
771 int i;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
772 char linebuf[1000];
16838
09f6c1372056 Improve SyntaxErrors for bad future statements. Set file and location
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 15591
diff changeset
773
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
774 if (filename == NULL || *filename == '\0' || lineno <= 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
775 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
776 fp = fopen(filename, "r" PY_STDIOTEXTMODE);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
777 if (fp == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
778 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
779 for (i = 0; i < lineno; i++) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
780 char *pLastChar = &linebuf[sizeof(linebuf) - 2];
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
781 do {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
782 *pLastChar = '\0';
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
783 if (Py_UniversalNewlineFgets(linebuf, sizeof linebuf, fp, NULL) == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
784 break;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
785 /* fgets read *something*; if it didn't get as
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
786 far as pLastChar, it must have found a newline
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
787 or hit the end of the file; if pLastChar is \n,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
788 it obviously found a newline; else we haven't
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
789 yet seen a newline, so must continue */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
790 } while (*pLastChar != '\0' && *pLastChar != '\n');
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
791 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
792 fclose(fp);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
793 if (i == lineno) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
794 char *p = linebuf;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
795 while (*p == ' ' || *p == '\t' || *p == '\014')
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
796 p++;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
797 return PyString_FromString(p);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
798 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57961
diff changeset
799 return NULL;
16838
09f6c1372056 Improve SyntaxErrors for bad future statements. Set file and location
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 15591
diff changeset
800 }
37655
2f3f2e32dfd9 spread the extern "C" { } magic pixie dust around. Python itself builds now
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36751
diff changeset
801
2f3f2e32dfd9 spread the extern "C" { } magic pixie dust around. Python itself builds now
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36751
diff changeset
802 #ifdef __cplusplus
2f3f2e32dfd9 spread the extern "C" { } magic pixie dust around. Python itself builds now
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36751
diff changeset
803 }
2f3f2e32dfd9 spread the extern "C" { } magic pixie dust around. Python itself builds now
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36751
diff changeset
804 #endif
2f3f2e32dfd9 spread the extern "C" { } magic pixie dust around. Python itself builds now
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36751
diff changeset
805