Mercurial > cpython-lru_cache-weakref
comparison Python/errors.c @ 61465:e38335d4df91 2.6
Recorded merge of revisions 81029 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r81029 | antoine.pitrou | 2010-05-09 16:46:46 +0200 (dim., 09 mai 2010) | 3 lines
Untabify C files. Will watch buildbots.
........
| author | Antoine Pitrou <solipsis@pitrou.net> |
|---|---|
| date | Sun, 09 May 2010 15:15:40 +0000 |
| parents | 3889d9528601 |
| children | 787b969d37f0 |
comparison
equal
deleted
inserted
replaced
| 61461:b6e03ca8a206 | 61465:e38335d4df91 |
|---|---|
| 22 | 22 |
| 23 | 23 |
| 24 void | 24 void |
| 25 PyErr_Restore(PyObject *type, PyObject *value, PyObject *traceback) | 25 PyErr_Restore(PyObject *type, PyObject *value, PyObject *traceback) |
| 26 { | 26 { |
| 27 PyThreadState *tstate = PyThreadState_GET(); | 27 PyThreadState *tstate = PyThreadState_GET(); |
| 28 PyObject *oldtype, *oldvalue, *oldtraceback; | 28 PyObject *oldtype, *oldvalue, *oldtraceback; |
| 29 | 29 |
| 30 if (traceback != NULL && !PyTraceBack_Check(traceback)) { | 30 if (traceback != NULL && !PyTraceBack_Check(traceback)) { |
| 31 /* XXX Should never happen -- fatal error instead? */ | 31 /* XXX Should never happen -- fatal error instead? */ |
| 32 /* Well, it could be None. */ | 32 /* Well, it could be None. */ |
| 33 Py_DECREF(traceback); | 33 Py_DECREF(traceback); |
| 34 traceback = NULL; | 34 traceback = NULL; |
| 35 } | 35 } |
| 36 | 36 |
| 37 /* Save these in locals to safeguard against recursive | 37 /* Save these in locals to safeguard against recursive |
| 38 invocation through Py_XDECREF */ | 38 invocation through Py_XDECREF */ |
| 39 oldtype = tstate->curexc_type; | 39 oldtype = tstate->curexc_type; |
| 40 oldvalue = tstate->curexc_value; | 40 oldvalue = tstate->curexc_value; |
| 41 oldtraceback = tstate->curexc_traceback; | 41 oldtraceback = tstate->curexc_traceback; |
| 42 | 42 |
| 43 tstate->curexc_type = type; | 43 tstate->curexc_type = type; |
| 44 tstate->curexc_value = value; | 44 tstate->curexc_value = value; |
| 45 tstate->curexc_traceback = traceback; | 45 tstate->curexc_traceback = traceback; |
| 46 | 46 |
| 47 Py_XDECREF(oldtype); | 47 Py_XDECREF(oldtype); |
| 48 Py_XDECREF(oldvalue); | 48 Py_XDECREF(oldvalue); |
| 49 Py_XDECREF(oldtraceback); | 49 Py_XDECREF(oldtraceback); |
| 50 } | 50 } |
| 51 | 51 |
| 52 void | 52 void |
| 53 PyErr_SetObject(PyObject *exception, PyObject *value) | 53 PyErr_SetObject(PyObject *exception, PyObject *value) |
| 54 { | 54 { |
| 55 Py_XINCREF(exception); | 55 Py_XINCREF(exception); |
| 56 Py_XINCREF(value); | 56 Py_XINCREF(value); |
| 57 PyErr_Restore(exception, value, (PyObject *)NULL); | 57 PyErr_Restore(exception, value, (PyObject *)NULL); |
| 58 } | 58 } |
| 59 | 59 |
| 60 void | 60 void |
| 61 PyErr_SetNone(PyObject *exception) | 61 PyErr_SetNone(PyObject *exception) |
| 62 { | 62 { |
| 63 PyErr_SetObject(exception, (PyObject *)NULL); | 63 PyErr_SetObject(exception, (PyObject *)NULL); |
| 64 } | 64 } |
| 65 | 65 |
| 66 void | 66 void |
| 67 PyErr_SetString(PyObject *exception, const char *string) | 67 PyErr_SetString(PyObject *exception, const char *string) |
| 68 { | 68 { |
| 69 PyObject *value = PyString_FromString(string); | 69 PyObject *value = PyString_FromString(string); |
| 70 PyErr_SetObject(exception, value); | 70 PyErr_SetObject(exception, value); |
| 71 Py_XDECREF(value); | 71 Py_XDECREF(value); |
| 72 } | 72 } |
| 73 | 73 |
| 74 | 74 |
| 75 PyObject * | 75 PyObject * |
| 76 PyErr_Occurred(void) | 76 PyErr_Occurred(void) |
| 77 { | 77 { |
| 78 PyThreadState *tstate = PyThreadState_GET(); | 78 PyThreadState *tstate = PyThreadState_GET(); |
| 79 | 79 |
| 80 return tstate->curexc_type; | 80 return tstate->curexc_type; |
| 81 } | 81 } |
| 82 | 82 |
| 83 | 83 |
| 84 int | 84 int |
| 85 PyErr_GivenExceptionMatches(PyObject *err, PyObject *exc) | 85 PyErr_GivenExceptionMatches(PyObject *err, PyObject *exc) |
| 86 { | 86 { |
| 87 if (err == NULL || exc == NULL) { | 87 if (err == NULL || exc == NULL) { |
| 88 /* maybe caused by "import exceptions" that failed early on */ | 88 /* maybe caused by "import exceptions" that failed early on */ |
| 89 return 0; | 89 return 0; |
| 90 } | 90 } |
| 91 if (PyTuple_Check(exc)) { | 91 if (PyTuple_Check(exc)) { |
| 92 Py_ssize_t i, n; | 92 Py_ssize_t i, n; |
| 93 n = PyTuple_Size(exc); | 93 n = PyTuple_Size(exc); |
| 94 for (i = 0; i < n; i++) { | 94 for (i = 0; i < n; i++) { |
| 95 /* Test recursively */ | 95 /* Test recursively */ |
| 96 if (PyErr_GivenExceptionMatches( | 96 if (PyErr_GivenExceptionMatches( |
| 97 err, PyTuple_GET_ITEM(exc, i))) | 97 err, PyTuple_GET_ITEM(exc, i))) |
| 98 { | 98 { |
| 99 return 1; | 99 return 1; |
| 100 } | 100 } |
| 101 } | 101 } |
| 102 return 0; | 102 return 0; |
| 103 } | 103 } |
| 104 /* err might be an instance, so check its class. */ | 104 /* err might be an instance, so check its class. */ |
| 105 if (PyExceptionInstance_Check(err)) | 105 if (PyExceptionInstance_Check(err)) |
| 106 err = PyExceptionInstance_Class(err); | 106 err = PyExceptionInstance_Class(err); |
| 107 | 107 |
| 108 if (PyExceptionClass_Check(err) && PyExceptionClass_Check(exc)) { | 108 if (PyExceptionClass_Check(err) && PyExceptionClass_Check(exc)) { |
| 109 int res = 0; | 109 int res = 0; |
| 110 PyObject *exception, *value, *tb; | 110 PyObject *exception, *value, *tb; |
| 111 PyErr_Fetch(&exception, &value, &tb); | 111 PyErr_Fetch(&exception, &value, &tb); |
| 112 res = PyObject_IsSubclass(err, exc); | 112 res = PyObject_IsSubclass(err, exc); |
| 113 /* This function must not fail, so print the error here */ | 113 /* This function must not fail, so print the error here */ |
| 114 if (res == -1) { | 114 if (res == -1) { |
| 115 PyErr_WriteUnraisable(err); | 115 PyErr_WriteUnraisable(err); |
| 116 res = 0; | 116 res = 0; |
| 117 } | 117 } |
| 118 PyErr_Restore(exception, value, tb); | 118 PyErr_Restore(exception, value, tb); |
| 119 return res; | 119 return res; |
| 120 } | 120 } |
| 121 | 121 |
| 122 return err == exc; | 122 return err == exc; |
| 123 } | 123 } |
| 124 | 124 |
| 125 | 125 |
| 126 int | 126 int |
| 127 PyErr_ExceptionMatches(PyObject *exc) | 127 PyErr_ExceptionMatches(PyObject *exc) |
| 128 { | 128 { |
| 129 return PyErr_GivenExceptionMatches(PyErr_Occurred(), exc); | 129 return PyErr_GivenExceptionMatches(PyErr_Occurred(), exc); |
| 130 } | 130 } |
| 131 | 131 |
| 132 | 132 |
| 133 /* Used in many places to normalize a raised exception, including in | 133 /* Used in many places to normalize a raised exception, including in |
| 134 eval_code2(), do_raise(), and PyErr_Print() | 134 eval_code2(), do_raise(), and PyErr_Print() |
| 135 */ | 135 */ |
| 136 void | 136 void |
| 137 PyErr_NormalizeException(PyObject **exc, PyObject **val, PyObject **tb) | 137 PyErr_NormalizeException(PyObject **exc, PyObject **val, PyObject **tb) |
| 138 { | 138 { |
| 139 PyObject *type = *exc; | 139 PyObject *type = *exc; |
| 140 PyObject *value = *val; | 140 PyObject *value = *val; |
| 141 PyObject *inclass = NULL; | 141 PyObject *inclass = NULL; |
| 142 PyObject *initial_tb = NULL; | 142 PyObject *initial_tb = NULL; |
| 143 PyThreadState *tstate = NULL; | 143 PyThreadState *tstate = NULL; |
| 144 | 144 |
| 145 if (type == NULL) { | 145 if (type == NULL) { |
| 146 /* There was no exception, so nothing to do. */ | 146 /* There was no exception, so nothing to do. */ |
| 147 return; | 147 return; |
| 148 } | 148 } |
| 149 | 149 |
| 150 /* If PyErr_SetNone() was used, the value will have been actually | 150 /* If PyErr_SetNone() was used, the value will have been actually |
| 151 set to NULL. | 151 set to NULL. |
| 152 */ | 152 */ |
| 153 if (!value) { | 153 if (!value) { |
| 154 value = Py_None; | 154 value = Py_None; |
| 155 Py_INCREF(value); | 155 Py_INCREF(value); |
| 156 } | 156 } |
| 157 | 157 |
| 158 if (PyExceptionInstance_Check(value)) | 158 if (PyExceptionInstance_Check(value)) |
| 159 inclass = PyExceptionInstance_Class(value); | 159 inclass = PyExceptionInstance_Class(value); |
| 160 | 160 |
| 161 /* Normalize the exception so that if the type is a class, the | 161 /* Normalize the exception so that if the type is a class, the |
| 162 value will be an instance. | 162 value will be an instance. |
| 163 */ | 163 */ |
| 164 if (PyExceptionClass_Check(type)) { | 164 if (PyExceptionClass_Check(type)) { |
| 165 /* if the value was not an instance, or is not an instance | 165 /* if the value was not an instance, or is not an instance |
| 166 whose class is (or is derived from) type, then use the | 166 whose class is (or is derived from) type, then use the |
| 167 value as an argument to instantiation of the type | 167 value as an argument to instantiation of the type |
| 168 class. | 168 class. |
| 169 */ | 169 */ |
| 170 if (!inclass || !PyObject_IsSubclass(inclass, type)) { | 170 if (!inclass || !PyObject_IsSubclass(inclass, type)) { |
| 171 PyObject *args, *res; | 171 PyObject *args, *res; |
| 172 | 172 |
| 173 if (value == Py_None) | 173 if (value == Py_None) |
| 174 args = PyTuple_New(0); | 174 args = PyTuple_New(0); |
| 175 else if (PyTuple_Check(value)) { | 175 else if (PyTuple_Check(value)) { |
| 176 Py_INCREF(value); | 176 Py_INCREF(value); |
| 177 args = value; | 177 args = value; |
| 178 } | 178 } |
| 179 else | 179 else |
| 180 args = PyTuple_Pack(1, value); | 180 args = PyTuple_Pack(1, value); |
| 181 | 181 |
| 182 if (args == NULL) | 182 if (args == NULL) |
| 183 goto finally; | 183 goto finally; |
| 184 res = PyEval_CallObject(type, args); | 184 res = PyEval_CallObject(type, args); |
| 185 Py_DECREF(args); | 185 Py_DECREF(args); |
| 186 if (res == NULL) | 186 if (res == NULL) |
| 187 goto finally; | 187 goto finally; |
| 188 Py_DECREF(value); | 188 Py_DECREF(value); |
| 189 value = res; | 189 value = res; |
| 190 } | 190 } |
| 191 /* if the class of the instance doesn't exactly match the | 191 /* if the class of the instance doesn't exactly match the |
| 192 class of the type, believe the instance | 192 class of the type, believe the instance |
| 193 */ | 193 */ |
| 194 else if (inclass != type) { | 194 else if (inclass != type) { |
| 195 Py_DECREF(type); | 195 Py_DECREF(type); |
| 196 type = inclass; | 196 type = inclass; |
| 197 Py_INCREF(type); | 197 Py_INCREF(type); |
| 198 } | 198 } |
| 199 } | 199 } |
| 200 *exc = type; | 200 *exc = type; |
| 201 *val = value; | 201 *val = value; |
| 202 return; | 202 return; |
| 203 finally: | 203 finally: |
| 204 Py_DECREF(type); | 204 Py_DECREF(type); |
| 205 Py_DECREF(value); | 205 Py_DECREF(value); |
| 206 /* If the new exception doesn't set a traceback and the old | 206 /* If the new exception doesn't set a traceback and the old |
| 207 exception had a traceback, use the old traceback for the | 207 exception had a traceback, use the old traceback for the |
| 208 new exception. It's better than nothing. | 208 new exception. It's better than nothing. |
| 209 */ | 209 */ |
| 210 initial_tb = *tb; | 210 initial_tb = *tb; |
| 211 PyErr_Fetch(exc, val, tb); | 211 PyErr_Fetch(exc, val, tb); |
| 212 if (initial_tb != NULL) { | 212 if (initial_tb != NULL) { |
| 213 if (*tb == NULL) | 213 if (*tb == NULL) |
| 214 *tb = initial_tb; | 214 *tb = initial_tb; |
| 215 else | 215 else |
| 216 Py_DECREF(initial_tb); | 216 Py_DECREF(initial_tb); |
| 217 } | 217 } |
| 218 /* normalize recursively */ | 218 /* normalize recursively */ |
| 219 tstate = PyThreadState_GET(); | 219 tstate = PyThreadState_GET(); |
| 220 if (++tstate->recursion_depth > Py_GetRecursionLimit()) { | 220 if (++tstate->recursion_depth > Py_GetRecursionLimit()) { |
| 221 --tstate->recursion_depth; | 221 --tstate->recursion_depth; |
| 222 /* throw away the old exception... */ | 222 /* throw away the old exception... */ |
| 223 Py_DECREF(*exc); | 223 Py_DECREF(*exc); |
| 224 Py_DECREF(*val); | 224 Py_DECREF(*val); |
| 225 /* ... and use the recursion error instead */ | 225 /* ... and use the recursion error instead */ |
| 226 *exc = PyExc_RuntimeError; | 226 *exc = PyExc_RuntimeError; |
| 227 *val = PyExc_RecursionErrorInst; | 227 *val = PyExc_RecursionErrorInst; |
| 228 Py_INCREF(*exc); | 228 Py_INCREF(*exc); |
| 229 Py_INCREF(*val); | 229 Py_INCREF(*val); |
| 230 /* just keeping the old traceback */ | 230 /* just keeping the old traceback */ |
| 231 return; | 231 return; |
| 232 } | 232 } |
| 233 PyErr_NormalizeException(exc, val, tb); | 233 PyErr_NormalizeException(exc, val, tb); |
| 234 --tstate->recursion_depth; | 234 --tstate->recursion_depth; |
| 235 } | 235 } |
| 236 | 236 |
| 237 | 237 |
| 238 void | 238 void |
| 239 PyErr_Fetch(PyObject **p_type, PyObject **p_value, PyObject **p_traceback) | 239 PyErr_Fetch(PyObject **p_type, PyObject **p_value, PyObject **p_traceback) |
| 240 { | 240 { |
| 241 PyThreadState *tstate = PyThreadState_GET(); | 241 PyThreadState *tstate = PyThreadState_GET(); |
| 242 | 242 |
| 243 *p_type = tstate->curexc_type; | 243 *p_type = tstate->curexc_type; |
| 244 *p_value = tstate->curexc_value; | 244 *p_value = tstate->curexc_value; |
| 245 *p_traceback = tstate->curexc_traceback; | 245 *p_traceback = tstate->curexc_traceback; |
| 246 | 246 |
| 247 tstate->curexc_type = NULL; | 247 tstate->curexc_type = NULL; |
| 248 tstate->curexc_value = NULL; | 248 tstate->curexc_value = NULL; |
| 249 tstate->curexc_traceback = NULL; | 249 tstate->curexc_traceback = NULL; |
| 250 } | 250 } |
| 251 | 251 |
| 252 void | 252 void |
| 253 PyErr_Clear(void) | 253 PyErr_Clear(void) |
| 254 { | 254 { |
| 255 PyErr_Restore(NULL, NULL, NULL); | 255 PyErr_Restore(NULL, NULL, NULL); |
| 256 } | 256 } |
| 257 | 257 |
| 258 /* Convenience functions to set a type error exception and return 0 */ | 258 /* Convenience functions to set a type error exception and return 0 */ |
| 259 | 259 |
| 260 int | 260 int |
| 261 PyErr_BadArgument(void) | 261 PyErr_BadArgument(void) |
| 262 { | 262 { |
| 263 PyErr_SetString(PyExc_TypeError, | 263 PyErr_SetString(PyExc_TypeError, |
| 264 "bad argument type for built-in operation"); | 264 "bad argument type for built-in operation"); |
| 265 return 0; | 265 return 0; |
| 266 } | 266 } |
| 267 | 267 |
| 268 PyObject * | 268 PyObject * |
| 269 PyErr_NoMemory(void) | 269 PyErr_NoMemory(void) |
| 270 { | 270 { |
| 271 if (PyErr_ExceptionMatches(PyExc_MemoryError)) | 271 if (PyErr_ExceptionMatches(PyExc_MemoryError)) |
| 272 /* already current */ | 272 /* already current */ |
| 273 return NULL; | 273 return NULL; |
| 274 | 274 |
| 275 /* raise the pre-allocated instance if it still exists */ | 275 /* raise the pre-allocated instance if it still exists */ |
| 276 if (PyExc_MemoryErrorInst) | 276 if (PyExc_MemoryErrorInst) |
| 277 PyErr_SetObject(PyExc_MemoryError, PyExc_MemoryErrorInst); | 277 PyErr_SetObject(PyExc_MemoryError, PyExc_MemoryErrorInst); |
| 278 else | 278 else |
| 279 /* this will probably fail since there's no memory and hee, | 279 /* this will probably fail since there's no memory and hee, |
| 280 hee, we have to instantiate this class | 280 hee, we have to instantiate this class |
| 281 */ | 281 */ |
| 282 PyErr_SetNone(PyExc_MemoryError); | 282 PyErr_SetNone(PyExc_MemoryError); |
| 283 | 283 |
| 284 return NULL; | 284 return NULL; |
| 285 } | 285 } |
| 286 | 286 |
| 287 PyObject * | 287 PyObject * |
| 288 PyErr_SetFromErrnoWithFilenameObject(PyObject *exc, PyObject *filenameObject) | 288 PyErr_SetFromErrnoWithFilenameObject(PyObject *exc, PyObject *filenameObject) |
| 289 { | 289 { |
| 290 PyObject *v; | 290 PyObject *v; |
| 291 char *s; | 291 char *s; |
| 292 int i = errno; | 292 int i = errno; |
| 293 #ifdef PLAN9 | 293 #ifdef PLAN9 |
| 294 char errbuf[ERRMAX]; | 294 char errbuf[ERRMAX]; |
| 295 #endif | 295 #endif |
| 296 #ifdef MS_WINDOWS | 296 #ifdef MS_WINDOWS |
| 297 char *s_buf = NULL; | 297 char *s_buf = NULL; |
| 298 char s_small_buf[28]; /* Room for "Windows Error 0xFFFFFFFF" */ | 298 char s_small_buf[28]; /* Room for "Windows Error 0xFFFFFFFF" */ |
| 299 #endif | 299 #endif |
| 300 #ifdef EINTR | 300 #ifdef EINTR |
| 301 if (i == EINTR && PyErr_CheckSignals()) | 301 if (i == EINTR && PyErr_CheckSignals()) |
| 302 return NULL; | 302 return NULL; |
| 303 #endif | 303 #endif |
| 304 #ifdef PLAN9 | 304 #ifdef PLAN9 |
| 305 rerrstr(errbuf, sizeof errbuf); | 305 rerrstr(errbuf, sizeof errbuf); |
| 306 s = errbuf; | 306 s = errbuf; |
| 307 #else | 307 #else |
| 308 if (i == 0) | 308 if (i == 0) |
| 309 s = "Error"; /* Sometimes errno didn't get set */ | 309 s = "Error"; /* Sometimes errno didn't get set */ |
| 310 else | 310 else |
| 311 #ifndef MS_WINDOWS | 311 #ifndef MS_WINDOWS |
| 312 s = strerror(i); | 312 s = strerror(i); |
| 313 #else | 313 #else |
| 314 { | 314 { |
| 315 /* Note that the Win32 errors do not lineup with the | 315 /* Note that the Win32 errors do not lineup with the |
| 316 errno error. So if the error is in the MSVC error | 316 errno error. So if the error is in the MSVC error |
| 317 table, we use it, otherwise we assume it really _is_ | 317 table, we use it, otherwise we assume it really _is_ |
| 318 a Win32 error code | 318 a Win32 error code |
| 319 */ | 319 */ |
| 320 if (i > 0 && i < _sys_nerr) { | 320 if (i > 0 && i < _sys_nerr) { |
| 321 s = _sys_errlist[i]; | 321 s = _sys_errlist[i]; |
| 322 } | 322 } |
| 323 else { | 323 else { |
| 324 int len = FormatMessage( | 324 int len = FormatMessage( |
| 325 FORMAT_MESSAGE_ALLOCATE_BUFFER | | 325 FORMAT_MESSAGE_ALLOCATE_BUFFER | |
| 326 FORMAT_MESSAGE_FROM_SYSTEM | | 326 FORMAT_MESSAGE_FROM_SYSTEM | |
| 327 FORMAT_MESSAGE_IGNORE_INSERTS, | 327 FORMAT_MESSAGE_IGNORE_INSERTS, |
| 328 NULL, /* no message source */ | 328 NULL, /* no message source */ |
| 329 i, | 329 i, |
| 330 MAKELANGID(LANG_NEUTRAL, | 330 MAKELANGID(LANG_NEUTRAL, |
| 331 SUBLANG_DEFAULT), | 331 SUBLANG_DEFAULT), |
| 332 /* Default language */ | 332 /* Default language */ |
| 333 (LPTSTR) &s_buf, | 333 (LPTSTR) &s_buf, |
| 334 0, /* size not used */ | 334 0, /* size not used */ |
| 335 NULL); /* no args */ | 335 NULL); /* no args */ |
| 336 if (len==0) { | 336 if (len==0) { |
| 337 /* Only ever seen this in out-of-mem | 337 /* Only ever seen this in out-of-mem |
| 338 situations */ | 338 situations */ |
| 339 sprintf(s_small_buf, "Windows Error 0x%X", i); | 339 sprintf(s_small_buf, "Windows Error 0x%X", i); |
| 340 s = s_small_buf; | 340 s = s_small_buf; |
| 341 s_buf = NULL; | 341 s_buf = NULL; |
| 342 } else { | 342 } else { |
| 343 s = s_buf; | 343 s = s_buf; |
| 344 /* remove trailing cr/lf and dots */ | 344 /* remove trailing cr/lf and dots */ |
| 345 while (len > 0 && (s[len-1] <= ' ' || s[len-1] == '.')) | 345 while (len > 0 && (s[len-1] <= ' ' || s[len-1] == '.')) |
| 346 s[--len] = '\0'; | 346 s[--len] = '\0'; |
| 347 } | 347 } |
| 348 } | 348 } |
| 349 } | 349 } |
| 350 #endif /* Unix/Windows */ | 350 #endif /* Unix/Windows */ |
| 351 #endif /* PLAN 9*/ | 351 #endif /* PLAN 9*/ |
| 352 if (filenameObject != NULL) | 352 if (filenameObject != NULL) |
| 353 v = Py_BuildValue("(isO)", i, s, filenameObject); | 353 v = Py_BuildValue("(isO)", i, s, filenameObject); |
| 354 else | 354 else |
| 355 v = Py_BuildValue("(is)", i, s); | 355 v = Py_BuildValue("(is)", i, s); |
| 356 if (v != NULL) { | 356 if (v != NULL) { |
| 357 PyErr_SetObject(exc, v); | 357 PyErr_SetObject(exc, v); |
| 358 Py_DECREF(v); | 358 Py_DECREF(v); |
| 359 } | 359 } |
| 360 #ifdef MS_WINDOWS | 360 #ifdef MS_WINDOWS |
| 361 LocalFree(s_buf); | 361 LocalFree(s_buf); |
| 362 #endif | 362 #endif |
| 363 return NULL; | 363 return NULL; |
| 364 } | 364 } |
| 365 | 365 |
| 366 | 366 |
| 367 PyObject * | 367 PyObject * |
| 368 PyErr_SetFromErrnoWithFilename(PyObject *exc, char *filename) | 368 PyErr_SetFromErrnoWithFilename(PyObject *exc, char *filename) |
| 369 { | 369 { |
| 370 PyObject *name = filename ? PyString_FromString(filename) : NULL; | 370 PyObject *name = filename ? PyString_FromString(filename) : NULL; |
| 371 PyObject *result = PyErr_SetFromErrnoWithFilenameObject(exc, name); | 371 PyObject *result = PyErr_SetFromErrnoWithFilenameObject(exc, name); |
| 372 Py_XDECREF(name); | 372 Py_XDECREF(name); |
| 373 return result; | 373 return result; |
| 374 } | 374 } |
| 375 | 375 |
| 376 #ifdef Py_WIN_WIDE_FILENAMES | 376 #ifdef Py_WIN_WIDE_FILENAMES |
| 377 PyObject * | 377 PyObject * |
| 378 PyErr_SetFromErrnoWithUnicodeFilename(PyObject *exc, Py_UNICODE *filename) | 378 PyErr_SetFromErrnoWithUnicodeFilename(PyObject *exc, Py_UNICODE *filename) |
| 379 { | 379 { |
| 380 PyObject *name = filename ? | 380 PyObject *name = filename ? |
| 381 PyUnicode_FromUnicode(filename, wcslen(filename)) : | 381 PyUnicode_FromUnicode(filename, wcslen(filename)) : |
| 382 NULL; | 382 NULL; |
| 383 PyObject *result = PyErr_SetFromErrnoWithFilenameObject(exc, name); | 383 PyObject *result = PyErr_SetFromErrnoWithFilenameObject(exc, name); |
| 384 Py_XDECREF(name); | 384 Py_XDECREF(name); |
| 385 return result; | 385 return result; |
| 386 } | 386 } |
| 387 #endif /* Py_WIN_WIDE_FILENAMES */ | 387 #endif /* Py_WIN_WIDE_FILENAMES */ |
| 388 | 388 |
| 389 PyObject * | 389 PyObject * |
| 390 PyErr_SetFromErrno(PyObject *exc) | 390 PyErr_SetFromErrno(PyObject *exc) |
| 391 { | 391 { |
| 392 return PyErr_SetFromErrnoWithFilenameObject(exc, NULL); | 392 return PyErr_SetFromErrnoWithFilenameObject(exc, NULL); |
| 393 } | 393 } |
| 394 | 394 |
| 395 #ifdef MS_WINDOWS | 395 #ifdef MS_WINDOWS |
| 396 /* Windows specific error code handling */ | 396 /* Windows specific error code handling */ |
| 397 PyObject *PyErr_SetExcFromWindowsErrWithFilenameObject( | 397 PyObject *PyErr_SetExcFromWindowsErrWithFilenameObject( |
| 398 PyObject *exc, | 398 PyObject *exc, |
| 399 int ierr, | 399 int ierr, |
| 400 PyObject *filenameObject) | 400 PyObject *filenameObject) |
| 401 { | 401 { |
| 402 int len; | 402 int len; |
| 403 char *s; | 403 char *s; |
| 404 char *s_buf = NULL; /* Free via LocalFree */ | 404 char *s_buf = NULL; /* Free via LocalFree */ |
| 405 char s_small_buf[28]; /* Room for "Windows Error 0xFFFFFFFF" */ | 405 char s_small_buf[28]; /* Room for "Windows Error 0xFFFFFFFF" */ |
| 406 PyObject *v; | 406 PyObject *v; |
| 407 DWORD err = (DWORD)ierr; | 407 DWORD err = (DWORD)ierr; |
| 408 if (err==0) err = GetLastError(); | 408 if (err==0) err = GetLastError(); |
| 409 len = FormatMessage( | 409 len = FormatMessage( |
| 410 /* Error API error */ | 410 /* Error API error */ |
| 411 FORMAT_MESSAGE_ALLOCATE_BUFFER | | 411 FORMAT_MESSAGE_ALLOCATE_BUFFER | |
| 412 FORMAT_MESSAGE_FROM_SYSTEM | | 412 FORMAT_MESSAGE_FROM_SYSTEM | |
| 413 FORMAT_MESSAGE_IGNORE_INSERTS, | 413 FORMAT_MESSAGE_IGNORE_INSERTS, |
| 414 NULL, /* no message source */ | 414 NULL, /* no message source */ |
| 415 err, | 415 err, |
| 416 MAKELANGID(LANG_NEUTRAL, | 416 MAKELANGID(LANG_NEUTRAL, |
| 417 SUBLANG_DEFAULT), /* Default language */ | 417 SUBLANG_DEFAULT), /* Default language */ |
| 418 (LPTSTR) &s_buf, | 418 (LPTSTR) &s_buf, |
| 419 0, /* size not used */ | 419 0, /* size not used */ |
| 420 NULL); /* no args */ | 420 NULL); /* no args */ |
| 421 if (len==0) { | 421 if (len==0) { |
| 422 /* Only seen this in out of mem situations */ | 422 /* Only seen this in out of mem situations */ |
| 423 sprintf(s_small_buf, "Windows Error 0x%X", err); | 423 sprintf(s_small_buf, "Windows Error 0x%X", err); |
| 424 s = s_small_buf; | 424 s = s_small_buf; |
| 425 s_buf = NULL; | 425 s_buf = NULL; |
| 426 } else { | 426 } else { |
| 427 s = s_buf; | 427 s = s_buf; |
| 428 /* remove trailing cr/lf and dots */ | 428 /* remove trailing cr/lf and dots */ |
| 429 while (len > 0 && (s[len-1] <= ' ' || s[len-1] == '.')) | 429 while (len > 0 && (s[len-1] <= ' ' || s[len-1] == '.')) |
| 430 s[--len] = '\0'; | 430 s[--len] = '\0'; |
| 431 } | 431 } |
| 432 if (filenameObject != NULL) | 432 if (filenameObject != NULL) |
| 433 v = Py_BuildValue("(isO)", err, s, filenameObject); | 433 v = Py_BuildValue("(isO)", err, s, filenameObject); |
| 434 else | 434 else |
| 435 v = Py_BuildValue("(is)", err, s); | 435 v = Py_BuildValue("(is)", err, s); |
| 436 if (v != NULL) { | 436 if (v != NULL) { |
| 437 PyErr_SetObject(exc, v); | 437 PyErr_SetObject(exc, v); |
| 438 Py_DECREF(v); | 438 Py_DECREF(v); |
| 439 } | 439 } |
| 440 LocalFree(s_buf); | 440 LocalFree(s_buf); |
| 441 return NULL; | 441 return NULL; |
| 442 } | 442 } |
| 443 | 443 |
| 444 PyObject *PyErr_SetExcFromWindowsErrWithFilename( | 444 PyObject *PyErr_SetExcFromWindowsErrWithFilename( |
| 445 PyObject *exc, | 445 PyObject *exc, |
| 446 int ierr, | 446 int ierr, |
| 447 const char *filename) | 447 const char *filename) |
| 448 { | 448 { |
| 449 PyObject *name = filename ? PyString_FromString(filename) : NULL; | 449 PyObject *name = filename ? PyString_FromString(filename) : NULL; |
| 450 PyObject *ret = PyErr_SetExcFromWindowsErrWithFilenameObject(exc, | 450 PyObject *ret = PyErr_SetExcFromWindowsErrWithFilenameObject(exc, |
| 451 ierr, | 451 ierr, |
| 452 name); | 452 name); |
| 453 Py_XDECREF(name); | 453 Py_XDECREF(name); |
| 454 return ret; | 454 return ret; |
| 455 } | 455 } |
| 456 | 456 |
| 457 #ifdef Py_WIN_WIDE_FILENAMES | 457 #ifdef Py_WIN_WIDE_FILENAMES |
| 458 PyObject *PyErr_SetExcFromWindowsErrWithUnicodeFilename( | 458 PyObject *PyErr_SetExcFromWindowsErrWithUnicodeFilename( |
| 459 PyObject *exc, | 459 PyObject *exc, |
| 460 int ierr, | 460 int ierr, |
| 461 const Py_UNICODE *filename) | 461 const Py_UNICODE *filename) |
| 462 { | 462 { |
| 463 PyObject *name = filename ? | 463 PyObject *name = filename ? |
| 464 PyUnicode_FromUnicode(filename, wcslen(filename)) : | 464 PyUnicode_FromUnicode(filename, wcslen(filename)) : |
| 465 NULL; | 465 NULL; |
| 466 PyObject *ret = PyErr_SetExcFromWindowsErrWithFilenameObject(exc, | 466 PyObject *ret = PyErr_SetExcFromWindowsErrWithFilenameObject(exc, |
| 467 ierr, | 467 ierr, |
| 468 name); | 468 name); |
| 469 Py_XDECREF(name); | 469 Py_XDECREF(name); |
| 470 return ret; | 470 return ret; |
| 471 } | 471 } |
| 472 #endif /* Py_WIN_WIDE_FILENAMES */ | 472 #endif /* Py_WIN_WIDE_FILENAMES */ |
| 473 | 473 |
| 474 PyObject *PyErr_SetExcFromWindowsErr(PyObject *exc, int ierr) | 474 PyObject *PyErr_SetExcFromWindowsErr(PyObject *exc, int ierr) |
| 475 { | 475 { |
| 476 return PyErr_SetExcFromWindowsErrWithFilename(exc, ierr, NULL); | 476 return PyErr_SetExcFromWindowsErrWithFilename(exc, ierr, NULL); |
| 477 } | 477 } |
| 478 | 478 |
| 479 PyObject *PyErr_SetFromWindowsErr(int ierr) | 479 PyObject *PyErr_SetFromWindowsErr(int ierr) |
| 480 { | 480 { |
| 481 return PyErr_SetExcFromWindowsErrWithFilename(PyExc_WindowsError, | 481 return PyErr_SetExcFromWindowsErrWithFilename(PyExc_WindowsError, |
| 482 ierr, NULL); | 482 ierr, NULL); |
| 483 } | 483 } |
| 484 PyObject *PyErr_SetFromWindowsErrWithFilename( | 484 PyObject *PyErr_SetFromWindowsErrWithFilename( |
| 485 int ierr, | 485 int ierr, |
| 486 const char *filename) | 486 const char *filename) |
| 487 { | 487 { |
| 488 PyObject *name = filename ? PyString_FromString(filename) : NULL; | 488 PyObject *name = filename ? PyString_FromString(filename) : NULL; |
| 489 PyObject *result = PyErr_SetExcFromWindowsErrWithFilenameObject( | 489 PyObject *result = PyErr_SetExcFromWindowsErrWithFilenameObject( |
| 490 PyExc_WindowsError, | 490 PyExc_WindowsError, |
| 491 ierr, name); | 491 ierr, name); |
| 492 Py_XDECREF(name); | 492 Py_XDECREF(name); |
| 493 return result; | 493 return result; |
| 494 } | 494 } |
| 495 | 495 |
| 496 #ifdef Py_WIN_WIDE_FILENAMES | 496 #ifdef Py_WIN_WIDE_FILENAMES |
| 497 PyObject *PyErr_SetFromWindowsErrWithUnicodeFilename( | 497 PyObject *PyErr_SetFromWindowsErrWithUnicodeFilename( |
| 498 int ierr, | 498 int ierr, |
| 499 const Py_UNICODE *filename) | 499 const Py_UNICODE *filename) |
| 500 { | 500 { |
| 501 PyObject *name = filename ? | 501 PyObject *name = filename ? |
| 502 PyUnicode_FromUnicode(filename, wcslen(filename)) : | 502 PyUnicode_FromUnicode(filename, wcslen(filename)) : |
| 503 NULL; | 503 NULL; |
| 504 PyObject *result = PyErr_SetExcFromWindowsErrWithFilenameObject( | 504 PyObject *result = PyErr_SetExcFromWindowsErrWithFilenameObject( |
| 505 PyExc_WindowsError, | 505 PyExc_WindowsError, |
| 506 ierr, name); | 506 ierr, name); |
| 507 Py_XDECREF(name); | 507 Py_XDECREF(name); |
| 508 return result; | 508 return result; |
| 509 } | 509 } |
| 510 #endif /* Py_WIN_WIDE_FILENAMES */ | 510 #endif /* Py_WIN_WIDE_FILENAMES */ |
| 511 #endif /* MS_WINDOWS */ | 511 #endif /* MS_WINDOWS */ |
| 512 | 512 |
| 513 void | 513 void |
| 514 _PyErr_BadInternalCall(char *filename, int lineno) | 514 _PyErr_BadInternalCall(char *filename, int lineno) |
| 515 { | 515 { |
| 516 PyErr_Format(PyExc_SystemError, | 516 PyErr_Format(PyExc_SystemError, |
| 517 "%s:%d: bad argument to internal function", | 517 "%s:%d: bad argument to internal function", |
| 518 filename, lineno); | 518 filename, lineno); |
| 519 } | 519 } |
| 520 | 520 |
| 521 /* Remove the preprocessor macro for PyErr_BadInternalCall() so that we can | 521 /* Remove the preprocessor macro for PyErr_BadInternalCall() so that we can |
| 522 export the entry point for existing object code: */ | 522 export the entry point for existing object code: */ |
| 523 #undef PyErr_BadInternalCall | 523 #undef PyErr_BadInternalCall |
| 524 void | 524 void |
| 525 PyErr_BadInternalCall(void) | 525 PyErr_BadInternalCall(void) |
| 526 { | 526 { |
| 527 PyErr_Format(PyExc_SystemError, | 527 PyErr_Format(PyExc_SystemError, |
| 528 "bad argument to internal function"); | 528 "bad argument to internal function"); |
| 529 } | 529 } |
| 530 #define PyErr_BadInternalCall() _PyErr_BadInternalCall(__FILE__, __LINE__) | 530 #define PyErr_BadInternalCall() _PyErr_BadInternalCall(__FILE__, __LINE__) |
| 531 | 531 |
| 532 | 532 |
| 533 | 533 |
| 534 PyObject * | 534 PyObject * |
| 535 PyErr_Format(PyObject *exception, const char *format, ...) | 535 PyErr_Format(PyObject *exception, const char *format, ...) |
| 536 { | 536 { |
| 537 va_list vargs; | 537 va_list vargs; |
| 538 PyObject* string; | 538 PyObject* string; |
| 539 | 539 |
| 540 #ifdef HAVE_STDARG_PROTOTYPES | 540 #ifdef HAVE_STDARG_PROTOTYPES |
| 541 va_start(vargs, format); | 541 va_start(vargs, format); |
| 542 #else | 542 #else |
| 543 va_start(vargs); | 543 va_start(vargs); |
| 544 #endif | 544 #endif |
| 545 | 545 |
| 546 string = PyString_FromFormatV(format, vargs); | 546 string = PyString_FromFormatV(format, vargs); |
| 547 PyErr_SetObject(exception, string); | 547 PyErr_SetObject(exception, string); |
| 548 Py_XDECREF(string); | 548 Py_XDECREF(string); |
| 549 va_end(vargs); | 549 va_end(vargs); |
| 550 return NULL; | 550 return NULL; |
| 551 } | 551 } |
| 552 | 552 |
| 553 | 553 |
| 554 | 554 |
| 555 PyObject * | 555 PyObject * |
| 556 PyErr_NewException(char *name, PyObject *base, PyObject *dict) | 556 PyErr_NewException(char *name, PyObject *base, PyObject *dict) |
| 557 { | 557 { |
| 558 char *dot; | 558 char *dot; |
| 559 PyObject *modulename = NULL; | 559 PyObject *modulename = NULL; |
| 560 PyObject *classname = NULL; | 560 PyObject *classname = NULL; |
| 561 PyObject *mydict = NULL; | 561 PyObject *mydict = NULL; |
| 562 PyObject *bases = NULL; | 562 PyObject *bases = NULL; |
| 563 PyObject *result = NULL; | 563 PyObject *result = NULL; |
| 564 dot = strrchr(name, '.'); | 564 dot = strrchr(name, '.'); |
| 565 if (dot == NULL) { | 565 if (dot == NULL) { |
| 566 PyErr_SetString(PyExc_SystemError, | 566 PyErr_SetString(PyExc_SystemError, |
| 567 "PyErr_NewException: name must be module.class"); | 567 "PyErr_NewException: name must be module.class"); |
| 568 return NULL; | 568 return NULL; |
| 569 } | 569 } |
| 570 if (base == NULL) | 570 if (base == NULL) |
| 571 base = PyExc_Exception; | 571 base = PyExc_Exception; |
| 572 if (dict == NULL) { | 572 if (dict == NULL) { |
| 573 dict = mydict = PyDict_New(); | 573 dict = mydict = PyDict_New(); |
| 574 if (dict == NULL) | 574 if (dict == NULL) |
| 575 goto failure; | 575 goto failure; |
| 576 } | 576 } |
| 577 if (PyDict_GetItemString(dict, "__module__") == NULL) { | 577 if (PyDict_GetItemString(dict, "__module__") == NULL) { |
| 578 modulename = PyString_FromStringAndSize(name, | 578 modulename = PyString_FromStringAndSize(name, |
| 579 (Py_ssize_t)(dot-name)); | 579 (Py_ssize_t)(dot-name)); |
| 580 if (modulename == NULL) | 580 if (modulename == NULL) |
| 581 goto failure; | 581 goto failure; |
| 582 if (PyDict_SetItemString(dict, "__module__", modulename) != 0) | 582 if (PyDict_SetItemString(dict, "__module__", modulename) != 0) |
| 583 goto failure; | 583 goto failure; |
| 584 } | 584 } |
| 585 if (PyTuple_Check(base)) { | 585 if (PyTuple_Check(base)) { |
| 586 bases = base; | 586 bases = base; |
| 587 /* INCREF as we create a new ref in the else branch */ | 587 /* INCREF as we create a new ref in the else branch */ |
| 588 Py_INCREF(bases); | 588 Py_INCREF(bases); |
| 589 } else { | 589 } else { |
| 590 bases = PyTuple_Pack(1, base); | 590 bases = PyTuple_Pack(1, base); |
| 591 if (bases == NULL) | 591 if (bases == NULL) |
| 592 goto failure; | 592 goto failure; |
| 593 } | 593 } |
| 594 /* Create a real new-style class. */ | 594 /* Create a real new-style class. */ |
| 595 result = PyObject_CallFunction((PyObject *)&PyType_Type, "sOO", | 595 result = PyObject_CallFunction((PyObject *)&PyType_Type, "sOO", |
| 596 dot+1, bases, dict); | 596 dot+1, bases, dict); |
| 597 failure: | 597 failure: |
| 598 Py_XDECREF(bases); | 598 Py_XDECREF(bases); |
| 599 Py_XDECREF(mydict); | 599 Py_XDECREF(mydict); |
| 600 Py_XDECREF(classname); | 600 Py_XDECREF(classname); |
| 601 Py_XDECREF(modulename); | 601 Py_XDECREF(modulename); |
| 602 return result; | 602 return result; |
| 603 } | 603 } |
| 604 | 604 |
| 605 /* Call when an exception has occurred but there is no way for Python | 605 /* Call when an exception has occurred but there is no way for Python |
| 606 to handle it. Examples: exception in __del__ or during GC. */ | 606 to handle it. Examples: exception in __del__ or during GC. */ |
| 607 void | 607 void |
| 608 PyErr_WriteUnraisable(PyObject *obj) | 608 PyErr_WriteUnraisable(PyObject *obj) |
| 609 { | 609 { |
| 610 PyObject *f, *t, *v, *tb; | 610 PyObject *f, *t, *v, *tb; |
| 611 PyErr_Fetch(&t, &v, &tb); | 611 PyErr_Fetch(&t, &v, &tb); |
| 612 f = PySys_GetObject("stderr"); | 612 f = PySys_GetObject("stderr"); |
| 613 if (f != NULL) { | 613 if (f != NULL) { |
| 614 PyFile_WriteString("Exception ", f); | 614 PyFile_WriteString("Exception ", f); |
| 615 if (t) { | 615 if (t) { |
| 616 PyObject* moduleName; | 616 PyObject* moduleName; |
| 617 char* className; | 617 char* className; |
| 618 assert(PyExceptionClass_Check(t)); | 618 assert(PyExceptionClass_Check(t)); |
| 619 className = PyExceptionClass_Name(t); | 619 className = PyExceptionClass_Name(t); |
| 620 if (className != NULL) { | 620 if (className != NULL) { |
| 621 char *dot = strrchr(className, '.'); | 621 char *dot = strrchr(className, '.'); |
| 622 if (dot != NULL) | 622 if (dot != NULL) |
| 623 className = dot+1; | 623 className = dot+1; |
| 624 } | 624 } |
| 625 | 625 |
| 626 moduleName = PyObject_GetAttrString(t, "__module__"); | 626 moduleName = PyObject_GetAttrString(t, "__module__"); |
| 627 if (moduleName == NULL) | 627 if (moduleName == NULL) |
| 628 PyFile_WriteString("<unknown>", f); | 628 PyFile_WriteString("<unknown>", f); |
| 629 else { | 629 else { |
| 630 char* modstr = PyString_AsString(moduleName); | 630 char* modstr = PyString_AsString(moduleName); |
| 631 if (modstr && | 631 if (modstr && |
| 632 strcmp(modstr, "exceptions") != 0) | 632 strcmp(modstr, "exceptions") != 0) |
| 633 { | 633 { |
| 634 PyFile_WriteString(modstr, f); | 634 PyFile_WriteString(modstr, f); |
| 635 PyFile_WriteString(".", f); | 635 PyFile_WriteString(".", f); |
| 636 } | 636 } |
| 637 } | 637 } |
| 638 if (className == NULL) | 638 if (className == NULL) |
| 639 PyFile_WriteString("<unknown>", f); | 639 PyFile_WriteString("<unknown>", f); |
| 640 else | 640 else |
| 641 PyFile_WriteString(className, f); | 641 PyFile_WriteString(className, f); |
| 642 if (v && v != Py_None) { | 642 if (v && v != Py_None) { |
| 643 PyFile_WriteString(": ", f); | 643 PyFile_WriteString(": ", f); |
| 644 PyFile_WriteObject(v, f, 0); | 644 PyFile_WriteObject(v, f, 0); |
| 645 } | 645 } |
| 646 Py_XDECREF(moduleName); | 646 Py_XDECREF(moduleName); |
| 647 } | 647 } |
| 648 PyFile_WriteString(" in ", f); | 648 PyFile_WriteString(" in ", f); |
| 649 PyFile_WriteObject(obj, f, 0); | 649 PyFile_WriteObject(obj, f, 0); |
| 650 PyFile_WriteString(" ignored\n", f); | 650 PyFile_WriteString(" ignored\n", f); |
| 651 PyErr_Clear(); /* Just in case */ | 651 PyErr_Clear(); /* Just in case */ |
| 652 } | 652 } |
| 653 Py_XDECREF(t); | 653 Py_XDECREF(t); |
| 654 Py_XDECREF(v); | 654 Py_XDECREF(v); |
| 655 Py_XDECREF(tb); | 655 Py_XDECREF(tb); |
| 656 } | 656 } |
| 657 | 657 |
| 658 extern PyObject *PyModule_GetWarningsModule(void); | 658 extern PyObject *PyModule_GetWarningsModule(void); |
| 659 | 659 |
| 660 | 660 |
| 663 to make printing of exceptions believe it is a syntax error. */ | 663 to make printing of exceptions believe it is a syntax error. */ |
| 664 | 664 |
| 665 void | 665 void |
| 666 PyErr_SyntaxLocation(const char *filename, int lineno) | 666 PyErr_SyntaxLocation(const char *filename, int lineno) |
| 667 { | 667 { |
| 668 PyObject *exc, *v, *tb, *tmp; | 668 PyObject *exc, *v, *tb, *tmp; |
| 669 | 669 |
| 670 /* add attributes for the line number and filename for the error */ | 670 /* add attributes for the line number and filename for the error */ |
| 671 PyErr_Fetch(&exc, &v, &tb); | 671 PyErr_Fetch(&exc, &v, &tb); |
| 672 PyErr_NormalizeException(&exc, &v, &tb); | 672 PyErr_NormalizeException(&exc, &v, &tb); |
| 673 /* XXX check that it is, indeed, a syntax error. It might not | 673 /* XXX check that it is, indeed, a syntax error. It might not |
| 674 * be, though. */ | 674 * be, though. */ |
| 675 tmp = PyInt_FromLong(lineno); | 675 tmp = PyInt_FromLong(lineno); |
| 676 if (tmp == NULL) | 676 if (tmp == NULL) |
| 677 PyErr_Clear(); | 677 PyErr_Clear(); |
| 678 else { | 678 else { |
| 679 if (PyObject_SetAttrString(v, "lineno", tmp)) | 679 if (PyObject_SetAttrString(v, "lineno", tmp)) |
| 680 PyErr_Clear(); | 680 PyErr_Clear(); |
| 681 Py_DECREF(tmp); | 681 Py_DECREF(tmp); |
| 682 } | 682 } |
| 683 if (filename != NULL) { | 683 if (filename != NULL) { |
| 684 tmp = PyString_FromString(filename); | 684 tmp = PyString_FromString(filename); |
| 685 if (tmp == NULL) | 685 if (tmp == NULL) |
| 686 PyErr_Clear(); | 686 PyErr_Clear(); |
| 687 else { | 687 else { |
| 688 if (PyObject_SetAttrString(v, "filename", tmp)) | 688 if (PyObject_SetAttrString(v, "filename", tmp)) |
| 689 PyErr_Clear(); | 689 PyErr_Clear(); |
| 690 Py_DECREF(tmp); | 690 Py_DECREF(tmp); |
| 691 } | 691 } |
| 692 | 692 |
| 693 tmp = PyErr_ProgramText(filename, lineno); | 693 tmp = PyErr_ProgramText(filename, lineno); |
| 694 if (tmp) { | 694 if (tmp) { |
| 695 if (PyObject_SetAttrString(v, "text", tmp)) | 695 if (PyObject_SetAttrString(v, "text", tmp)) |
| 696 PyErr_Clear(); | 696 PyErr_Clear(); |
| 697 Py_DECREF(tmp); | 697 Py_DECREF(tmp); |
| 698 } | 698 } |
| 699 } | 699 } |
| 700 if (PyObject_SetAttrString(v, "offset", Py_None)) { | 700 if (PyObject_SetAttrString(v, "offset", Py_None)) { |
| 701 PyErr_Clear(); | 701 PyErr_Clear(); |
| 702 } | 702 } |
| 703 if (exc != PyExc_SyntaxError) { | 703 if (exc != PyExc_SyntaxError) { |
| 704 if (!PyObject_HasAttrString(v, "msg")) { | 704 if (!PyObject_HasAttrString(v, "msg")) { |
| 705 tmp = PyObject_Str(v); | 705 tmp = PyObject_Str(v); |
| 706 if (tmp) { | 706 if (tmp) { |
| 707 if (PyObject_SetAttrString(v, "msg", tmp)) | 707 if (PyObject_SetAttrString(v, "msg", tmp)) |
| 708 PyErr_Clear(); | 708 PyErr_Clear(); |
| 709 Py_DECREF(tmp); | 709 Py_DECREF(tmp); |
| 710 } else { | 710 } else { |
| 711 PyErr_Clear(); | 711 PyErr_Clear(); |
| 712 } | 712 } |
| 713 } | 713 } |
| 714 if (!PyObject_HasAttrString(v, "print_file_and_line")) { | 714 if (!PyObject_HasAttrString(v, "print_file_and_line")) { |
| 715 if (PyObject_SetAttrString(v, "print_file_and_line", | 715 if (PyObject_SetAttrString(v, "print_file_and_line", |
| 716 Py_None)) | 716 Py_None)) |
| 717 PyErr_Clear(); | 717 PyErr_Clear(); |
| 718 } | 718 } |
| 719 } | 719 } |
| 720 PyErr_Restore(exc, v, tb); | 720 PyErr_Restore(exc, v, tb); |
| 721 } | 721 } |
| 722 | 722 |
| 723 /* com_fetch_program_text will attempt to load the line of text that | 723 /* com_fetch_program_text will attempt to load the line of text that |
| 724 the exception refers to. If it fails, it will return NULL but will | 724 the exception refers to. If it fails, it will return NULL but will |
| 725 not set an exception. | 725 not set an exception. |
| 729 */ | 729 */ |
| 730 | 730 |
| 731 PyObject * | 731 PyObject * |
| 732 PyErr_ProgramText(const char *filename, int lineno) | 732 PyErr_ProgramText(const char *filename, int lineno) |
| 733 { | 733 { |
| 734 FILE *fp; | 734 FILE *fp; |
| 735 int i; | 735 int i; |
| 736 char linebuf[1000]; | 736 char linebuf[1000]; |
| 737 | 737 |
| 738 if (filename == NULL || *filename == '\0' || lineno <= 0) | 738 if (filename == NULL || *filename == '\0' || lineno <= 0) |
| 739 return NULL; | 739 return NULL; |
| 740 fp = fopen(filename, "r" PY_STDIOTEXTMODE); | 740 fp = fopen(filename, "r" PY_STDIOTEXTMODE); |
| 741 if (fp == NULL) | 741 if (fp == NULL) |
| 742 return NULL; | 742 return NULL; |
| 743 for (i = 0; i < lineno; i++) { | 743 for (i = 0; i < lineno; i++) { |
| 744 char *pLastChar = &linebuf[sizeof(linebuf) - 2]; | 744 char *pLastChar = &linebuf[sizeof(linebuf) - 2]; |
| 745 do { | 745 do { |
| 746 *pLastChar = '\0'; | 746 *pLastChar = '\0'; |
| 747 if (Py_UniversalNewlineFgets(linebuf, sizeof linebuf, fp, NULL) == NULL) | 747 if (Py_UniversalNewlineFgets(linebuf, sizeof linebuf, fp, NULL) == NULL) |
| 748 break; | 748 break; |
| 749 /* fgets read *something*; if it didn't get as | 749 /* fgets read *something*; if it didn't get as |
| 750 far as pLastChar, it must have found a newline | 750 far as pLastChar, it must have found a newline |
| 751 or hit the end of the file; if pLastChar is \n, | 751 or hit the end of the file; if pLastChar is \n, |
| 752 it obviously found a newline; else we haven't | 752 it obviously found a newline; else we haven't |
| 753 yet seen a newline, so must continue */ | 753 yet seen a newline, so must continue */ |
| 754 } while (*pLastChar != '\0' && *pLastChar != '\n'); | 754 } while (*pLastChar != '\0' && *pLastChar != '\n'); |
| 755 } | 755 } |
| 756 fclose(fp); | 756 fclose(fp); |
| 757 if (i == lineno) { | 757 if (i == lineno) { |
| 758 char *p = linebuf; | 758 char *p = linebuf; |
| 759 while (*p == ' ' || *p == '\t' || *p == '\014') | 759 while (*p == ' ' || *p == '\t' || *p == '\014') |
| 760 p++; | 760 p++; |
| 761 return PyString_FromString(p); | 761 return PyString_FromString(p); |
| 762 } | 762 } |
| 763 return NULL; | 763 return NULL; |
| 764 } | 764 } |
| 765 | 765 |
| 766 #ifdef __cplusplus | 766 #ifdef __cplusplus |
| 767 } | 767 } |
| 768 #endif | 768 #endif |
