blob: d7aac6d504182ac15bfe8127827d9070faa76808 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Guido van Rossum373c8691997-04-29 18:22:47 +00002/* Error handling */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003
Guido van Rossum373c8691997-04-29 18:22:47 +00004#include "Python.h"
Guido van Rossumf22120a1990-12-20 23:05:40 +00005
Guido van Rossum53e8d441995-03-09 12:11:31 +00006#ifndef __STDC__
Guido van Rossum7844e381997-04-11 20:44:04 +00007#ifndef MS_WINDOWS
Tim Petersdbd9ba62000-07-09 03:09:57 +00008extern char *strerror(int);
Guido van Rossum53e8d441995-03-09 12:11:31 +00009#endif
Guido van Rossum7844e381997-04-11 20:44:04 +000010#endif
Guido van Rossumf5401bd1990-11-02 17:50:28 +000011
Martin v. Löwis6238d2b2002-06-30 15:26:10 +000012#ifdef MS_WINDOWS
Martin v. Löwis5d12abe2007-09-03 07:40:24 +000013#include <windows.h>
14#include <winbase.h>
Guido van Rossum743007d1999-04-21 15:27:31 +000015#endif
16
Jeremy Hyltonb69a27e2000-09-01 03:49:47 +000017#include <ctype.h>
18
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000019#ifdef __cplusplus
20extern "C" {
21#endif
22
23
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000024void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000025PyErr_Restore(PyObject *type, PyObject *value, PyObject *traceback)
Guido van Rossum1ae940a1995-01-02 19:04:15 +000026{
Guido van Rossum885553e1998-12-21 18:33:30 +000027 PyThreadState *tstate = PyThreadState_GET();
Guido van Rossuma027efa1997-05-05 20:56:21 +000028 PyObject *oldtype, *oldvalue, *oldtraceback;
Guido van Rossum1ae940a1995-01-02 19:04:15 +000029
Guido van Rossuma027efa1997-05-05 20:56:21 +000030 if (traceback != NULL && !PyTraceBack_Check(traceback)) {
31 /* XXX Should never happen -- fatal error instead? */
Guido van Rossum1a5e21e2006-02-28 21:57:43 +000032 /* Well, it could be None. */
Guido van Rossuma027efa1997-05-05 20:56:21 +000033 Py_DECREF(traceback);
34 traceback = NULL;
35 }
36
37 /* Save these in locals to safeguard against recursive
38 invocation through Py_XDECREF */
39 oldtype = tstate->curexc_type;
40 oldvalue = tstate->curexc_value;
41 oldtraceback = tstate->curexc_traceback;
42
43 tstate->curexc_type = type;
44 tstate->curexc_value = value;
45 tstate->curexc_traceback = traceback;
46
47 Py_XDECREF(oldtype);
48 Py_XDECREF(oldvalue);
49 Py_XDECREF(oldtraceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +000050}
51
52void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000053PyErr_SetObject(PyObject *exception, PyObject *value)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000054{
Guido van Rossumb4fb6e42008-06-14 20:20:24 +000055 PyThreadState *tstate = PyThreadState_GET();
56 PyObject *tb = NULL;
57
Thomas Wouters303de6a2006-04-20 22:42:37 +000058 if (exception != NULL &&
59 !PyExceptionClass_Check(exception)) {
Thomas Wouters303de6a2006-04-20 22:42:37 +000060 PyErr_Format(PyExc_SystemError,
Walter Dörwald573c08c2007-05-25 15:46:59 +000061 "exception %R not a BaseException subclass",
62 exception);
Thomas Wouters303de6a2006-04-20 22:42:37 +000063 return;
64 }
Guido van Rossum373c8691997-04-29 18:22:47 +000065 Py_XINCREF(value);
Guido van Rossumb4fb6e42008-06-14 20:20:24 +000066 if (tstate->exc_value != NULL && tstate->exc_value != Py_None) {
67 /* Implicit exception chaining */
68 if (value == NULL || !PyExceptionInstance_Check(value)) {
69 /* We must normalize the value right now */
70 PyObject *args, *fixed_value;
71 if (value == NULL || value == Py_None)
72 args = PyTuple_New(0);
73 else if (PyTuple_Check(value)) {
74 Py_INCREF(value);
75 args = value;
76 }
77 else
78 args = PyTuple_Pack(1, value);
79 fixed_value = args ?
80 PyEval_CallObject(exception, args) : NULL;
81 Py_XDECREF(args);
82 Py_XDECREF(value);
83 if (fixed_value == NULL)
84 return;
85 value = fixed_value;
86 }
Benjamin Petersone6528212008-07-15 15:32:09 +000087 /* Avoid reference cycles through the context chain.
88 This is O(chain length) but context chains are
89 usually very short. Sensitive readers may try
90 to inline the call to PyException_GetContext. */
91 if (tstate->exc_value != value) {
92 PyObject *o = tstate->exc_value, *context;
93 while ((context = PyException_GetContext(o))) {
94 Py_DECREF(context);
95 if (context == value) {
96 PyException_SetContext(o, NULL);
97 break;
98 }
99 o = context;
100 }
101 Py_INCREF(tstate->exc_value);
102 PyException_SetContext(value, tstate->exc_value);
103 }
Guido van Rossumb4fb6e42008-06-14 20:20:24 +0000104 }
105 if (value != NULL && PyExceptionInstance_Check(value))
106 tb = PyException_GetTraceback(value);
107 Py_XINCREF(exception);
108 PyErr_Restore(exception, value, tb);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000109}
110
111void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000112PyErr_SetNone(PyObject *exception)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000113{
Guido van Rossum373c8691997-04-29 18:22:47 +0000114 PyErr_SetObject(exception, (PyObject *)NULL);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000115}
116
117void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000118PyErr_SetString(PyObject *exception, const char *string)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000119{
Walter Dörwald573c08c2007-05-25 15:46:59 +0000120 PyObject *value = PyUnicode_FromString(string);
Guido van Rossum373c8691997-04-29 18:22:47 +0000121 PyErr_SetObject(exception, value);
122 Py_XDECREF(value);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000123}
124
Guido van Rossum3a241811994-08-29 12:14:12 +0000125
Guido van Rossum373c8691997-04-29 18:22:47 +0000126PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000127PyErr_Occurred(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000128{
Tim Peters024da352001-05-30 06:09:50 +0000129 PyThreadState *tstate = PyThreadState_GET();
Guido van Rossuma027efa1997-05-05 20:56:21 +0000130
131 return tstate->curexc_type;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000132}
133
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000134
135int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000136PyErr_GivenExceptionMatches(PyObject *err, PyObject *exc)
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000137{
Barry Warsawfa5c3152000-05-02 19:27:51 +0000138 if (err == NULL || exc == NULL) {
139 /* maybe caused by "import exceptions" that failed early on */
140 return 0;
141 }
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000142 if (PyTuple_Check(exc)) {
Martin v. Löwisd96ee902006-02-16 14:37:16 +0000143 Py_ssize_t i, n;
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000144 n = PyTuple_Size(exc);
145 for (i = 0; i < n; i++) {
146 /* Test recursively */
147 if (PyErr_GivenExceptionMatches(
148 err, PyTuple_GET_ITEM(exc, i)))
149 {
150 return 1;
151 }
152 }
153 return 0;
154 }
155 /* err might be an instance, so check its class. */
Brett Cannonbf364092006-03-01 04:25:17 +0000156 if (PyExceptionInstance_Check(err))
157 err = PyExceptionInstance_Class(err);
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000158
Brett Cannonbf364092006-03-01 04:25:17 +0000159 if (PyExceptionClass_Check(err) && PyExceptionClass_Check(exc)) {
Benjamin Peterson69c88f72008-07-31 01:47:08 +0000160 int res = 0;
161 PyObject *exception, *value, *tb;
162 PyErr_Fetch(&exception, &value, &tb);
Antoine Pitrouec569b72008-08-26 22:40:48 +0000163 /* PyObject_IsSubclass() can recurse and therefore is
164 not safe (see test_bad_getattr in test.pickletester). */
165 res = PyType_IsSubtype((PyTypeObject *)err, (PyTypeObject *)exc);
Benjamin Peterson69c88f72008-07-31 01:47:08 +0000166 /* This function must not fail, so print the error here */
167 if (res == -1) {
168 PyErr_WriteUnraisable(err);
Benjamin Peterson69c88f72008-07-31 01:47:08 +0000169 res = 0;
170 }
171 PyErr_Restore(exception, value, tb);
172 return res;
Brett Cannonbf364092006-03-01 04:25:17 +0000173 }
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000174
175 return err == exc;
176}
Guido van Rossum743007d1999-04-21 15:27:31 +0000177
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000178
179int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000180PyErr_ExceptionMatches(PyObject *exc)
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000181{
182 return PyErr_GivenExceptionMatches(PyErr_Occurred(), exc);
183}
184
185
186/* Used in many places to normalize a raised exception, including in
187 eval_code2(), do_raise(), and PyErr_Print()
Benjamin Petersone6528212008-07-15 15:32:09 +0000188
189 XXX: should PyErr_NormalizeException() also call
190 PyException_SetTraceback() with the resulting value and tb?
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000191*/
192void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000193PyErr_NormalizeException(PyObject **exc, PyObject **val, PyObject **tb)
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000194{
195 PyObject *type = *exc;
196 PyObject *value = *val;
197 PyObject *inclass = NULL;
Jeremy Hyltone2e2c9f2001-09-26 19:58:38 +0000198 PyObject *initial_tb = NULL;
Thomas Wouters89d996e2007-09-08 17:39:28 +0000199 PyThreadState *tstate = NULL;
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000200
Guido van Rossumed473a42000-08-07 19:18:27 +0000201 if (type == NULL) {
Guido van Rossum6b3fffa2003-04-10 20:29:48 +0000202 /* There was no exception, so nothing to do. */
203 return;
Guido van Rossumed473a42000-08-07 19:18:27 +0000204 }
205
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000206 /* If PyErr_SetNone() was used, the value will have been actually
207 set to NULL.
208 */
209 if (!value) {
210 value = Py_None;
211 Py_INCREF(value);
212 }
213
Brett Cannonbf364092006-03-01 04:25:17 +0000214 if (PyExceptionInstance_Check(value))
215 inclass = PyExceptionInstance_Class(value);
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000216
217 /* Normalize the exception so that if the type is a class, the
218 value will be an instance.
219 */
Brett Cannonbf364092006-03-01 04:25:17 +0000220 if (PyExceptionClass_Check(type)) {
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000221 /* if the value was not an instance, or is not an instance
222 whose class is (or is derived from) type, then use the
223 value as an argument to instantiation of the type
224 class.
225 */
Brett Cannonbf364092006-03-01 04:25:17 +0000226 if (!inclass || !PyObject_IsSubclass(inclass, type)) {
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000227 PyObject *args, *res;
228
229 if (value == Py_None)
Raymond Hettinger8ae46892003-10-12 19:09:37 +0000230 args = PyTuple_New(0);
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000231 else if (PyTuple_Check(value)) {
232 Py_INCREF(value);
233 args = value;
234 }
235 else
Raymond Hettinger8ae46892003-10-12 19:09:37 +0000236 args = PyTuple_Pack(1, value);
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000237
238 if (args == NULL)
239 goto finally;
240 res = PyEval_CallObject(type, args);
241 Py_DECREF(args);
242 if (res == NULL)
243 goto finally;
244 Py_DECREF(value);
245 value = res;
246 }
Barry Warsaw3a749931997-09-30 15:00:18 +0000247 /* if the class of the instance doesn't exactly match the
248 class of the type, believe the instance
249 */
250 else if (inclass != type) {
251 Py_DECREF(type);
252 type = inclass;
253 Py_INCREF(type);
254 }
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000255 }
256 *exc = type;
257 *val = value;
258 return;
259finally:
Guido van Rossum19b55f21997-12-09 14:11:39 +0000260 Py_DECREF(type);
261 Py_DECREF(value);
Jeremy Hyltone2e2c9f2001-09-26 19:58:38 +0000262 /* If the new exception doesn't set a traceback and the old
263 exception had a traceback, use the old traceback for the
264 new exception. It's better than nothing.
265 */
266 initial_tb = *tb;
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000267 PyErr_Fetch(exc, val, tb);
Jeremy Hyltone2e2c9f2001-09-26 19:58:38 +0000268 if (initial_tb != NULL) {
269 if (*tb == NULL)
270 *tb = initial_tb;
271 else
272 Py_DECREF(initial_tb);
273 }
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000274 /* normalize recursively */
Thomas Wouters89d996e2007-09-08 17:39:28 +0000275 tstate = PyThreadState_GET();
276 if (++tstate->recursion_depth > Py_GetRecursionLimit()) {
277 --tstate->recursion_depth;
278 PyErr_SetObject(PyExc_RuntimeError, PyExc_RecursionErrorInst);
279 return;
280 }
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000281 PyErr_NormalizeException(exc, val, tb);
Thomas Wouters89d996e2007-09-08 17:39:28 +0000282 --tstate->recursion_depth;
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000283}
284
285
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000286void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000287PyErr_Fetch(PyObject **p_type, PyObject **p_value, PyObject **p_traceback)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000288{
Nicholas Bastine5662ae2004-03-24 22:22:12 +0000289 PyThreadState *tstate = PyThreadState_GET();
Guido van Rossuma027efa1997-05-05 20:56:21 +0000290
291 *p_type = tstate->curexc_type;
292 *p_value = tstate->curexc_value;
293 *p_traceback = tstate->curexc_traceback;
294
295 tstate->curexc_type = NULL;
296 tstate->curexc_value = NULL;
297 tstate->curexc_traceback = NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000298}
299
300void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000301PyErr_Clear(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000302{
Guido van Rossuma027efa1997-05-05 20:56:21 +0000303 PyErr_Restore(NULL, NULL, NULL);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000304}
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000305
306/* Convenience functions to set a type error exception and return 0 */
307
308int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000309PyErr_BadArgument(void)
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000310{
Guido van Rossuma027efa1997-05-05 20:56:21 +0000311 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +0000312 "bad argument type for built-in operation");
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000313 return 0;
314}
315
Guido van Rossum373c8691997-04-29 18:22:47 +0000316PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000317PyErr_NoMemory(void)
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000318{
Vladimir Marangozov0888ff12000-08-18 18:01:06 +0000319 if (PyErr_ExceptionMatches(PyExc_MemoryError))
320 /* already current */
321 return NULL;
322
Barry Warsaw2d8adff1997-08-29 21:54:35 +0000323 /* raise the pre-allocated instance if it still exists */
324 if (PyExc_MemoryErrorInst)
Amaury Forgeot d'Arce19cadb2008-07-31 22:56:02 +0000325 {
326 /* Clear the previous traceback, otherwise it will be appended
327 * to the current one.
328 *
329 * The following statement is not likely to raise any error;
330 * if it does, we simply discard it.
331 */
332 PyException_SetTraceback(PyExc_MemoryErrorInst, Py_None);
333
Barry Warsaw2d8adff1997-08-29 21:54:35 +0000334 PyErr_SetObject(PyExc_MemoryError, PyExc_MemoryErrorInst);
Amaury Forgeot d'Arce19cadb2008-07-31 22:56:02 +0000335 }
Barry Warsaw2d8adff1997-08-29 21:54:35 +0000336 else
337 /* this will probably fail since there's no memory and hee,
338 hee, we have to instantiate this class
339 */
340 PyErr_SetNone(PyExc_MemoryError);
341
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000342 return NULL;
343}
344
Guido van Rossum373c8691997-04-29 18:22:47 +0000345PyObject *
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000346PyErr_SetFromErrnoWithFilenameObject(PyObject *exc, PyObject *filenameObject)
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000347{
Martin v. Löwis5d12abe2007-09-03 07:40:24 +0000348 PyObject *message;
Guido van Rossum373c8691997-04-29 18:22:47 +0000349 PyObject *v;
Guido van Rossum3a241811994-08-29 12:14:12 +0000350 int i = errno;
Martin v. Löwis3484a182002-03-09 12:07:51 +0000351#ifdef PLAN9
352 char errbuf[ERRMAX];
Martin v. Löwis5d12abe2007-09-03 07:40:24 +0000353#else
354#ifndef MS_WINDOWS
355 char *s;
356#else
357 WCHAR *s_buf = NULL;
358#endif /* Unix/Windows */
359#endif /* PLAN 9*/
360
Guido van Rossume9fbc091995-02-18 14:52:19 +0000361#ifdef EINTR
Guido van Rossum373c8691997-04-29 18:22:47 +0000362 if (i == EINTR && PyErr_CheckSignals())
Guido van Rossum5063bab1991-10-20 20:14:56 +0000363 return NULL;
Guido van Rossume9fbc091995-02-18 14:52:19 +0000364#endif
Martin v. Löwis5d12abe2007-09-03 07:40:24 +0000365
Martin v. Löwis3484a182002-03-09 12:07:51 +0000366#ifdef PLAN9
367 rerrstr(errbuf, sizeof errbuf);
Martin v. Löwis5d12abe2007-09-03 07:40:24 +0000368 message = PyUnicode_DecodeUTF8(errbuf, strlen(errbuf), "ignore");
Martin v. Löwis3484a182002-03-09 12:07:51 +0000369#else
Martin v. Löwis5d12abe2007-09-03 07:40:24 +0000370#ifndef MS_WINDOWS
Guido van Rossume0e59821998-10-14 20:38:13 +0000371 if (i == 0)
372 s = "Error"; /* Sometimes errno didn't get set */
Barry Warsaw97d95151998-07-23 16:05:56 +0000373 else
Guido van Rossume0e59821998-10-14 20:38:13 +0000374 s = strerror(i);
Martin v. Löwis5d12abe2007-09-03 07:40:24 +0000375 message = PyUnicode_DecodeUTF8(s, strlen(s), "ignore");
Guido van Rossum743007d1999-04-21 15:27:31 +0000376#else
Martin v. Löwis5d12abe2007-09-03 07:40:24 +0000377 if (i == 0)
378 message = PyUnicode_FromString("Error"); /* Sometimes errno didn't get set */
379 else
Guido van Rossum743007d1999-04-21 15:27:31 +0000380 {
Guido van Rossum795e1892000-02-17 15:19:15 +0000381 /* Note that the Win32 errors do not lineup with the
382 errno error. So if the error is in the MSVC error
Brett Cannonbf364092006-03-01 04:25:17 +0000383 table, we use it, otherwise we assume it really _is_
Guido van Rossum795e1892000-02-17 15:19:15 +0000384 a Win32 error code
385 */
Guido van Rossum584b16a2000-02-21 16:50:31 +0000386 if (i > 0 && i < _sys_nerr) {
Martin v. Löwis5d12abe2007-09-03 07:40:24 +0000387 message = PyUnicode_FromString(_sys_errlist[i]);
Guido van Rossum795e1892000-02-17 15:19:15 +0000388 }
389 else {
Martin v. Löwis5d12abe2007-09-03 07:40:24 +0000390 int len = FormatMessageW(
Guido van Rossum795e1892000-02-17 15:19:15 +0000391 FORMAT_MESSAGE_ALLOCATE_BUFFER |
392 FORMAT_MESSAGE_FROM_SYSTEM |
393 FORMAT_MESSAGE_IGNORE_INSERTS,
394 NULL, /* no message source */
395 i,
396 MAKELANGID(LANG_NEUTRAL,
397 SUBLANG_DEFAULT),
398 /* Default language */
Martin v. Löwis5d12abe2007-09-03 07:40:24 +0000399 (LPWSTR) &s_buf,
Guido van Rossum795e1892000-02-17 15:19:15 +0000400 0, /* size not used */
401 NULL); /* no args */
Mark Hammond3d61a062002-10-04 00:13:02 +0000402 if (len==0) {
Brett Cannonbf364092006-03-01 04:25:17 +0000403 /* Only ever seen this in out-of-mem
Mark Hammond3d61a062002-10-04 00:13:02 +0000404 situations */
Mark Hammond3d61a062002-10-04 00:13:02 +0000405 s_buf = NULL;
Martin v. Löwis5d12abe2007-09-03 07:40:24 +0000406 message = PyUnicode_FromFormat("Windows Error 0x%X", i);
Mark Hammond3d61a062002-10-04 00:13:02 +0000407 } else {
Mark Hammond3d61a062002-10-04 00:13:02 +0000408 /* remove trailing cr/lf and dots */
Martin v. Löwis5d12abe2007-09-03 07:40:24 +0000409 while (len > 0 && (s_buf[len-1] <= L' ' || s_buf[len-1] == L'.'))
410 s_buf[--len] = L'\0';
411 message = PyUnicode_FromUnicode(s_buf, len);
Mark Hammond3d61a062002-10-04 00:13:02 +0000412 }
Guido van Rossum795e1892000-02-17 15:19:15 +0000413 }
Guido van Rossum743007d1999-04-21 15:27:31 +0000414 }
Martin v. Löwis3484a182002-03-09 12:07:51 +0000415#endif /* Unix/Windows */
416#endif /* PLAN 9*/
Martin v. Löwis5d12abe2007-09-03 07:40:24 +0000417
418 if (message == NULL)
419 {
420#ifdef MS_WINDOWS
421 LocalFree(s_buf);
422#endif
423 return NULL;
424 }
425
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000426 if (filenameObject != NULL)
Martin v. Löwis5d12abe2007-09-03 07:40:24 +0000427 v = Py_BuildValue("(iOO)", i, message, filenameObject);
Guido van Rossume0e59821998-10-14 20:38:13 +0000428 else
Martin v. Löwis5d12abe2007-09-03 07:40:24 +0000429 v = Py_BuildValue("(iO)", i, message);
430 Py_DECREF(message);
431
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000432 if (v != NULL) {
Guido van Rossum373c8691997-04-29 18:22:47 +0000433 PyErr_SetObject(exc, v);
434 Py_DECREF(v);
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000435 }
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000436#ifdef MS_WINDOWS
Guido van Rossum795e1892000-02-17 15:19:15 +0000437 LocalFree(s_buf);
Guido van Rossum743007d1999-04-21 15:27:31 +0000438#endif
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000439 return NULL;
440}
Guido van Rossum743007d1999-04-21 15:27:31 +0000441
Barry Warsaw97d95151998-07-23 16:05:56 +0000442
443PyObject *
Neal Norwitzb382b842007-08-24 20:00:37 +0000444PyErr_SetFromErrnoWithFilename(PyObject *exc, const char *filename)
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000445{
Neal Norwitzcd795962007-08-24 19:54:13 +0000446 PyObject *name = filename ? PyUnicode_FromString(filename) : NULL;
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000447 PyObject *result = PyErr_SetFromErrnoWithFilenameObject(exc, name);
Mark Hammondda7efaa2002-10-04 00:09:38 +0000448 Py_XDECREF(name);
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000449 return result;
450}
451
452#ifdef Py_WIN_WIDE_FILENAMES
453PyObject *
Neal Norwitzb382b842007-08-24 20:00:37 +0000454PyErr_SetFromErrnoWithUnicodeFilename(PyObject *exc, const Py_UNICODE *filename)
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000455{
Brett Cannonbf364092006-03-01 04:25:17 +0000456 PyObject *name = filename ?
457 PyUnicode_FromUnicode(filename, wcslen(filename)) :
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000458 NULL;
459 PyObject *result = PyErr_SetFromErrnoWithFilenameObject(exc, name);
460 Py_XDECREF(name);
461 return result;
462}
463#endif /* Py_WIN_WIDE_FILENAMES */
464
465PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000466PyErr_SetFromErrno(PyObject *exc)
Barry Warsaw97d95151998-07-23 16:05:56 +0000467{
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000468 return PyErr_SetFromErrnoWithFilenameObject(exc, NULL);
Barry Warsaw97d95151998-07-23 16:05:56 +0000469}
Guido van Rossum683a0721990-10-21 22:09:12 +0000470
Brett Cannonbf364092006-03-01 04:25:17 +0000471#ifdef MS_WINDOWS
Guido van Rossum795e1892000-02-17 15:19:15 +0000472/* Windows specific error code handling */
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000473PyObject *PyErr_SetExcFromWindowsErrWithFilenameObject(
Thomas Heller085358a2002-07-29 14:27:41 +0000474 PyObject *exc,
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000475 int ierr,
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000476 PyObject *filenameObject)
Guido van Rossum795e1892000-02-17 15:19:15 +0000477{
478 int len;
Martin v. Löwis5d12abe2007-09-03 07:40:24 +0000479 WCHAR *s_buf = NULL; /* Free via LocalFree */
480 PyObject *message;
Guido van Rossum795e1892000-02-17 15:19:15 +0000481 PyObject *v;
482 DWORD err = (DWORD)ierr;
483 if (err==0) err = GetLastError();
Martin v. Löwis5d12abe2007-09-03 07:40:24 +0000484 len = FormatMessageW(
Guido van Rossum795e1892000-02-17 15:19:15 +0000485 /* Error API error */
486 FORMAT_MESSAGE_ALLOCATE_BUFFER |
487 FORMAT_MESSAGE_FROM_SYSTEM |
488 FORMAT_MESSAGE_IGNORE_INSERTS,
489 NULL, /* no message source */
490 err,
491 MAKELANGID(LANG_NEUTRAL,
492 SUBLANG_DEFAULT), /* Default language */
Martin v. Löwis5d12abe2007-09-03 07:40:24 +0000493 (LPWSTR) &s_buf,
Guido van Rossum795e1892000-02-17 15:19:15 +0000494 0, /* size not used */
495 NULL); /* no args */
Mark Hammond3d61a062002-10-04 00:13:02 +0000496 if (len==0) {
497 /* Only seen this in out of mem situations */
Martin v. Löwis5d12abe2007-09-03 07:40:24 +0000498 message = PyUnicode_FromFormat("Windows Error 0x%X", err);
Mark Hammond3d61a062002-10-04 00:13:02 +0000499 s_buf = NULL;
500 } else {
Mark Hammond3d61a062002-10-04 00:13:02 +0000501 /* remove trailing cr/lf and dots */
Martin v. Löwis5d12abe2007-09-03 07:40:24 +0000502 while (len > 0 && (s_buf[len-1] <= L' ' || s_buf[len-1] == L'.'))
503 s_buf[--len] = L'\0';
504 message = PyUnicode_FromUnicode(s_buf, len);
Mark Hammond3d61a062002-10-04 00:13:02 +0000505 }
Martin v. Löwis5d12abe2007-09-03 07:40:24 +0000506
507 if (message == NULL)
508 {
509 LocalFree(s_buf);
510 return NULL;
511 }
512
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000513 if (filenameObject != NULL)
Martin v. Löwis5d12abe2007-09-03 07:40:24 +0000514 v = Py_BuildValue("(iOO)", err, message, filenameObject);
Guido van Rossum795e1892000-02-17 15:19:15 +0000515 else
Martin v. Löwis5d12abe2007-09-03 07:40:24 +0000516 v = Py_BuildValue("(iO)", err, message);
517 Py_DECREF(message);
518
Guido van Rossum795e1892000-02-17 15:19:15 +0000519 if (v != NULL) {
Thomas Heller085358a2002-07-29 14:27:41 +0000520 PyErr_SetObject(exc, v);
Guido van Rossum795e1892000-02-17 15:19:15 +0000521 Py_DECREF(v);
522 }
Mark Hammond3d61a062002-10-04 00:13:02 +0000523 LocalFree(s_buf);
Guido van Rossum795e1892000-02-17 15:19:15 +0000524 return NULL;
525}
526
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000527PyObject *PyErr_SetExcFromWindowsErrWithFilename(
528 PyObject *exc,
529 int ierr,
530 const char *filename)
531{
Neal Norwitzcd795962007-08-24 19:54:13 +0000532 PyObject *name = filename ? PyUnicode_FromString(filename) : NULL;
Brett Cannonbf364092006-03-01 04:25:17 +0000533 PyObject *ret = PyErr_SetExcFromWindowsErrWithFilenameObject(exc,
534 ierr,
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000535 name);
536 Py_XDECREF(name);
537 return ret;
538}
539
540#ifdef Py_WIN_WIDE_FILENAMES
541PyObject *PyErr_SetExcFromWindowsErrWithUnicodeFilename(
542 PyObject *exc,
543 int ierr,
544 const Py_UNICODE *filename)
545{
Brett Cannonbf364092006-03-01 04:25:17 +0000546 PyObject *name = filename ?
547 PyUnicode_FromUnicode(filename, wcslen(filename)) :
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000548 NULL;
Brett Cannonbf364092006-03-01 04:25:17 +0000549 PyObject *ret = PyErr_SetExcFromWindowsErrWithFilenameObject(exc,
550 ierr,
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000551 name);
552 Py_XDECREF(name);
553 return ret;
554}
555#endif /* Py_WIN_WIDE_FILENAMES */
556
Thomas Heller085358a2002-07-29 14:27:41 +0000557PyObject *PyErr_SetExcFromWindowsErr(PyObject *exc, int ierr)
558{
559 return PyErr_SetExcFromWindowsErrWithFilename(exc, ierr, NULL);
560}
561
Guido van Rossum795e1892000-02-17 15:19:15 +0000562PyObject *PyErr_SetFromWindowsErr(int ierr)
563{
Thomas Heller085358a2002-07-29 14:27:41 +0000564 return PyErr_SetExcFromWindowsErrWithFilename(PyExc_WindowsError,
565 ierr, NULL);
566}
567PyObject *PyErr_SetFromWindowsErrWithFilename(
568 int ierr,
569 const char *filename)
570{
Neal Norwitzcd795962007-08-24 19:54:13 +0000571 PyObject *name = filename ? PyUnicode_FromString(filename) : NULL;
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000572 PyObject *result = PyErr_SetExcFromWindowsErrWithFilenameObject(
573 PyExc_WindowsError,
574 ierr, name);
Mark Hammondda7efaa2002-10-04 00:09:38 +0000575 Py_XDECREF(name);
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000576 return result;
Guido van Rossum795e1892000-02-17 15:19:15 +0000577}
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000578
579#ifdef Py_WIN_WIDE_FILENAMES
580PyObject *PyErr_SetFromWindowsErrWithUnicodeFilename(
581 int ierr,
582 const Py_UNICODE *filename)
583{
Brett Cannonbf364092006-03-01 04:25:17 +0000584 PyObject *name = filename ?
585 PyUnicode_FromUnicode(filename, wcslen(filename)) :
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000586 NULL;
587 PyObject *result = PyErr_SetExcFromWindowsErrWithFilenameObject(
588 PyExc_WindowsError,
589 ierr, name);
Mark Hammondda7efaa2002-10-04 00:09:38 +0000590 Py_XDECREF(name);
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000591 return result;
592}
593#endif /* Py_WIN_WIDE_FILENAMES */
Guido van Rossum795e1892000-02-17 15:19:15 +0000594#endif /* MS_WINDOWS */
595
Guido van Rossum683a0721990-10-21 22:09:12 +0000596void
Neal Norwitzb382b842007-08-24 20:00:37 +0000597_PyErr_BadInternalCall(const char *filename, int lineno)
Fred Drake6d63adf2000-08-24 22:38:39 +0000598{
599 PyErr_Format(PyExc_SystemError,
600 "%s:%d: bad argument to internal function",
601 filename, lineno);
602}
603
604/* Remove the preprocessor macro for PyErr_BadInternalCall() so that we can
605 export the entry point for existing object code: */
606#undef PyErr_BadInternalCall
607void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000608PyErr_BadInternalCall(void)
Guido van Rossum683a0721990-10-21 22:09:12 +0000609{
Fred Drake6d63adf2000-08-24 22:38:39 +0000610 PyErr_Format(PyExc_SystemError,
611 "bad argument to internal function");
Guido van Rossum683a0721990-10-21 22:09:12 +0000612}
Fred Drake6d63adf2000-08-24 22:38:39 +0000613#define PyErr_BadInternalCall() _PyErr_BadInternalCall(__FILE__, __LINE__)
614
Guido van Rossum1548bac1997-02-14 17:09:47 +0000615
616
Guido van Rossum1548bac1997-02-14 17:09:47 +0000617PyObject *
618PyErr_Format(PyObject *exception, const char *format, ...)
Guido van Rossum1548bac1997-02-14 17:09:47 +0000619{
620 va_list vargs;
Jeremy Hyltonb69a27e2000-09-01 03:49:47 +0000621 PyObject* string;
Guido van Rossum1548bac1997-02-14 17:09:47 +0000622
Jeremy Hyltonb69a27e2000-09-01 03:49:47 +0000623#ifdef HAVE_STDARG_PROTOTYPES
Guido van Rossum1548bac1997-02-14 17:09:47 +0000624 va_start(vargs, format);
Jeremy Hyltonb69a27e2000-09-01 03:49:47 +0000625#else
626 va_start(vargs);
627#endif
Guido van Rossum1548bac1997-02-14 17:09:47 +0000628
Walter Dörwald573c08c2007-05-25 15:46:59 +0000629 string = PyUnicode_FromFormatV(format, vargs);
Jeremy Hyltonb69a27e2000-09-01 03:49:47 +0000630 PyErr_SetObject(exception, string);
631 Py_XDECREF(string);
Tim Petersc15c4f12001-10-02 21:32:07 +0000632 va_end(vargs);
Guido van Rossum1548bac1997-02-14 17:09:47 +0000633 return NULL;
634}
Guido van Rossum7617e051997-09-16 18:43:50 +0000635
636
Thomas Wouters477c8d52006-05-27 19:21:47 +0000637
Guido van Rossum7617e051997-09-16 18:43:50 +0000638PyObject *
Neal Norwitzb382b842007-08-24 20:00:37 +0000639PyErr_NewException(const char *name, PyObject *base, PyObject *dict)
Guido van Rossum7617e051997-09-16 18:43:50 +0000640{
Neal Norwitzb382b842007-08-24 20:00:37 +0000641 const char *dot;
Guido van Rossum2ac650f1997-10-03 19:50:55 +0000642 PyObject *modulename = NULL;
643 PyObject *classname = NULL;
644 PyObject *mydict = NULL;
645 PyObject *bases = NULL;
Guido van Rossum7617e051997-09-16 18:43:50 +0000646 PyObject *result = NULL;
Guido van Rossum2ac650f1997-10-03 19:50:55 +0000647 dot = strrchr(name, '.');
648 if (dot == NULL) {
649 PyErr_SetString(PyExc_SystemError,
650 "PyErr_NewException: name must be module.class");
Guido van Rossum7617e051997-09-16 18:43:50 +0000651 return NULL;
Guido van Rossum7617e051997-09-16 18:43:50 +0000652 }
653 if (base == NULL)
654 base = PyExc_Exception;
Guido van Rossum2ac650f1997-10-03 19:50:55 +0000655 if (dict == NULL) {
656 dict = mydict = PyDict_New();
657 if (dict == NULL)
658 goto failure;
659 }
660 if (PyDict_GetItemString(dict, "__module__") == NULL) {
Neal Norwitzcd795962007-08-24 19:54:13 +0000661 modulename = PyUnicode_FromStringAndSize(name,
Thomas Wouters89f507f2006-12-13 04:49:30 +0000662 (Py_ssize_t)(dot-name));
Guido van Rossum2ac650f1997-10-03 19:50:55 +0000663 if (modulename == NULL)
664 goto failure;
665 if (PyDict_SetItemString(dict, "__module__", modulename) != 0)
666 goto failure;
667 }
Thomas Wouters477c8d52006-05-27 19:21:47 +0000668 if (PyTuple_Check(base)) {
669 bases = base;
670 /* INCREF as we create a new ref in the else branch */
671 Py_INCREF(bases);
672 } else {
673 bases = PyTuple_Pack(1, base);
674 if (bases == NULL)
675 goto failure;
676 }
677 /* Create a real new-style class. */
Guido van Rossume845c0f2007-11-02 23:07:07 +0000678 result = PyObject_CallFunction((PyObject *)&PyType_Type, "UOO",
Thomas Wouters477c8d52006-05-27 19:21:47 +0000679 dot+1, bases, dict);
Guido van Rossum7617e051997-09-16 18:43:50 +0000680 failure:
Guido van Rossum2ac650f1997-10-03 19:50:55 +0000681 Py_XDECREF(bases);
682 Py_XDECREF(mydict);
683 Py_XDECREF(classname);
684 Py_XDECREF(modulename);
Guido van Rossum7617e051997-09-16 18:43:50 +0000685 return result;
686}
Jeremy Hyltonb709df32000-09-01 02:47:25 +0000687
688/* Call when an exception has occurred but there is no way for Python
689 to handle it. Examples: exception in __del__ or during GC. */
690void
691PyErr_WriteUnraisable(PyObject *obj)
692{
693 PyObject *f, *t, *v, *tb;
694 PyErr_Fetch(&t, &v, &tb);
695 f = PySys_GetObject("stderr");
Christian Heimes2be03732007-11-15 02:26:46 +0000696 if (f != NULL && f != Py_None) {
Jeremy Hyltonb709df32000-09-01 02:47:25 +0000697 PyFile_WriteString("Exception ", f);
698 if (t) {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000699 PyObject* moduleName;
Guido van Rossumd8faa362007-04-27 19:54:29 +0000700 char* className;
701 assert(PyExceptionClass_Check(t));
702 className = PyExceptionClass_Name(t);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000703 if (className != NULL) {
704 char *dot = strrchr(className, '.');
705 if (dot != NULL)
706 className = dot+1;
707 }
708
709 moduleName = PyObject_GetAttrString(t, "__module__");
Brett Cannonbf364092006-03-01 04:25:17 +0000710 if (moduleName == NULL)
711 PyFile_WriteString("<unknown>", f);
712 else {
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +0000713 char* modstr = _PyUnicode_AsString(moduleName);
Neal Norwitz2633c692007-02-26 22:22:47 +0000714 if (modstr &&
Georg Brandl1a3284e2007-12-02 09:40:06 +0000715 strcmp(modstr, "builtins") != 0)
Brett Cannonbf364092006-03-01 04:25:17 +0000716 {
717 PyFile_WriteString(modstr, f);
718 PyFile_WriteString(".", f);
719 }
720 }
721 if (className == NULL)
722 PyFile_WriteString("<unknown>", f);
723 else
724 PyFile_WriteString(className, f);
Jeremy Hyltonb709df32000-09-01 02:47:25 +0000725 if (v && v != Py_None) {
726 PyFile_WriteString(": ", f);
727 PyFile_WriteObject(v, f, 0);
728 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000729 Py_XDECREF(moduleName);
Jeremy Hyltonb709df32000-09-01 02:47:25 +0000730 }
731 PyFile_WriteString(" in ", f);
732 PyFile_WriteObject(obj, f, 0);
733 PyFile_WriteString(" ignored\n", f);
734 PyErr_Clear(); /* Just in case */
735 }
736 Py_XDECREF(t);
737 Py_XDECREF(v);
738 Py_XDECREF(tb);
739}
Guido van Rossumcfd42b52000-12-15 21:58:52 +0000740
Armin Rigo092381a2003-10-25 14:29:27 +0000741extern PyObject *PyModule_GetWarningsModule(void);
Guido van Rossumcfd42b52000-12-15 21:58:52 +0000742
Guido van Rossum2fd45652001-02-28 21:46:24 +0000743
Martin v. Löwiscfeb3b62002-03-03 21:30:27 +0000744/* Set file and line information for the current exception.
745 If the exception is not a SyntaxError, also sets additional attributes
746 to make printing of exceptions believe it is a syntax error. */
Guido van Rossum2fd45652001-02-28 21:46:24 +0000747
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000748void
Martin v. Löwis95292d62002-12-11 14:04:59 +0000749PyErr_SyntaxLocation(const char *filename, int lineno)
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000750{
751 PyObject *exc, *v, *tb, *tmp;
752
753 /* add attributes for the line number and filename for the error */
754 PyErr_Fetch(&exc, &v, &tb);
755 PyErr_NormalizeException(&exc, &v, &tb);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000756 /* XXX check that it is, indeed, a syntax error. It might not
757 * be, though. */
Christian Heimes217cfd12007-12-02 14:31:20 +0000758 tmp = PyLong_FromLong(lineno);
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000759 if (tmp == NULL)
760 PyErr_Clear();
761 else {
762 if (PyObject_SetAttrString(v, "lineno", tmp))
763 PyErr_Clear();
764 Py_DECREF(tmp);
765 }
766 if (filename != NULL) {
Neal Norwitzcd795962007-08-24 19:54:13 +0000767 tmp = PyUnicode_FromString(filename);
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000768 if (tmp == NULL)
769 PyErr_Clear();
770 else {
771 if (PyObject_SetAttrString(v, "filename", tmp))
772 PyErr_Clear();
773 Py_DECREF(tmp);
774 }
775
776 tmp = PyErr_ProgramText(filename, lineno);
777 if (tmp) {
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000778 if (PyObject_SetAttrString(v, "text", tmp))
779 PyErr_Clear();
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000780 Py_DECREF(tmp);
781 }
782 }
Martin v. Löwiscfeb3b62002-03-03 21:30:27 +0000783 if (PyObject_SetAttrString(v, "offset", Py_None)) {
784 PyErr_Clear();
785 }
786 if (exc != PyExc_SyntaxError) {
787 if (!PyObject_HasAttrString(v, "msg")) {
788 tmp = PyObject_Str(v);
789 if (tmp) {
790 if (PyObject_SetAttrString(v, "msg", tmp))
791 PyErr_Clear();
792 Py_DECREF(tmp);
793 } else {
794 PyErr_Clear();
795 }
796 }
797 if (!PyObject_HasAttrString(v, "print_file_and_line")) {
798 if (PyObject_SetAttrString(v, "print_file_and_line",
799 Py_None))
800 PyErr_Clear();
801 }
802 }
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000803 PyErr_Restore(exc, v, tb);
804}
805
Guido van Rossumebe8f8a2007-10-10 18:53:36 +0000806/* Attempt to load the line of text that the exception refers to. If it
807 fails, it will return NULL but will not set an exception.
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000808
809 XXX The functionality of this function is quite similar to the
Guido van Rossumebe8f8a2007-10-10 18:53:36 +0000810 functionality in tb_displayline() in traceback.c. */
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000811
812PyObject *
Martin v. Löwis95292d62002-12-11 14:04:59 +0000813PyErr_ProgramText(const char *filename, int lineno)
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000814{
815 FILE *fp;
816 int i;
817 char linebuf[1000];
818
Tim Petersa7444f42006-02-27 23:29:46 +0000819 if (filename == NULL || *filename == '\0' || lineno <= 0)
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000820 return NULL;
Jack Jansen7b8c7542002-04-14 20:12:41 +0000821 fp = fopen(filename, "r" PY_STDIOTEXTMODE);
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000822 if (fp == NULL)
823 return NULL;
824 for (i = 0; i < lineno; i++) {
825 char *pLastChar = &linebuf[sizeof(linebuf) - 2];
826 do {
827 *pLastChar = '\0';
Guido van Rossumebe8f8a2007-10-10 18:53:36 +0000828 if (Py_UniversalNewlineFgets(linebuf, sizeof linebuf,
829 fp, NULL) == NULL)
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000830 break;
831 /* fgets read *something*; if it didn't get as
832 far as pLastChar, it must have found a newline
Thomas Wouters477c8d52006-05-27 19:21:47 +0000833 or hit the end of the file; if pLastChar is \n,
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000834 it obviously found a newline; else we haven't
835 yet seen a newline, so must continue */
836 } while (*pLastChar != '\0' && *pLastChar != '\n');
837 }
838 fclose(fp);
839 if (i == lineno) {
840 char *p = linebuf;
Guido van Rossumebe8f8a2007-10-10 18:53:36 +0000841 PyObject *res;
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000842 while (*p == ' ' || *p == '\t' || *p == '\014')
843 p++;
Guido van Rossumebe8f8a2007-10-10 18:53:36 +0000844 res = PyUnicode_FromString(p);
845 if (res == NULL)
846 PyErr_Clear();
847 return res;
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000848 }
849 return NULL;
850}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000851
852#ifdef __cplusplus
853}
854#endif