| OLD | NEW |
| 1 | 1 |
| 2 /* Python interpreter top-level routines, including init/exit */ | 2 /* Python interpreter top-level routines, including init/exit */ |
| 3 | 3 |
| 4 #include "Python.h" | 4 #include "Python.h" |
| 5 | 5 |
| 6 #include "Python-ast.h" | 6 #include "Python-ast.h" |
| 7 #undef Yield /* undefine macro conflicting with winbase.h */ | 7 #undef Yield /* undefine macro conflicting with winbase.h */ |
| 8 #include "grammar.h" | 8 #include "grammar.h" |
| 9 #include "node.h" | 9 #include "node.h" |
| 10 #include "token.h" | 10 #include "token.h" |
| 11 #include "parsetok.h" | 11 #include "parsetok.h" |
| 12 #include "errcode.h" | 12 #include "errcode.h" |
| 13 #include "code.h" | 13 #include "code.h" |
| 14 #include "compile.h" | 14 #include "compile.h" |
| 15 #include "symtable.h" | 15 #include "symtable.h" |
| 16 #include "pyarena.h" | 16 #include "pyarena.h" |
| 17 #include "ast.h" | 17 #include "ast.h" |
| 18 #include "eval.h" | 18 #include "eval.h" |
| 19 #include "marshal.h" | 19 #include "marshal.h" |
| 20 #include "osdefs.h" | 20 #include "osdefs.h" |
| 21 | 21 |
| 22 #ifdef HAVE_SIGNAL_H | 22 #ifdef HAVE_SIGNAL_H |
| 23 #include <signal.h> | 23 #include <signal.h> |
| 24 #endif | 24 #endif |
| 25 | 25 |
| 26 #ifdef HAVE_LANGINFO_H | 26 #ifdef HAVE_LANGINFO_H |
| 27 #include <locale.h> | 27 #include <locale.h> |
| 28 #include <langinfo.h> | 28 #include <langinfo.h> |
| 29 #endif | 29 #endif |
| 30 | 30 |
| 31 #ifdef MS_WINDOWS | 31 #ifdef MS_WINDOWS |
| 32 #undef BYTE | 32 #undef BYTE |
| 33 #include "windows.h" | 33 #include "windows.h" |
| 34 #define PATH_MAX MAXPATHLEN | 34 #define PATH_MAX MAXPATHLEN |
| 35 #endif | 35 #endif |
| 36 | 36 |
| 37 #ifndef Py_REF_DEBUG | 37 #ifndef Py_REF_DEBUG |
| 38 #define PRINT_TOTAL_REFS() | 38 #define PRINT_TOTAL_REFS() |
| 39 #else /* Py_REF_DEBUG */ | 39 #else /* Py_REF_DEBUG */ |
| 40 #define PRINT_TOTAL_REFS() fprintf(stderr, \ | 40 #define PRINT_TOTAL_REFS() fprintf(stderr, \ |
| 41 "[%" PY_FORMAT_SIZE_T "d refs]\n", \ | 41 "[%" PY_FORMAT_SIZE_T "d refs]\n", \ |
| 42 _Py_GetRefTotal()) | 42 _Py_GetRefTotal()) |
| 43 #endif | 43 #endif |
| 44 | 44 |
| 45 #ifdef __cplusplus | 45 #ifdef __cplusplus |
| 46 extern "C" { | 46 extern "C" { |
| 47 #endif | 47 #endif |
| 48 | 48 |
| 49 extern wchar_t *Py_GetPath(void); | 49 extern wchar_t *Py_GetPath(void); |
| 50 | 50 |
| (...skipping 666 matching lines...) Show 10 above Show 10 below |
| 717 goto error; | 717 goto error; |
| 718 } | 718 } |
| 719 | 719 |
| 720 if (!(iomod = PyImport_ImportModule("io"))) { | 720 if (!(iomod = PyImport_ImportModule("io"))) { |
| 721 goto error; | 721 goto error; |
| 722 } | 722 } |
| 723 if (!(wrapper = PyObject_GetAttrString(iomod, "OpenWrapper"))) { | 723 if (!(wrapper = PyObject_GetAttrString(iomod, "OpenWrapper"))) { |
| 724 goto error; | 724 goto error; |
| 725 } | 725 } |
| 726 | 726 |
| 727 /* Set builtins.open */ | 727 /* Set builtins.open */ |
| 728 if (PyObject_SetAttrString(bimod, "open", wrapper) == -1) { | 728 if (PyObject_SetAttrString(bimod, "open", wrapper) == -1) { |
| 729 goto error; | 729 goto error; |
| 730 } | 730 } |
| 731 | 731 |
| 732 /* Set sys.stdin */ | 732 /* Set sys.stdin */ |
| 733 fd = fileno(stdin); | 733 fd = fileno(stdin); |
| 734 /* Under some conditions stdin, stdout and stderr may not be connected | 734 /* Under some conditions stdin, stdout and stderr may not be connected |
| 735 * and fileno() may point to an invalid file descriptor. For example | 735 * and fileno() may point to an invalid file descriptor. For example |
| 736 * GUI apps don't have valid standard streams by default. | 736 * GUI apps don't have valid standard streams by default. |
| 737 */ | 737 */ |
| 738 if (fd < 0) { | 738 if (fd < 0) { |
| 739 #ifdef MS_WINDOWS | 739 #ifdef MS_WINDOWS |
| 740 std = Py_None; | 740 std = Py_None; |
| 741 Py_INCREF(std); | 741 Py_INCREF(std); |
| 742 #else | 742 #else |
| 743 goto error; | 743 goto error; |
| 744 #endif | 744 #endif |
| 745 } | 745 } |
| 746 else { | 746 else { |
| 747 if (!(std = PyFile_FromFd(fd, "<stdin>", "r", -1, NULL, NULL, | 747 if (!(std = PyFile_FromFd(fd, "<stdin>", "r", -1, NULL, NULL, |
| 748 "\n", 0))) { | 748 "\n", 0))) { |
| 749 goto error; | 749 goto error; |
| 750 } | 750 } |
| 751 } /* if (fd < 0) */ | 751 } /* if (fd < 0) */ |
| 752 PySys_SetObject("__stdin__", std); | 752 PySys_SetObject("__stdin__", std); |
| 753 PySys_SetObject("stdin", std); | 753 PySys_SetObject("stdin", std); |
| 754 Py_DECREF(std); | 754 Py_DECREF(std); |
| 755 | 755 |
| 756 /* Set sys.stdout */ | 756 /* Set sys.stdout */ |
| 757 fd = fileno(stdout); | 757 fd = fileno(stdout); |
| 758 if (fd < 0) { | 758 if (fd < 0) { |
| 759 #ifdef MS_WINDOWS | 759 #ifdef MS_WINDOWS |
| 760 std = Py_None; | 760 std = Py_None; |
| 761 Py_INCREF(std); | 761 Py_INCREF(std); |
| 762 #else | 762 #else |
| 763 goto error; | 763 goto error; |
| 764 #endif | 764 #endif |
| 765 } | 765 } |
| 766 else { | 766 else { |
| 767 if (!(std = PyFile_FromFd(fd, "<stdout>", "w", -1, NULL, NULL, | 767 if (!(std = PyFile_FromFd(fd, "<stdout>", "w", -1, NULL, |
| 768 "\n", 0))) { | 768 "backslashreplace", "\n", 0))) { |
| 769 goto error; | 769 goto error; |
| 770 } | 770 } |
| 771 } /* if (fd < 0) */ | 771 } /* if (fd < 0) */ |
| 772 PySys_SetObject("__stdout__", std); | 772 PySys_SetObject("__stdout__", std); |
| 773 PySys_SetObject("stdout", std); | 773 PySys_SetObject("stdout", std); |
| 774 Py_DECREF(std); | 774 Py_DECREF(std); |
| 775 | 775 |
| 776 #if 1 /* Disable this if you have trouble debugging bootstrap stuff */ | 776 #if 1 /* Disable this if you have trouble debugging bootstrap stuff */ |
| 777 /* Set sys.stderr, replaces the preliminary stderr */ | 777 /* Set sys.stderr, replaces the preliminary stderr */ |
| 778 fd = fileno(stderr); | 778 fd = fileno(stderr); |
| 779 if (fd < 0) { | 779 if (fd < 0) { |
| 780 #ifdef MS_WINDOWS | 780 #ifdef MS_WINDOWS |
| 781 std = Py_None; | 781 std = Py_None; |
| 782 Py_INCREF(std); | 782 Py_INCREF(std); |
| 783 #else | 783 #else |
| 784 goto error; | 784 goto error; |
| 785 #endif | 785 #endif |
| 786 } | 786 } |
| 787 else { | 787 else { |
| 788 if (!(std = PyFile_FromFd(fd, "<stderr>", "w", -1, NULL, NULL, | 788 if (!(std = PyFile_FromFd(fd, "<stderr>", "w", -1, NULL, |
| 789 "\n", 0))) { | 789 "backslashreplace", "\n", 0))) { |
| 790 goto error; | 790 goto error; |
| 791 } | 791 } |
| 792 } /* if (fd < 0) */ | 792 } /* if (fd < 0) */ |
| 793 | 793 |
| 794 /* Same as hack above, pre-import stderr's codec to avoid recursion | 794 /* Same as hack above, pre-import stderr's codec to avoid recursion |
| 795 when import.c tries to write to stderr in verbose mode. */ | 795 when import.c tries to write to stderr in verbose mode. */ |
| 796 encoding_attr = PyObject_GetAttrString(std, "encoding"); | 796 encoding_attr = PyObject_GetAttrString(std, "encoding"); |
| 797 if (encoding_attr != NULL) { | 797 if (encoding_attr != NULL) { |
| 798 const char * encoding; | 798 const char * encoding; |
| 799 encoding = PyUnicode_AsString(encoding_attr); | 799 encoding = PyUnicode_AsString(encoding_attr); |
| 800 if (encoding != NULL) { | 800 if (encoding != NULL) { |
| 801 _PyCodec_Lookup(encoding); | 801 _PyCodec_Lookup(encoding); |
| 802 } | 802 } |
| 803 } | 803 } |
| 804 PyErr_Clear(); /* Not a fatal error if codec isn't available */ | 804 PyErr_Clear(); /* Not a fatal error if codec isn't available */ |
| 805 | 805 |
| 806 PySys_SetObject("__stderr__", std); | 806 PySys_SetObject("__stderr__", std); |
| 807 PySys_SetObject("stderr", std); | 807 PySys_SetObject("stderr", std); |
| 808 Py_DECREF(std); | 808 Py_DECREF(std); |
| 809 #endif | 809 #endif |
| 810 | 810 |
| 811 if (0) { | 811 if (0) { |
| 812 error: | 812 error: |
| 813 status = -1; | 813 status = -1; |
| 814 } | 814 } |
| 815 | 815 |
| 816 Py_XDECREF(bimod); | 816 Py_XDECREF(bimod); |
| 817 Py_XDECREF(iomod); | 817 Py_XDECREF(iomod); |
| 818 return status; | 818 return status; |
| 819 } | 819 } |
| 820 | 820 |
| 821 /* Parse input from a file and execute it */ | 821 /* Parse input from a file and execute it */ |
| 822 | 822 |
| 823 int | 823 int |
| 824 PyRun_AnyFileExFlags(FILE *fp, const char *filename, int closeit, | 824 PyRun_AnyFileExFlags(FILE *fp, const char *filename, int closeit, |
| 825 PyCompilerFlags *flags) | 825 PyCompilerFlags *flags) |
| 826 { | 826 { |
| 827 if (filename == NULL) | 827 if (filename == NULL) |
| 828 filename = "???"; | 828 filename = "???"; |
| 829 if (Py_FdIsInteractive(fp, filename)) { | 829 if (Py_FdIsInteractive(fp, filename)) { |
| 830 int err = PyRun_InteractiveLoopFlags(fp, filename, flags); | 830 int err = PyRun_InteractiveLoopFlags(fp, filename, flags); |
| 831 if (closeit) | 831 if (closeit) |
| 832 fclose(fp); | 832 fclose(fp); |
| 833 return err; | 833 return err; |
| 834 } | 834 } |
| 835 else | 835 else |
| 836 return PyRun_SimpleFileExFlags(fp, filename, closeit, flags); | 836 return PyRun_SimpleFileExFlags(fp, filename, closeit, flags); |
| 837 } | 837 } |
| 838 | 838 |
| 839 int | 839 int |
| (...skipping 1199 matching lines...) Show 10 above Show 10 below |
| 2039 { | 2039 { |
| 2040 return PyRun_SimpleFileExFlags(f, p, 0, NULL); | 2040 return PyRun_SimpleFileExFlags(f, p, 0, NULL); |
| 2041 } | 2041 } |
| 2042 | 2042 |
| 2043 #undef PyRun_SimpleFileEx | 2043 #undef PyRun_SimpleFileEx |
| 2044 PyAPI_FUNC(int) | 2044 PyAPI_FUNC(int) |
| 2045 PyRun_SimpleFileEx(FILE *f, const char *p, int c) | 2045 PyRun_SimpleFileEx(FILE *f, const char *p, int c) |
| 2046 { | 2046 { |
| 2047 return PyRun_SimpleFileExFlags(f, p, c, NULL); | 2047 return PyRun_SimpleFileExFlags(f, p, c, NULL); |
| 2048 } | 2048 } |
| 2049 | 2049 |
| 2050 | 2050 |
| 2051 #undef PyRun_String | 2051 #undef PyRun_String |
| 2052 PyAPI_FUNC(PyObject *) | 2052 PyAPI_FUNC(PyObject *) |
| 2053 PyRun_String(const char *str, int s, PyObject *g, PyObject *l) | 2053 PyRun_String(const char *str, int s, PyObject *g, PyObject *l) |
| 2054 { | 2054 { |
| 2055 return PyRun_StringFlags(str, s, g, l, NULL); | 2055 return PyRun_StringFlags(str, s, g, l, NULL); |
| 2056 } | 2056 } |
| 2057 | 2057 |
| 2058 #undef PyRun_SimpleString | 2058 #undef PyRun_SimpleString |
| 2059 PyAPI_FUNC(int) | 2059 PyAPI_FUNC(int) |
| 2060 PyRun_SimpleString(const char *s) | 2060 PyRun_SimpleString(const char *s) |
| 2061 { | 2061 { |
| 2062 return PyRun_SimpleStringFlags(s, NULL); | 2062 return PyRun_SimpleStringFlags(s, NULL); |
| 2063 } | 2063 } |
| 2064 | 2064 |
| 2065 #undef Py_CompileString | 2065 #undef Py_CompileString |
| 2066 PyAPI_FUNC(PyObject *) | 2066 PyAPI_FUNC(PyObject *) |
| 2067 Py_CompileString(const char *str, const char *p, int s) | 2067 Py_CompileString(const char *str, const char *p, int s) |
| 2068 { | 2068 { |
| 2069 return Py_CompileStringFlags(str, p, s, NULL); | 2069 return Py_CompileStringFlags(str, p, s, NULL); |
| 2070 } | 2070 } |
| 2071 | 2071 |
| 2072 #undef PyRun_InteractiveOne | 2072 #undef PyRun_InteractiveOne |
| 2073 PyAPI_FUNC(int) | 2073 PyAPI_FUNC(int) |
| 2074 PyRun_InteractiveOne(FILE *f, const char *p) | 2074 PyRun_InteractiveOne(FILE *f, const char *p) |
| 2075 { | 2075 { |
| 2076 return PyRun_InteractiveOneFlags(f, p, NULL); | 2076 return PyRun_InteractiveOneFlags(f, p, NULL); |
| 2077 } | 2077 } |
| 2078 | 2078 |
| 2079 #undef PyRun_InteractiveLoop | 2079 #undef PyRun_InteractiveLoop |
| 2080 PyAPI_FUNC(int) | 2080 PyAPI_FUNC(int) |
| 2081 PyRun_InteractiveLoop(FILE *f, const char *p) | 2081 PyRun_InteractiveLoop(FILE *f, const char *p) |
| 2082 { | 2082 { |
| 2083 return PyRun_InteractiveLoopFlags(f, p, NULL); | 2083 return PyRun_InteractiveLoopFlags(f, p, NULL); |
| 2084 } | 2084 } |
| 2085 | 2085 |
| 2086 #ifdef __cplusplus | 2086 #ifdef __cplusplus |
| 2087 } | 2087 } |
| 2088 #endif | 2088 #endif |
| OLD | NEW |