Changeset 391 for python/trunk/Python/_warnings.c
- Timestamp:
- Mar 19, 2014, 11:31:01 PM (11 years ago)
- Location:
- python/trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
python/trunk
-
Property svn:mergeinfo
set to
/python/vendor/Python-2.7.6 merged eligible /python/vendor/current merged eligible
-
Property svn:mergeinfo
set to
-
python/trunk/Python/_warnings.c
r2 r391 1 1 #include "Python.h" 2 #include "code.h" /* For DeprecationWarning about adding 'line'. */3 2 #include "frameobject.h" 4 3 5 4 #define MODULE_NAME "_warnings" 6 #define DEFAULT_ACTION_NAME "default_action"7 5 8 6 PyDoc_STRVAR(warnings__doc__, … … 14 12 static PyObject *_filters; /* List */ 15 13 static PyObject *_once_registry; /* Dict */ 14 static PyObject *_default_action; /* String */ 16 15 17 16 … … 80 79 81 80 81 static PyObject * 82 get_default_action(void) 83 { 84 PyObject *default_action; 85 86 default_action = get_warnings_attr("defaultaction"); 87 if (default_action == NULL) { 88 if (PyErr_Occurred()) { 89 return NULL; 90 } 91 return _default_action; 92 } 93 94 Py_DECREF(_default_action); 95 _default_action = default_action; 96 return default_action; 97 } 98 99 82 100 /* The item is a borrowed reference. */ 83 101 static const char * … … 85 103 PyObject *module, PyObject **item) 86 104 { 87 PyObject *action , *m, *d;105 PyObject *action; 88 106 Py_ssize_t i; 89 107 PyObject *warnings_filters; … … 137 155 } 138 156 139 m = PyImport_ImportModule(MODULE_NAME); 140 if (m == NULL) 141 return NULL; 142 d = PyModule_GetDict(m); 143 Py_DECREF(m); 144 if (d == NULL) 145 return NULL; 146 action = PyDict_GetItemString(d, DEFAULT_ACTION_NAME); 147 if (action != NULL) 157 action = get_default_action(); 158 if (action != NULL) { 148 159 return PyString_AsString(action); 160 } 149 161 150 162 PyErr_SetString(PyExc_ValueError, 151 MODULE_NAME ". " DEFAULT_ACTION_NAME "not found");163 MODULE_NAME ".defaultaction not found"); 152 164 return NULL; 153 165 } 166 154 167 155 168 static int … … 190 203 mod_str = PyString_AsString(filename); 191 204 if (mod_str == NULL) 192 205 return NULL; 193 206 len = PyString_Size(filename); 194 207 if (len < 0) 195 208 return NULL; 196 209 if (len >= 3 && 197 210 strncmp(mod_str + (len - 3), ".py", 3) == 0) { 198 211 module = PyString_FromStringAndSize(mod_str, len-3); 199 212 } … … 239 252 name = PyObject_GetAttrString(category, "__name__"); 240 253 if (name == NULL) /* XXX Can an object lack a '__name__' attribute? */ 241 254 return; 242 255 243 256 f_stderr = PySys_GetObject("stderr"); … … 268 281 } 269 282 else 270 _Py_DisplaySourceLine(f_stderr, PyString_AS_STRING(filename), 283 _Py_DisplaySourceLine(f_stderr, PyString_AS_STRING(filename), 271 284 lineno, 2); 272 285 PyErr_Clear(); … … 282 295 const char *action; 283 296 int rc; 284 297 285 298 if (registry && !PyDict_Check(registry) && (registry != Py_None)) { 286 299 PyErr_SetString(PyExc_TypeError, "'registry' must be a dict"); … … 329 342 if (rc == -1) 330 343 goto cleanup; 331 344 else if (rc == 1) 332 345 goto return_none; 333 346 /* Else this warning hasn't been generated before. */ … … 390 403 } 391 404 else { 392 const char *msg = "functions overriding warnings.showwarning() " 393 "must support the 'line' argument"; 394 const char *text_char = PyString_AS_STRING(text); 395 396 if (strcmp(msg, text_char) == 0) { 397 /* Prevent infinite recursion by using built-in implementation 398 of showwarning(). */ 399 show_warning(filename, lineno, text, category, sourceline); 400 } 401 else { 402 PyObject *check_fxn; 403 PyObject *defaults; 404 PyObject *res; 405 406 if (PyMethod_Check(show_fxn)) 407 check_fxn = PyMethod_Function(show_fxn); 408 else if (PyFunction_Check(show_fxn)) 409 check_fxn = show_fxn; 410 else { 411 PyErr_SetString(PyExc_TypeError, 412 "warnings.showwarning() must be set to a " 413 "function or method"); 414 Py_DECREF(show_fxn); 415 goto cleanup; 416 } 417 418 defaults = PyFunction_GetDefaults(check_fxn); 419 /* A proper implementation of warnings.showwarning() should 420 have at least two default arguments. */ 421 if ((defaults == NULL) || (PyTuple_Size(defaults) < 2)) { 422 PyCodeObject *code = (PyCodeObject *) 423 PyFunction_GetCode(check_fxn); 424 if (!(code->co_flags & CO_VARARGS)) { 425 if (PyErr_WarnEx(PyExc_DeprecationWarning, msg, 1) < 426 0) { 427 Py_DECREF(show_fxn); 428 goto cleanup; 429 } 430 } 431 } 432 res = PyObject_CallFunctionObjArgs(show_fxn, message, category, 433 filename, lineno_obj, 434 NULL); 435 Py_DECREF(show_fxn); 436 Py_XDECREF(res); 437 if (res == NULL) 438 goto cleanup; 439 } 405 PyObject *res; 406 407 if (!PyMethod_Check(show_fxn) && !PyFunction_Check(show_fxn)) { 408 PyErr_SetString(PyExc_TypeError, 409 "warnings.showwarning() must be set to a " 410 "function or method"); 411 Py_DECREF(show_fxn); 412 goto cleanup; 413 } 414 415 res = PyObject_CallFunctionObjArgs(show_fxn, message, category, 416 filename, lineno_obj, 417 NULL); 418 Py_DECREF(show_fxn); 419 Py_XDECREF(res); 420 if (res == NULL) 421 goto cleanup; 440 422 } 441 423 } … … 475 457 else { 476 458 globals = f->f_globals; 477 *lineno = Py Code_Addr2Line(f->f_code, f->f_lasti);459 *lineno = PyFrame_GetLineNumber(f); 478 460 } 479 461 … … 510 492 /* Setup filename. */ 511 493 *filename = PyDict_GetItemString(globals, "__file__"); 512 if (*filename != NULL ) {513 494 if (*filename != NULL && PyString_Check(*filename)) { 495 Py_ssize_t len = PyString_Size(*filename); 514 496 const char *file_str = PyString_AsString(*filename); 515 497 if (file_str == NULL || (len < 0 && PyErr_Occurred())) 516 498 goto handle_error; 517 499 … … 525 507 { 526 508 *filename = PyString_FromStringAndSize(file_str, len-1); 527 528 529 530 509 if (*filename == NULL) 510 goto handle_error; 511 } 512 else 531 513 Py_INCREF(*filename); 532 514 } 533 515 else { 534 516 const char *module_str = PyString_AsString(*module); 517 *filename = NULL; 535 518 if (module_str && strcmp(module_str, "__main__") == 0) { 536 519 PyObject *argv = PySys_GetObject("argv"); … … 555 538 /* embedded interpreters don't have sys.argv, see bug #839151 */ 556 539 *filename = PyString_FromString("__main__"); 557 558 540 if (*filename == NULL) 541 goto handle_error; 559 542 } 560 543 } … … 744 727 } 745 728 746 /* PyErr_Warn is only for backwards compat ability and will be removed.729 /* PyErr_Warn is only for backwards compatibility and will be removed. 747 730 Use PyErr_WarnEx instead. */ 748 731 … … 858 841 init_filters(void) 859 842 { 860 PyObject *filters = PyList_New(3); 843 /* Don't silence DeprecationWarning if -3 or -Q were used. */ 844 PyObject *filters = PyList_New(Py_Py3kWarningFlag || 845 Py_DivisionWarningFlag ? 3 : 4); 846 unsigned int pos = 0; /* Post-incremented in each use. */ 847 unsigned int x; 861 848 const char *bytes_action; 849 862 850 if (filters == NULL) 863 851 return NULL; 864 852 865 PyList_SET_ITEM(filters, 0, 853 /* If guard changes, make sure to update 'filters' initialization above. */ 854 if (!Py_Py3kWarningFlag && !Py_DivisionWarningFlag) { 855 PyList_SET_ITEM(filters, pos++, 856 create_filter(PyExc_DeprecationWarning, "ignore")); 857 } 858 PyList_SET_ITEM(filters, pos++, 866 859 create_filter(PyExc_PendingDeprecationWarning, "ignore")); 867 PyList_SET_ITEM(filters, 1, create_filter(PyExc_ImportWarning, "ignore")); 860 PyList_SET_ITEM(filters, pos++, 861 create_filter(PyExc_ImportWarning, "ignore")); 868 862 if (Py_BytesWarningFlag > 1) 869 863 bytes_action = "error"; … … 872 866 else 873 867 bytes_action = "ignore"; 874 PyList_SET_ITEM(filters, 2, create_filter(PyExc_BytesWarning,868 PyList_SET_ITEM(filters, pos++, create_filter(PyExc_BytesWarning, 875 869 bytes_action)); 876 870 877 if (PyList_GET_ITEM(filters, 0) == NULL ||878 PyList_GET_ITEM(filters, 1) == NULL ||879 PyList_GET_ITEM(filters, 2) == NULL) {880 Py_DECREF(filters);881 return NULL;871 for (x = 0; x < pos; x += 1) { 872 if (PyList_GET_ITEM(filters, x) == NULL) { 873 Py_DECREF(filters); 874 return NULL; 875 } 882 876 } 883 877 … … 889 883 _PyWarnings_Init(void) 890 884 { 891 PyObject *m , *default_action;885 PyObject *m; 892 886 893 887 m = Py_InitModule3(MODULE_NAME, warnings_functions, warnings__doc__); … … 909 903 return; 910 904 911 default_action = PyString_InternFromString("default");912 if ( default_action == NULL)905 _default_action = PyString_FromString("default"); 906 if (_default_action == NULL) 913 907 return; 914 if (PyModule_AddObject(m, DEFAULT_ACTION_NAME,default_action) < 0)908 if (PyModule_AddObject(m, "default_action", _default_action) < 0) 915 909 return; 916 910 }
Note:
See TracChangeset
for help on using the changeset viewer.