Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(84786)

Delta Between Two Patch Sets: Python/_warnings.c

Issue 11619: On Windows, don't encode filenames in the import machinery
Left Patch Set: Created 8 years, 4 months ago
Right Patch Set: Created 5 years, 12 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « Python/symtable.c ('k') | no next file » | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 #include "Python.h" 1 #include "Python.h"
2 #include "frameobject.h" 2 #include "frameobject.h"
3 3
4 #define MODULE_NAME "_warnings" 4 #define MODULE_NAME "_warnings"
5 5
6 PyDoc_STRVAR(warnings__doc__, 6 PyDoc_STRVAR(warnings__doc__,
7 MODULE_NAME " provides basic warning filtering support.\n" 7 MODULE_NAME " provides basic warning filtering support.\n"
8 "It is a helper module to speed up interpreter start-up."); 8 "It is a helper module to speed up interpreter start-up.");
9 9
10 /* Both 'filters' and 'onceregistry' can be set in warnings.py; 10 /* Both 'filters' and 'onceregistry' can be set in warnings.py;
11 get_warnings_attr() will reset these variables accordingly. */ 11 get_warnings_attr() will reset these variables accordingly. */
12 static PyObject *_filters; /* List */ 12 static PyObject *_filters; /* List */
13 static PyObject *_once_registry; /* Dict */ 13 static PyObject *_once_registry; /* Dict */
14 static PyObject *_default_action; /* String */ 14 static PyObject *_default_action; /* String */
15 15
16 16
17 static int 17 static int
18 check_matched(PyObject *obj, PyObject *arg) 18 check_matched(PyObject *obj, PyObject *arg)
19 { 19 {
20 PyObject *result; 20 PyObject *result;
21 _Py_IDENTIFIER(match);
21 int rc; 22 int rc;
22 23
23 if (obj == Py_None) 24 if (obj == Py_None)
24 return 1; 25 return 1;
25 result = PyObject_CallMethod(obj, "match", "O", arg); 26 result = _PyObject_CallMethodId(obj, &PyId_match, "O", arg);
26 if (result == NULL) 27 if (result == NULL)
27 return -1; 28 return -1;
28 29
29 rc = PyObject_IsTrue(result); 30 rc = PyObject_IsTrue(result);
30 Py_DECREF(result); 31 Py_DECREF(result);
31 return rc; 32 return rc;
32 } 33 }
33 34
34 /* 35 /*
35 Returns a new reference. 36 Returns a new reference.
(...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after
195 Py_ssize_t len; 196 Py_ssize_t len;
196 197
197 int rc = PyObject_IsTrue(filename); 198 int rc = PyObject_IsTrue(filename);
198 if (rc == -1) 199 if (rc == -1)
199 return NULL; 200 return NULL;
200 else if (rc == 0) 201 else if (rc == 0)
201 return PyUnicode_FromString("<unknown>"); 202 return PyUnicode_FromString("<unknown>");
202 203
203 mod_str = _PyUnicode_AsString(filename); 204 mod_str = _PyUnicode_AsString(filename);
204 if (mod_str == NULL) 205 if (mod_str == NULL)
205 return NULL; 206 return NULL;
206 len = PyUnicode_GetSize(filename); 207 len = PyUnicode_GetLength(filename);
207 if (len < 0) 208 if (len < 0)
208 return NULL; 209 return NULL;
209 if (len >= 3 && 210 if (len >= 3 &&
210 strncmp(mod_str + (len - 3), ".py", 3) == 0) { 211 strncmp(mod_str + (len - 3), ".py", 3) == 0) {
211 module = PyUnicode_FromStringAndSize(mod_str, len-3); 212 module = PyUnicode_Substring(filename, 0, len-3);
212 } 213 }
213 else { 214 else {
214 module = filename; 215 module = filename;
215 Py_INCREF(module); 216 Py_INCREF(module);
216 } 217 }
217 return module; 218 return module;
218 } 219 }
219 220
220 static int 221 static int
221 update_registry(PyObject *registry, PyObject *text, PyObject *category, 222 update_registry(PyObject *registry, PyObject *text, PyObject *category,
(...skipping 17 matching lines...) Expand all
239 return rc; 240 return rc;
240 } 241 }
241 242
242 static void 243 static void
243 show_warning(PyObject *filename, int lineno, PyObject *text, PyObject 244 show_warning(PyObject *filename, int lineno, PyObject *text, PyObject
244 *category, PyObject *sourceline) 245 *category, PyObject *sourceline)
245 { 246 {
246 PyObject *f_stderr; 247 PyObject *f_stderr;
247 PyObject *name; 248 PyObject *name;
248 char lineno_str[128]; 249 char lineno_str[128];
250 _Py_IDENTIFIER(__name__);
249 251
250 PyOS_snprintf(lineno_str, sizeof(lineno_str), ":%d: ", lineno); 252 PyOS_snprintf(lineno_str, sizeof(lineno_str), ":%d: ", lineno);
251 253
252 name = PyObject_GetAttrString(category, "__name__"); 254 name = _PyObject_GetAttrId(category, &PyId___name__);
253 if (name == NULL) /* XXX Can an object lack a '__name__' attribute? */ 255 if (name == NULL) /* XXX Can an object lack a '__name__' attribute? */
254 return; 256 return;
255 257
256 f_stderr = PySys_GetObject("stderr"); 258 f_stderr = PySys_GetObject("stderr");
257 if (f_stderr == NULL) { 259 if (f_stderr == NULL) {
258 fprintf(stderr, "lost sys.stderr\n"); 260 fprintf(stderr, "lost sys.stderr\n");
259 Py_DECREF(name); 261 Py_DECREF(name);
260 return; 262 return;
261 } 263 }
262 264
(...skipping 11 matching lines...) Expand all
274 char *source_line_str = _PyUnicode_AsString(sourceline); 276 char *source_line_str = _PyUnicode_AsString(sourceline);
275 if (source_line_str == NULL) 277 if (source_line_str == NULL)
276 return; 278 return;
277 while (*source_line_str == ' ' || *source_line_str == '\t' || 279 while (*source_line_str == ' ' || *source_line_str == '\t' ||
278 *source_line_str == '\014') 280 *source_line_str == '\014')
279 source_line_str++; 281 source_line_str++;
280 282
281 PyFile_WriteString(source_line_str, f_stderr); 283 PyFile_WriteString(source_line_str, f_stderr);
282 PyFile_WriteString("\n", f_stderr); 284 PyFile_WriteString("\n", f_stderr);
283 } 285 }
284 else 286 else {
285 if (_Py_DisplaySourceLine(f_stderr, filename, lineno, 2) < 0) 287 _Py_DisplaySourceLine(f_stderr, filename, lineno, 2);
286 return; 288 }
287 PyErr_Clear(); 289 PyErr_Clear();
288 } 290 }
289 291
290 static PyObject * 292 static PyObject *
291 warn_explicit(PyObject *category, PyObject *message, 293 warn_explicit(PyObject *category, PyObject *message,
292 PyObject *filename, int lineno, 294 PyObject *filename, int lineno,
293 PyObject *module, PyObject *registry, PyObject *sourceline) 295 PyObject *module, PyObject *registry, PyObject *sourceline)
294 { 296 {
295 PyObject *key = NULL, *text = NULL, *result = NULL, *lineno_obj = NULL; 297 PyObject *key = NULL, *text = NULL, *result = NULL, *lineno_obj = NULL;
296 PyObject *item = Py_None; 298 PyObject *item = Py_None;
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after
402 if (rc == 0) { 404 if (rc == 0) {
403 PyObject *show_fxn = get_warnings_attr("showwarning"); 405 PyObject *show_fxn = get_warnings_attr("showwarning");
404 if (show_fxn == NULL) { 406 if (show_fxn == NULL) {
405 if (PyErr_Occurred()) 407 if (PyErr_Occurred())
406 goto cleanup; 408 goto cleanup;
407 show_warning(filename, lineno, text, category, sourceline); 409 show_warning(filename, lineno, text, category, sourceline);
408 } 410 }
409 else { 411 else {
410 PyObject *res; 412 PyObject *res;
411 413
412 if (!PyMethod_Check(show_fxn) && !PyFunction_Check(show_fxn)) { 414 if (!PyCallable_Check(show_fxn)) {
413 PyErr_SetString(PyExc_TypeError, 415 PyErr_SetString(PyExc_TypeError,
414 "warnings.showwarning() must be set to a " 416 "warnings.showwarning() must be set to a "
415 "function or method"); 417 "callable");
416 Py_DECREF(show_fxn); 418 Py_DECREF(show_fxn);
417 goto cleanup; 419 goto cleanup;
418 } 420 }
419 421
420 res = PyObject_CallFunctionObjArgs(show_fxn, message, category, 422 res = PyObject_CallFunctionObjArgs(show_fxn, message, category,
421 filename, lineno_obj, 423 filename, lineno_obj,
422 NULL); 424 NULL);
423 Py_DECREF(show_fxn); 425 Py_DECREF(show_fxn);
424 Py_XDECREF(res); 426 Py_XDECREF(res);
425 if (res == NULL) 427 if (res == NULL)
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
489 if (*module == NULL) { 491 if (*module == NULL) {
490 *module = PyUnicode_FromString("<string>"); 492 *module = PyUnicode_FromString("<string>");
491 if (*module == NULL) 493 if (*module == NULL)
492 goto handle_error; 494 goto handle_error;
493 } 495 }
494 else 496 else
495 Py_INCREF(*module); 497 Py_INCREF(*module);
496 498
497 /* Setup filename. */ 499 /* Setup filename. */
498 *filename = PyDict_GetItemString(globals, "__file__"); 500 *filename = PyDict_GetItemString(globals, "__file__");
499 if (*filename != NULL) { 501 if (*filename != NULL && PyUnicode_Check(*filename)) {
500 Py_ssize_t len = PyUnicode_GetSize(*filename); 502 Py_ssize_t len;
501 Py_UNICODE *unicode = PyUnicode_AS_UNICODE(*filename); 503 int kind;
502 504 void *data;
505
506 if (PyUnicode_READY(*filename))
507 goto handle_error;
508
509 len = PyUnicode_GetLength(*filename);
510 kind = PyUnicode_KIND(*filename);
511 data = PyUnicode_DATA(*filename);
512
513 #define ascii_lower(c) ((c <= 127) ? Py_TOLOWER(c) : 0)
503 /* if filename.lower().endswith((".pyc", ".pyo")): */ 514 /* if filename.lower().endswith((".pyc", ".pyo")): */
504 if (len >= 4 && 515 if (len >= 4 &&
505 unicode[len-4] == '.' && 516 PyUnicode_READ(kind, data, len-4) == '.' &&
506 Py_UNICODE_TOLOWER(unicode[len-3]) == 'p' && 517 ascii_lower(PyUnicode_READ(kind, data, len-3)) == 'p' &&
507 Py_UNICODE_TOLOWER(unicode[len-2]) == 'y' && 518 ascii_lower(PyUnicode_READ(kind, data, len-2)) == 'y' &&
508 (Py_UNICODE_TOLOWER(unicode[len-1]) == 'c' || 519 (ascii_lower(PyUnicode_READ(kind, data, len-1)) == 'c' ||
509 Py_UNICODE_TOLOWER(unicode[len-1]) == 'o')) 520 ascii_lower(PyUnicode_READ(kind, data, len-1)) == 'o'))
510 { 521 {
511 *filename = PyUnicode_FromUnicode(unicode, len-1); 522 *filename = PyUnicode_Substring(*filename, 0,
523 PyUnicode_GET_LENGTH(*filename)-1);
512 if (*filename == NULL) 524 if (*filename == NULL)
513 goto handle_error; 525 goto handle_error;
514 } 526 }
515 else 527 else
516 Py_INCREF(*filename); 528 Py_INCREF(*filename);
517 } 529 }
518 else { 530 else {
519 const char *module_str = _PyUnicode_AsString(*module); 531 const char *module_str = _PyUnicode_AsString(*module);
532 *filename = NULL;
520 if (module_str == NULL) 533 if (module_str == NULL)
521 goto handle_error; 534 goto handle_error;
522 if (strcmp(module_str, "__main__") == 0) { 535 if (strcmp(module_str, "__main__") == 0) {
523 PyObject *argv = PySys_GetObject("argv"); 536 PyObject *argv = PySys_GetObject("argv");
524 if (argv != NULL && PyList_Size(argv) > 0) { 537 if (argv != NULL && PyList_Size(argv) > 0) {
525 int is_true; 538 int is_true;
526 *filename = PyList_GetItem(argv, 0); 539 *filename = PyList_GetItem(argv, 0);
527 Py_INCREF(*filename); 540 Py_INCREF(*filename);
528 /* If sys.argv[0] is false, then use '__main__'. */ 541 /* If sys.argv[0] is false, then use '__main__'. */
529 is_true = PyObject_IsTrue(*filename); 542 is_true = PyObject_IsTrue(*filename);
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after
635 PyObject *module = NULL; 648 PyObject *module = NULL;
636 PyObject *registry = NULL; 649 PyObject *registry = NULL;
637 PyObject *module_globals = NULL; 650 PyObject *module_globals = NULL;
638 651
639 if (!PyArg_ParseTupleAndKeywords(args, kwds, "OOOi|OOO:warn_explicit", 652 if (!PyArg_ParseTupleAndKeywords(args, kwds, "OOOi|OOO:warn_explicit",
640 kwd_list, &message, &category, &filename, &lineno, &module, 653 kwd_list, &message, &category, &filename, &lineno, &module,
641 &registry, &module_globals)) 654 &registry, &module_globals))
642 return NULL; 655 return NULL;
643 656
644 if (module_globals) { 657 if (module_globals) {
645 static PyObject *get_source_name = NULL; 658 _Py_IDENTIFIER(get_source);
646 static PyObject *splitlines_name = NULL; 659 _Py_IDENTIFIER(splitlines);
660 PyObject *tmp;
647 PyObject *loader; 661 PyObject *loader;
648 PyObject *module_name; 662 PyObject *module_name;
649 PyObject *source; 663 PyObject *source;
650 PyObject *source_list; 664 PyObject *source_list;
651 PyObject *source_line; 665 PyObject *source_line;
652 PyObject *returned; 666 PyObject *returned;
653 667
654 if (get_source_name == NULL) { 668 if ((tmp = _PyUnicode_FromId(&PyId_get_source)) == NULL)
655 get_source_name = PyUnicode_InternFromString("get_source"); 669 return NULL;
656 if (!get_source_name) 670 if ((tmp = _PyUnicode_FromId(&PyId_splitlines)) == NULL)
657 return NULL; 671 return NULL;
658 }
659 if (splitlines_name == NULL) {
660 splitlines_name = PyUnicode_InternFromString("splitlines");
661 if (!splitlines_name)
662 return NULL;
663 }
664 672
665 /* Check/get the requisite pieces needed for the loader. */ 673 /* Check/get the requisite pieces needed for the loader. */
666 loader = PyDict_GetItemString(module_globals, "__loader__"); 674 loader = PyDict_GetItemString(module_globals, "__loader__");
667 module_name = PyDict_GetItemString(module_globals, "__name__"); 675 module_name = PyDict_GetItemString(module_globals, "__name__");
668 676
669 if (loader == NULL || module_name == NULL) 677 if (loader == NULL || module_name == NULL)
670 goto standard_call; 678 goto standard_call;
671 679
672 /* Make sure the loader implements the optional get_source() method. */ 680 /* Make sure the loader implements the optional get_source() method. */
673 if (!PyObject_HasAttrString(loader, "get_source")) 681 if (!_PyObject_HasAttrId(loader, &PyId_get_source))
674 goto standard_call; 682 goto standard_call;
675 /* Call get_source() to get the source code. */ 683 /* Call get_source() to get the source code. */
676 source = PyObject_CallMethodObjArgs(loader, get_source_name, 684 source = PyObject_CallMethodObjArgs(loader, PyId_get_source.object,
677 module_name, NULL); 685 module_name, NULL);
678 if (!source) 686 if (!source)
679 return NULL; 687 return NULL;
680 else if (source == Py_None) { 688 else if (source == Py_None) {
681 Py_DECREF(Py_None); 689 Py_DECREF(Py_None);
682 goto standard_call; 690 goto standard_call;
683 } 691 }
684 692
685 /* Split the source into lines. */ 693 /* Split the source into lines. */
686 source_list = PyObject_CallMethodObjArgs(source, splitlines_name, 694 source_list = PyObject_CallMethodObjArgs(source,
687 NULL); 695 PyId_splitlines.object,
696 NULL);
688 Py_DECREF(source); 697 Py_DECREF(source);
689 if (!source_list) 698 if (!source_list)
690 return NULL; 699 return NULL;
691 700
692 /* Get the source line. */ 701 /* Get the source line. */
693 source_line = PyList_GetItem(source_list, lineno-1); 702 source_line = PyList_GetItem(source_list, lineno-1);
694 if (!source_line) { 703 if (!source_line) {
695 Py_DECREF(source_list); 704 Py_DECREF(source_list);
696 return NULL; 705 return NULL;
697 } 706 }
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after
799 { 808 {
800 PyObject *message = PyUnicode_FromString(text); 809 PyObject *message = PyUnicode_FromString(text);
801 PyObject *filename = PyUnicode_DecodeFSDefault(filename_str); 810 PyObject *filename = PyUnicode_DecodeFSDefault(filename_str);
802 PyObject *module = NULL; 811 PyObject *module = NULL;
803 int ret = -1; 812 int ret = -1;
804 813
805 if (message == NULL || filename == NULL) 814 if (message == NULL || filename == NULL)
806 goto exit; 815 goto exit;
807 if (module_str != NULL) { 816 if (module_str != NULL) {
808 module = PyUnicode_FromString(module_str); 817 module = PyUnicode_FromString(module_str);
809 if (module == NULL) 818 if (module == NULL)
810 goto exit; 819 goto exit;
811 } 820 }
812 821
813 ret = PyErr_WarnExplicitObject(category, message, filename, lineno, 822 ret = PyErr_WarnExplicitObject(category, message, filename, lineno,
814 module, registry); 823 module, registry);
815 824
816 exit: 825 exit:
817 Py_XDECREF(message); 826 Py_XDECREF(message);
827 Py_XDECREF(module);
828 Py_XDECREF(filename);
829 return ret;
830 }
831
832 int
833 PyErr_WarnExplicitFormat(PyObject *category,
834 const char *filename_str, int lineno,
835 const char *module_str, PyObject *registry,
836 const char *format, ...)
837 {
838 PyObject *message;
839 PyObject *module = NULL;
840 PyObject *filename = PyUnicode_DecodeFSDefault(filename_str);
841 int ret = -1;
842 va_list vargs;
843
844 if (filename == NULL)
845 goto exit;
846 if (module_str != NULL) {
847 module = PyUnicode_FromString(module_str);
848 if (module == NULL)
849 goto exit;
850 }
851
852 #ifdef HAVE_STDARG_PROTOTYPES
853 va_start(vargs, format);
854 #else
855 va_start(vargs);
856 #endif
857 message = PyUnicode_FromFormatV(format, vargs);
858 if (message != NULL) {
859 PyObject *res;
860 res = warn_explicit(category, message, filename, lineno,
861 module, registry, NULL);
862 Py_DECREF(message);
863 if (res != NULL) {
864 Py_DECREF(res);
865 ret = 0;
866 }
867 }
868 va_end(vargs);
869 exit:
818 Py_XDECREF(module); 870 Py_XDECREF(module);
819 Py_XDECREF(filename); 871 Py_XDECREF(filename);
820 return ret; 872 return ret;
821 } 873 }
822 874
823 875
824 PyDoc_STRVAR(warn_doc, 876 PyDoc_STRVAR(warn_doc,
825 "Issue a warning, or maybe ignore it or raise an exception."); 877 "Issue a warning, or maybe ignore it or raise an exception.");
826 878
827 PyDoc_STRVAR(warn_explicit_doc, 879 PyDoc_STRVAR(warn_explicit_doc,
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
889 if (lineno == NULL) 941 if (lineno == NULL)
890 return NULL; 942 return NULL;
891 result = PyTuple_Pack(5, action_obj, Py_None, category, Py_None, lineno); 943 result = PyTuple_Pack(5, action_obj, Py_None, category, Py_None, lineno);
892 Py_DECREF(lineno); 944 Py_DECREF(lineno);
893 return result; 945 return result;
894 } 946 }
895 947
896 static PyObject * 948 static PyObject *
897 init_filters(void) 949 init_filters(void)
898 { 950 {
899 /* Don't silence DeprecationWarning if -3 was used. */
900 PyObject *filters = PyList_New(5); 951 PyObject *filters = PyList_New(5);
901 unsigned int pos = 0; /* Post-incremented in each use. */ 952 unsigned int pos = 0; /* Post-incremented in each use. */
902 unsigned int x; 953 unsigned int x;
903 const char *bytes_action, *resource_action; 954 const char *bytes_action, *resource_action;
904 955
905 if (filters == NULL) 956 if (filters == NULL)
906 return NULL; 957 return NULL;
907 958
908 PyList_SET_ITEM(filters, pos++, 959 PyList_SET_ITEM(filters, pos++,
909 create_filter(PyExc_DeprecationWarning, "ignore")); 960 create_filter(PyExc_DeprecationWarning, "ignore"));
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
952 1003
953 PyMODINIT_FUNC 1004 PyMODINIT_FUNC
954 _PyWarnings_Init(void) 1005 _PyWarnings_Init(void)
955 { 1006 {
956 PyObject *m; 1007 PyObject *m;
957 1008
958 m = PyModule_Create(&warningsmodule); 1009 m = PyModule_Create(&warningsmodule);
959 if (m == NULL) 1010 if (m == NULL)
960 return NULL; 1011 return NULL;
961 1012
962 _filters = init_filters(); 1013 if (_filters == NULL) {
963 if (_filters == NULL) 1014 _filters = init_filters();
964 return NULL; 1015 if (_filters == NULL)
1016 return NULL;
1017 }
965 Py_INCREF(_filters); 1018 Py_INCREF(_filters);
966 if (PyModule_AddObject(m, "filters", _filters) < 0) 1019 if (PyModule_AddObject(m, "filters", _filters) < 0)
967 return NULL; 1020 return NULL;
968 1021
969 _once_registry = PyDict_New(); 1022 if (_once_registry == NULL) {
970 if (_once_registry == NULL) 1023 _once_registry = PyDict_New();
971 return NULL; 1024 if (_once_registry == NULL)
1025 return NULL;
1026 }
972 Py_INCREF(_once_registry); 1027 Py_INCREF(_once_registry);
973 if (PyModule_AddObject(m, "_onceregistry", _once_registry) < 0) 1028 if (PyModule_AddObject(m, "_onceregistry", _once_registry) < 0)
974 return NULL; 1029 return NULL;
975 1030
976 _default_action = PyUnicode_FromString("default"); 1031 if (_default_action == NULL) {
977 if (_default_action == NULL) 1032 _default_action = PyUnicode_FromString("default");
978 return NULL; 1033 if (_default_action == NULL)
1034 return NULL;
1035 }
1036 Py_INCREF(_default_action);
979 if (PyModule_AddObject(m, "_defaultaction", _default_action) < 0) 1037 if (PyModule_AddObject(m, "_defaultaction", _default_action) < 0)
980 return NULL; 1038 return NULL;
981 return m; 1039 return m;
982 } 1040 }
LEFTRIGHT

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+