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

Side by Side Diff: Python/ast.c

Issue 11619: On Windows, don't encode filenames in the import machinery
Patch Set: Created 6 years, 1 month 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:
View unified diff | Download patch
« no previous file with comments | « Parser/parsetok.c ('k') | Python/compile.c » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * This file includes functions to transform a concrete syntax tree (CST) to 2 * This file includes functions to transform a concrete syntax tree (CST) to
3 * an abstract syntax tree (AST). The main function is PyAST_FromNode(). 3 * an abstract syntax tree (AST). The main function is PyAST_FromNode().
4 * 4 *
5 */ 5 */
6 #include "Python.h" 6 #include "Python.h"
7 #include "Python-ast.h" 7 #include "Python-ast.h"
8 #include "node.h" 8 #include "node.h"
9 #include "ast.h" 9 #include "ast.h"
10 #include "token.h" 10 #include "token.h"
(...skipping 473 matching lines...) Expand 10 before | Expand all | Expand 10 after
484 484
485 /* This is done here, so defines like "test" don't interfere with AST use above. */ 485 /* This is done here, so defines like "test" don't interfere with AST use above. */
486 #include "grammar.h" 486 #include "grammar.h"
487 #include "parsetok.h" 487 #include "parsetok.h"
488 #include "graminit.h" 488 #include "graminit.h"
489 489
490 /* Data structure used internally */ 490 /* Data structure used internally */
491 struct compiling { 491 struct compiling {
492 char *c_encoding; /* source encoding */ 492 char *c_encoding; /* source encoding */
493 PyArena *c_arena; /* arena for allocating memeory */ 493 PyArena *c_arena; /* arena for allocating memeory */
494 const char *c_filename; /* filename */ 494 PyObject *c_filename; /* filename */
495 PyObject *c_normalize; /* Normalization function from unicodedata. */ 495 PyObject *c_normalize; /* Normalization function from unicodedata. */
496 PyObject *c_normalize_args; /* Normalization argument tuple. */ 496 PyObject *c_normalize_args; /* Normalization argument tuple. */
497 }; 497 };
498 498
499 static asdl_seq *seq_for_testlist(struct compiling *, const node *); 499 static asdl_seq *seq_for_testlist(struct compiling *, const node *);
500 static expr_ty ast_for_expr(struct compiling *, const node *); 500 static expr_ty ast_for_expr(struct compiling *, const node *);
501 static stmt_ty ast_for_stmt(struct compiling *, const node *); 501 static stmt_ty ast_for_stmt(struct compiling *, const node *);
502 static asdl_seq *ast_for_suite(struct compiling *, const node *); 502 static asdl_seq *ast_for_suite(struct compiling *, const node *);
503 static asdl_seq *ast_for_exprlist(struct compiling *, const node *, 503 static asdl_seq *ast_for_exprlist(struct compiling *, const node *,
504 expr_context_ty); 504 expr_context_ty);
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
566 } 566 }
567 return id; 567 return id;
568 } 568 }
569 569
570 #define NEW_IDENTIFIER(n) new_identifier(STR(n), c) 570 #define NEW_IDENTIFIER(n) new_identifier(STR(n), c)
571 571
572 static int 572 static int
573 ast_error(struct compiling *c, const node *n, const char *errmsg) 573 ast_error(struct compiling *c, const node *n, const char *errmsg)
574 { 574 {
575 PyObject *value, *errstr, *loc, *tmp; 575 PyObject *value, *errstr, *loc, *tmp;
576 PyObject *filename_obj;
577 576
578 loc = PyErr_ProgramText(c->c_filename, LINENO(n)); 577 loc = PyErr_ProgramTextObject(c->c_filename, LINENO(n));
579 if (!loc) { 578 if (!loc) {
580 Py_INCREF(Py_None); 579 Py_INCREF(Py_None);
581 loc = Py_None; 580 loc = Py_None;
582 } 581 }
583 if (c->c_filename) { 582 tmp = Py_BuildValue("(OiiN)", c->c_filename, LINENO(n), n->n_col_offset, loc );
584 filename_obj = PyUnicode_DecodeFSDefault(c->c_filename);
585 if (!filename_obj) {
586 Py_DECREF(loc);
587 return 0;
588 }
589 } else {
590 Py_INCREF(Py_None);
591 filename_obj = Py_None;
592 }
593 tmp = Py_BuildValue("(NiiN)", filename_obj, LINENO(n), n->n_col_offset, loc) ;
594 if (!tmp) 583 if (!tmp)
595 return 0; 584 return 0;
596 errstr = PyUnicode_FromString(errmsg); 585 errstr = PyUnicode_FromString(errmsg);
597 if (!errstr) { 586 if (!errstr) {
598 Py_DECREF(tmp); 587 Py_DECREF(tmp);
599 return 0; 588 return 0;
600 } 589 }
601 value = PyTuple_Pack(2, errstr, tmp); 590 value = PyTuple_Pack(2, errstr, tmp);
602 Py_DECREF(errstr); 591 Py_DECREF(errstr);
603 Py_DECREF(tmp); 592 Py_DECREF(tmp);
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
666 } 655 }
667 } 656 }
668 assert(0); 657 assert(0);
669 return 0; 658 return 0;
670 } 659 }
671 660
672 /* Transform the CST rooted at node * to the appropriate AST 661 /* Transform the CST rooted at node * to the appropriate AST
673 */ 662 */
674 663
675 mod_ty 664 mod_ty
676 PyAST_FromNode(const node *n, PyCompilerFlags *flags, const char *filename, 665 PyAST_FromNodeObject(const node *n, PyCompilerFlags *flags,
677 PyArena *arena) 666 PyObject *filename, PyArena *arena)
678 { 667 {
679 int i, j, k, num; 668 int i, j, k, num;
680 asdl_seq *stmts = NULL; 669 asdl_seq *stmts = NULL;
681 stmt_ty s; 670 stmt_ty s;
682 node *ch; 671 node *ch;
683 struct compiling c; 672 struct compiling c;
684 mod_ty res = NULL; 673 mod_ty res = NULL;
685 674
686 c.c_arena = arena; 675 c.c_arena = arena;
676 /* borrowed reference */
687 c.c_filename = filename; 677 c.c_filename = filename;
688 c.c_normalize = c.c_normalize_args = NULL; 678 c.c_normalize = c.c_normalize_args = NULL;
689 if (flags && flags->cf_flags & PyCF_SOURCE_IS_UTF8) { 679 if (flags && flags->cf_flags & PyCF_SOURCE_IS_UTF8) {
690 c.c_encoding = "utf-8"; 680 c.c_encoding = "utf-8";
691 if (TYPE(n) == encoding_decl) { 681 if (TYPE(n) == encoding_decl) {
692 #if 0 682 #if 0
693 ast_error(c, n, "encoding declaration in Unicode string"); 683 ast_error(c, n, "encoding declaration in Unicode string");
694 goto out; 684 goto out;
695 #endif 685 #endif
696 n = CHILD(n, 0); 686 n = CHILD(n, 0);
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after
788 "invalid node %d for PyAST_FromNode", TYPE(n)); 778 "invalid node %d for PyAST_FromNode", TYPE(n));
789 goto out; 779 goto out;
790 } 780 }
791 out: 781 out:
792 if (c.c_normalize) { 782 if (c.c_normalize) {
793 Py_DECREF(c.c_normalize); 783 Py_DECREF(c.c_normalize);
794 PyTuple_SET_ITEM(c.c_normalize_args, 1, NULL); 784 PyTuple_SET_ITEM(c.c_normalize_args, 1, NULL);
795 Py_DECREF(c.c_normalize_args); 785 Py_DECREF(c.c_normalize_args);
796 } 786 }
797 return res; 787 return res;
788 }
789
790 mod_ty
791 PyAST_FromNode(const node *n, PyCompilerFlags *flags, const char *filename_str,
792 PyArena *arena)
793 {
794 mod_ty mod;
795 PyObject *filename;
796 filename = PyUnicode_DecodeFSDefault(filename_str);
797 if (filename == NULL)
798 return NULL;
799 mod = PyAST_FromNodeObject(n, flags, filename, arena);
800 Py_DECREF(filename);
801 return mod;
802
798 } 803 }
799 804
800 /* Return the AST repr. of the operator represented as syntax (|, ^, etc.) 805 /* Return the AST repr. of the operator represented as syntax (|, ^, etc.)
801 */ 806 */
802 807
803 static operator_ty 808 static operator_ty
804 get_operator(const node *n) 809 get_operator(const node *n)
805 { 810 {
806 switch (TYPE(n)) { 811 switch (TYPE(n)) {
807 case VBAR: 812 case VBAR:
(...skipping 3072 matching lines...) Expand 10 before | Expand all | Expand 10 after
3880 goto onError; 3885 goto onError;
3881 } 3886 }
3882 } 3887 }
3883 } 3888 }
3884 return v; 3889 return v;
3885 3890
3886 onError: 3891 onError:
3887 Py_XDECREF(v); 3892 Py_XDECREF(v);
3888 return NULL; 3893 return NULL;
3889 } 3894 }
OLDNEW
« no previous file with comments | « Parser/parsetok.c ('k') | Python/compile.c » ('j') | no next file with comments »

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