annotate Python/pythonrun.c @ 35297:26bf6c27e9ce legacy-trunk

* Improve code for the empty frozenset singleton: - Handle both frozenset() and frozenset([]). - Do not use singleton for frozenset subclasses. - Finalize the singleton. - Add test cases. * Factor-out set_update_internal() from set_update(). Simplifies the code for several internal callers. * Factor constant expressions out of loop in set_merge_internal(). * Minor comment touch-ups.
author Raymond Hettinger <python@rcn.com>
date Mon, 01 Aug 2005 21:39:29 +0000
parents 1dd8a0d64432
children e48fcd4c358d
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
2 /* Python interpreter top-level routines, including init/exit */
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
3
5170
f5d707ce4223 Greatly renamed. Not a very thorough job -- I'm going to restructure
Guido van Rossum <guido@python.org>
parents: 5108
diff changeset
4 #include "Python.h"
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
5
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
6 #include "grammar.h"
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
7 #include "node.h"
13539
feab677269e0 Create two new exceptions: IndentationError and TabError. These are
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
8 #include "token.h"
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
9 #include "parsetok.h"
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
10 #include "errcode.h"
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
11 #include "compile.h"
16421
aa844711463a Move a bunch of definitions that were internal to compile.c to
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16311
diff changeset
12 #include "symtable.h"
926
c156d4960c2b * Makefile: cosmetics
Guido van Rossum <guido@python.org>
parents: 923
diff changeset
13 #include "eval.h"
1886
ac6a9f90f6fd * Import/pythonrun.h, Python/{import,pythonrun}.c,
Guido van Rossum <guido@python.org>
parents: 1870
diff changeset
14 #include "marshal.h"
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
15
1079
e4c6a0dd5c2b * bltinmodule.c: added built-in function cmp(a, b)
Guido van Rossum <guido@python.org>
parents: 1073
diff changeset
16 #include <signal.h>
e4c6a0dd5c2b * bltinmodule.c: added built-in function cmp(a, b)
Guido van Rossum <guido@python.org>
parents: 1073
diff changeset
17
28174
3f90d21debe9 Always initialize Py_FileSystemDefaultEncoding on Unix in Py_Initialize,
Martin v. Löwis <martin@v.loewis.de>
parents: 28065
diff changeset
18 #ifdef HAVE_LANGINFO_H
3f90d21debe9 Always initialize Py_FileSystemDefaultEncoding on Unix in Py_Initialize,
Martin v. Löwis <martin@v.loewis.de>
parents: 28065
diff changeset
19 #include <locale.h>
3f90d21debe9 Always initialize Py_FileSystemDefaultEncoding on Unix in Py_Initialize,
Martin v. Löwis <martin@v.loewis.de>
parents: 28065
diff changeset
20 #include <langinfo.h>
3f90d21debe9 Always initialize Py_FileSystemDefaultEncoding on Unix in Py_Initialize,
Martin v. Löwis <martin@v.loewis.de>
parents: 28065
diff changeset
21 #endif
3f90d21debe9 Always initialize Py_FileSystemDefaultEncoding on Unix in Py_Initialize,
Martin v. Löwis <martin@v.loewis.de>
parents: 28065
diff changeset
22
24071
8142f8d668a0 Patch #569753: Remove support for WIN16.
Martin v. Löwis <martin@v.loewis.de>
parents: 23252
diff changeset
23 #ifdef MS_WINDOWS
2529
16ea57c38c4f In NT, write fatal() msg to debugging device
Guido van Rossum <guido@python.org>
parents: 2318
diff changeset
24 #undef BYTE
16ea57c38c4f In NT, write fatal() msg to debugging device
Guido van Rossum <guido@python.org>
parents: 2318
diff changeset
25 #include "windows.h"
16ea57c38c4f In NT, write fatal() msg to debugging device
Guido van Rossum <guido@python.org>
parents: 2318
diff changeset
26 #endif
16ea57c38c4f In NT, write fatal() msg to debugging device
Guido van Rossum <guido@python.org>
parents: 2318
diff changeset
27
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13620
diff changeset
28 extern char *Py_GetPath(void);
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
29
5170
f5d707ce4223 Greatly renamed. Not a very thorough job -- I'm going to restructure
Guido van Rossum <guido@python.org>
parents: 5108
diff changeset
30 extern grammar _PyParser_Grammar; /* From graminit.c */
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
31
1391
7c3245c18677 * ceval.c, longobject.c, methodobject.c, listnode.c, arraymodule.c,
Guido van Rossum <guido@python.org>
parents: 1347
diff changeset
32 /* Forward */
13447
a89f7f268de4 Nuke all remaining occurrences of Py_PROTO and Py_FPROTO.
Tim Peters <tim.peters@gmail.com>
parents: 13256
diff changeset
33 static void initmain(void);
a89f7f268de4 Nuke all remaining occurrences of Py_PROTO and Py_FPROTO.
Tim Peters <tim.peters@gmail.com>
parents: 13256
diff changeset
34 static void initsite(void);
26601
cc05c2c8660e Constify filenames and scripts. Fixes #651362.
Martin v. Löwis <martin@v.loewis.de>
parents: 26263
diff changeset
35 static PyObject *run_err_node(node *, const char *, PyObject *, PyObject *,
16916
b0456d3a0f1f Useful future statement support for the interactive interpreter
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16845
diff changeset
36 PyCompilerFlags *);
26601
cc05c2c8660e Constify filenames and scripts. Fixes #651362.
Martin v. Löwis <martin@v.loewis.de>
parents: 26263
diff changeset
37 static PyObject *run_node(node *, const char *, PyObject *, PyObject *,
16916
b0456d3a0f1f Useful future statement support for the interactive interpreter
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16845
diff changeset
38 PyCompilerFlags *);
26601
cc05c2c8660e Constify filenames and scripts. Fixes #651362.
Martin v. Löwis <martin@v.loewis.de>
parents: 26263
diff changeset
39 static PyObject *run_pyc_file(FILE *, const char *, PyObject *, PyObject *,
17125
2054cea38754 Extend support for from __future__ import nested_scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16916
diff changeset
40 PyCompilerFlags *);
13447
a89f7f268de4 Nuke all remaining occurrences of Py_PROTO and Py_FPROTO.
Tim Peters <tim.peters@gmail.com>
parents: 13256
diff changeset
41 static void err_input(perrdetail *);
a89f7f268de4 Nuke all remaining occurrences of Py_PROTO and Py_FPROTO.
Tim Peters <tim.peters@gmail.com>
parents: 13256
diff changeset
42 static void initsigs(void);
a89f7f268de4 Nuke all remaining occurrences of Py_PROTO and Py_FPROTO.
Tim Peters <tim.peters@gmail.com>
parents: 13256
diff changeset
43 static void call_sys_exitfunc(void);
a89f7f268de4 Nuke all remaining occurrences of Py_PROTO and Py_FPROTO.
Tim Peters <tim.peters@gmail.com>
parents: 13256
diff changeset
44 static void call_ll_exitfuncs(void);
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13620
diff changeset
45 extern void _PyUnicode_Init(void);
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13620
diff changeset
46 extern void _PyUnicode_Fini(void);
11977
8b88e1bc0e1b Marc-Andre Lemburg: add calls to initialize and finalize Unicode and
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
47
28694
a4154dd5939a New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents: 28653
diff changeset
48 #ifdef WITH_THREAD
a4154dd5939a New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents: 28653
diff changeset
49 extern void _PyGILState_Init(PyInterpreterState *, PyThreadState *);
a4154dd5939a New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents: 28653
diff changeset
50 extern void _PyGILState_Fini(void);
a4154dd5939a New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents: 28653
diff changeset
51 #endif /* WITH_THREAD */
a4154dd5939a New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents: 28653
diff changeset
52
5170
f5d707ce4223 Greatly renamed. Not a very thorough job -- I'm going to restructure
Guido van Rossum <guido@python.org>
parents: 5108
diff changeset
53 int Py_DebugFlag; /* Needed by parser.c */
f5d707ce4223 Greatly renamed. Not a very thorough job -- I'm going to restructure
Guido van Rossum <guido@python.org>
parents: 5108
diff changeset
54 int Py_VerboseFlag; /* Needed by import.c */
5108
a9a6d416b945 Added new global flag variable Py_InteractiveFlag and new function
Guido van Rossum <guido@python.org>
parents: 4596
diff changeset
55 int Py_InteractiveFlag; /* Needed by Py_FdIsInteractive() below */
6201
dc974b3a6cf4 Two independent changes (oops):
Guido van Rossum <guido@python.org>
parents: 6197
diff changeset
56 int Py_NoSiteFlag; /* Suppress 'import site' */
12555
04cf788d862b Py_UseClassExceptionsFlag is deprecated. We keep the C variable for C
Barry Warsaw <barry@python.org>
parents: 12539
diff changeset
57 int Py_UseClassExceptionsFlag = 1; /* Needed by bltinmodule.c: deprecated */
7206
3f255aedf621 Ehm, three unrelated changes.
Guido van Rossum <guido@python.org>
parents: 7113
diff changeset
58 int Py_FrozenFlag; /* Needed by getpath.c */
12539
d7484a743354 Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12518
diff changeset
59 int Py_UnicodeFlag = 0; /* Needed by compile.c */
18808
a8b1acc7a21f Add -E command line switch (ignore environment variables like PYTHONHOME
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 18630
diff changeset
60 int Py_IgnoreEnvironmentFlag; /* e.g. PYTHONPATH, PYTHONHOME */
21251
45e1177424e1 SF bug #488514: -Qnew needs work
Tim Peters <tim.peters@gmail.com>
parents: 21077
diff changeset
61 /* _XXX Py_QnewFlag should go away in 2.3. It's true iff -Qnew is passed,
45e1177424e1 SF bug #488514: -Qnew needs work
Tim Peters <tim.peters@gmail.com>
parents: 21077
diff changeset
62 on the command line, and is used in 2.2 by ceval.c to make all "/" divisions
45e1177424e1 SF bug #488514: -Qnew needs work
Tim Peters <tim.peters@gmail.com>
parents: 21077
diff changeset
63 true divisions (which they will be in 2.3). */
45e1177424e1 SF bug #488514: -Qnew needs work
Tim Peters <tim.peters@gmail.com>
parents: 21077
diff changeset
64 int _Py_QnewFlag = 0;
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
65
27958
e07b21ea3c45 Fix bug 683658 - PyErr_Warn may cause import deadlock.
Mark Hammond <mhammond@skippinet.com.au>
parents: 27878
diff changeset
66 /* Reference to 'warnings' module, to avoid importing it
29877
72cbc41ccc2f Fix [ 771097 ] frozen programs fail due to implicit import of "warnings".
Mark Hammond <mhammond@skippinet.com.au>
parents: 28720
diff changeset
67 on the fly when the import lock may be held. See 683658/771097
72cbc41ccc2f Fix [ 771097 ] frozen programs fail due to implicit import of "warnings".
Mark Hammond <mhammond@skippinet.com.au>
parents: 28720
diff changeset
68 */
72cbc41ccc2f Fix [ 771097 ] frozen programs fail due to implicit import of "warnings".
Mark Hammond <mhammond@skippinet.com.au>
parents: 28720
diff changeset
69 static PyObject *warnings_module = NULL;
72cbc41ccc2f Fix [ 771097 ] frozen programs fail due to implicit import of "warnings".
Mark Hammond <mhammond@skippinet.com.au>
parents: 28720
diff changeset
70
72cbc41ccc2f Fix [ 771097 ] frozen programs fail due to implicit import of "warnings".
Mark Hammond <mhammond@skippinet.com.au>
parents: 28720
diff changeset
71 /* Returns a borrowed reference to the 'warnings' module, or NULL.
72cbc41ccc2f Fix [ 771097 ] frozen programs fail due to implicit import of "warnings".
Mark Hammond <mhammond@skippinet.com.au>
parents: 28720
diff changeset
72 If the module is returned, it is guaranteed to have been obtained
72cbc41ccc2f Fix [ 771097 ] frozen programs fail due to implicit import of "warnings".
Mark Hammond <mhammond@skippinet.com.au>
parents: 28720
diff changeset
73 without acquiring the import lock
27958
e07b21ea3c45 Fix bug 683658 - PyErr_Warn may cause import deadlock.
Mark Hammond <mhammond@skippinet.com.au>
parents: 27878
diff changeset
74 */
30108
a88e6dfbfd6d Move initialization of sys.std{in,out}.encoding to Py_Initialize.
Martin v. Löwis <martin@v.loewis.de>
parents: 29879
diff changeset
75 PyObject *PyModule_GetWarningsModule(void)
29877
72cbc41ccc2f Fix [ 771097 ] frozen programs fail due to implicit import of "warnings".
Mark Hammond <mhammond@skippinet.com.au>
parents: 28720
diff changeset
76 {
72cbc41ccc2f Fix [ 771097 ] frozen programs fail due to implicit import of "warnings".
Mark Hammond <mhammond@skippinet.com.au>
parents: 28720
diff changeset
77 PyObject *typ, *val, *tb;
72cbc41ccc2f Fix [ 771097 ] frozen programs fail due to implicit import of "warnings".
Mark Hammond <mhammond@skippinet.com.au>
parents: 28720
diff changeset
78 PyObject *all_modules;
72cbc41ccc2f Fix [ 771097 ] frozen programs fail due to implicit import of "warnings".
Mark Hammond <mhammond@skippinet.com.au>
parents: 28720
diff changeset
79 /* If we managed to get the module at init time, just use it */
72cbc41ccc2f Fix [ 771097 ] frozen programs fail due to implicit import of "warnings".
Mark Hammond <mhammond@skippinet.com.au>
parents: 28720
diff changeset
80 if (warnings_module)
72cbc41ccc2f Fix [ 771097 ] frozen programs fail due to implicit import of "warnings".
Mark Hammond <mhammond@skippinet.com.au>
parents: 28720
diff changeset
81 return warnings_module;
72cbc41ccc2f Fix [ 771097 ] frozen programs fail due to implicit import of "warnings".
Mark Hammond <mhammond@skippinet.com.au>
parents: 28720
diff changeset
82 /* If it wasn't available at init time, it may be available
72cbc41ccc2f Fix [ 771097 ] frozen programs fail due to implicit import of "warnings".
Mark Hammond <mhammond@skippinet.com.au>
parents: 28720
diff changeset
83 now in sys.modules (common scenario is frozen apps: import
72cbc41ccc2f Fix [ 771097 ] frozen programs fail due to implicit import of "warnings".
Mark Hammond <mhammond@skippinet.com.au>
parents: 28720
diff changeset
84 at init time fails, but the frozen init code sets up sys.path
72cbc41ccc2f Fix [ 771097 ] frozen programs fail due to implicit import of "warnings".
Mark Hammond <mhammond@skippinet.com.au>
parents: 28720
diff changeset
85 correctly, then does an implicit import of warnings for us
72cbc41ccc2f Fix [ 771097 ] frozen programs fail due to implicit import of "warnings".
Mark Hammond <mhammond@skippinet.com.au>
parents: 28720
diff changeset
86 */
72cbc41ccc2f Fix [ 771097 ] frozen programs fail due to implicit import of "warnings".
Mark Hammond <mhammond@skippinet.com.au>
parents: 28720
diff changeset
87 /* Save and restore any exceptions */
72cbc41ccc2f Fix [ 771097 ] frozen programs fail due to implicit import of "warnings".
Mark Hammond <mhammond@skippinet.com.au>
parents: 28720
diff changeset
88 PyErr_Fetch(&typ, &val, &tb);
72cbc41ccc2f Fix [ 771097 ] frozen programs fail due to implicit import of "warnings".
Mark Hammond <mhammond@skippinet.com.au>
parents: 28720
diff changeset
89
29879
ed8709b1a3ef Correct previous patch looking for warnings module: sys.modules, not
Mark Hammond <mhammond@skippinet.com.au>
parents: 29877
diff changeset
90 all_modules = PySys_GetObject("modules");
29877
72cbc41ccc2f Fix [ 771097 ] frozen programs fail due to implicit import of "warnings".
Mark Hammond <mhammond@skippinet.com.au>
parents: 28720
diff changeset
91 if (all_modules) {
72cbc41ccc2f Fix [ 771097 ] frozen programs fail due to implicit import of "warnings".
Mark Hammond <mhammond@skippinet.com.au>
parents: 28720
diff changeset
92 warnings_module = PyDict_GetItemString(all_modules, "warnings");
72cbc41ccc2f Fix [ 771097 ] frozen programs fail due to implicit import of "warnings".
Mark Hammond <mhammond@skippinet.com.au>
parents: 28720
diff changeset
93 /* We keep a ref in the global */
72cbc41ccc2f Fix [ 771097 ] frozen programs fail due to implicit import of "warnings".
Mark Hammond <mhammond@skippinet.com.au>
parents: 28720
diff changeset
94 Py_XINCREF(warnings_module);
72cbc41ccc2f Fix [ 771097 ] frozen programs fail due to implicit import of "warnings".
Mark Hammond <mhammond@skippinet.com.au>
parents: 28720
diff changeset
95 }
72cbc41ccc2f Fix [ 771097 ] frozen programs fail due to implicit import of "warnings".
Mark Hammond <mhammond@skippinet.com.au>
parents: 28720
diff changeset
96 PyErr_Restore(typ, val, tb);
72cbc41ccc2f Fix [ 771097 ] frozen programs fail due to implicit import of "warnings".
Mark Hammond <mhammond@skippinet.com.au>
parents: 28720
diff changeset
97 return warnings_module;
72cbc41ccc2f Fix [ 771097 ] frozen programs fail due to implicit import of "warnings".
Mark Hammond <mhammond@skippinet.com.au>
parents: 28720
diff changeset
98 }
27958
e07b21ea3c45 Fix bug 683658 - PyErr_Warn may cause import deadlock.
Mark Hammond <mhammond@skippinet.com.au>
parents: 27878
diff changeset
99
5926
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
100 static int initialized = 0;
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
101
13620
5b727ab37092 Spelling fixes supplied by Rob W. W. Hooft. All these are fixes in either
Thomas Wouters <thomas@python.org>
parents: 13552
diff changeset
102 /* API to access the initialized flag -- useful for esoteric use */
6136
57045f51dd12 Added new Py_IsInitalized() API function to test the 'initialized' flag.
Guido van Rossum <guido@python.org>
parents: 6117
diff changeset
103
57045f51dd12 Added new Py_IsInitalized() API function to test the 'initialized' flag.
Guido van Rossum <guido@python.org>
parents: 6117
diff changeset
104 int
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13620
diff changeset
105 Py_IsInitialized(void)
6136
57045f51dd12 Added new Py_IsInitalized() API function to test the 'initialized' flag.
Guido van Rossum <guido@python.org>
parents: 6117
diff changeset
106 {
57045f51dd12 Added new Py_IsInitalized() API function to test the 'initialized' flag.
Guido van Rossum <guido@python.org>
parents: 6117
diff changeset
107 return initialized;
57045f51dd12 Added new Py_IsInitalized() API function to test the 'initialized' flag.
Guido van Rossum <guido@python.org>
parents: 6117
diff changeset
108 }
57045f51dd12 Added new Py_IsInitalized() API function to test the 'initialized' flag.
Guido van Rossum <guido@python.org>
parents: 6117
diff changeset
109
5926
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
110 /* Global initializations. Can be undone by Py_Finalize(). Don't
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
111 call this twice without an intervening Py_Finalize() call. When
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
112 initializations fail, a fatal error is issued and the function does
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
113 not return. On return, the first thread and interpreter state have
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
114 been created.
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
115
5926
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
116 Locking: you must hold the interpreter lock while calling this.
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
117 (If the lock has not yet been initialized, that's equivalent to
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
118 having the lock, but you cannot use multiple threads.)
2098
b7be0382f9ee __builtins__ mods (and sys_checkinterval for ceval.c)
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
119
5926
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
120 */
5405
b7871ca930ad Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents: 5234
diff changeset
121
20401
b74610c92d8b SF patch #467455 : Enhanced environment variables, by Toby Dickenson.
Guido van Rossum <guido@python.org>
parents: 19633
diff changeset
122 static int
b74610c92d8b SF patch #467455 : Enhanced environment variables, by Toby Dickenson.
Guido van Rossum <guido@python.org>
parents: 19633
diff changeset
123 add_flag(int flag, const char *envs)
b74610c92d8b SF patch #467455 : Enhanced environment variables, by Toby Dickenson.
Guido van Rossum <guido@python.org>
parents: 19633
diff changeset
124 {
b74610c92d8b SF patch #467455 : Enhanced environment variables, by Toby Dickenson.
Guido van Rossum <guido@python.org>
parents: 19633
diff changeset
125 int env = atoi(envs);
b74610c92d8b SF patch #467455 : Enhanced environment variables, by Toby Dickenson.
Guido van Rossum <guido@python.org>
parents: 19633
diff changeset
126 if (flag < env)
b74610c92d8b SF patch #467455 : Enhanced environment variables, by Toby Dickenson.
Guido van Rossum <guido@python.org>
parents: 19633
diff changeset
127 flag = env;
b74610c92d8b SF patch #467455 : Enhanced environment variables, by Toby Dickenson.
Guido van Rossum <guido@python.org>
parents: 19633
diff changeset
128 if (flag < 1)
b74610c92d8b SF patch #467455 : Enhanced environment variables, by Toby Dickenson.
Guido van Rossum <guido@python.org>
parents: 19633
diff changeset
129 flag = 1;
b74610c92d8b SF patch #467455 : Enhanced environment variables, by Toby Dickenson.
Guido van Rossum <guido@python.org>
parents: 19633
diff changeset
130 return flag;
b74610c92d8b SF patch #467455 : Enhanced environment variables, by Toby Dickenson.
Guido van Rossum <guido@python.org>
parents: 19633
diff changeset
131 }
b74610c92d8b SF patch #467455 : Enhanced environment variables, by Toby Dickenson.
Guido van Rossum <guido@python.org>
parents: 19633
diff changeset
132
5405
b7871ca930ad Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents: 5234
diff changeset
133 void
33189
de804edf8bcd Patch #900727: Add Py_InitializeEx to allow embedding without signals.
Martin v. Löwis <martin@v.loewis.de>
parents: 32781
diff changeset
134 Py_InitializeEx(int install_sigs)
5405
b7871ca930ad Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents: 5234
diff changeset
135 {
5926
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
136 PyInterpreterState *interp;
5405
b7871ca930ad Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents: 5234
diff changeset
137 PyThreadState *tstate;
5926
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
138 PyObject *bimod, *sysmod;
5850
6f67a1bda15a Moved some stuff here from main.c (part of a big restructuring - wait
Guido van Rossum <guido@python.org>
parents: 5611
diff changeset
139 char *p;
30108
a88e6dfbfd6d Move initialization of sys.std{in,out}.encoding to Py_Initialize.
Martin v. Löwis <martin@v.loewis.de>
parents: 29879
diff changeset
140 #if defined(Py_USING_UNICODE) && defined(HAVE_LANGINFO_H) && defined(CODESET)
a88e6dfbfd6d Move initialization of sys.std{in,out}.encoding to Py_Initialize.
Martin v. Löwis <martin@v.loewis.de>
parents: 29879
diff changeset
141 char *codeset;
a88e6dfbfd6d Move initialization of sys.std{in,out}.encoding to Py_Initialize.
Martin v. Löwis <martin@v.loewis.de>
parents: 29879
diff changeset
142 char *saved_locale;
a88e6dfbfd6d Move initialization of sys.std{in,out}.encoding to Py_Initialize.
Martin v. Löwis <martin@v.loewis.de>
parents: 29879
diff changeset
143 PyObject *sys_stream, *sys_isatty;
a88e6dfbfd6d Move initialization of sys.std{in,out}.encoding to Py_Initialize.
Martin v. Löwis <martin@v.loewis.de>
parents: 29879
diff changeset
144 #endif
19295
157bbb896198 Bunchathings:
Guido van Rossum <guido@python.org>
parents: 19237
diff changeset
145 extern void _Py_ReadyTypes(void);
5926
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
146
6201
dc974b3a6cf4 Two independent changes (oops):
Guido van Rossum <guido@python.org>
parents: 6197
diff changeset
147 if (initialized)
6117
98a282cfb013 Use a counter instead of a Boolean to check for initialized; n calls
Guido van Rossum <guido@python.org>
parents: 5939
diff changeset
148 return;
6201
dc974b3a6cf4 Two independent changes (oops):
Guido van Rossum <guido@python.org>
parents: 6197
diff changeset
149 initialized = 1;
28648
0e8416be208e Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 28647
diff changeset
150
18808
a8b1acc7a21f Add -E command line switch (ignore environment variables like PYTHONHOME
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 18630
diff changeset
151 if ((p = Py_GETENV("PYTHONDEBUG")) && *p != '\0')
20401
b74610c92d8b SF patch #467455 : Enhanced environment variables, by Toby Dickenson.
Guido van Rossum <guido@python.org>
parents: 19633
diff changeset
152 Py_DebugFlag = add_flag(Py_DebugFlag, p);
18808
a8b1acc7a21f Add -E command line switch (ignore environment variables like PYTHONHOME
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 18630
diff changeset
153 if ((p = Py_GETENV("PYTHONVERBOSE")) && *p != '\0')
20401
b74610c92d8b SF patch #467455 : Enhanced environment variables, by Toby Dickenson.
Guido van Rossum <guido@python.org>
parents: 19633
diff changeset
154 Py_VerboseFlag = add_flag(Py_VerboseFlag, p);
18808
a8b1acc7a21f Add -E command line switch (ignore environment variables like PYTHONHOME
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 18630
diff changeset
155 if ((p = Py_GETENV("PYTHONOPTIMIZE")) && *p != '\0')
20401
b74610c92d8b SF patch #467455 : Enhanced environment variables, by Toby Dickenson.
Guido van Rossum <guido@python.org>
parents: 19633
diff changeset
156 Py_OptimizeFlag = add_flag(Py_OptimizeFlag, p);
5850
6f67a1bda15a Moved some stuff here from main.c (part of a big restructuring - wait
Guido van Rossum <guido@python.org>
parents: 5611
diff changeset
157
5405
b7871ca930ad Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents: 5234
diff changeset
158 interp = PyInterpreterState_New();
b7871ca930ad Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents: 5234
diff changeset
159 if (interp == NULL)
5926
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
160 Py_FatalError("Py_Initialize: can't make first interpreter");
5850
6f67a1bda15a Moved some stuff here from main.c (part of a big restructuring - wait
Guido van Rossum <guido@python.org>
parents: 5611
diff changeset
161
5405
b7871ca930ad Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents: 5234
diff changeset
162 tstate = PyThreadState_New(interp);
b7871ca930ad Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents: 5234
diff changeset
163 if (tstate == NULL)
5926
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
164 Py_FatalError("Py_Initialize: can't make first thread");
5405
b7871ca930ad Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents: 5234
diff changeset
165 (void) PyThreadState_Swap(tstate);
b7871ca930ad Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents: 5234
diff changeset
166
19295
157bbb896198 Bunchathings:
Guido van Rossum <guido@python.org>
parents: 19237
diff changeset
167 _Py_ReadyTypes();
19040
4df72ea78ae8 - Rename PyType_InitDict() to PyType_Ready().
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
168
26923
dbfc3ef7d003 Since the *_Init() are private, prefix with _, suggested by Skip
Neal Norwitz <nnorwitz@gmail.com>
parents: 26889
diff changeset
169 if (!_PyFrame_Init())
26889
12753e7b15f7 SF #561244, Micro optimizations
Neal Norwitz <nnorwitz@gmail.com>
parents: 26885
diff changeset
170 Py_FatalError("Py_Initialize: can't init frames");
12753e7b15f7 SF #561244, Micro optimizations
Neal Norwitz <nnorwitz@gmail.com>
parents: 26885
diff changeset
171
26923
dbfc3ef7d003 Since the *_Init() are private, prefix with _, suggested by Skip
Neal Norwitz <nnorwitz@gmail.com>
parents: 26889
diff changeset
172 if (!_PyInt_Init())
26889
12753e7b15f7 SF #561244, Micro optimizations
Neal Norwitz <nnorwitz@gmail.com>
parents: 26885
diff changeset
173 Py_FatalError("Py_Initialize: can't init ints");
12753e7b15f7 SF #561244, Micro optimizations
Neal Norwitz <nnorwitz@gmail.com>
parents: 26885
diff changeset
174
34972
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34834
diff changeset
175 _PyFloat_Init();
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34834
diff changeset
176
5926
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
177 interp->modules = PyDict_New();
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
178 if (interp->modules == NULL)
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
179 Py_FatalError("Py_Initialize: can't make modules dictionary");
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
180
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19296
diff changeset
181 #ifdef Py_USING_UNICODE
11977
8b88e1bc0e1b Marc-Andre Lemburg: add calls to initialize and finalize Unicode and
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
182 /* Init Unicode implementation; relies on the codec registry */
8b88e1bc0e1b Marc-Andre Lemburg: add calls to initialize and finalize Unicode and
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
183 _PyUnicode_Init();
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19296
diff changeset
184 #endif
11977
8b88e1bc0e1b Marc-Andre Lemburg: add calls to initialize and finalize Unicode and
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
185
12754
5213abfe5230 Py_Initialize(): Now that standard exceptions are builtin, we don't
Barry Warsaw <barry@python.org>
parents: 12576
diff changeset
186 bimod = _PyBuiltin_Init();
5926
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
187 if (bimod == NULL)
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
188 Py_FatalError("Py_Initialize: can't initialize __builtin__");
6557
b331cd47e0bc Undo half of the previous change :-(
Guido van Rossum <guido@python.org>
parents: 6547
diff changeset
189 interp->builtins = PyModule_GetDict(bimod);
b331cd47e0bc Undo half of the previous change :-(
Guido van Rossum <guido@python.org>
parents: 6547
diff changeset
190 Py_INCREF(interp->builtins);
5926
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
191
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
192 sysmod = _PySys_Init();
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
193 if (sysmod == NULL)
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
194 Py_FatalError("Py_Initialize: can't initialize sys");
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
195 interp->sysdict = PyModule_GetDict(sysmod);
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
196 Py_INCREF(interp->sysdict);
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
197 _PyImport_FixupExtension("sys", "sys");
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
198 PySys_SetPath(Py_GetPath());
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
199 PyDict_SetItemString(interp->sysdict, "modules",
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
200 interp->modules);
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
201
11113
be8ec828c072 Marc-Andre Lemburg discovered that the switch from .pyc to .pyo files,
Guido van Rossum <guido@python.org>
parents: 10534
diff changeset
202 _PyImport_Init();
be8ec828c072 Marc-Andre Lemburg discovered that the switch from .pyc to .pyo files,
Guido van Rossum <guido@python.org>
parents: 10534
diff changeset
203
12754
5213abfe5230 Py_Initialize(): Now that standard exceptions are builtin, we don't
Barry Warsaw <barry@python.org>
parents: 12576
diff changeset
204 /* initialize builtin exceptions */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18808
diff changeset
205 _PyExc_Init();
19237
e110cd4fc3c6 Py_Initialize(): Apply patch by Jürgen Hermann to call
Barry Warsaw <barry@python.org>
parents: 19158
diff changeset
206 _PyImport_FixupExtension("exceptions", "exceptions");
12754
5213abfe5230 Py_Initialize(): Now that standard exceptions are builtin, we don't
Barry Warsaw <barry@python.org>
parents: 12576
diff changeset
207
6197
7da7ba5c6525 Added Py_UseClassExceptionsFlag, the variable containing the state of
Barry Warsaw <barry@python.org>
parents: 6165
diff changeset
208 /* phase 2 of builtins */
6328
dfeaf965f48a Py_Initialize(): move the call to _PyImport_FixupExtension() to after
Barry Warsaw <barry@python.org>
parents: 6322
diff changeset
209 _PyImport_FixupExtension("__builtin__", "__builtin__");
6197
7da7ba5c6525 Added Py_UseClassExceptionsFlag, the variable containing the state of
Barry Warsaw <barry@python.org>
parents: 6165
diff changeset
210
26885
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26643
diff changeset
211 _PyImportHooks_Init();
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26643
diff changeset
212
33189
de804edf8bcd Patch #900727: Add Py_InitializeEx to allow embedding without signals.
Martin v. Löwis <martin@v.loewis.de>
parents: 32781
diff changeset
213 if (install_sigs)
de804edf8bcd Patch #900727: Add Py_InitializeEx to allow embedding without signals.
Martin v. Löwis <martin@v.loewis.de>
parents: 32781
diff changeset
214 initsigs(); /* Signal handling stuff, including initintr() */
5926
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
215
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
216 initmain(); /* Module __main__ */
6201
dc974b3a6cf4 Two independent changes (oops):
Guido van Rossum <guido@python.org>
parents: 6197
diff changeset
217 if (!Py_NoSiteFlag)
dc974b3a6cf4 Two independent changes (oops):
Guido van Rossum <guido@python.org>
parents: 6197
diff changeset
218 initsite(); /* Module site */
28065
c61ef183ce2f Addendum to #683658:
Just van Rossum <just@letterror.com>
parents: 27958
diff changeset
219
28694
a4154dd5939a New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents: 28653
diff changeset
220 /* auto-thread-state API, if available */
a4154dd5939a New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents: 28653
diff changeset
221 #ifdef WITH_THREAD
a4154dd5939a New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents: 28653
diff changeset
222 _PyGILState_Init(interp, tstate);
a4154dd5939a New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents: 28653
diff changeset
223 #endif /* WITH_THREAD */
a4154dd5939a New PyGILState_ API - implements pep 311, from patch 684256.
Mark Hammond <mhammond@skippinet.com.au>
parents: 28653
diff changeset
224
29877
72cbc41ccc2f Fix [ 771097 ] frozen programs fail due to implicit import of "warnings".
Mark Hammond <mhammond@skippinet.com.au>
parents: 28720
diff changeset
225 warnings_module = PyImport_ImportModule("warnings");
72cbc41ccc2f Fix [ 771097 ] frozen programs fail due to implicit import of "warnings".
Mark Hammond <mhammond@skippinet.com.au>
parents: 28720
diff changeset
226 if (!warnings_module)
72cbc41ccc2f Fix [ 771097 ] frozen programs fail due to implicit import of "warnings".
Mark Hammond <mhammond@skippinet.com.au>
parents: 28720
diff changeset
227 PyErr_Clear();
28174
3f90d21debe9 Always initialize Py_FileSystemDefaultEncoding on Unix in Py_Initialize,
Martin v. Löwis <martin@v.loewis.de>
parents: 28065
diff changeset
228
3f90d21debe9 Always initialize Py_FileSystemDefaultEncoding on Unix in Py_Initialize,
Martin v. Löwis <martin@v.loewis.de>
parents: 28065
diff changeset
229 #if defined(Py_USING_UNICODE) && defined(HAVE_LANGINFO_H) && defined(CODESET)
3f90d21debe9 Always initialize Py_FileSystemDefaultEncoding on Unix in Py_Initialize,
Martin v. Löwis <martin@v.loewis.de>
parents: 28065
diff changeset
230 /* On Unix, set the file system encoding according to the
3f90d21debe9 Always initialize Py_FileSystemDefaultEncoding on Unix in Py_Initialize,
Martin v. Löwis <martin@v.loewis.de>
parents: 28065
diff changeset
231 user's preference, if the CODESET names a well-known
3f90d21debe9 Always initialize Py_FileSystemDefaultEncoding on Unix in Py_Initialize,
Martin v. Löwis <martin@v.loewis.de>
parents: 28065
diff changeset
232 Python codec, and Py_FileSystemDefaultEncoding isn't
30108
a88e6dfbfd6d Move initialization of sys.std{in,out}.encoding to Py_Initialize.
Martin v. Löwis <martin@v.loewis.de>
parents: 29879
diff changeset
233 initialized by other means. Also set the encoding of
a88e6dfbfd6d Move initialization of sys.std{in,out}.encoding to Py_Initialize.
Martin v. Löwis <martin@v.loewis.de>
parents: 29879
diff changeset
234 stdin and stdout if these are terminals. */
a88e6dfbfd6d Move initialization of sys.std{in,out}.encoding to Py_Initialize.
Martin v. Löwis <martin@v.loewis.de>
parents: 29879
diff changeset
235
30910
a29a585b03d9 Patch #804543: strdup saved locales. Backported to 2.3.
Martin v. Löwis <martin@v.loewis.de>
parents: 30683
diff changeset
236 saved_locale = strdup(setlocale(LC_CTYPE, NULL));
30108
a88e6dfbfd6d Move initialization of sys.std{in,out}.encoding to Py_Initialize.
Martin v. Löwis <martin@v.loewis.de>
parents: 29879
diff changeset
237 setlocale(LC_CTYPE, "");
a88e6dfbfd6d Move initialization of sys.std{in,out}.encoding to Py_Initialize.
Martin v. Löwis <martin@v.loewis.de>
parents: 29879
diff changeset
238 codeset = nl_langinfo(CODESET);
a88e6dfbfd6d Move initialization of sys.std{in,out}.encoding to Py_Initialize.
Martin v. Löwis <martin@v.loewis.de>
parents: 29879
diff changeset
239 if (codeset && *codeset) {
a88e6dfbfd6d Move initialization of sys.std{in,out}.encoding to Py_Initialize.
Martin v. Löwis <martin@v.loewis.de>
parents: 29879
diff changeset
240 PyObject *enc = PyCodec_Encoder(codeset);
a88e6dfbfd6d Move initialization of sys.std{in,out}.encoding to Py_Initialize.
Martin v. Löwis <martin@v.loewis.de>
parents: 29879
diff changeset
241 if (enc) {
a88e6dfbfd6d Move initialization of sys.std{in,out}.encoding to Py_Initialize.
Martin v. Löwis <martin@v.loewis.de>
parents: 29879
diff changeset
242 codeset = strdup(codeset);
a88e6dfbfd6d Move initialization of sys.std{in,out}.encoding to Py_Initialize.
Martin v. Löwis <martin@v.loewis.de>
parents: 29879
diff changeset
243 Py_DECREF(enc);
a88e6dfbfd6d Move initialization of sys.std{in,out}.encoding to Py_Initialize.
Martin v. Löwis <martin@v.loewis.de>
parents: 29879
diff changeset
244 } else {
a88e6dfbfd6d Move initialization of sys.std{in,out}.encoding to Py_Initialize.
Martin v. Löwis <martin@v.loewis.de>
parents: 29879
diff changeset
245 codeset = NULL;
a88e6dfbfd6d Move initialization of sys.std{in,out}.encoding to Py_Initialize.
Martin v. Löwis <martin@v.loewis.de>
parents: 29879
diff changeset
246 PyErr_Clear();
28174
3f90d21debe9 Always initialize Py_FileSystemDefaultEncoding on Unix in Py_Initialize,
Martin v. Löwis <martin@v.loewis.de>
parents: 28065
diff changeset
247 }
30108
a88e6dfbfd6d Move initialization of sys.std{in,out}.encoding to Py_Initialize.
Martin v. Löwis <martin@v.loewis.de>
parents: 29879
diff changeset
248 } else
a88e6dfbfd6d Move initialization of sys.std{in,out}.encoding to Py_Initialize.
Martin v. Löwis <martin@v.loewis.de>
parents: 29879
diff changeset
249 codeset = NULL;
a88e6dfbfd6d Move initialization of sys.std{in,out}.encoding to Py_Initialize.
Martin v. Löwis <martin@v.loewis.de>
parents: 29879
diff changeset
250 setlocale(LC_CTYPE, saved_locale);
30910
a29a585b03d9 Patch #804543: strdup saved locales. Backported to 2.3.
Martin v. Löwis <martin@v.loewis.de>
parents: 30683
diff changeset
251 free(saved_locale);
30108
a88e6dfbfd6d Move initialization of sys.std{in,out}.encoding to Py_Initialize.
Martin v. Löwis <martin@v.loewis.de>
parents: 29879
diff changeset
252
a88e6dfbfd6d Move initialization of sys.std{in,out}.encoding to Py_Initialize.
Martin v. Löwis <martin@v.loewis.de>
parents: 29879
diff changeset
253 if (codeset) {
30122
210676ec9d5f Fix refcounting and cut & paste error (?) in last checkin.
Michael W. Hudson <mwh@python.net>
parents: 30108
diff changeset
254 sys_stream = PySys_GetObject("stdin");
30108
a88e6dfbfd6d Move initialization of sys.std{in,out}.encoding to Py_Initialize.
Martin v. Löwis <martin@v.loewis.de>
parents: 29879
diff changeset
255 sys_isatty = PyObject_CallMethod(sys_stream, "isatty", "");
a88e6dfbfd6d Move initialization of sys.std{in,out}.encoding to Py_Initialize.
Martin v. Löwis <martin@v.loewis.de>
parents: 29879
diff changeset
256 if (!sys_isatty)
a88e6dfbfd6d Move initialization of sys.std{in,out}.encoding to Py_Initialize.
Martin v. Löwis <martin@v.loewis.de>
parents: 29879
diff changeset
257 PyErr_Clear();
a88e6dfbfd6d Move initialization of sys.std{in,out}.encoding to Py_Initialize.
Martin v. Löwis <martin@v.loewis.de>
parents: 29879
diff changeset
258 if(sys_isatty && PyObject_IsTrue(sys_isatty)) {
a88e6dfbfd6d Move initialization of sys.std{in,out}.encoding to Py_Initialize.
Martin v. Löwis <martin@v.loewis.de>
parents: 29879
diff changeset
259 if (!PyFile_SetEncoding(sys_stream, codeset))
a88e6dfbfd6d Move initialization of sys.std{in,out}.encoding to Py_Initialize.
Martin v. Löwis <martin@v.loewis.de>
parents: 29879
diff changeset
260 Py_FatalError("Cannot set codeset of stdin");
a88e6dfbfd6d Move initialization of sys.std{in,out}.encoding to Py_Initialize.
Martin v. Löwis <martin@v.loewis.de>
parents: 29879
diff changeset
261 }
a88e6dfbfd6d Move initialization of sys.std{in,out}.encoding to Py_Initialize.
Martin v. Löwis <martin@v.loewis.de>
parents: 29879
diff changeset
262 Py_XDECREF(sys_isatty);
a88e6dfbfd6d Move initialization of sys.std{in,out}.encoding to Py_Initialize.
Martin v. Löwis <martin@v.loewis.de>
parents: 29879
diff changeset
263
a88e6dfbfd6d Move initialization of sys.std{in,out}.encoding to Py_Initialize.
Martin v. Löwis <martin@v.loewis.de>
parents: 29879
diff changeset
264 sys_stream = PySys_GetObject("stdout");
a88e6dfbfd6d Move initialization of sys.std{in,out}.encoding to Py_Initialize.
Martin v. Löwis <martin@v.loewis.de>
parents: 29879
diff changeset
265 sys_isatty = PyObject_CallMethod(sys_stream, "isatty", "");
a88e6dfbfd6d Move initialization of sys.std{in,out}.encoding to Py_Initialize.
Martin v. Löwis <martin@v.loewis.de>
parents: 29879
diff changeset
266 if (!sys_isatty)
a88e6dfbfd6d Move initialization of sys.std{in,out}.encoding to Py_Initialize.
Martin v. Löwis <martin@v.loewis.de>
parents: 29879
diff changeset
267 PyErr_Clear();
a88e6dfbfd6d Move initialization of sys.std{in,out}.encoding to Py_Initialize.
Martin v. Löwis <martin@v.loewis.de>
parents: 29879
diff changeset
268 if(sys_isatty && PyObject_IsTrue(sys_isatty)) {
a88e6dfbfd6d Move initialization of sys.std{in,out}.encoding to Py_Initialize.
Martin v. Löwis <martin@v.loewis.de>
parents: 29879
diff changeset
269 if (!PyFile_SetEncoding(sys_stream, codeset))
a88e6dfbfd6d Move initialization of sys.std{in,out}.encoding to Py_Initialize.
Martin v. Löwis <martin@v.loewis.de>
parents: 29879
diff changeset
270 Py_FatalError("Cannot set codeset of stdout");
a88e6dfbfd6d Move initialization of sys.std{in,out}.encoding to Py_Initialize.
Martin v. Löwis <martin@v.loewis.de>
parents: 29879
diff changeset
271 }
a88e6dfbfd6d Move initialization of sys.std{in,out}.encoding to Py_Initialize.
Martin v. Löwis <martin@v.loewis.de>
parents: 29879
diff changeset
272 Py_XDECREF(sys_isatty);
a88e6dfbfd6d Move initialization of sys.std{in,out}.encoding to Py_Initialize.
Martin v. Löwis <martin@v.loewis.de>
parents: 29879
diff changeset
273
a88e6dfbfd6d Move initialization of sys.std{in,out}.encoding to Py_Initialize.
Martin v. Löwis <martin@v.loewis.de>
parents: 29879
diff changeset
274 if (!Py_FileSystemDefaultEncoding)
a88e6dfbfd6d Move initialization of sys.std{in,out}.encoding to Py_Initialize.
Martin v. Löwis <martin@v.loewis.de>
parents: 29879
diff changeset
275 Py_FileSystemDefaultEncoding = codeset;
a88e6dfbfd6d Move initialization of sys.std{in,out}.encoding to Py_Initialize.
Martin v. Löwis <martin@v.loewis.de>
parents: 29879
diff changeset
276 else
a88e6dfbfd6d Move initialization of sys.std{in,out}.encoding to Py_Initialize.
Martin v. Löwis <martin@v.loewis.de>
parents: 29879
diff changeset
277 free(codeset);
28174
3f90d21debe9 Always initialize Py_FileSystemDefaultEncoding on Unix in Py_Initialize,
Martin v. Löwis <martin@v.loewis.de>
parents: 28065
diff changeset
278 }
3f90d21debe9 Always initialize Py_FileSystemDefaultEncoding on Unix in Py_Initialize,
Martin v. Löwis <martin@v.loewis.de>
parents: 28065
diff changeset
279 #endif
5926
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
280 }
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
281
33189
de804edf8bcd Patch #900727: Add Py_InitializeEx to allow embedding without signals.
Martin v. Löwis <martin@v.loewis.de>
parents: 32781
diff changeset
282 void
de804edf8bcd Patch #900727: Add Py_InitializeEx to allow embedding without signals.
Martin v. Löwis <martin@v.loewis.de>
parents: 32781
diff changeset
283 Py_Initialize(void)
de804edf8bcd Patch #900727: Add Py_InitializeEx to allow embedding without signals.
Martin v. Löwis <martin@v.loewis.de>
parents: 32781
diff changeset
284 {
de804edf8bcd Patch #900727: Add Py_InitializeEx to allow embedding without signals.
Martin v. Löwis <martin@v.loewis.de>
parents: 32781
diff changeset
285 Py_InitializeEx(1);
de804edf8bcd Patch #900727: Add Py_InitializeEx to allow embedding without signals.
Martin v. Löwis <martin@v.loewis.de>
parents: 32781
diff changeset
286 }
de804edf8bcd Patch #900727: Add Py_InitializeEx to allow embedding without signals.
Martin v. Löwis <martin@v.loewis.de>
parents: 32781
diff changeset
287
de804edf8bcd Patch #900727: Add Py_InitializeEx to allow embedding without signals.
Martin v. Löwis <martin@v.loewis.de>
parents: 32781
diff changeset
288
9641
d152adf3f094 Move the prototype for dump_counts() to before where it is used.
Guido van Rossum <guido@python.org>
parents: 9285
diff changeset
289 #ifdef COUNT_ALLOCS
13447
a89f7f268de4 Nuke all remaining occurrences of Py_PROTO and Py_FPROTO.
Tim Peters <tim.peters@gmail.com>
parents: 13256
diff changeset
290 extern void dump_counts(void);
9641
d152adf3f094 Move the prototype for dump_counts() to before where it is used.
Guido van Rossum <guido@python.org>
parents: 9285
diff changeset
291 #endif
d152adf3f094 Move the prototype for dump_counts() to before where it is used.
Guido van Rossum <guido@python.org>
parents: 9285
diff changeset
292
5926
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
293 /* Undo the effect of Py_Initialize().
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
294
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
295 Beware: if multiple interpreter and/or thread states exist, these
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
296 are not wiped out; only the current thread and interpreter state
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
297 are deleted. But since everything else is deleted, those other
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
298 interpreter and thread states should no longer be used.
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
299
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
300 (XXX We should do better, e.g. wipe out all interpreters and
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
301 threads.)
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
302
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
303 Locking: as above.
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
304
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
305 */
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
306
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
307 void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13620
diff changeset
308 Py_Finalize(void)
5926
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
309 {
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
310 PyInterpreterState *interp;
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
311 PyThreadState *tstate;
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
312
6201
dc974b3a6cf4 Two independent changes (oops):
Guido van Rossum <guido@python.org>
parents: 6197
diff changeset
313 if (!initialized)
6117
98a282cfb013 Use a counter instead of a Boolean to check for initialized; n calls
Guido van Rossum <guido@python.org>
parents: 5939
diff changeset
314 return;
16073
713ed6c6bcb5 Bug #128475: mimetools.encode (sometimes) fails when called from a thread.
Tim Peters <tim.peters@gmail.com>
parents: 15963
diff changeset
315
713ed6c6bcb5 Bug #128475: mimetools.encode (sometimes) fails when called from a thread.
Tim Peters <tim.peters@gmail.com>
parents: 15963
diff changeset
316 /* The interpreter is still entirely intact at this point, and the
713ed6c6bcb5 Bug #128475: mimetools.encode (sometimes) fails when called from a thread.
Tim Peters <tim.peters@gmail.com>
parents: 15963
diff changeset
317 * exit funcs may be relying on that. In particular, if some thread
713ed6c6bcb5 Bug #128475: mimetools.encode (sometimes) fails when called from a thread.
Tim Peters <tim.peters@gmail.com>
parents: 15963
diff changeset
318 * or exit func is still waiting to do an import, the import machinery
713ed6c6bcb5 Bug #128475: mimetools.encode (sometimes) fails when called from a thread.
Tim Peters <tim.peters@gmail.com>
parents: 15963
diff changeset
319 * expects Py_IsInitialized() to return true. So don't say the
713ed6c6bcb5 Bug #128475: mimetools.encode (sometimes) fails when called from a thread.
Tim Peters <tim.peters@gmail.com>
parents: 15963
diff changeset
320 * interpreter is uninitialized until after the exit funcs have run.
713ed6c6bcb5 Bug #128475: mimetools.encode (sometimes) fails when called from a thread.
Tim Peters <tim.peters@gmail.com>
parents: 15963
diff changeset
321 * Note that Threading.py uses an exit func to do a join on all the
713ed6c6bcb5 Bug #128475: mimetools.encode (sometimes) fails when called from a thread.
Tim Peters <tim.peters@gmail.com>
parents: 15963
diff changeset
322 * threads created thru it, so this also protects pending imports in
713ed6c6bcb5 Bug #128475: mimetools.encode (sometimes) fails when called from a thread.
Tim Peters <tim.peters@gmail.com>
parents: 15963
diff changeset
323 * the threads created via Threading.
713ed6c6bcb5 Bug #128475: mimetools.encode (sometimes) fails when called from a thread.
Tim Peters <tim.peters@gmail.com>
parents: 15963
diff changeset
324 */
713ed6c6bcb5 Bug #128475: mimetools.encode (sometimes) fails when called from a thread.
Tim Peters <tim.peters@gmail.com>
parents: 15963
diff changeset
325 call_sys_exitfunc();
6201
dc974b3a6cf4 Two independent changes (oops):
Guido van Rossum <guido@python.org>
parents: 6197
diff changeset
326 initialized = 0;
5926
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
327
6547
724382af5339 Two independent changes (alas):
Guido van Rossum <guido@python.org>
parents: 6376
diff changeset
328 /* Get current thread state and interpreter pointer */
31797
846b3ee7e106 Changed random calls to PyThreadState_Get() to use the macro
Nicholas Bastin <nick.bastin@gmail.com>
parents: 31771
diff changeset
329 tstate = PyThreadState_GET();
5926
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
330 interp = tstate->interp;
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
331
6547
724382af5339 Two independent changes (alas):
Guido van Rossum <guido@python.org>
parents: 6376
diff changeset
332 /* Disable signal handling */
724382af5339 Two independent changes (alas):
Guido van Rossum <guido@python.org>
parents: 6376
diff changeset
333 PyOS_FiniInterrupts();
724382af5339 Two independent changes (alas):
Guido van Rossum <guido@python.org>
parents: 6376
diff changeset
334
27958
e07b21ea3c45 Fix bug 683658 - PyErr_Warn may cause import deadlock.
Mark Hammond <mhammond@skippinet.com.au>
parents: 27878
diff changeset
335 /* drop module references we saved */
29877
72cbc41ccc2f Fix [ 771097 ] frozen programs fail due to implicit import of "warnings".
Mark Hammond <mhammond@skippinet.com.au>
parents: 28720
diff changeset
336 Py_XDECREF(warnings_module);
72cbc41ccc2f Fix [ 771097 ] frozen programs fail due to implicit import of "warnings".
Mark Hammond <mhammond@skippinet.com.au>
parents: 28720
diff changeset
337 warnings_module = NULL;
27958
e07b21ea3c45 Fix bug 683658 - PyErr_Warn may cause import deadlock.
Mark Hammond <mhammond@skippinet.com.au>
parents: 27878
diff changeset
338
28651
7fda09f751ad - New C API PyGC_Collect(), same as calling gc.collect().
Guido van Rossum <guido@python.org>
parents: 28649
diff changeset
339 /* Collect garbage. This may call finalizers; it's nice to call these
31057
f69589179c55 Py_Finalize(): disabled the second call of cyclic gc, and added extensive
Tim Peters <tim.peters@gmail.com>
parents: 30979
diff changeset
340 * before all modules are destroyed.
f69589179c55 Py_Finalize(): disabled the second call of cyclic gc, and added extensive
Tim Peters <tim.peters@gmail.com>
parents: 30979
diff changeset
341 * XXX If a __del__ or weakref callback is triggered here, and tries to
f69589179c55 Py_Finalize(): disabled the second call of cyclic gc, and added extensive
Tim Peters <tim.peters@gmail.com>
parents: 30979
diff changeset
342 * XXX import a module, bad things can happen, because Python no
f69589179c55 Py_Finalize(): disabled the second call of cyclic gc, and added extensive
Tim Peters <tim.peters@gmail.com>
parents: 30979
diff changeset
343 * XXX longer believes it's initialized.
f69589179c55 Py_Finalize(): disabled the second call of cyclic gc, and added extensive
Tim Peters <tim.peters@gmail.com>
parents: 30979
diff changeset
344 * XXX Fatal Python error: Interpreter not initialized (version mismatch?)
f69589179c55 Py_Finalize(): disabled the second call of cyclic gc, and added extensive
Tim Peters <tim.peters@gmail.com>
parents: 30979
diff changeset
345 * XXX is easy to provoke that way. I've also seen, e.g.,
f69589179c55 Py_Finalize(): disabled the second call of cyclic gc, and added extensive
Tim Peters <tim.peters@gmail.com>
parents: 30979
diff changeset
346 * XXX Exception exceptions.ImportError: 'No module named sha'
f69589179c55 Py_Finalize(): disabled the second call of cyclic gc, and added extensive
Tim Peters <tim.peters@gmail.com>
parents: 30979
diff changeset
347 * XXX in <function callback at 0x008F5718> ignored
f69589179c55 Py_Finalize(): disabled the second call of cyclic gc, and added extensive
Tim Peters <tim.peters@gmail.com>
parents: 30979
diff changeset
348 * XXX but I'm unclear on exactly how that one happens. In any case,
f69589179c55 Py_Finalize(): disabled the second call of cyclic gc, and added extensive
Tim Peters <tim.peters@gmail.com>
parents: 30979
diff changeset
349 * XXX I haven't seen a real-life report of either of these.
f69589179c55 Py_Finalize(): disabled the second call of cyclic gc, and added extensive
Tim Peters <tim.peters@gmail.com>
parents: 30979
diff changeset
350 */
28651
7fda09f751ad - New C API PyGC_Collect(), same as calling gc.collect().
Guido van Rossum <guido@python.org>
parents: 28649
diff changeset
351 PyGC_Collect();
7fda09f751ad - New C API PyGC_Collect(), same as calling gc.collect().
Guido van Rossum <guido@python.org>
parents: 28649
diff changeset
352
6547
724382af5339 Two independent changes (alas):
Guido van Rossum <guido@python.org>
parents: 6376
diff changeset
353 /* Destroy all modules */
5926
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
354 PyImport_Cleanup();
6547
724382af5339 Two independent changes (alas):
Guido van Rossum <guido@python.org>
parents: 6376
diff changeset
355
28651
7fda09f751ad - New C API PyGC_Collect(), same as calling gc.collect().
Guido van Rossum <guido@python.org>
parents: 28649
diff changeset
356 /* Collect final garbage. This disposes of cycles created by
31057
f69589179c55 Py_Finalize(): disabled the second call of cyclic gc, and added extensive
Tim Peters <tim.peters@gmail.com>
parents: 30979
diff changeset
357 * new-style class definitions, for example.
f69589179c55 Py_Finalize(): disabled the second call of cyclic gc, and added extensive
Tim Peters <tim.peters@gmail.com>
parents: 30979
diff changeset
358 * XXX This is disabled because it caused too many problems. If
f69589179c55 Py_Finalize(): disabled the second call of cyclic gc, and added extensive
Tim Peters <tim.peters@gmail.com>
parents: 30979
diff changeset
359 * XXX a __del__ or weakref callback triggers here, Python code has
f69589179c55 Py_Finalize(): disabled the second call of cyclic gc, and added extensive
Tim Peters <tim.peters@gmail.com>
parents: 30979
diff changeset
360 * XXX a hard time running, because even the sys module has been
f69589179c55 Py_Finalize(): disabled the second call of cyclic gc, and added extensive
Tim Peters <tim.peters@gmail.com>
parents: 30979
diff changeset
361 * XXX cleared out (sys.stdout is gone, sys.excepthook is gone, etc).
f69589179c55 Py_Finalize(): disabled the second call of cyclic gc, and added extensive
Tim Peters <tim.peters@gmail.com>
parents: 30979
diff changeset
362 * XXX One symptom is a sequence of information-free messages
f69589179c55 Py_Finalize(): disabled the second call of cyclic gc, and added extensive
Tim Peters <tim.peters@gmail.com>
parents: 30979
diff changeset
363 * XXX coming from threads (if a __del__ or callback is invoked,
f69589179c55 Py_Finalize(): disabled the second call of cyclic gc, and added extensive
Tim Peters <tim.peters@gmail.com>
parents: 30979
diff changeset
364 * XXX other threads can execute too, and any exception they encounter
f69589179c55 Py_Finalize(): disabled the second call of cyclic gc, and added extensive
Tim Peters <tim.peters@gmail.com>
parents: 30979
diff changeset
365 * XXX triggers a comedy of errors as subsystem after subsystem
f69589179c55 Py_Finalize(): disabled the second call of cyclic gc, and added extensive
Tim Peters <tim.peters@gmail.com>
parents: 30979
diff changeset
366 * XXX fails to find what it *expects* to find in sys to help report
f69589179c55 Py_Finalize(): disabled the second call of cyclic gc, and added extensive
Tim Peters <tim.peters@gmail.com>
parents: 30979
diff changeset
367 * XXX the exception and consequent unexpected failures). I've also
f69589179c55 Py_Finalize(): disabled the second call of cyclic gc, and added extensive
Tim Peters <tim.peters@gmail.com>
parents: 30979
diff changeset
368 * XXX seen segfaults then, after adding print statements to the
f69589179c55 Py_Finalize(): disabled the second call of cyclic gc, and added extensive
Tim Peters <tim.peters@gmail.com>
parents: 30979
diff changeset
369 * XXX Python code getting called.
f69589179c55 Py_Finalize(): disabled the second call of cyclic gc, and added extensive
Tim Peters <tim.peters@gmail.com>
parents: 30979
diff changeset
370 */
f69589179c55 Py_Finalize(): disabled the second call of cyclic gc, and added extensive
Tim Peters <tim.peters@gmail.com>
parents: 30979
diff changeset
371 #if 0
28651
7fda09f751ad - New C API PyGC_Collect(), same as calling gc.collect().
Guido van Rossum <guido@python.org>
parents: 28649
diff changeset
372 PyGC_Collect();
31057
f69589179c55 Py_Finalize(): disabled the second call of cyclic gc, and added extensive
Tim Peters <tim.peters@gmail.com>
parents: 30979
diff changeset
373 #endif
28651
7fda09f751ad - New C API PyGC_Collect(), same as calling gc.collect().
Guido van Rossum <guido@python.org>
parents: 28649
diff changeset
374
6786
bc007cf1d6ba Changed the finalization order again so that the reference count
Guido van Rossum <guido@python.org>
parents: 6661
diff changeset
375 /* Destroy the database used by _PyImport_{Fixup,Find}Extension */
bc007cf1d6ba Changed the finalization order again so that the reference count
Guido van Rossum <guido@python.org>
parents: 6661
diff changeset
376 _PyImport_Fini();
bc007cf1d6ba Changed the finalization order again so that the reference count
Guido van Rossum <guido@python.org>
parents: 6661
diff changeset
377
bc007cf1d6ba Changed the finalization order again so that the reference count
Guido van Rossum <guido@python.org>
parents: 6661
diff changeset
378 /* Debugging stuff */
bc007cf1d6ba Changed the finalization order again so that the reference count
Guido van Rossum <guido@python.org>
parents: 6661
diff changeset
379 #ifdef COUNT_ALLOCS
bc007cf1d6ba Changed the finalization order again so that the reference count
Guido van Rossum <guido@python.org>
parents: 6661
diff changeset
380 dump_counts();
bc007cf1d6ba Changed the finalization order again so that the reference count
Guido van Rossum <guido@python.org>
parents: 6661
diff changeset
381 #endif
bc007cf1d6ba Changed the finalization order again so that the reference count
Guido van Rossum <guido@python.org>
parents: 6661
diff changeset
382
bc007cf1d6ba Changed the finalization order again so that the reference count
Guido van Rossum <guido@python.org>
parents: 6661
diff changeset
383 #ifdef Py_REF_DEBUG
bc007cf1d6ba Changed the finalization order again so that the reference count
Guido van Rossum <guido@python.org>
parents: 6661
diff changeset
384 fprintf(stderr, "[%ld refs]\n", _Py_RefTotal);
bc007cf1d6ba Changed the finalization order again so that the reference count
Guido van Rossum <guido@python.org>
parents: 6661
diff changeset
385 #endif
bc007cf1d6ba Changed the finalization order again so that the reference count
Guido van Rossum <guido@python.org>
parents: 6661
diff changeset
386
28647
9d4e138494fb Py_Finalize(): Reverted recent changes that tried to move the
Tim Peters <tim.peters@gmail.com>
parents: 28616
diff changeset
387 #ifdef Py_TRACE_REFS
9d4e138494fb Py_Finalize(): Reverted recent changes that tried to move the
Tim Peters <tim.peters@gmail.com>
parents: 28616
diff changeset
388 /* Display all objects still alive -- this can invoke arbitrary
9d4e138494fb Py_Finalize(): Reverted recent changes that tried to move the
Tim Peters <tim.peters@gmail.com>
parents: 28616
diff changeset
389 * __repr__ overrides, so requires a mostly-intact interpreter.
9d4e138494fb Py_Finalize(): Reverted recent changes that tried to move the
Tim Peters <tim.peters@gmail.com>
parents: 28616
diff changeset
390 * Alas, a lot of stuff may still be alive now that will be cleaned
9d4e138494fb Py_Finalize(): Reverted recent changes that tried to move the
Tim Peters <tim.peters@gmail.com>
parents: 28616
diff changeset
391 * up later.
9d4e138494fb Py_Finalize(): Reverted recent changes that tried to move the
Tim Peters <tim.peters@gmail.com>
parents: 28616
diff changeset
392 */
28653
56006d97bcef _Py_PrintReferences(): Changed to print object address at start of each
Tim Peters <tim.peters@gmail.com>
parents: 28651
diff changeset
393 if (Py_GETENV("PYTHONDUMPREFS"))
28647
9d4e138494fb Py_Finalize(): Reverted recent changes that tried to move the
Tim Peters <tim.peters@gmail.com>
parents: 28616
diff changeset
394 _Py_PrintReferences(stderr);
9d4e138494fb Py_Finalize(): Reverted recent changes that tried to move the
Tim Peters <tim.peters@gmail.com>
parents: 28616
diff changeset
395 #endif /* Py_TRACE_REFS */
9d4e138494fb Py_Finalize(): Reverted recent changes that tried to move the
Tim Peters <tim.peters@gmail.com>
parents: 28616
diff changeset
396
28720
03af46d1ac28 PyGILState cleanup was too early - destructors called via module cleanup may use the API.
Mark Hammond <mhammond@skippinet.com.au>
parents: 28698
diff changeset
397 /* Cleanup auto-thread-state */
03af46d1ac28 PyGILState cleanup was too early - destructors called via module cleanup may use the API.
Mark Hammond <mhammond@skippinet.com.au>
parents: 28698
diff changeset
398 #ifdef WITH_THREAD
03af46d1ac28 PyGILState cleanup was too early - destructors called via module cleanup may use the API.
Mark Hammond <mhammond@skippinet.com.au>
parents: 28698
diff changeset
399 _PyGILState_Fini();
03af46d1ac28 PyGILState cleanup was too early - destructors called via module cleanup may use the API.
Mark Hammond <mhammond@skippinet.com.au>
parents: 28698
diff changeset
400 #endif /* WITH_THREAD */
03af46d1ac28 PyGILState cleanup was too early - destructors called via module cleanup may use the API.
Mark Hammond <mhammond@skippinet.com.au>
parents: 28698
diff changeset
401
28613
b1671550c7e1 Move the call to _Py_PrintReferences() a bit further down. This
Guido van Rossum <guido@python.org>
parents: 28574
diff changeset
402 /* Clear interpreter state */
b1671550c7e1 Move the call to _Py_PrintReferences() a bit further down. This
Guido van Rossum <guido@python.org>
parents: 28574
diff changeset
403 PyInterpreterState_Clear(interp);
b1671550c7e1 Move the call to _Py_PrintReferences() a bit further down. This
Guido van Rossum <guido@python.org>
parents: 28574
diff changeset
404
34834
7cfda9898863 Move exception finalisation later in the shutdown process - this
Anthony Baxter <anthonybaxter@gmail.com>
parents: 34664
diff changeset
405 /* Now we decref the exception classes. After this point nothing
7cfda9898863 Move exception finalisation later in the shutdown process - this
Anthony Baxter <anthonybaxter@gmail.com>
parents: 34664
diff changeset
406 can raise an exception. That's okay, because each Fini() method
7cfda9898863 Move exception finalisation later in the shutdown process - this
Anthony Baxter <anthonybaxter@gmail.com>
parents: 34664
diff changeset
407 below has been checked to make sure no exceptions are ever
7cfda9898863 Move exception finalisation later in the shutdown process - this
Anthony Baxter <anthonybaxter@gmail.com>
parents: 34664
diff changeset
408 raised.
7cfda9898863 Move exception finalisation later in the shutdown process - this
Anthony Baxter <anthonybaxter@gmail.com>
parents: 34664
diff changeset
409 */
7cfda9898863 Move exception finalisation later in the shutdown process - this
Anthony Baxter <anthonybaxter@gmail.com>
parents: 34664
diff changeset
410
7cfda9898863 Move exception finalisation later in the shutdown process - this
Anthony Baxter <anthonybaxter@gmail.com>
parents: 34664
diff changeset
411 _PyExc_Fini();
7cfda9898863 Move exception finalisation later in the shutdown process - this
Anthony Baxter <anthonybaxter@gmail.com>
parents: 34664
diff changeset
412
6557
b331cd47e0bc Undo half of the previous change :-(
Guido van Rossum <guido@python.org>
parents: 6547
diff changeset
413 /* Delete current thread */
5926
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
414 PyThreadState_Swap(NULL);
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
415 PyInterpreterState_Delete(interp);
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
416
28613
b1671550c7e1 Move the call to _Py_PrintReferences() a bit further down. This
Guido van Rossum <guido@python.org>
parents: 28574
diff changeset
417 /* Sundry finalizers */
5939
2c65bb6ab87c Merge Py_Cleanup() into Py_Finalize(). Call the various small Fini()
Guido van Rossum <guido@python.org>
parents: 5926
diff changeset
418 PyMethod_Fini();
2c65bb6ab87c Merge Py_Cleanup() into Py_Finalize(). Call the various small Fini()
Guido van Rossum <guido@python.org>
parents: 5926
diff changeset
419 PyFrame_Fini();
2c65bb6ab87c Merge Py_Cleanup() into Py_Finalize(). Call the various small Fini()
Guido van Rossum <guido@python.org>
parents: 5926
diff changeset
420 PyCFunction_Fini();
2c65bb6ab87c Merge Py_Cleanup() into Py_Finalize(). Call the various small Fini()
Guido van Rossum <guido@python.org>
parents: 5926
diff changeset
421 PyTuple_Fini();
33679
55fba88930bc Finalize the freelist of list objects.
Raymond Hettinger <python@rcn.com>
parents: 33189
diff changeset
422 PyList_Fini();
35297
26bf6c27e9ce * Improve code for the empty frozenset singleton:
Raymond Hettinger <python@rcn.com>
parents: 34972
diff changeset
423 PySet_Fini();
5926
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
424 PyString_Fini();
5939
2c65bb6ab87c Merge Py_Cleanup() into Py_Finalize(). Call the various small Fini()
Guido van Rossum <guido@python.org>
parents: 5926
diff changeset
425 PyInt_Fini();
2c65bb6ab87c Merge Py_Cleanup() into Py_Finalize(). Call the various small Fini()
Guido van Rossum <guido@python.org>
parents: 5926
diff changeset
426 PyFloat_Fini();
2c65bb6ab87c Merge Py_Cleanup() into Py_Finalize(). Call the various small Fini()
Guido van Rossum <guido@python.org>
parents: 5926
diff changeset
427
22883
d8bc68cb17e0 Move Unicode finalization further down in the chain.
Marc-André Lemburg <mal@egenix.com>
parents: 22721
diff changeset
428 #ifdef Py_USING_UNICODE
d8bc68cb17e0 Move Unicode finalization further down in the chain.
Marc-André Lemburg <mal@egenix.com>
parents: 22721
diff changeset
429 /* Cleanup Unicode implementation */
d8bc68cb17e0 Move Unicode finalization further down in the chain.
Marc-André Lemburg <mal@egenix.com>
parents: 22721
diff changeset
430 _PyUnicode_Fini();
d8bc68cb17e0 Move Unicode finalization further down in the chain.
Marc-André Lemburg <mal@egenix.com>
parents: 22721
diff changeset
431 #endif
d8bc68cb17e0 Move Unicode finalization further down in the chain.
Marc-André Lemburg <mal@egenix.com>
parents: 22721
diff changeset
432
5939
2c65bb6ab87c Merge Py_Cleanup() into Py_Finalize(). Call the various small Fini()
Guido van Rossum <guido@python.org>
parents: 5926
diff changeset
433 /* XXX Still allocated:
2c65bb6ab87c Merge Py_Cleanup() into Py_Finalize(). Call the various small Fini()
Guido van Rossum <guido@python.org>
parents: 5926
diff changeset
434 - various static ad-hoc pointers to interned strings
2c65bb6ab87c Merge Py_Cleanup() into Py_Finalize(). Call the various small Fini()
Guido van Rossum <guido@python.org>
parents: 5926
diff changeset
435 - int and float free list blocks
2c65bb6ab87c Merge Py_Cleanup() into Py_Finalize(). Call the various small Fini()
Guido van Rossum <guido@python.org>
parents: 5926
diff changeset
436 - whatever various modules and libraries allocate
2c65bb6ab87c Merge Py_Cleanup() into Py_Finalize(). Call the various small Fini()
Guido van Rossum <guido@python.org>
parents: 5926
diff changeset
437 */
5926
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
438
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
439 PyGrammar_RemoveAccelerators(&_PyParser_Grammar);
5939
2c65bb6ab87c Merge Py_Cleanup() into Py_Finalize(). Call the various small Fini()
Guido van Rossum <guido@python.org>
parents: 5926
diff changeset
440
28653
56006d97bcef _Py_PrintReferences(): Changed to print object address at start of each
Tim Peters <tim.peters@gmail.com>
parents: 28651
diff changeset
441 #ifdef Py_TRACE_REFS
56006d97bcef _Py_PrintReferences(): Changed to print object address at start of each
Tim Peters <tim.peters@gmail.com>
parents: 28651
diff changeset
442 /* Display addresses (& refcnts) of all objects still alive.
56006d97bcef _Py_PrintReferences(): Changed to print object address at start of each
Tim Peters <tim.peters@gmail.com>
parents: 28651
diff changeset
443 * An address can be used to find the repr of the object, printed
56006d97bcef _Py_PrintReferences(): Changed to print object address at start of each
Tim Peters <tim.peters@gmail.com>
parents: 28651
diff changeset
444 * above by _Py_PrintReferences.
56006d97bcef _Py_PrintReferences(): Changed to print object address at start of each
Tim Peters <tim.peters@gmail.com>
parents: 28651
diff changeset
445 */
56006d97bcef _Py_PrintReferences(): Changed to print object address at start of each
Tim Peters <tim.peters@gmail.com>
parents: 28651
diff changeset
446 if (Py_GETENV("PYTHONDUMPREFS"))
56006d97bcef _Py_PrintReferences(): Changed to print object address at start of each
Tim Peters <tim.peters@gmail.com>
parents: 28651
diff changeset
447 _Py_PrintReferenceAddresses(stderr);
56006d97bcef _Py_PrintReferences(): Changed to print object address at start of each
Tim Peters <tim.peters@gmail.com>
parents: 28651
diff changeset
448 #endif /* Py_TRACE_REFS */
23020
e2848618bd19 _PyObject_DebugDumpStats: renamed to _PyObject_DebugMallocStats.
Tim Peters <tim.peters@gmail.com>
parents: 22883
diff changeset
449 #ifdef PYMALLOC_DEBUG
e2848618bd19 _PyObject_DebugDumpStats: renamed to _PyObject_DebugMallocStats.
Tim Peters <tim.peters@gmail.com>
parents: 22883
diff changeset
450 if (Py_GETENV("PYTHONMALLOCSTATS"))
e2848618bd19 _PyObject_DebugDumpStats: renamed to _PyObject_DebugMallocStats.
Tim Peters <tim.peters@gmail.com>
parents: 22883
diff changeset
451 _PyObject_DebugMallocStats();
e2848618bd19 _PyObject_DebugDumpStats: renamed to _PyObject_DebugMallocStats.
Tim Peters <tim.peters@gmail.com>
parents: 22883
diff changeset
452 #endif
e2848618bd19 _PyObject_DebugDumpStats: renamed to _PyObject_DebugMallocStats.
Tim Peters <tim.peters@gmail.com>
parents: 22883
diff changeset
453
5939
2c65bb6ab87c Merge Py_Cleanup() into Py_Finalize(). Call the various small Fini()
Guido van Rossum <guido@python.org>
parents: 5926
diff changeset
454 call_ll_exitfuncs();
5926
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
455 }
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
456
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
457 /* Create and initialize a new interpreter and thread, and return the
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
458 new thread. This requires that Py_Initialize() has been called
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
459 first.
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
460
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
461 Unsuccessful initialization yields a NULL pointer. Note that *no*
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
462 exception information is available even in this case -- the
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
463 exception information is held in the thread, and there is no
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
464 thread.
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
465
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
466 Locking: as above.
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
467
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
468 */
5405
b7871ca930ad Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents: 5234
diff changeset
469
5926
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
470 PyThreadState *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13620
diff changeset
471 Py_NewInterpreter(void)
5926
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
472 {
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
473 PyInterpreterState *interp;
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
474 PyThreadState *tstate, *save_tstate;
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
475 PyObject *bimod, *sysmod;
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
476
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
477 if (!initialized)
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
478 Py_FatalError("Py_NewInterpreter: call Py_Initialize first");
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
479
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
480 interp = PyInterpreterState_New();
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
481 if (interp == NULL)
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
482 return NULL;
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
483
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
484 tstate = PyThreadState_New(interp);
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
485 if (tstate == NULL) {
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
486 PyInterpreterState_Delete(interp);
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
487 return NULL;
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
488 }
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
489
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
490 save_tstate = PyThreadState_Swap(tstate);
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
491
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
492 /* XXX The following is lax in error checking */
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
493
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
494 interp->modules = PyDict_New();
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
495
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
496 bimod = _PyImport_FindExtension("__builtin__", "__builtin__");
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
497 if (bimod != NULL) {
6557
b331cd47e0bc Undo half of the previous change :-(
Guido van Rossum <guido@python.org>
parents: 6547
diff changeset
498 interp->builtins = PyModule_GetDict(bimod);
b331cd47e0bc Undo half of the previous change :-(
Guido van Rossum <guido@python.org>
parents: 6547
diff changeset
499 Py_INCREF(interp->builtins);
5926
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
500 }
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
501 sysmod = _PyImport_FindExtension("sys", "sys");
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
502 if (bimod != NULL && sysmod != NULL) {
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
503 interp->sysdict = PyModule_GetDict(sysmod);
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
504 Py_INCREF(interp->sysdict);
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
505 PySys_SetPath(Py_GetPath());
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
506 PyDict_SetItemString(interp->sysdict, "modules",
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
507 interp->modules);
27340
8b53b0ce06d4 Patch #671459: Invoke import hooks in Py_NewInterpreter.
Martin v. Löwis <martin@v.loewis.de>
parents: 26923
diff changeset
508 _PyImportHooks_Init();
5926
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
509 initmain();
6201
dc974b3a6cf4 Two independent changes (oops):
Guido van Rossum <guido@python.org>
parents: 6197
diff changeset
510 if (!Py_NoSiteFlag)
dc974b3a6cf4 Two independent changes (oops):
Guido van Rossum <guido@python.org>
parents: 6197
diff changeset
511 initsite();
5926
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
512 }
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
513
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
514 if (!PyErr_Occurred())
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
515 return tstate;
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
516
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
517 /* Oops, it didn't work. Undo it all. */
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
518
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
519 PyErr_Print();
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
520 PyThreadState_Clear(tstate);
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
521 PyThreadState_Swap(save_tstate);
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
522 PyThreadState_Delete(tstate);
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
523 PyInterpreterState_Delete(interp);
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
524
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
525 return NULL;
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
526 }
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
527
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
528 /* Delete an interpreter and its last thread. This requires that the
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
529 given thread state is current, that the thread has no remaining
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
530 frames, and that it is its interpreter's only remaining thread.
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
531 It is a fatal error to violate these constraints.
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
532
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
533 (Py_Finalize() doesn't have these constraints -- it zaps
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
534 everything, regardless.)
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
535
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
536 Locking: as above.
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
537
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
538 */
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
539
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
540 void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13620
diff changeset
541 Py_EndInterpreter(PyThreadState *tstate)
5926
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
542 {
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
543 PyInterpreterState *interp = tstate->interp;
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
544
31797
846b3ee7e106 Changed random calls to PyThreadState_Get() to use the macro
Nicholas Bastin <nick.bastin@gmail.com>
parents: 31771
diff changeset
545 if (tstate != PyThreadState_GET())
5926
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
546 Py_FatalError("Py_EndInterpreter: thread is not current");
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
547 if (tstate->frame != NULL)
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
548 Py_FatalError("Py_EndInterpreter: thread still has a frame");
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
549 if (tstate != interp->tstate_head || tstate->next != NULL)
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
550 Py_FatalError("Py_EndInterpreter: not the last thread");
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
551
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
552 PyImport_Cleanup();
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
553 PyInterpreterState_Clear(interp);
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
554 PyThreadState_Swap(NULL);
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
555 PyInterpreterState_Delete(interp);
5850
6f67a1bda15a Moved some stuff here from main.c (part of a big restructuring - wait
Guido van Rossum <guido@python.org>
parents: 5611
diff changeset
556 }
6f67a1bda15a Moved some stuff here from main.c (part of a big restructuring - wait
Guido van Rossum <guido@python.org>
parents: 5611
diff changeset
557
6f67a1bda15a Moved some stuff here from main.c (part of a big restructuring - wait
Guido van Rossum <guido@python.org>
parents: 5611
diff changeset
558 static char *progname = "python";
6f67a1bda15a Moved some stuff here from main.c (part of a big restructuring - wait
Guido van Rossum <guido@python.org>
parents: 5611
diff changeset
559
6f67a1bda15a Moved some stuff here from main.c (part of a big restructuring - wait
Guido van Rossum <guido@python.org>
parents: 5611
diff changeset
560 void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13620
diff changeset
561 Py_SetProgramName(char *pn)
5850
6f67a1bda15a Moved some stuff here from main.c (part of a big restructuring - wait
Guido van Rossum <guido@python.org>
parents: 5611
diff changeset
562 {
6f67a1bda15a Moved some stuff here from main.c (part of a big restructuring - wait
Guido van Rossum <guido@python.org>
parents: 5611
diff changeset
563 if (pn && *pn)
6f67a1bda15a Moved some stuff here from main.c (part of a big restructuring - wait
Guido van Rossum <guido@python.org>
parents: 5611
diff changeset
564 progname = pn;
6f67a1bda15a Moved some stuff here from main.c (part of a big restructuring - wait
Guido van Rossum <guido@python.org>
parents: 5611
diff changeset
565 }
6f67a1bda15a Moved some stuff here from main.c (part of a big restructuring - wait
Guido van Rossum <guido@python.org>
parents: 5611
diff changeset
566
6f67a1bda15a Moved some stuff here from main.c (part of a big restructuring - wait
Guido van Rossum <guido@python.org>
parents: 5611
diff changeset
567 char *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13620
diff changeset
568 Py_GetProgramName(void)
5850
6f67a1bda15a Moved some stuff here from main.c (part of a big restructuring - wait
Guido van Rossum <guido@python.org>
parents: 5611
diff changeset
569 {
6f67a1bda15a Moved some stuff here from main.c (part of a big restructuring - wait
Guido van Rossum <guido@python.org>
parents: 5611
diff changeset
570 return progname;
5405
b7871ca930ad Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents: 5234
diff changeset
571 }
b7871ca930ad Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents: 5234
diff changeset
572
7206
3f255aedf621 Ehm, three unrelated changes.
Guido van Rossum <guido@python.org>
parents: 7113
diff changeset
573 static char *default_home = NULL;
3f255aedf621 Ehm, three unrelated changes.
Guido van Rossum <guido@python.org>
parents: 7113
diff changeset
574
3f255aedf621 Ehm, three unrelated changes.
Guido van Rossum <guido@python.org>
parents: 7113
diff changeset
575 void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13620
diff changeset
576 Py_SetPythonHome(char *home)
7206
3f255aedf621 Ehm, three unrelated changes.
Guido van Rossum <guido@python.org>
parents: 7113
diff changeset
577 {
3f255aedf621 Ehm, three unrelated changes.
Guido van Rossum <guido@python.org>
parents: 7113
diff changeset
578 default_home = home;
3f255aedf621 Ehm, three unrelated changes.
Guido van Rossum <guido@python.org>
parents: 7113
diff changeset
579 }
3f255aedf621 Ehm, three unrelated changes.
Guido van Rossum <guido@python.org>
parents: 7113
diff changeset
580
3f255aedf621 Ehm, three unrelated changes.
Guido van Rossum <guido@python.org>
parents: 7113
diff changeset
581 char *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13620
diff changeset
582 Py_GetPythonHome(void)
7206
3f255aedf621 Ehm, three unrelated changes.
Guido van Rossum <guido@python.org>
parents: 7113
diff changeset
583 {
3f255aedf621 Ehm, three unrelated changes.
Guido van Rossum <guido@python.org>
parents: 7113
diff changeset
584 char *home = default_home;
18808
a8b1acc7a21f Add -E command line switch (ignore environment variables like PYTHONHOME
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 18630
diff changeset
585 if (home == NULL && !Py_IgnoreEnvironmentFlag)
a8b1acc7a21f Add -E command line switch (ignore environment variables like PYTHONHOME
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 18630
diff changeset
586 home = Py_GETENV("PYTHONHOME");
7206
3f255aedf621 Ehm, three unrelated changes.
Guido van Rossum <guido@python.org>
parents: 7113
diff changeset
587 return home;
3f255aedf621 Ehm, three unrelated changes.
Guido van Rossum <guido@python.org>
parents: 7113
diff changeset
588 }
3f255aedf621 Ehm, three unrelated changes.
Guido van Rossum <guido@python.org>
parents: 7113
diff changeset
589
2098
b7be0382f9ee __builtins__ mods (and sys_checkinterval for ceval.c)
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
590 /* Create __main__ module */
b7be0382f9ee __builtins__ mods (and sys_checkinterval for ceval.c)
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
591
b7be0382f9ee __builtins__ mods (and sys_checkinterval for ceval.c)
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
592 static void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13620
diff changeset
593 initmain(void)
2098
b7be0382f9ee __builtins__ mods (and sys_checkinterval for ceval.c)
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
594 {
5170
f5d707ce4223 Greatly renamed. Not a very thorough job -- I'm going to restructure
Guido van Rossum <guido@python.org>
parents: 5108
diff changeset
595 PyObject *m, *d;
f5d707ce4223 Greatly renamed. Not a very thorough job -- I'm going to restructure
Guido van Rossum <guido@python.org>
parents: 5108
diff changeset
596 m = PyImport_AddModule("__main__");
2098
b7be0382f9ee __builtins__ mods (and sys_checkinterval for ceval.c)
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
597 if (m == NULL)
5170
f5d707ce4223 Greatly renamed. Not a very thorough job -- I'm going to restructure
Guido van Rossum <guido@python.org>
parents: 5108
diff changeset
598 Py_FatalError("can't create __main__ module");
f5d707ce4223 Greatly renamed. Not a very thorough job -- I'm going to restructure
Guido van Rossum <guido@python.org>
parents: 5108
diff changeset
599 d = PyModule_GetDict(m);
f5d707ce4223 Greatly renamed. Not a very thorough job -- I'm going to restructure
Guido van Rossum <guido@python.org>
parents: 5108
diff changeset
600 if (PyDict_GetItemString(d, "__builtins__") == NULL) {
6600
83ccf44bf195 Two changes (here we go again :-( ).
Guido van Rossum <guido@python.org>
parents: 6557
diff changeset
601 PyObject *bimod = PyImport_ImportModule("__builtin__");
83ccf44bf195 Two changes (here we go again :-( ).
Guido van Rossum <guido@python.org>
parents: 6557
diff changeset
602 if (bimod == NULL ||
83ccf44bf195 Two changes (here we go again :-( ).
Guido van Rossum <guido@python.org>
parents: 6557
diff changeset
603 PyDict_SetItemString(d, "__builtins__", bimod) != 0)
5170
f5d707ce4223 Greatly renamed. Not a very thorough job -- I'm going to restructure
Guido van Rossum <guido@python.org>
parents: 5108
diff changeset
604 Py_FatalError("can't add __builtins__ to __main__");
10024
caf2c65a6bcf initmain(): Nailed a memory leak. bimod must be DECREF'd!
Barry Warsaw <barry@python.org>
parents: 9965
diff changeset
605 Py_DECREF(bimod);
2098
b7be0382f9ee __builtins__ mods (and sys_checkinterval for ceval.c)
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
606 }
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
607 }
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
608
6201
dc974b3a6cf4 Two independent changes (oops):
Guido van Rossum <guido@python.org>
parents: 6197
diff changeset
609 /* Import the site module (not into __main__ though) */
dc974b3a6cf4 Two independent changes (oops):
Guido van Rossum <guido@python.org>
parents: 6197
diff changeset
610
dc974b3a6cf4 Two independent changes (oops):
Guido van Rossum <guido@python.org>
parents: 6197
diff changeset
611 static void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13620
diff changeset
612 initsite(void)
6201
dc974b3a6cf4 Two independent changes (oops):
Guido van Rossum <guido@python.org>
parents: 6197
diff changeset
613 {
dc974b3a6cf4 Two independent changes (oops):
Guido van Rossum <guido@python.org>
parents: 6197
diff changeset
614 PyObject *m, *f;
dc974b3a6cf4 Two independent changes (oops):
Guido van Rossum <guido@python.org>
parents: 6197
diff changeset
615 m = PyImport_ImportModule("site");
dc974b3a6cf4 Two independent changes (oops):
Guido van Rossum <guido@python.org>
parents: 6197
diff changeset
616 if (m == NULL) {
dc974b3a6cf4 Two independent changes (oops):
Guido van Rossum <guido@python.org>
parents: 6197
diff changeset
617 f = PySys_GetObject("stderr");
dc974b3a6cf4 Two independent changes (oops):
Guido van Rossum <guido@python.org>
parents: 6197
diff changeset
618 if (Py_VerboseFlag) {
dc974b3a6cf4 Two independent changes (oops):
Guido van Rossum <guido@python.org>
parents: 6197
diff changeset
619 PyFile_WriteString(
dc974b3a6cf4 Two independent changes (oops):
Guido van Rossum <guido@python.org>
parents: 6197
diff changeset
620 "'import site' failed; traceback:\n", f);
dc974b3a6cf4 Two independent changes (oops):
Guido van Rossum <guido@python.org>
parents: 6197
diff changeset
621 PyErr_Print();
dc974b3a6cf4 Two independent changes (oops):
Guido van Rossum <guido@python.org>
parents: 6197
diff changeset
622 }
dc974b3a6cf4 Two independent changes (oops):
Guido van Rossum <guido@python.org>
parents: 6197
diff changeset
623 else {
dc974b3a6cf4 Two independent changes (oops):
Guido van Rossum <guido@python.org>
parents: 6197
diff changeset
624 PyFile_WriteString(
dc974b3a6cf4 Two independent changes (oops):
Guido van Rossum <guido@python.org>
parents: 6197
diff changeset
625 "'import site' failed; use -v for traceback\n", f);
dc974b3a6cf4 Two independent changes (oops):
Guido van Rossum <guido@python.org>
parents: 6197
diff changeset
626 PyErr_Clear();
dc974b3a6cf4 Two independent changes (oops):
Guido van Rossum <guido@python.org>
parents: 6197
diff changeset
627 }
dc974b3a6cf4 Two independent changes (oops):
Guido van Rossum <guido@python.org>
parents: 6197
diff changeset
628 }
dc974b3a6cf4 Two independent changes (oops):
Guido van Rossum <guido@python.org>
parents: 6197
diff changeset
629 else {
dc974b3a6cf4 Two independent changes (oops):
Guido van Rossum <guido@python.org>
parents: 6197
diff changeset
630 Py_DECREF(m);
dc974b3a6cf4 Two independent changes (oops):
Guido van Rossum <guido@python.org>
parents: 6197
diff changeset
631 }
dc974b3a6cf4 Two independent changes (oops):
Guido van Rossum <guido@python.org>
parents: 6197
diff changeset
632 }
dc974b3a6cf4 Two independent changes (oops):
Guido van Rossum <guido@python.org>
parents: 6197
diff changeset
633
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
634 /* Parse input from a file and execute it */
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
635
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
636 int
26601
cc05c2c8660e Constify filenames and scripts. Fixes #651362.
Martin v. Löwis <martin@v.loewis.de>
parents: 26263
diff changeset
637 PyRun_AnyFile(FILE *fp, const char *filename)
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
638 {
17125
2054cea38754 Extend support for from __future__ import nested_scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16916
diff changeset
639 return PyRun_AnyFileExFlags(fp, filename, 0, NULL);
2054cea38754 Extend support for from __future__ import nested_scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16916
diff changeset
640 }
2054cea38754 Extend support for from __future__ import nested_scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16916
diff changeset
641
2054cea38754 Extend support for from __future__ import nested_scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16916
diff changeset
642 int
26601
cc05c2c8660e Constify filenames and scripts. Fixes #651362.
Martin v. Löwis <martin@v.loewis.de>
parents: 26263
diff changeset
643 PyRun_AnyFileFlags(FILE *fp, const char *filename, PyCompilerFlags *flags)
17125
2054cea38754 Extend support for from __future__ import nested_scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16916
diff changeset
644 {
2054cea38754 Extend support for from __future__ import nested_scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16916
diff changeset
645 return PyRun_AnyFileExFlags(fp, filename, 0, flags);
14228
20377f15b96a Add three new APIs: PyRun_AnyFileEx(), PyRun_SimpleFileEx(),
Guido van Rossum <guido@python.org>
parents: 14227
diff changeset
646 }
20377f15b96a Add three new APIs: PyRun_AnyFileEx(), PyRun_SimpleFileEx(),
Guido van Rossum <guido@python.org>
parents: 14227
diff changeset
647
20377f15b96a Add three new APIs: PyRun_AnyFileEx(), PyRun_SimpleFileEx(),
Guido van Rossum <guido@python.org>
parents: 14227
diff changeset
648 int
26601
cc05c2c8660e Constify filenames and scripts. Fixes #651362.
Martin v. Löwis <martin@v.loewis.de>
parents: 26263
diff changeset
649 PyRun_AnyFileEx(FILE *fp, const char *filename, int closeit)
14228
20377f15b96a Add three new APIs: PyRun_AnyFileEx(), PyRun_SimpleFileEx(),
Guido van Rossum <guido@python.org>
parents: 14227
diff changeset
650 {
17125
2054cea38754 Extend support for from __future__ import nested_scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16916
diff changeset
651 return PyRun_AnyFileExFlags(fp, filename, closeit, NULL);
2054cea38754 Extend support for from __future__ import nested_scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16916
diff changeset
652 }
2054cea38754 Extend support for from __future__ import nested_scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16916
diff changeset
653
2054cea38754 Extend support for from __future__ import nested_scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16916
diff changeset
654 int
28648
0e8416be208e Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 28647
diff changeset
655 PyRun_AnyFileExFlags(FILE *fp, const char *filename, int closeit,
17125
2054cea38754 Extend support for from __future__ import nested_scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16916
diff changeset
656 PyCompilerFlags *flags)
2054cea38754 Extend support for from __future__ import nested_scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16916
diff changeset
657 {
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
658 if (filename == NULL)
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
659 filename = "???";
14228
20377f15b96a Add three new APIs: PyRun_AnyFileEx(), PyRun_SimpleFileEx(),
Guido van Rossum <guido@python.org>
parents: 14227
diff changeset
660 if (Py_FdIsInteractive(fp, filename)) {
17125
2054cea38754 Extend support for from __future__ import nested_scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16916
diff changeset
661 int err = PyRun_InteractiveLoopFlags(fp, filename, flags);
14228
20377f15b96a Add three new APIs: PyRun_AnyFileEx(), PyRun_SimpleFileEx(),
Guido van Rossum <guido@python.org>
parents: 14227
diff changeset
662 if (closeit)
20377f15b96a Add three new APIs: PyRun_AnyFileEx(), PyRun_SimpleFileEx(),
Guido van Rossum <guido@python.org>
parents: 14227
diff changeset
663 fclose(fp);
20377f15b96a Add three new APIs: PyRun_AnyFileEx(), PyRun_SimpleFileEx(),
Guido van Rossum <guido@python.org>
parents: 14227
diff changeset
664 return err;
20377f15b96a Add three new APIs: PyRun_AnyFileEx(), PyRun_SimpleFileEx(),
Guido van Rossum <guido@python.org>
parents: 14227
diff changeset
665 }
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
666 else
17125
2054cea38754 Extend support for from __future__ import nested_scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16916
diff changeset
667 return PyRun_SimpleFileExFlags(fp, filename, closeit, flags);
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
668 }
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
669
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
670 int
26601
cc05c2c8660e Constify filenames and scripts. Fixes #651362.
Martin v. Löwis <martin@v.loewis.de>
parents: 26263
diff changeset
671 PyRun_InteractiveLoop(FILE *fp, const char *filename)
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
672 {
17125
2054cea38754 Extend support for from __future__ import nested_scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16916
diff changeset
673 return PyRun_InteractiveLoopFlags(fp, filename, NULL);
2054cea38754 Extend support for from __future__ import nested_scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16916
diff changeset
674 }
2054cea38754 Extend support for from __future__ import nested_scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16916
diff changeset
675
2054cea38754 Extend support for from __future__ import nested_scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16916
diff changeset
676 int
26601
cc05c2c8660e Constify filenames and scripts. Fixes #651362.
Martin v. Löwis <martin@v.loewis.de>
parents: 26263
diff changeset
677 PyRun_InteractiveLoopFlags(FILE *fp, const char *filename, PyCompilerFlags *flags)
17125
2054cea38754 Extend support for from __future__ import nested_scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16916
diff changeset
678 {
5170
f5d707ce4223 Greatly renamed. Not a very thorough job -- I'm going to restructure
Guido van Rossum <guido@python.org>
parents: 5108
diff changeset
679 PyObject *v;
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
680 int ret;
17125
2054cea38754 Extend support for from __future__ import nested_scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16916
diff changeset
681 PyCompilerFlags local_flags;
16916
b0456d3a0f1f Useful future statement support for the interactive interpreter
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16845
diff changeset
682
17125
2054cea38754 Extend support for from __future__ import nested_scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16916
diff changeset
683 if (flags == NULL) {
2054cea38754 Extend support for from __future__ import nested_scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16916
diff changeset
684 flags = &local_flags;
18611
fdb36a0c7e6e Part way to allowing "from __future__ import generators" to communicate
Tim Peters <tim.peters@gmail.com>
parents: 18101
diff changeset
685 local_flags.cf_flags = 0;
17125
2054cea38754 Extend support for from __future__ import nested_scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16916
diff changeset
686 }
5170
f5d707ce4223 Greatly renamed. Not a very thorough job -- I'm going to restructure
Guido van Rossum <guido@python.org>
parents: 5108
diff changeset
687 v = PySys_GetObject("ps1");
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
688 if (v == NULL) {
5170
f5d707ce4223 Greatly renamed. Not a very thorough job -- I'm going to restructure
Guido van Rossum <guido@python.org>
parents: 5108
diff changeset
689 PySys_SetObject("ps1", v = PyString_FromString(">>> "));
f5d707ce4223 Greatly renamed. Not a very thorough job -- I'm going to restructure
Guido van Rossum <guido@python.org>
parents: 5108
diff changeset
690 Py_XDECREF(v);
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
691 }
5170
f5d707ce4223 Greatly renamed. Not a very thorough job -- I'm going to restructure
Guido van Rossum <guido@python.org>
parents: 5108
diff changeset
692 v = PySys_GetObject("ps2");
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
693 if (v == NULL) {
5170
f5d707ce4223 Greatly renamed. Not a very thorough job -- I'm going to restructure
Guido van Rossum <guido@python.org>
parents: 5108
diff changeset
694 PySys_SetObject("ps2", v = PyString_FromString("... "));
f5d707ce4223 Greatly renamed. Not a very thorough job -- I'm going to restructure
Guido van Rossum <guido@python.org>
parents: 5108
diff changeset
695 Py_XDECREF(v);
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
696 }
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
697 for (;;) {
17125
2054cea38754 Extend support for from __future__ import nested_scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16916
diff changeset
698 ret = PyRun_InteractiveOneFlags(fp, filename, flags);
3575
e2b3a696d69c Use new names for debug macros. Don't include pythonrun.h.
Guido van Rossum <guido@python.org>
parents: 3489
diff changeset
699 #ifdef Py_REF_DEBUG
2612
ca6b60d4ea90 fix dusty debugging macros
Guido van Rossum <guido@python.org>
parents: 2529
diff changeset
700 fprintf(stderr, "[%ld refs]\n", _Py_RefTotal);
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
701 #endif
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
702 if (ret == E_EOF)
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
703 return 0;
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
704 /*
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
705 if (ret == E_NOMEM)
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
706 return -1;
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
707 */
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
708 }
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
709 }
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
710
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
711 int
26601
cc05c2c8660e Constify filenames and scripts. Fixes #651362.
Martin v. Löwis <martin@v.loewis.de>
parents: 26263
diff changeset
712 PyRun_InteractiveOne(FILE *fp, const char *filename)
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
713 {
16916
b0456d3a0f1f Useful future statement support for the interactive interpreter
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16845
diff changeset
714 return PyRun_InteractiveOneFlags(fp, filename, NULL);
b0456d3a0f1f Useful future statement support for the interactive interpreter
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16845
diff changeset
715 }
b0456d3a0f1f Useful future statement support for the interactive interpreter
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16845
diff changeset
716
22468
eebae4f60d05 Disable the parser hacks that enabled the "yield" keyword using a future
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 22121
diff changeset
717 /* compute parser flags based on compiler flags */
eebae4f60d05 Disable the parser hacks that enabled the "yield" keyword using a future
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 22121
diff changeset
718 #define PARSER_FLAGS(flags) \
27878
be7683a6f521 - Finally fixed the bug in compile() and exec where a string ending
Guido van Rossum <guido@python.org>
parents: 27358
diff changeset
719 (((flags) && (flags)->cf_flags & PyCF_DONT_IMPLY_DEDENT) ? \
be7683a6f521 - Finally fixed the bug in compile() and exec where a string ending
Guido van Rossum <guido@python.org>
parents: 27358
diff changeset
720 PyPARSE_DONT_IMPLY_DEDENT : 0)
22468
eebae4f60d05 Disable the parser hacks that enabled the "yield" keyword using a future
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 22121
diff changeset
721
16916
b0456d3a0f1f Useful future statement support for the interactive interpreter
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16845
diff changeset
722 int
26601
cc05c2c8660e Constify filenames and scripts. Fixes #651362.
Martin v. Löwis <martin@v.loewis.de>
parents: 26263
diff changeset
723 PyRun_InteractiveOneFlags(FILE *fp, const char *filename, PyCompilerFlags *flags)
16916
b0456d3a0f1f Useful future statement support for the interactive interpreter
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16845
diff changeset
724 {
5170
f5d707ce4223 Greatly renamed. Not a very thorough job -- I'm going to restructure
Guido van Rossum <guido@python.org>
parents: 5108
diff changeset
725 PyObject *m, *d, *v, *w;
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
726 node *n;
1839
1f4a86dbcd79 * Python/pythonrun.c (print_error): added INCREF/DECREF pair --
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
727 perrdetail err;
6661
99955b65408d Apply str() to sys.ps1 or sys.ps2 before using them as a prompt, so
Guido van Rossum <guido@python.org>
parents: 6600
diff changeset
728 char *ps1 = "", *ps2 = "";
18615
0a90e5ec61b6 Ugly. A pile of new xxxFlags() functions, to communicate to the parser
Tim Peters <tim.peters@gmail.com>
parents: 18612
diff changeset
729
5170
f5d707ce4223 Greatly renamed. Not a very thorough job -- I'm going to restructure
Guido van Rossum <guido@python.org>
parents: 5108
diff changeset
730 v = PySys_GetObject("ps1");
6661
99955b65408d Apply str() to sys.ps1 or sys.ps2 before using them as a prompt, so
Guido van Rossum <guido@python.org>
parents: 6600
diff changeset
731 if (v != NULL) {
99955b65408d Apply str() to sys.ps1 or sys.ps2 before using them as a prompt, so
Guido van Rossum <guido@python.org>
parents: 6600
diff changeset
732 v = PyObject_Str(v);
99955b65408d Apply str() to sys.ps1 or sys.ps2 before using them as a prompt, so
Guido van Rossum <guido@python.org>
parents: 6600
diff changeset
733 if (v == NULL)
99955b65408d Apply str() to sys.ps1 or sys.ps2 before using them as a prompt, so
Guido van Rossum <guido@python.org>
parents: 6600
diff changeset
734 PyErr_Clear();
99955b65408d Apply str() to sys.ps1 or sys.ps2 before using them as a prompt, so
Guido van Rossum <guido@python.org>
parents: 6600
diff changeset
735 else if (PyString_Check(v))
99955b65408d Apply str() to sys.ps1 or sys.ps2 before using them as a prompt, so
Guido van Rossum <guido@python.org>
parents: 6600
diff changeset
736 ps1 = PyString_AsString(v);
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
737 }
6661
99955b65408d Apply str() to sys.ps1 or sys.ps2 before using them as a prompt, so
Guido van Rossum <guido@python.org>
parents: 6600
diff changeset
738 w = PySys_GetObject("ps2");
99955b65408d Apply str() to sys.ps1 or sys.ps2 before using them as a prompt, so
Guido van Rossum <guido@python.org>
parents: 6600
diff changeset
739 if (w != NULL) {
99955b65408d Apply str() to sys.ps1 or sys.ps2 before using them as a prompt, so
Guido van Rossum <guido@python.org>
parents: 6600
diff changeset
740 w = PyObject_Str(w);
99955b65408d Apply str() to sys.ps1 or sys.ps2 before using them as a prompt, so
Guido van Rossum <guido@python.org>
parents: 6600
diff changeset
741 if (w == NULL)
99955b65408d Apply str() to sys.ps1 or sys.ps2 before using them as a prompt, so
Guido van Rossum <guido@python.org>
parents: 6600
diff changeset
742 PyErr_Clear();
99955b65408d Apply str() to sys.ps1 or sys.ps2 before using them as a prompt, so
Guido van Rossum <guido@python.org>
parents: 6600
diff changeset
743 else if (PyString_Check(w))
99955b65408d Apply str() to sys.ps1 or sys.ps2 before using them as a prompt, so
Guido van Rossum <guido@python.org>
parents: 6600
diff changeset
744 ps2 = PyString_AsString(w);
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
745 }
18615
0a90e5ec61b6 Ugly. A pile of new xxxFlags() functions, to communicate to the parser
Tim Peters <tim.peters@gmail.com>
parents: 18612
diff changeset
746 n = PyParser_ParseFileFlags(fp, filename, &_PyParser_Grammar,
0a90e5ec61b6 Ugly. A pile of new xxxFlags() functions, to communicate to the parser
Tim Peters <tim.peters@gmail.com>
parents: 18612
diff changeset
747 Py_single_input, ps1, ps2, &err,
22468
eebae4f60d05 Disable the parser hacks that enabled the "yield" keyword using a future
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 22121
diff changeset
748 PARSER_FLAGS(flags));
5170
f5d707ce4223 Greatly renamed. Not a very thorough job -- I'm going to restructure
Guido van Rossum <guido@python.org>
parents: 5108
diff changeset
749 Py_XDECREF(v);
f5d707ce4223 Greatly renamed. Not a very thorough job -- I'm going to restructure
Guido van Rossum <guido@python.org>
parents: 5108
diff changeset
750 Py_XDECREF(w);
1839
1f4a86dbcd79 * Python/pythonrun.c (print_error): added INCREF/DECREF pair --
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
751 if (n == NULL) {
1f4a86dbcd79 * Python/pythonrun.c (print_error): added INCREF/DECREF pair --
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
752 if (err.error == E_EOF) {
1f4a86dbcd79 * Python/pythonrun.c (print_error): added INCREF/DECREF pair --
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
753 if (err.text)
12576
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 12555
diff changeset
754 PyMem_DEL(err.text);
1839
1f4a86dbcd79 * Python/pythonrun.c (print_error): added INCREF/DECREF pair --
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
755 return E_EOF;
1f4a86dbcd79 * Python/pythonrun.c (print_error): added INCREF/DECREF pair --
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
756 }
1f4a86dbcd79 * Python/pythonrun.c (print_error): added INCREF/DECREF pair --
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
757 err_input(&err);
5170
f5d707ce4223 Greatly renamed. Not a very thorough job -- I'm going to restructure
Guido van Rossum <guido@python.org>
parents: 5108
diff changeset
758 PyErr_Print();
1839
1f4a86dbcd79 * Python/pythonrun.c (print_error): added INCREF/DECREF pair --
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
759 return err.error;
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
760 }
5170
f5d707ce4223 Greatly renamed. Not a very thorough job -- I'm going to restructure
Guido van Rossum <guido@python.org>
parents: 5108
diff changeset
761 m = PyImport_AddModule("__main__");
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
762 if (m == NULL)
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
763 return -1;
5170
f5d707ce4223 Greatly renamed. Not a very thorough job -- I'm going to restructure
Guido van Rossum <guido@python.org>
parents: 5108
diff changeset
764 d = PyModule_GetDict(m);
16916
b0456d3a0f1f Useful future statement support for the interactive interpreter
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16845
diff changeset
765 v = run_node(n, filename, d, d, flags);
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
766 if (v == NULL) {
5170
f5d707ce4223 Greatly renamed. Not a very thorough job -- I'm going to restructure
Guido van Rossum <guido@python.org>
parents: 5108
diff changeset
767 PyErr_Print();
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
768 return -1;
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
769 }
5170
f5d707ce4223 Greatly renamed. Not a very thorough job -- I'm going to restructure
Guido van Rossum <guido@python.org>
parents: 5108
diff changeset
770 Py_DECREF(v);
5607
cbb4e3cf086e Py_FlushLine and PyFile_WriteString now return error indicators
Guido van Rossum <guido@python.org>
parents: 5593
diff changeset
771 if (Py_FlushLine())
cbb4e3cf086e Py_FlushLine and PyFile_WriteString now return error indicators
Guido van Rossum <guido@python.org>
parents: 5593
diff changeset
772 PyErr_Clear();
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
773 return 0;
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
774 }
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
775
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
776 int
26601
cc05c2c8660e Constify filenames and scripts. Fixes #651362.
Martin v. Löwis <martin@v.loewis.de>
parents: 26263
diff changeset
777 PyRun_SimpleFile(FILE *fp, const char *filename)
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
778 {
14228
20377f15b96a Add three new APIs: PyRun_AnyFileEx(), PyRun_SimpleFileEx(),
Guido van Rossum <guido@python.org>
parents: 14227
diff changeset
779 return PyRun_SimpleFileEx(fp, filename, 0);
20377f15b96a Add three new APIs: PyRun_AnyFileEx(), PyRun_SimpleFileEx(),
Guido van Rossum <guido@python.org>
parents: 14227
diff changeset
780 }
20377f15b96a Add three new APIs: PyRun_AnyFileEx(), PyRun_SimpleFileEx(),
Guido van Rossum <guido@python.org>
parents: 14227
diff changeset
781
15738
76caf0855d1f Recognize pyc files even if they don't end in pyc.
Martin v. Löwis <martin@v.loewis.de>
parents: 14637
diff changeset
782 /* Check whether a file maybe a pyc file: Look at the extension,
76caf0855d1f Recognize pyc files even if they don't end in pyc.
Martin v. Löwis <martin@v.loewis.de>
parents: 14637
diff changeset
783 the file type, and, if we may close it, at the first few bytes. */
76caf0855d1f Recognize pyc files even if they don't end in pyc.
Martin v. Löwis <martin@v.loewis.de>
parents: 14637
diff changeset
784
76caf0855d1f Recognize pyc files even if they don't end in pyc.
Martin v. Löwis <martin@v.loewis.de>
parents: 14637
diff changeset
785 static int
26601
cc05c2c8660e Constify filenames and scripts. Fixes #651362.
Martin v. Löwis <martin@v.loewis.de>
parents: 26263
diff changeset
786 maybe_pyc_file(FILE *fp, const char* filename, const char* ext, int closeit)
15738
76caf0855d1f Recognize pyc files even if they don't end in pyc.
Martin v. Löwis <martin@v.loewis.de>
parents: 14637
diff changeset
787 {
76caf0855d1f Recognize pyc files even if they don't end in pyc.
Martin v. Löwis <martin@v.loewis.de>
parents: 14637
diff changeset
788 if (strcmp(ext, ".pyc") == 0 || strcmp(ext, ".pyo") == 0)
76caf0855d1f Recognize pyc files even if they don't end in pyc.
Martin v. Löwis <martin@v.loewis.de>
parents: 14637
diff changeset
789 return 1;
76caf0855d1f Recognize pyc files even if they don't end in pyc.
Martin v. Löwis <martin@v.loewis.de>
parents: 14637
diff changeset
790
76caf0855d1f Recognize pyc files even if they don't end in pyc.
Martin v. Löwis <martin@v.loewis.de>
parents: 14637
diff changeset
791 /* Only look into the file if we are allowed to close it, since
76caf0855d1f Recognize pyc files even if they don't end in pyc.
Martin v. Löwis <martin@v.loewis.de>
parents: 14637
diff changeset
792 it then should also be seekable. */
76caf0855d1f Recognize pyc files even if they don't end in pyc.
Martin v. Löwis <martin@v.loewis.de>
parents: 14637
diff changeset
793 if (closeit) {
76caf0855d1f Recognize pyc files even if they don't end in pyc.
Martin v. Löwis <martin@v.loewis.de>
parents: 14637
diff changeset
794 /* Read only two bytes of the magic. If the file was opened in
76caf0855d1f Recognize pyc files even if they don't end in pyc.
Martin v. Löwis <martin@v.loewis.de>
parents: 14637
diff changeset
795 text mode, the bytes 3 and 4 of the magic (\r\n) might not
76caf0855d1f Recognize pyc files even if they don't end in pyc.
Martin v. Löwis <martin@v.loewis.de>
parents: 14637
diff changeset
796 be read as they are on disk. */
76caf0855d1f Recognize pyc files even if they don't end in pyc.
Martin v. Löwis <martin@v.loewis.de>
parents: 14637
diff changeset
797 unsigned int halfmagic = PyImport_GetMagicNumber() & 0xFFFF;
76caf0855d1f Recognize pyc files even if they don't end in pyc.
Martin v. Löwis <martin@v.loewis.de>
parents: 14637
diff changeset
798 unsigned char buf[2];
16560
e8e636cc1a09 Ugly fix for SF bug 131239 (-x flag busted).
Tim Peters <tim.peters@gmail.com>
parents: 16421
diff changeset
799 /* Mess: In case of -x, the stream is NOT at its start now,
e8e636cc1a09 Ugly fix for SF bug 131239 (-x flag busted).
Tim Peters <tim.peters@gmail.com>
parents: 16421
diff changeset
800 and ungetc() was used to push back the first newline,
16648
a51b9961f3bb Bug #132850 unix line terminator on windows.
Tim Peters <tim.peters@gmail.com>
parents: 16560
diff changeset
801 which makes the current stream position formally undefined,
a51b9961f3bb Bug #132850 unix line terminator on windows.
Tim Peters <tim.peters@gmail.com>
parents: 16560
diff changeset
802 and a x-platform nightmare.
a51b9961f3bb Bug #132850 unix line terminator on windows.
Tim Peters <tim.peters@gmail.com>
parents: 16560
diff changeset
803 Unfortunately, we have no direct way to know whether -x
a51b9961f3bb Bug #132850 unix line terminator on windows.
Tim Peters <tim.peters@gmail.com>
parents: 16560
diff changeset
804 was specified. So we use a terrible hack: if the current
a51b9961f3bb Bug #132850 unix line terminator on windows.
Tim Peters <tim.peters@gmail.com>
parents: 16560
diff changeset
805 stream position is not 0, we assume -x was specified, and
a51b9961f3bb Bug #132850 unix line terminator on windows.
Tim Peters <tim.peters@gmail.com>
parents: 16560
diff changeset
806 give up. Bug 132850 on SourceForge spells out the
a51b9961f3bb Bug #132850 unix line terminator on windows.
Tim Peters <tim.peters@gmail.com>
parents: 16560
diff changeset
807 hopelessness of trying anything else (fseek and ftell
a51b9961f3bb Bug #132850 unix line terminator on windows.
Tim Peters <tim.peters@gmail.com>
parents: 16560
diff changeset
808 don't work predictably x-platform for text-mode files).
16560
e8e636cc1a09 Ugly fix for SF bug 131239 (-x flag busted).
Tim Peters <tim.peters@gmail.com>
parents: 16421
diff changeset
809 */
e8e636cc1a09 Ugly fix for SF bug 131239 (-x flag busted).
Tim Peters <tim.peters@gmail.com>
parents: 16421
diff changeset
810 int ispyc = 0;
16648
a51b9961f3bb Bug #132850 unix line terminator on windows.
Tim Peters <tim.peters@gmail.com>
parents: 16560
diff changeset
811 if (ftell(fp) == 0) {
a51b9961f3bb Bug #132850 unix line terminator on windows.
Tim Peters <tim.peters@gmail.com>
parents: 16560
diff changeset
812 if (fread(buf, 1, 2, fp) == 2 &&
28648
0e8416be208e Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 28647
diff changeset
813 ((unsigned int)buf[1]<<8 | buf[0]) == halfmagic)
16648
a51b9961f3bb Bug #132850 unix line terminator on windows.
Tim Peters <tim.peters@gmail.com>
parents: 16560
diff changeset
814 ispyc = 1;
a51b9961f3bb Bug #132850 unix line terminator on windows.
Tim Peters <tim.peters@gmail.com>
parents: 16560
diff changeset
815 rewind(fp);
a51b9961f3bb Bug #132850 unix line terminator on windows.
Tim Peters <tim.peters@gmail.com>
parents: 16560
diff changeset
816 }
16560
e8e636cc1a09 Ugly fix for SF bug 131239 (-x flag busted).
Tim Peters <tim.peters@gmail.com>
parents: 16421
diff changeset
817 return ispyc;
15738
76caf0855d1f Recognize pyc files even if they don't end in pyc.
Martin v. Löwis <martin@v.loewis.de>
parents: 14637
diff changeset
818 }
76caf0855d1f Recognize pyc files even if they don't end in pyc.
Martin v. Löwis <martin@v.loewis.de>
parents: 14637
diff changeset
819 return 0;
28648
0e8416be208e Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 28647
diff changeset
820 }
15738
76caf0855d1f Recognize pyc files even if they don't end in pyc.
Martin v. Löwis <martin@v.loewis.de>
parents: 14637
diff changeset
821
14228
20377f15b96a Add three new APIs: PyRun_AnyFileEx(), PyRun_SimpleFileEx(),
Guido van Rossum <guido@python.org>
parents: 14227
diff changeset
822 int
26601
cc05c2c8660e Constify filenames and scripts. Fixes #651362.
Martin v. Löwis <martin@v.loewis.de>
parents: 26263
diff changeset
823 PyRun_SimpleFileEx(FILE *fp, const char *filename, int closeit)
14228
20377f15b96a Add three new APIs: PyRun_AnyFileEx(), PyRun_SimpleFileEx(),
Guido van Rossum <guido@python.org>
parents: 14227
diff changeset
824 {
17125
2054cea38754 Extend support for from __future__ import nested_scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16916
diff changeset
825 return PyRun_SimpleFileExFlags(fp, filename, closeit, NULL);
2054cea38754 Extend support for from __future__ import nested_scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16916
diff changeset
826 }
2054cea38754 Extend support for from __future__ import nested_scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16916
diff changeset
827
2054cea38754 Extend support for from __future__ import nested_scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16916
diff changeset
828 int
26601
cc05c2c8660e Constify filenames and scripts. Fixes #651362.
Martin v. Löwis <martin@v.loewis.de>
parents: 26263
diff changeset
829 PyRun_SimpleFileExFlags(FILE *fp, const char *filename, int closeit,
17125
2054cea38754 Extend support for from __future__ import nested_scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16916
diff changeset
830 PyCompilerFlags *flags)
2054cea38754 Extend support for from __future__ import nested_scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16916
diff changeset
831 {
5170
f5d707ce4223 Greatly renamed. Not a very thorough job -- I'm going to restructure
Guido van Rossum <guido@python.org>
parents: 5108
diff changeset
832 PyObject *m, *d, *v;
26601
cc05c2c8660e Constify filenames and scripts. Fixes #651362.
Martin v. Löwis <martin@v.loewis.de>
parents: 26263
diff changeset
833 const char *ext;
1886
ac6a9f90f6fd * Import/pythonrun.h, Python/{import,pythonrun}.c,
Guido van Rossum <guido@python.org>
parents: 1870
diff changeset
834
5170
f5d707ce4223 Greatly renamed. Not a very thorough job -- I'm going to restructure
Guido van Rossum <guido@python.org>
parents: 5108
diff changeset
835 m = PyImport_AddModule("__main__");
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
836 if (m == NULL)
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
837 return -1;
5170
f5d707ce4223 Greatly renamed. Not a very thorough job -- I'm going to restructure
Guido van Rossum <guido@python.org>
parents: 5108
diff changeset
838 d = PyModule_GetDict(m);
25827
093fe5b9f6f9 If we have a filename and __main__.__file__ hasn't already been set,
Fred Drake <fdrake@acm.org>
parents: 25573
diff changeset
839 if (PyDict_GetItemString(d, "__file__") == NULL) {
093fe5b9f6f9 If we have a filename and __main__.__file__ hasn't already been set,
Fred Drake <fdrake@acm.org>
parents: 25573
diff changeset
840 PyObject *f = PyString_FromString(filename);
093fe5b9f6f9 If we have a filename and __main__.__file__ hasn't already been set,
Fred Drake <fdrake@acm.org>
parents: 25573
diff changeset
841 if (f == NULL)
093fe5b9f6f9 If we have a filename and __main__.__file__ hasn't already been set,
Fred Drake <fdrake@acm.org>
parents: 25573
diff changeset
842 return -1;
093fe5b9f6f9 If we have a filename and __main__.__file__ hasn't already been set,
Fred Drake <fdrake@acm.org>
parents: 25573
diff changeset
843 if (PyDict_SetItemString(d, "__file__", f) < 0) {
093fe5b9f6f9 If we have a filename and __main__.__file__ hasn't already been set,
Fred Drake <fdrake@acm.org>
parents: 25573
diff changeset
844 Py_DECREF(f);
093fe5b9f6f9 If we have a filename and __main__.__file__ hasn't already been set,
Fred Drake <fdrake@acm.org>
parents: 25573
diff changeset
845 return -1;
093fe5b9f6f9 If we have a filename and __main__.__file__ hasn't already been set,
Fred Drake <fdrake@acm.org>
parents: 25573
diff changeset
846 }
093fe5b9f6f9 If we have a filename and __main__.__file__ hasn't already been set,
Fred Drake <fdrake@acm.org>
parents: 25573
diff changeset
847 Py_DECREF(f);
093fe5b9f6f9 If we have a filename and __main__.__file__ hasn't already been set,
Fred Drake <fdrake@acm.org>
parents: 25573
diff changeset
848 }
1886
ac6a9f90f6fd * Import/pythonrun.h, Python/{import,pythonrun}.c,
Guido van Rossum <guido@python.org>
parents: 1870
diff changeset
849 ext = filename + strlen(filename) - 4;
15738
76caf0855d1f Recognize pyc files even if they don't end in pyc.
Martin v. Löwis <martin@v.loewis.de>
parents: 14637
diff changeset
850 if (maybe_pyc_file(fp, filename, ext, closeit)) {
1886
ac6a9f90f6fd * Import/pythonrun.h, Python/{import,pythonrun}.c,
Guido van Rossum <guido@python.org>
parents: 1870
diff changeset
851 /* Try to run a pyc file. First, re-open in binary */
14228
20377f15b96a Add three new APIs: PyRun_AnyFileEx(), PyRun_SimpleFileEx(),
Guido van Rossum <guido@python.org>
parents: 14227
diff changeset
852 if (closeit)
20377f15b96a Add three new APIs: PyRun_AnyFileEx(), PyRun_SimpleFileEx(),
Guido van Rossum <guido@python.org>
parents: 14227
diff changeset
853 fclose(fp);
25827
093fe5b9f6f9 If we have a filename and __main__.__file__ hasn't already been set,
Fred Drake <fdrake@acm.org>
parents: 25573
diff changeset
854 if ((fp = fopen(filename, "rb")) == NULL) {
1886
ac6a9f90f6fd * Import/pythonrun.h, Python/{import,pythonrun}.c,
Guido van Rossum <guido@python.org>
parents: 1870
diff changeset
855 fprintf(stderr, "python: Can't reopen .pyc file\n");
ac6a9f90f6fd * Import/pythonrun.h, Python/{import,pythonrun}.c,
Guido van Rossum <guido@python.org>
parents: 1870
diff changeset
856 return -1;
ac6a9f90f6fd * Import/pythonrun.h, Python/{import,pythonrun}.c,
Guido van Rossum <guido@python.org>
parents: 1870
diff changeset
857 }
5234
07ed52e200db Allow passing a .pyo file.
Guido van Rossum <guido@python.org>
parents: 5170
diff changeset
858 /* Turn on optimization if a .pyo file is given */
07ed52e200db Allow passing a .pyo file.
Guido van Rossum <guido@python.org>
parents: 5170
diff changeset
859 if (strcmp(ext, ".pyo") == 0)
07ed52e200db Allow passing a .pyo file.
Guido van Rossum <guido@python.org>
parents: 5170
diff changeset
860 Py_OptimizeFlag = 1;
17125
2054cea38754 Extend support for from __future__ import nested_scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16916
diff changeset
861 v = run_pyc_file(fp, filename, d, d, flags);
1886
ac6a9f90f6fd * Import/pythonrun.h, Python/{import,pythonrun}.c,
Guido van Rossum <guido@python.org>
parents: 1870
diff changeset
862 } else {
28648
0e8416be208e Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 28647
diff changeset
863 v = PyRun_FileExFlags(fp, filename, Py_file_input, d, d,
17125
2054cea38754 Extend support for from __future__ import nested_scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16916
diff changeset
864 closeit, flags);
1886
ac6a9f90f6fd * Import/pythonrun.h, Python/{import,pythonrun}.c,
Guido van Rossum <guido@python.org>
parents: 1870
diff changeset
865 }
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
866 if (v == NULL) {
5170
f5d707ce4223 Greatly renamed. Not a very thorough job -- I'm going to restructure
Guido van Rossum <guido@python.org>
parents: 5108
diff changeset
867 PyErr_Print();
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
868 return -1;
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
869 }
5170
f5d707ce4223 Greatly renamed. Not a very thorough job -- I'm going to restructure
Guido van Rossum <guido@python.org>
parents: 5108
diff changeset
870 Py_DECREF(v);
5607
cbb4e3cf086e Py_FlushLine and PyFile_WriteString now return error indicators
Guido van Rossum <guido@python.org>
parents: 5593
diff changeset
871 if (Py_FlushLine())
cbb4e3cf086e Py_FlushLine and PyFile_WriteString now return error indicators
Guido van Rossum <guido@python.org>
parents: 5593
diff changeset
872 PyErr_Clear();
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
873 return 0;
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
874 }
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
875
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
876 int
26601
cc05c2c8660e Constify filenames and scripts. Fixes #651362.
Martin v. Löwis <martin@v.loewis.de>
parents: 26263
diff changeset
877 PyRun_SimpleString(const char *command)
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
878 {
19633
c0a44829bcd5 Add warning mode for classic division, almost exactly as specified in
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
879 return PyRun_SimpleStringFlags(command, NULL);
c0a44829bcd5 Add warning mode for classic division, almost exactly as specified in
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
880 }
c0a44829bcd5 Add warning mode for classic division, almost exactly as specified in
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
881
c0a44829bcd5 Add warning mode for classic division, almost exactly as specified in
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
882 int
26601
cc05c2c8660e Constify filenames and scripts. Fixes #651362.
Martin v. Löwis <martin@v.loewis.de>
parents: 26263
diff changeset
883 PyRun_SimpleStringFlags(const char *command, PyCompilerFlags *flags)
19633
c0a44829bcd5 Add warning mode for classic division, almost exactly as specified in
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
884 {
5170
f5d707ce4223 Greatly renamed. Not a very thorough job -- I'm going to restructure
Guido van Rossum <guido@python.org>
parents: 5108
diff changeset
885 PyObject *m, *d, *v;
f5d707ce4223 Greatly renamed. Not a very thorough job -- I'm going to restructure
Guido van Rossum <guido@python.org>
parents: 5108
diff changeset
886 m = PyImport_AddModule("__main__");
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
887 if (m == NULL)
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
888 return -1;
5170
f5d707ce4223 Greatly renamed. Not a very thorough job -- I'm going to restructure
Guido van Rossum <guido@python.org>
parents: 5108
diff changeset
889 d = PyModule_GetDict(m);
19633
c0a44829bcd5 Add warning mode for classic division, almost exactly as specified in
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
890 v = PyRun_StringFlags(command, Py_file_input, d, d, flags);
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
891 if (v == NULL) {
5170
f5d707ce4223 Greatly renamed. Not a very thorough job -- I'm going to restructure
Guido van Rossum <guido@python.org>
parents: 5108
diff changeset
892 PyErr_Print();
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
893 return -1;
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
894 }
5170
f5d707ce4223 Greatly renamed. Not a very thorough job -- I'm going to restructure
Guido van Rossum <guido@python.org>
parents: 5108
diff changeset
895 Py_DECREF(v);
5607
cbb4e3cf086e Py_FlushLine and PyFile_WriteString now return error indicators
Guido van Rossum <guido@python.org>
parents: 5593
diff changeset
896 if (Py_FlushLine())
cbb4e3cf086e Py_FlushLine and PyFile_WriteString now return error indicators
Guido van Rossum <guido@python.org>
parents: 5593
diff changeset
897 PyErr_Clear();
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
898 return 0;
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
899 }
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
900
6197
7da7ba5c6525 Added Py_UseClassExceptionsFlag, the variable containing the state of
Barry Warsaw <barry@python.org>
parents: 6165
diff changeset
901 static int
26601
cc05c2c8660e Constify filenames and scripts. Fixes #651362.
Martin v. Löwis <martin@v.loewis.de>
parents: 26263
diff changeset
902 parse_syntax_error(PyObject *err, PyObject **message, const char **filename,
cc05c2c8660e Constify filenames and scripts. Fixes #651362.
Martin v. Löwis <martin@v.loewis.de>
parents: 26263
diff changeset
903 int *lineno, int *offset, const char **text)
6197
7da7ba5c6525 Added Py_UseClassExceptionsFlag, the variable containing the state of
Barry Warsaw <barry@python.org>
parents: 6165
diff changeset
904 {
7da7ba5c6525 Added Py_UseClassExceptionsFlag, the variable containing the state of
Barry Warsaw <barry@python.org>
parents: 6165
diff changeset
905 long hold;
7da7ba5c6525 Added Py_UseClassExceptionsFlag, the variable containing the state of
Barry Warsaw <barry@python.org>
parents: 6165
diff changeset
906 PyObject *v;
7da7ba5c6525 Added Py_UseClassExceptionsFlag, the variable containing the state of
Barry Warsaw <barry@python.org>
parents: 6165
diff changeset
907
7da7ba5c6525 Added Py_UseClassExceptionsFlag, the variable containing the state of
Barry Warsaw <barry@python.org>
parents: 6165
diff changeset
908 /* old style errors */
7da7ba5c6525 Added Py_UseClassExceptionsFlag, the variable containing the state of
Barry Warsaw <barry@python.org>
parents: 6165
diff changeset
909 if (PyTuple_Check(err))
22721
253de588f2fb Get rid of another use of PyArg_Parse()
Neal Norwitz <nnorwitz@gmail.com>
parents: 22468
diff changeset
910 return PyArg_ParseTuple(err, "O(ziiz)", message, filename,
253de588f2fb Get rid of another use of PyArg_Parse()
Neal Norwitz <nnorwitz@gmail.com>
parents: 22468
diff changeset
911 lineno, offset, text);
6197
7da7ba5c6525 Added Py_UseClassExceptionsFlag, the variable containing the state of
Barry Warsaw <barry@python.org>
parents: 6165
diff changeset
912
7da7ba5c6525 Added Py_UseClassExceptionsFlag, the variable containing the state of
Barry Warsaw <barry@python.org>
parents: 6165
diff changeset
913 /* new style errors. `err' is an instance */
7da7ba5c6525 Added Py_UseClassExceptionsFlag, the variable containing the state of
Barry Warsaw <barry@python.org>
parents: 6165
diff changeset
914
7da7ba5c6525 Added Py_UseClassExceptionsFlag, the variable containing the state of
Barry Warsaw <barry@python.org>
parents: 6165
diff changeset
915 if (! (v = PyObject_GetAttrString(err, "msg")))
7da7ba5c6525 Added Py_UseClassExceptionsFlag, the variable containing the state of
Barry Warsaw <barry@python.org>
parents: 6165
diff changeset
916 goto finally;
7da7ba5c6525 Added Py_UseClassExceptionsFlag, the variable containing the state of
Barry Warsaw <barry@python.org>
parents: 6165
diff changeset
917 *message = v;
7da7ba5c6525 Added Py_UseClassExceptionsFlag, the variable containing the state of
Barry Warsaw <barry@python.org>
parents: 6165
diff changeset
918
7da7ba5c6525 Added Py_UseClassExceptionsFlag, the variable containing the state of
Barry Warsaw <barry@python.org>
parents: 6165
diff changeset
919 if (!(v = PyObject_GetAttrString(err, "filename")))
7da7ba5c6525 Added Py_UseClassExceptionsFlag, the variable containing the state of
Barry Warsaw <barry@python.org>
parents: 6165
diff changeset
920 goto finally;
7da7ba5c6525 Added Py_UseClassExceptionsFlag, the variable containing the state of
Barry Warsaw <barry@python.org>
parents: 6165
diff changeset
921 if (v == Py_None)
7da7ba5c6525 Added Py_UseClassExceptionsFlag, the variable containing the state of
Barry Warsaw <barry@python.org>
parents: 6165
diff changeset
922 *filename = NULL;
7da7ba5c6525 Added Py_UseClassExceptionsFlag, the variable containing the state of
Barry Warsaw <barry@python.org>
parents: 6165
diff changeset
923 else if (! (*filename = PyString_AsString(v)))
7da7ba5c6525 Added Py_UseClassExceptionsFlag, the variable containing the state of
Barry Warsaw <barry@python.org>
parents: 6165
diff changeset
924 goto finally;
7da7ba5c6525 Added Py_UseClassExceptionsFlag, the variable containing the state of
Barry Warsaw <barry@python.org>
parents: 6165
diff changeset
925
7da7ba5c6525 Added Py_UseClassExceptionsFlag, the variable containing the state of
Barry Warsaw <barry@python.org>
parents: 6165
diff changeset
926 Py_DECREF(v);
7da7ba5c6525 Added Py_UseClassExceptionsFlag, the variable containing the state of
Barry Warsaw <barry@python.org>
parents: 6165
diff changeset
927 if (!(v = PyObject_GetAttrString(err, "lineno")))
7da7ba5c6525 Added Py_UseClassExceptionsFlag, the variable containing the state of
Barry Warsaw <barry@python.org>
parents: 6165
diff changeset
928 goto finally;
7da7ba5c6525 Added Py_UseClassExceptionsFlag, the variable containing the state of
Barry Warsaw <barry@python.org>
parents: 6165
diff changeset
929 hold = PyInt_AsLong(v);
7da7ba5c6525 Added Py_UseClassExceptionsFlag, the variable containing the state of
Barry Warsaw <barry@python.org>
parents: 6165
diff changeset
930 Py_DECREF(v);
7da7ba5c6525 Added Py_UseClassExceptionsFlag, the variable containing the state of
Barry Warsaw <barry@python.org>
parents: 6165
diff changeset
931 v = NULL;
7da7ba5c6525 Added Py_UseClassExceptionsFlag, the variable containing the state of
Barry Warsaw <barry@python.org>
parents: 6165
diff changeset
932 if (hold < 0 && PyErr_Occurred())
7da7ba5c6525 Added Py_UseClassExceptionsFlag, the variable containing the state of
Barry Warsaw <barry@python.org>
parents: 6165
diff changeset
933 goto finally;
7da7ba5c6525 Added Py_UseClassExceptionsFlag, the variable containing the state of
Barry Warsaw <barry@python.org>
parents: 6165
diff changeset
934 *lineno = (int)hold;
7da7ba5c6525 Added Py_UseClassExceptionsFlag, the variable containing the state of
Barry Warsaw <barry@python.org>
parents: 6165
diff changeset
935
7da7ba5c6525 Added Py_UseClassExceptionsFlag, the variable containing the state of
Barry Warsaw <barry@python.org>
parents: 6165
diff changeset
936 if (!(v = PyObject_GetAttrString(err, "offset")))
7da7ba5c6525 Added Py_UseClassExceptionsFlag, the variable containing the state of
Barry Warsaw <barry@python.org>
parents: 6165
diff changeset
937 goto finally;
16831
27f04d714ecb Print the offending line of code in the traceback for SyntaxErrors
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16648
diff changeset
938 if (v == Py_None) {
27f04d714ecb Print the offending line of code in the traceback for SyntaxErrors
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16648
diff changeset
939 *offset = -1;
27f04d714ecb Print the offending line of code in the traceback for SyntaxErrors
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16648
diff changeset
940 Py_DECREF(v);
27f04d714ecb Print the offending line of code in the traceback for SyntaxErrors
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16648
diff changeset
941 v = NULL;
27f04d714ecb Print the offending line of code in the traceback for SyntaxErrors
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16648
diff changeset
942 } else {
27f04d714ecb Print the offending line of code in the traceback for SyntaxErrors
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16648
diff changeset
943 hold = PyInt_AsLong(v);
27f04d714ecb Print the offending line of code in the traceback for SyntaxErrors
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16648
diff changeset
944 Py_DECREF(v);
27f04d714ecb Print the offending line of code in the traceback for SyntaxErrors
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16648
diff changeset
945 v = NULL;
27f04d714ecb Print the offending line of code in the traceback for SyntaxErrors
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16648
diff changeset
946 if (hold < 0 && PyErr_Occurred())
27f04d714ecb Print the offending line of code in the traceback for SyntaxErrors
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16648
diff changeset
947 goto finally;
27f04d714ecb Print the offending line of code in the traceback for SyntaxErrors
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16648
diff changeset
948 *offset = (int)hold;
27f04d714ecb Print the offending line of code in the traceback for SyntaxErrors
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16648
diff changeset
949 }
6197
7da7ba5c6525 Added Py_UseClassExceptionsFlag, the variable containing the state of
Barry Warsaw <barry@python.org>
parents: 6165
diff changeset
950
7da7ba5c6525 Added Py_UseClassExceptionsFlag, the variable containing the state of
Barry Warsaw <barry@python.org>
parents: 6165
diff changeset
951 if (!(v = PyObject_GetAttrString(err, "text")))
7da7ba5c6525 Added Py_UseClassExceptionsFlag, the variable containing the state of
Barry Warsaw <barry@python.org>
parents: 6165
diff changeset
952 goto finally;
7da7ba5c6525 Added Py_UseClassExceptionsFlag, the variable containing the state of
Barry Warsaw <barry@python.org>
parents: 6165
diff changeset
953 if (v == Py_None)
7da7ba5c6525 Added Py_UseClassExceptionsFlag, the variable containing the state of
Barry Warsaw <barry@python.org>
parents: 6165
diff changeset
954 *text = NULL;
7da7ba5c6525 Added Py_UseClassExceptionsFlag, the variable containing the state of
Barry Warsaw <barry@python.org>
parents: 6165
diff changeset
955 else if (! (*text = PyString_AsString(v)))
7da7ba5c6525 Added Py_UseClassExceptionsFlag, the variable containing the state of
Barry Warsaw <barry@python.org>
parents: 6165
diff changeset
956 goto finally;
7da7ba5c6525 Added Py_UseClassExceptionsFlag, the variable containing the state of
Barry Warsaw <barry@python.org>
parents: 6165
diff changeset
957 Py_DECREF(v);
7da7ba5c6525 Added Py_UseClassExceptionsFlag, the variable containing the state of
Barry Warsaw <barry@python.org>
parents: 6165
diff changeset
958 return 1;
7da7ba5c6525 Added Py_UseClassExceptionsFlag, the variable containing the state of
Barry Warsaw <barry@python.org>
parents: 6165
diff changeset
959
7da7ba5c6525 Added Py_UseClassExceptionsFlag, the variable containing the state of
Barry Warsaw <barry@python.org>
parents: 6165
diff changeset
960 finally:
7da7ba5c6525 Added Py_UseClassExceptionsFlag, the variable containing the state of
Barry Warsaw <barry@python.org>
parents: 6165
diff changeset
961 Py_XDECREF(v);
7da7ba5c6525 Added Py_UseClassExceptionsFlag, the variable containing the state of
Barry Warsaw <barry@python.org>
parents: 6165
diff changeset
962 return 0;
7da7ba5c6525 Added Py_UseClassExceptionsFlag, the variable containing the state of
Barry Warsaw <barry@python.org>
parents: 6165
diff changeset
963 }
7da7ba5c6525 Added Py_UseClassExceptionsFlag, the variable containing the state of
Barry Warsaw <barry@python.org>
parents: 6165
diff changeset
964
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
965 void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13620
diff changeset
966 PyErr_Print(void)
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
967 {
7206
3f255aedf621 Ehm, three unrelated changes.
Guido van Rossum <guido@python.org>
parents: 7113
diff changeset
968 PyErr_PrintEx(1);
3f255aedf621 Ehm, three unrelated changes.
Guido van Rossum <guido@python.org>
parents: 7113
diff changeset
969 }
3f255aedf621 Ehm, three unrelated changes.
Guido van Rossum <guido@python.org>
parents: 7113
diff changeset
970
16831
27f04d714ecb Print the offending line of code in the traceback for SyntaxErrors
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16648
diff changeset
971 static void
26601
cc05c2c8660e Constify filenames and scripts. Fixes #651362.
Martin v. Löwis <martin@v.loewis.de>
parents: 26263
diff changeset
972 print_error_text(PyObject *f, int offset, const char *text)
16831
27f04d714ecb Print the offending line of code in the traceback for SyntaxErrors
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16648
diff changeset
973 {
27f04d714ecb Print the offending line of code in the traceback for SyntaxErrors
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16648
diff changeset
974 char *nl;
27f04d714ecb Print the offending line of code in the traceback for SyntaxErrors
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16648
diff changeset
975 if (offset >= 0) {
27f04d714ecb Print the offending line of code in the traceback for SyntaxErrors
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16648
diff changeset
976 if (offset > 0 && offset == (int)strlen(text))
27f04d714ecb Print the offending line of code in the traceback for SyntaxErrors
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16648
diff changeset
977 offset--;
27f04d714ecb Print the offending line of code in the traceback for SyntaxErrors
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16648
diff changeset
978 for (;;) {
27f04d714ecb Print the offending line of code in the traceback for SyntaxErrors
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16648
diff changeset
979 nl = strchr(text, '\n');
27f04d714ecb Print the offending line of code in the traceback for SyntaxErrors
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16648
diff changeset
980 if (nl == NULL || nl-text >= offset)
27f04d714ecb Print the offending line of code in the traceback for SyntaxErrors
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16648
diff changeset
981 break;
27f04d714ecb Print the offending line of code in the traceback for SyntaxErrors
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16648
diff changeset
982 offset -= (nl+1-text);
27f04d714ecb Print the offending line of code in the traceback for SyntaxErrors
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16648
diff changeset
983 text = nl+1;
27f04d714ecb Print the offending line of code in the traceback for SyntaxErrors
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16648
diff changeset
984 }
27f04d714ecb Print the offending line of code in the traceback for SyntaxErrors
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16648
diff changeset
985 while (*text == ' ' || *text == '\t') {
27f04d714ecb Print the offending line of code in the traceback for SyntaxErrors
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16648
diff changeset
986 text++;
27f04d714ecb Print the offending line of code in the traceback for SyntaxErrors
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16648
diff changeset
987 offset--;
27f04d714ecb Print the offending line of code in the traceback for SyntaxErrors
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16648
diff changeset
988 }
27f04d714ecb Print the offending line of code in the traceback for SyntaxErrors
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16648
diff changeset
989 }
27f04d714ecb Print the offending line of code in the traceback for SyntaxErrors
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16648
diff changeset
990 PyFile_WriteString(" ", f);
27f04d714ecb Print the offending line of code in the traceback for SyntaxErrors
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16648
diff changeset
991 PyFile_WriteString(text, f);
27f04d714ecb Print the offending line of code in the traceback for SyntaxErrors
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16648
diff changeset
992 if (*text == '\0' || text[strlen(text)-1] != '\n')
27f04d714ecb Print the offending line of code in the traceback for SyntaxErrors
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16648
diff changeset
993 PyFile_WriteString("\n", f);
27f04d714ecb Print the offending line of code in the traceback for SyntaxErrors
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16648
diff changeset
994 if (offset == -1)
27f04d714ecb Print the offending line of code in the traceback for SyntaxErrors
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16648
diff changeset
995 return;
27f04d714ecb Print the offending line of code in the traceback for SyntaxErrors
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16648
diff changeset
996 PyFile_WriteString(" ", f);
27f04d714ecb Print the offending line of code in the traceback for SyntaxErrors
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16648
diff changeset
997 offset--;
27f04d714ecb Print the offending line of code in the traceback for SyntaxErrors
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16648
diff changeset
998 while (offset > 0) {
27f04d714ecb Print the offending line of code in the traceback for SyntaxErrors
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16648
diff changeset
999 PyFile_WriteString(" ", f);
27f04d714ecb Print the offending line of code in the traceback for SyntaxErrors
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16648
diff changeset
1000 offset--;
27f04d714ecb Print the offending line of code in the traceback for SyntaxErrors
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16648
diff changeset
1001 }
27f04d714ecb Print the offending line of code in the traceback for SyntaxErrors
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16648
diff changeset
1002 PyFile_WriteString("^\n", f);
27f04d714ecb Print the offending line of code in the traceback for SyntaxErrors
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16648
diff changeset
1003 }
27f04d714ecb Print the offending line of code in the traceback for SyntaxErrors
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16648
diff changeset
1004
17212
215dc725db84 Finishing touch to Ping's changes. This is a patch that Ping sent me
Guido van Rossum <guido@python.org>
parents: 17207
diff changeset
1005 static void
215dc725db84 Finishing touch to Ping's changes. This is a patch that Ping sent me
Guido van Rossum <guido@python.org>
parents: 17207
diff changeset
1006 handle_system_exit(void)
17191
eb06a02e0507 Allow sys.excepthook and sys.exitfunc to quietly exit with a sys.exit().
Ka-Ping Yee <ping@zesty.ca>
parents: 17168
diff changeset
1007 {
17212
215dc725db84 Finishing touch to Ping's changes. This is a patch that Ping sent me
Guido van Rossum <guido@python.org>
parents: 17207
diff changeset
1008 PyObject *exception, *value, *tb;
28698
aef01a076ef9 handle_system_exit(): This leaked the current exception info, in
Tim Peters <tim.peters@gmail.com>
parents: 28694
diff changeset
1009 int exitcode = 0;
aef01a076ef9 handle_system_exit(): This leaked the current exception info, in
Tim Peters <tim.peters@gmail.com>
parents: 28694
diff changeset
1010
17212
215dc725db84 Finishing touch to Ping's changes. This is a patch that Ping sent me
Guido van Rossum <guido@python.org>
parents: 17207
diff changeset
1011 PyErr_Fetch(&exception, &value, &tb);
17191
eb06a02e0507 Allow sys.excepthook and sys.exitfunc to quietly exit with a sys.exit().
Ka-Ping Yee <ping@zesty.ca>
parents: 17168
diff changeset
1012 if (Py_FlushLine())
eb06a02e0507 Allow sys.excepthook and sys.exitfunc to quietly exit with a sys.exit().
Ka-Ping Yee <ping@zesty.ca>
parents: 17168
diff changeset
1013 PyErr_Clear();
eb06a02e0507 Allow sys.excepthook and sys.exitfunc to quietly exit with a sys.exit().
Ka-Ping Yee <ping@zesty.ca>
parents: 17168
diff changeset
1014 fflush(stdout);
eb06a02e0507 Allow sys.excepthook and sys.exitfunc to quietly exit with a sys.exit().
Ka-Ping Yee <ping@zesty.ca>
parents: 17168
diff changeset
1015 if (value == NULL || value == Py_None)
28698
aef01a076ef9 handle_system_exit(): This leaked the current exception info, in
Tim Peters <tim.peters@gmail.com>
parents: 28694
diff changeset
1016 goto done;
17191
eb06a02e0507 Allow sys.excepthook and sys.exitfunc to quietly exit with a sys.exit().
Ka-Ping Yee <ping@zesty.ca>
parents: 17168
diff changeset
1017 if (PyInstance_Check(value)) {
eb06a02e0507 Allow sys.excepthook and sys.exitfunc to quietly exit with a sys.exit().
Ka-Ping Yee <ping@zesty.ca>
parents: 17168
diff changeset
1018 /* The error code should be in the `code' attribute. */
eb06a02e0507 Allow sys.excepthook and sys.exitfunc to quietly exit with a sys.exit().
Ka-Ping Yee <ping@zesty.ca>
parents: 17168
diff changeset
1019 PyObject *code = PyObject_GetAttrString(value, "code");
eb06a02e0507 Allow sys.excepthook and sys.exitfunc to quietly exit with a sys.exit().
Ka-Ping Yee <ping@zesty.ca>
parents: 17168
diff changeset
1020 if (code) {
eb06a02e0507 Allow sys.excepthook and sys.exitfunc to quietly exit with a sys.exit().
Ka-Ping Yee <ping@zesty.ca>
parents: 17168
diff changeset
1021 Py_DECREF(value);
eb06a02e0507 Allow sys.excepthook and sys.exitfunc to quietly exit with a sys.exit().
Ka-Ping Yee <ping@zesty.ca>
parents: 17168
diff changeset
1022 value = code;
eb06a02e0507 Allow sys.excepthook and sys.exitfunc to quietly exit with a sys.exit().
Ka-Ping Yee <ping@zesty.ca>
parents: 17168
diff changeset
1023 if (value == Py_None)
28698
aef01a076ef9 handle_system_exit(): This leaked the current exception info, in
Tim Peters <tim.peters@gmail.com>
parents: 28694
diff changeset
1024 goto done;
17191
eb06a02e0507 Allow sys.excepthook and sys.exitfunc to quietly exit with a sys.exit().
Ka-Ping Yee <ping@zesty.ca>
parents: 17168
diff changeset
1025 }
eb06a02e0507 Allow sys.excepthook and sys.exitfunc to quietly exit with a sys.exit().
Ka-Ping Yee <ping@zesty.ca>
parents: 17168
diff changeset
1026 /* If we failed to dig out the 'code' attribute,
eb06a02e0507 Allow sys.excepthook and sys.exitfunc to quietly exit with a sys.exit().
Ka-Ping Yee <ping@zesty.ca>
parents: 17168
diff changeset
1027 just let the else clause below print the error. */
eb06a02e0507 Allow sys.excepthook and sys.exitfunc to quietly exit with a sys.exit().
Ka-Ping Yee <ping@zesty.ca>
parents: 17168
diff changeset
1028 }
eb06a02e0507 Allow sys.excepthook and sys.exitfunc to quietly exit with a sys.exit().
Ka-Ping Yee <ping@zesty.ca>
parents: 17168
diff changeset
1029 if (PyInt_Check(value))
28698
aef01a076ef9 handle_system_exit(): This leaked the current exception info, in
Tim Peters <tim.peters@gmail.com>
parents: 28694
diff changeset
1030 exitcode = (int)PyInt_AsLong(value);
17191
eb06a02e0507 Allow sys.excepthook and sys.exitfunc to quietly exit with a sys.exit().
Ka-Ping Yee <ping@zesty.ca>
parents: 17168
diff changeset
1031 else {
eb06a02e0507 Allow sys.excepthook and sys.exitfunc to quietly exit with a sys.exit().
Ka-Ping Yee <ping@zesty.ca>
parents: 17168
diff changeset
1032 PyObject_Print(value, stderr, Py_PRINT_RAW);
eb06a02e0507 Allow sys.excepthook and sys.exitfunc to quietly exit with a sys.exit().
Ka-Ping Yee <ping@zesty.ca>
parents: 17168
diff changeset
1033 PySys_WriteStderr("\n");
28698
aef01a076ef9 handle_system_exit(): This leaked the current exception info, in
Tim Peters <tim.peters@gmail.com>
parents: 28694
diff changeset
1034 exitcode = 1;
17191
eb06a02e0507 Allow sys.excepthook and sys.exitfunc to quietly exit with a sys.exit().
Ka-Ping Yee <ping@zesty.ca>
parents: 17168
diff changeset
1035 }
28698
aef01a076ef9 handle_system_exit(): This leaked the current exception info, in
Tim Peters <tim.peters@gmail.com>
parents: 28694
diff changeset
1036 done:
aef01a076ef9 handle_system_exit(): This leaked the current exception info, in
Tim Peters <tim.peters@gmail.com>
parents: 28694
diff changeset
1037 /* Restore and clear the exception info, in order to properly decref
aef01a076ef9 handle_system_exit(): This leaked the current exception info, in
Tim Peters <tim.peters@gmail.com>
parents: 28694
diff changeset
1038 * the exception, value, and traceback. If we just exit instead,
aef01a076ef9 handle_system_exit(): This leaked the current exception info, in
Tim Peters <tim.peters@gmail.com>
parents: 28694
diff changeset
1039 * these leak, which confuses PYTHONDUMPREFS output, and may prevent
aef01a076ef9 handle_system_exit(): This leaked the current exception info, in
Tim Peters <tim.peters@gmail.com>
parents: 28694
diff changeset
1040 * some finalizers from running.
aef01a076ef9 handle_system_exit(): This leaked the current exception info, in
Tim Peters <tim.peters@gmail.com>
parents: 28694
diff changeset
1041 */
aef01a076ef9 handle_system_exit(): This leaked the current exception info, in
Tim Peters <tim.peters@gmail.com>
parents: 28694
diff changeset
1042 PyErr_Restore(exception, value, tb);
aef01a076ef9 handle_system_exit(): This leaked the current exception info, in
Tim Peters <tim.peters@gmail.com>
parents: 28694
diff changeset
1043 PyErr_Clear();
aef01a076ef9 handle_system_exit(): This leaked the current exception info, in
Tim Peters <tim.peters@gmail.com>
parents: 28694
diff changeset
1044 Py_Exit(exitcode);
aef01a076ef9 handle_system_exit(): This leaked the current exception info, in
Tim Peters <tim.peters@gmail.com>
parents: 28694
diff changeset
1045 /* NOTREACHED */
17191
eb06a02e0507 Allow sys.excepthook and sys.exitfunc to quietly exit with a sys.exit().
Ka-Ping Yee <ping@zesty.ca>
parents: 17168
diff changeset
1046 }
eb06a02e0507 Allow sys.excepthook and sys.exitfunc to quietly exit with a sys.exit().
Ka-Ping Yee <ping@zesty.ca>
parents: 17168
diff changeset
1047
eb06a02e0507 Allow sys.excepthook and sys.exitfunc to quietly exit with a sys.exit().
Ka-Ping Yee <ping@zesty.ca>
parents: 17168
diff changeset
1048 void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13620
diff changeset
1049 PyErr_PrintEx(int set_sys_last_vars)
7206
3f255aedf621 Ehm, three unrelated changes.
Guido van Rossum <guido@python.org>
parents: 7113
diff changeset
1050 {
17164
b44e77a12978 Add sys.excepthook.
Ka-Ping Yee <ping@zesty.ca>
parents: 17125
diff changeset
1051 PyObject *exception, *v, *tb, *hook;
17212
215dc725db84 Finishing touch to Ping's changes. This is a patch that Ping sent me
Guido van Rossum <guido@python.org>
parents: 17207
diff changeset
1052
215dc725db84 Finishing touch to Ping's changes. This is a patch that Ping sent me
Guido van Rossum <guido@python.org>
parents: 17207
diff changeset
1053 if (PyErr_ExceptionMatches(PyExc_SystemExit)) {
215dc725db84 Finishing touch to Ping's changes. This is a patch that Ping sent me
Guido van Rossum <guido@python.org>
parents: 17207
diff changeset
1054 handle_system_exit();
215dc725db84 Finishing touch to Ping's changes. This is a patch that Ping sent me
Guido van Rossum <guido@python.org>
parents: 17207
diff changeset
1055 }
5170
f5d707ce4223 Greatly renamed. Not a very thorough job -- I'm going to restructure
Guido van Rossum <guido@python.org>
parents: 5108
diff changeset
1056 PyErr_Fetch(&exception, &v, &tb);
6197
7da7ba5c6525 Added Py_UseClassExceptionsFlag, the variable containing the state of
Barry Warsaw <barry@python.org>
parents: 6165
diff changeset
1057 PyErr_NormalizeException(&exception, &v, &tb);
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2022
diff changeset
1058 if (exception == NULL)
5611
5ed3a21cc122 Can't return 0 from void function...
Guido van Rossum <guido@python.org>
parents: 5607
diff changeset
1059 return;
7206
3f255aedf621 Ehm, three unrelated changes.
Guido van Rossum <guido@python.org>
parents: 7113
diff changeset
1060 if (set_sys_last_vars) {
3f255aedf621 Ehm, three unrelated changes.
Guido van Rossum <guido@python.org>
parents: 7113
diff changeset
1061 PySys_SetObject("last_type", exception);
3f255aedf621 Ehm, three unrelated changes.
Guido van Rossum <guido@python.org>
parents: 7113
diff changeset
1062 PySys_SetObject("last_value", v);
3f255aedf621 Ehm, three unrelated changes.
Guido van Rossum <guido@python.org>
parents: 7113
diff changeset
1063 PySys_SetObject("last_traceback", tb);
3f255aedf621 Ehm, three unrelated changes.
Guido van Rossum <guido@python.org>
parents: 7113
diff changeset
1064 }
17164
b44e77a12978 Add sys.excepthook.
Ka-Ping Yee <ping@zesty.ca>
parents: 17125
diff changeset
1065 hook = PySys_GetObject("excepthook");
b44e77a12978 Add sys.excepthook.
Ka-Ping Yee <ping@zesty.ca>
parents: 17125
diff changeset
1066 if (hook) {
30683
3b59fa5d25b1 Simplify and speedup uses of Py_BuildValue():
Raymond Hettinger <python@rcn.com>
parents: 30122
diff changeset
1067 PyObject *args = PyTuple_Pack(3,
17164
b44e77a12978 Add sys.excepthook.
Ka-Ping Yee <ping@zesty.ca>
parents: 17125
diff changeset
1068 exception, v ? v : Py_None, tb ? tb : Py_None);
b44e77a12978 Add sys.excepthook.
Ka-Ping Yee <ping@zesty.ca>
parents: 17125
diff changeset
1069 PyObject *result = PyEval_CallObject(hook, args);
b44e77a12978 Add sys.excepthook.
Ka-Ping Yee <ping@zesty.ca>
parents: 17125
diff changeset
1070 if (result == NULL) {
b44e77a12978 Add sys.excepthook.
Ka-Ping Yee <ping@zesty.ca>
parents: 17125
diff changeset
1071 PyObject *exception2, *v2, *tb2;
17212
215dc725db84 Finishing touch to Ping's changes. This is a patch that Ping sent me
Guido van Rossum <guido@python.org>
parents: 17207
diff changeset
1072 if (PyErr_ExceptionMatches(PyExc_SystemExit)) {
215dc725db84 Finishing touch to Ping's changes. This is a patch that Ping sent me
Guido van Rossum <guido@python.org>
parents: 17207
diff changeset
1073 handle_system_exit();
215dc725db84 Finishing touch to Ping's changes. This is a patch that Ping sent me
Guido van Rossum <guido@python.org>
parents: 17207
diff changeset
1074 }
17164
b44e77a12978 Add sys.excepthook.
Ka-Ping Yee <ping@zesty.ca>
parents: 17125
diff changeset
1075 PyErr_Fetch(&exception2, &v2, &tb2);
b44e77a12978 Add sys.excepthook.
Ka-Ping Yee <ping@zesty.ca>
parents: 17125
diff changeset
1076 PyErr_NormalizeException(&exception2, &v2, &tb2);
b44e77a12978 Add sys.excepthook.
Ka-Ping Yee <ping@zesty.ca>
parents: 17125
diff changeset
1077 if (Py_FlushLine())
b44e77a12978 Add sys.excepthook.
Ka-Ping Yee <ping@zesty.ca>
parents: 17125
diff changeset
1078 PyErr_Clear();
b44e77a12978 Add sys.excepthook.
Ka-Ping Yee <ping@zesty.ca>
parents: 17125
diff changeset
1079 fflush(stdout);
b44e77a12978 Add sys.excepthook.
Ka-Ping Yee <ping@zesty.ca>
parents: 17125
diff changeset
1080 PySys_WriteStderr("Error in sys.excepthook:\n");
b44e77a12978 Add sys.excepthook.
Ka-Ping Yee <ping@zesty.ca>
parents: 17125
diff changeset
1081 PyErr_Display(exception2, v2, tb2);
b44e77a12978 Add sys.excepthook.
Ka-Ping Yee <ping@zesty.ca>
parents: 17125
diff changeset
1082 PySys_WriteStderr("\nOriginal exception was:\n");
b44e77a12978 Add sys.excepthook.
Ka-Ping Yee <ping@zesty.ca>
parents: 17125
diff changeset
1083 PyErr_Display(exception, v, tb);
21284
43b8a962a32a Missing DECREFs when exception is raised in sys.excepthook.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 21251
diff changeset
1084 Py_XDECREF(exception2);
43b8a962a32a Missing DECREFs when exception is raised in sys.excepthook.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 21251
diff changeset
1085 Py_XDECREF(v2);
43b8a962a32a Missing DECREFs when exception is raised in sys.excepthook.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 21251
diff changeset
1086 Py_XDECREF(tb2);
17164
b44e77a12978 Add sys.excepthook.
Ka-Ping Yee <ping@zesty.ca>
parents: 17125
diff changeset
1087 }
b44e77a12978 Add sys.excepthook.
Ka-Ping Yee <ping@zesty.ca>
parents: 17125
diff changeset
1088 Py_XDECREF(result);
b44e77a12978 Add sys.excepthook.
Ka-Ping Yee <ping@zesty.ca>
parents: 17125
diff changeset
1089 Py_XDECREF(args);
b44e77a12978 Add sys.excepthook.
Ka-Ping Yee <ping@zesty.ca>
parents: 17125
diff changeset
1090 } else {
b44e77a12978 Add sys.excepthook.
Ka-Ping Yee <ping@zesty.ca>
parents: 17125
diff changeset
1091 PySys_WriteStderr("sys.excepthook is missing\n");
b44e77a12978 Add sys.excepthook.
Ka-Ping Yee <ping@zesty.ca>
parents: 17125
diff changeset
1092 PyErr_Display(exception, v, tb);
b44e77a12978 Add sys.excepthook.
Ka-Ping Yee <ping@zesty.ca>
parents: 17125
diff changeset
1093 }
b44e77a12978 Add sys.excepthook.
Ka-Ping Yee <ping@zesty.ca>
parents: 17125
diff changeset
1094 Py_XDECREF(exception);
b44e77a12978 Add sys.excepthook.
Ka-Ping Yee <ping@zesty.ca>
parents: 17125
diff changeset
1095 Py_XDECREF(v);
b44e77a12978 Add sys.excepthook.
Ka-Ping Yee <ping@zesty.ca>
parents: 17125
diff changeset
1096 Py_XDECREF(tb);
b44e77a12978 Add sys.excepthook.
Ka-Ping Yee <ping@zesty.ca>
parents: 17125
diff changeset
1097 }
b44e77a12978 Add sys.excepthook.
Ka-Ping Yee <ping@zesty.ca>
parents: 17125
diff changeset
1098
b44e77a12978 Add sys.excepthook.
Ka-Ping Yee <ping@zesty.ca>
parents: 17125
diff changeset
1099 void PyErr_Display(PyObject *exception, PyObject *value, PyObject *tb)
b44e77a12978 Add sys.excepthook.
Ka-Ping Yee <ping@zesty.ca>
parents: 17125
diff changeset
1100 {
b44e77a12978 Add sys.excepthook.
Ka-Ping Yee <ping@zesty.ca>
parents: 17125
diff changeset
1101 int err = 0;
b44e77a12978 Add sys.excepthook.
Ka-Ping Yee <ping@zesty.ca>
parents: 17125
diff changeset
1102 PyObject *f = PySys_GetObject("stderr");
31771
76053a1dd452 Lost reference.
Armin Rigo <arigo@tunes.org>
parents: 31504
diff changeset
1103 Py_INCREF(value);
1073
2c0432fcc270 Modified most (but not yet all) I/O to always go through sys.stdout or
Guido van Rossum <guido@python.org>
parents: 1015
diff changeset
1104 if (f == NULL)
2c0432fcc270 Modified most (but not yet all) I/O to always go through sys.stdout or
Guido van Rossum <guido@python.org>
parents: 1015
diff changeset
1105 fprintf(stderr, "lost sys.stderr\n");
2c0432fcc270 Modified most (but not yet all) I/O to always go through sys.stdout or
Guido van Rossum <guido@python.org>
parents: 1015
diff changeset
1106 else {
7509
b5d52c415b15 Fix the handling of errors in Py_FlushLine() in a few places.
Guido van Rossum <guido@python.org>
parents: 7206
diff changeset
1107 if (Py_FlushLine())
b5d52c415b15 Fix the handling of errors in Py_FlushLine() in a few places.
Guido van Rossum <guido@python.org>
parents: 7206
diff changeset
1108 PyErr_Clear();
5607
cbb4e3cf086e Py_FlushLine and PyFile_WriteString now return error indicators
Guido van Rossum <guido@python.org>
parents: 5593
diff changeset
1109 fflush(stdout);
17164
b44e77a12978 Add sys.excepthook.
Ka-Ping Yee <ping@zesty.ca>
parents: 17125
diff changeset
1110 if (tb && tb != Py_None)
b44e77a12978 Add sys.excepthook.
Ka-Ping Yee <ping@zesty.ca>
parents: 17125
diff changeset
1111 err = PyTraceBack_Print(tb, f);
6165
29ddf1049842 PyErr_Print(): Use PyErr_GivenExceptionMatches() instead of pointer
Barry Warsaw <barry@python.org>
parents: 6136
diff changeset
1112 if (err == 0 &&
31771
76053a1dd452 Lost reference.
Armin Rigo <arigo@tunes.org>
parents: 31504
diff changeset
1113 PyObject_HasAttrString(value, "print_file_and_line"))
6165
29ddf1049842 PyErr_Print(): Use PyErr_GivenExceptionMatches() instead of pointer
Barry Warsaw <barry@python.org>
parents: 6136
diff changeset
1114 {
5170
f5d707ce4223 Greatly renamed. Not a very thorough job -- I'm going to restructure
Guido van Rossum <guido@python.org>
parents: 5108
diff changeset
1115 PyObject *message;
26601
cc05c2c8660e Constify filenames and scripts. Fixes #651362.
Martin v. Löwis <martin@v.loewis.de>
parents: 26263
diff changeset
1116 const char *filename, *text;
1839
1f4a86dbcd79 * Python/pythonrun.c (print_error): added INCREF/DECREF pair --
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
1117 int lineno, offset;
31771
76053a1dd452 Lost reference.
Armin Rigo <arigo@tunes.org>
parents: 31504
diff changeset
1118 if (!parse_syntax_error(value, &message, &filename,
6197
7da7ba5c6525 Added Py_UseClassExceptionsFlag, the variable containing the state of
Barry Warsaw <barry@python.org>
parents: 6165
diff changeset
1119 &lineno, &offset, &text))
5170
f5d707ce4223 Greatly renamed. Not a very thorough job -- I'm going to restructure
Guido van Rossum <guido@python.org>
parents: 5108
diff changeset
1120 PyErr_Clear();
1839
1f4a86dbcd79 * Python/pythonrun.c (print_error): added INCREF/DECREF pair --
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
1121 else {
1f4a86dbcd79 * Python/pythonrun.c (print_error): added INCREF/DECREF pair --
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
1122 char buf[10];
5170
f5d707ce4223 Greatly renamed. Not a very thorough job -- I'm going to restructure
Guido van Rossum <guido@python.org>
parents: 5108
diff changeset
1123 PyFile_WriteString(" File \"", f);
1839
1f4a86dbcd79 * Python/pythonrun.c (print_error): added INCREF/DECREF pair --
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
1124 if (filename == NULL)
5170
f5d707ce4223 Greatly renamed. Not a very thorough job -- I'm going to restructure
Guido van Rossum <guido@python.org>
parents: 5108
diff changeset
1125 PyFile_WriteString("<string>", f);
1839
1f4a86dbcd79 * Python/pythonrun.c (print_error): added INCREF/DECREF pair --
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
1126 else
5170
f5d707ce4223 Greatly renamed. Not a very thorough job -- I'm going to restructure
Guido van Rossum <guido@python.org>
parents: 5108
diff changeset
1127 PyFile_WriteString(filename, f);
f5d707ce4223 Greatly renamed. Not a very thorough job -- I'm going to restructure
Guido van Rossum <guido@python.org>
parents: 5108
diff changeset
1128 PyFile_WriteString("\", line ", f);
21077
379fc66f4c96 Use PyOS_snprintf instead of sprintf.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20969
diff changeset
1129 PyOS_snprintf(buf, sizeof(buf), "%d", lineno);
5170
f5d707ce4223 Greatly renamed. Not a very thorough job -- I'm going to restructure
Guido van Rossum <guido@python.org>
parents: 5108
diff changeset
1130 PyFile_WriteString(buf, f);
f5d707ce4223 Greatly renamed. Not a very thorough job -- I'm going to restructure
Guido van Rossum <guido@python.org>
parents: 5108
diff changeset
1131 PyFile_WriteString("\n", f);
16831
27f04d714ecb Print the offending line of code in the traceback for SyntaxErrors
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16648
diff changeset
1132 if (text != NULL)
27f04d714ecb Print the offending line of code in the traceback for SyntaxErrors
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16648
diff changeset
1133 print_error_text(f, offset, text);
31771
76053a1dd452 Lost reference.
Armin Rigo <arigo@tunes.org>
parents: 31504
diff changeset
1134 Py_DECREF(value);
76053a1dd452 Lost reference.
Armin Rigo <arigo@tunes.org>
parents: 31504
diff changeset
1135 value = message;
5607
cbb4e3cf086e Py_FlushLine and PyFile_WriteString now return error indicators
Guido van Rossum <guido@python.org>
parents: 5593
diff changeset
1136 /* Can't be bothered to check all those
cbb4e3cf086e Py_FlushLine and PyFile_WriteString now return error indicators
Guido van Rossum <guido@python.org>
parents: 5593
diff changeset
1137 PyFile_WriteString() calls */
cbb4e3cf086e Py_FlushLine and PyFile_WriteString now return error indicators
Guido van Rossum <guido@python.org>
parents: 5593
diff changeset
1138 if (PyErr_Occurred())
cbb4e3cf086e Py_FlushLine and PyFile_WriteString now return error indicators
Guido van Rossum <guido@python.org>
parents: 5593
diff changeset
1139 err = -1;
1839
1f4a86dbcd79 * Python/pythonrun.c (print_error): added INCREF/DECREF pair --
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
1140 }
1f4a86dbcd79 * Python/pythonrun.c (print_error): added INCREF/DECREF pair --
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
1141 }
5607
cbb4e3cf086e Py_FlushLine and PyFile_WriteString now return error indicators
Guido van Rossum <guido@python.org>
parents: 5593
diff changeset
1142 if (err) {
cbb4e3cf086e Py_FlushLine and PyFile_WriteString now return error indicators
Guido van Rossum <guido@python.org>
parents: 5593
diff changeset
1143 /* Don't do anything else */
cbb4e3cf086e Py_FlushLine and PyFile_WriteString now return error indicators
Guido van Rossum <guido@python.org>
parents: 5593
diff changeset
1144 }
cbb4e3cf086e Py_FlushLine and PyFile_WriteString now return error indicators
Guido van Rossum <guido@python.org>
parents: 5593
diff changeset
1145 else if (PyClass_Check(exception)) {
6322
05d943c4597c PyErr_Print(): When printing a class exception, try to dig out the
Barry Warsaw <barry@python.org>
parents: 6246
diff changeset
1146 PyClassObject* exc = (PyClassObject*)exception;
05d943c4597c PyErr_Print(): When printing a class exception, try to dig out the
Barry Warsaw <barry@python.org>
parents: 6246
diff changeset
1147 PyObject* className = exc->cl_name;
05d943c4597c PyErr_Print(): When printing a class exception, try to dig out the
Barry Warsaw <barry@python.org>
parents: 6246
diff changeset
1148 PyObject* moduleName =
05d943c4597c PyErr_Print(): When printing a class exception, try to dig out the
Barry Warsaw <barry@python.org>
parents: 6246
diff changeset
1149 PyDict_GetItemString(exc->cl_dict, "__module__");
05d943c4597c PyErr_Print(): When printing a class exception, try to dig out the
Barry Warsaw <barry@python.org>
parents: 6246
diff changeset
1150
05d943c4597c PyErr_Print(): When printing a class exception, try to dig out the
Barry Warsaw <barry@python.org>
parents: 6246
diff changeset
1151 if (moduleName == NULL)
5607
cbb4e3cf086e Py_FlushLine and PyFile_WriteString now return error indicators
Guido van Rossum <guido@python.org>
parents: 5593
diff changeset
1152 err = PyFile_WriteString("<unknown>", f);
6322
05d943c4597c PyErr_Print(): When printing a class exception, try to dig out the
Barry Warsaw <barry@python.org>
parents: 6246
diff changeset
1153 else {
05d943c4597c PyErr_Print(): When printing a class exception, try to dig out the
Barry Warsaw <barry@python.org>
parents: 6246
diff changeset
1154 char* modstr = PyString_AsString(moduleName);
28648
0e8416be208e Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 28647
diff changeset
1155 if (modstr && strcmp(modstr, "exceptions"))
6322
05d943c4597c PyErr_Print(): When printing a class exception, try to dig out the
Barry Warsaw <barry@python.org>
parents: 6246
diff changeset
1156 {
05d943c4597c PyErr_Print(): When printing a class exception, try to dig out the
Barry Warsaw <barry@python.org>
parents: 6246
diff changeset
1157 err = PyFile_WriteString(modstr, f);
05d943c4597c PyErr_Print(): When printing a class exception, try to dig out the
Barry Warsaw <barry@python.org>
parents: 6246
diff changeset
1158 err += PyFile_WriteString(".", f);
05d943c4597c PyErr_Print(): When printing a class exception, try to dig out the
Barry Warsaw <barry@python.org>
parents: 6246
diff changeset
1159 }
05d943c4597c PyErr_Print(): When printing a class exception, try to dig out the
Barry Warsaw <barry@python.org>
parents: 6246
diff changeset
1160 }
05d943c4597c PyErr_Print(): When printing a class exception, try to dig out the
Barry Warsaw <barry@python.org>
parents: 6246
diff changeset
1161 if (err == 0) {
05d943c4597c PyErr_Print(): When printing a class exception, try to dig out the
Barry Warsaw <barry@python.org>
parents: 6246
diff changeset
1162 if (className == NULL)
05d943c4597c PyErr_Print(): When printing a class exception, try to dig out the
Barry Warsaw <barry@python.org>
parents: 6246
diff changeset
1163 err = PyFile_WriteString("<unknown>", f);
05d943c4597c PyErr_Print(): When printing a class exception, try to dig out the
Barry Warsaw <barry@python.org>
parents: 6246
diff changeset
1164 else
05d943c4597c PyErr_Print(): When printing a class exception, try to dig out the
Barry Warsaw <barry@python.org>
parents: 6246
diff changeset
1165 err = PyFile_WriteObject(className, f,
05d943c4597c PyErr_Print(): When printing a class exception, try to dig out the
Barry Warsaw <barry@python.org>
parents: 6246
diff changeset
1166 Py_PRINT_RAW);
05d943c4597c PyErr_Print(): When printing a class exception, try to dig out the
Barry Warsaw <barry@python.org>
parents: 6246
diff changeset
1167 }
5607
cbb4e3cf086e Py_FlushLine and PyFile_WriteString now return error indicators
Guido van Rossum <guido@python.org>
parents: 5593
diff changeset
1168 }
cbb4e3cf086e Py_FlushLine and PyFile_WriteString now return error indicators
Guido van Rossum <guido@python.org>
parents: 5593
diff changeset
1169 else
cbb4e3cf086e Py_FlushLine and PyFile_WriteString now return error indicators
Guido van Rossum <guido@python.org>
parents: 5593
diff changeset
1170 err = PyFile_WriteObject(exception, f, Py_PRINT_RAW);
cbb4e3cf086e Py_FlushLine and PyFile_WriteString now return error indicators
Guido van Rossum <guido@python.org>
parents: 5593
diff changeset
1171 if (err == 0) {
31771
76053a1dd452 Lost reference.
Armin Rigo <arigo@tunes.org>
parents: 31504
diff changeset
1172 if (value != Py_None) {
76053a1dd452 Lost reference.
Armin Rigo <arigo@tunes.org>
parents: 31504
diff changeset
1173 PyObject *s = PyObject_Str(value);
6197
7da7ba5c6525 Added Py_UseClassExceptionsFlag, the variable containing the state of
Barry Warsaw <barry@python.org>
parents: 6165
diff changeset
1174 /* only print colon if the str() of the
7da7ba5c6525 Added Py_UseClassExceptionsFlag, the variable containing the state of
Barry Warsaw <barry@python.org>
parents: 6165
diff changeset
1175 object is not the empty string
7da7ba5c6525 Added Py_UseClassExceptionsFlag, the variable containing the state of
Barry Warsaw <barry@python.org>
parents: 6165
diff changeset
1176 */
6246
4cf20fa883a9 Fixed some details of printing the str() of an exception. This fixes
Guido van Rossum <guido@python.org>
parents: 6201
diff changeset
1177 if (s == NULL)
4cf20fa883a9 Fixed some details of printing the str() of an exception. This fixes
Guido van Rossum <guido@python.org>
parents: 6201
diff changeset
1178 err = -1;
4cf20fa883a9 Fixed some details of printing the str() of an exception. This fixes
Guido van Rossum <guido@python.org>
parents: 6201
diff changeset
1179 else if (!PyString_Check(s) ||
4cf20fa883a9 Fixed some details of printing the str() of an exception. This fixes
Guido van Rossum <guido@python.org>
parents: 6201
diff changeset
1180 PyString_GET_SIZE(s) != 0)
6197
7da7ba5c6525 Added Py_UseClassExceptionsFlag, the variable containing the state of
Barry Warsaw <barry@python.org>
parents: 6165
diff changeset
1181 err = PyFile_WriteString(": ", f);
5607
cbb4e3cf086e Py_FlushLine and PyFile_WriteString now return error indicators
Guido van Rossum <guido@python.org>
parents: 5593
diff changeset
1182 if (err == 0)
6246
4cf20fa883a9 Fixed some details of printing the str() of an exception. This fixes
Guido van Rossum <guido@python.org>
parents: 6201
diff changeset
1183 err = PyFile_WriteObject(s, f, Py_PRINT_RAW);
4cf20fa883a9 Fixed some details of printing the str() of an exception. This fixes
Guido van Rossum <guido@python.org>
parents: 6201
diff changeset
1184 Py_XDECREF(s);
2293
a24146241991 print class name for exceptions that are classes
Guido van Rossum <guido@python.org>
parents: 2279
diff changeset
1185 }
a24146241991 print class name for exceptions that are classes
Guido van Rossum <guido@python.org>
parents: 2279
diff changeset
1186 }
5607
cbb4e3cf086e Py_FlushLine and PyFile_WriteString now return error indicators
Guido van Rossum <guido@python.org>
parents: 5593
diff changeset
1187 if (err == 0)
cbb4e3cf086e Py_FlushLine and PyFile_WriteString now return error indicators
Guido van Rossum <guido@python.org>
parents: 5593
diff changeset
1188 err = PyFile_WriteString("\n", f);
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1189 }
31771
76053a1dd452 Lost reference.
Armin Rigo <arigo@tunes.org>
parents: 31504
diff changeset
1190 Py_DECREF(value);
5607
cbb4e3cf086e Py_FlushLine and PyFile_WriteString now return error indicators
Guido van Rossum <guido@python.org>
parents: 5593
diff changeset
1191 /* If an error happened here, don't show it.
cbb4e3cf086e Py_FlushLine and PyFile_WriteString now return error indicators
Guido van Rossum <guido@python.org>
parents: 5593
diff changeset
1192 XXX This is wrong, but too many callers rely on this behavior. */
cbb4e3cf086e Py_FlushLine and PyFile_WriteString now return error indicators
Guido van Rossum <guido@python.org>
parents: 5593
diff changeset
1193 if (err != 0)
cbb4e3cf086e Py_FlushLine and PyFile_WriteString now return error indicators
Guido van Rossum <guido@python.org>
parents: 5593
diff changeset
1194 PyErr_Clear();
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1195 }
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1196
5170
f5d707ce4223 Greatly renamed. Not a very thorough job -- I'm going to restructure
Guido van Rossum <guido@python.org>
parents: 5108
diff changeset
1197 PyObject *
26601
cc05c2c8660e Constify filenames and scripts. Fixes #651362.
Martin v. Löwis <martin@v.loewis.de>
parents: 26263
diff changeset
1198 PyRun_String(const char *str, int start, PyObject *globals, PyObject *locals)
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1199 {
5170
f5d707ce4223 Greatly renamed. Not a very thorough job -- I'm going to restructure
Guido van Rossum <guido@python.org>
parents: 5108
diff changeset
1200 return run_err_node(PyParser_SimpleParseString(str, start),
16916
b0456d3a0f1f Useful future statement support for the interactive interpreter
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16845
diff changeset
1201 "<string>", globals, locals, NULL);
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1202 }
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1203
5170
f5d707ce4223 Greatly renamed. Not a very thorough job -- I'm going to restructure
Guido van Rossum <guido@python.org>
parents: 5108
diff changeset
1204 PyObject *
26601
cc05c2c8660e Constify filenames and scripts. Fixes #651362.
Martin v. Löwis <martin@v.loewis.de>
parents: 26263
diff changeset
1205 PyRun_File(FILE *fp, const char *filename, int start, PyObject *globals,
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13620
diff changeset
1206 PyObject *locals)
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1207 {
14231
396b4ab3b9c2 Hard to believe Guido compiled this! Function lacked a return stmt.
Tim Peters <tim.peters@gmail.com>
parents: 14228
diff changeset
1208 return PyRun_FileEx(fp, filename, start, globals, locals, 0);
14228
20377f15b96a Add three new APIs: PyRun_AnyFileEx(), PyRun_SimpleFileEx(),
Guido van Rossum <guido@python.org>
parents: 14227
diff changeset
1209 }
20377f15b96a Add three new APIs: PyRun_AnyFileEx(), PyRun_SimpleFileEx(),
Guido van Rossum <guido@python.org>
parents: 14227
diff changeset
1210
20377f15b96a Add three new APIs: PyRun_AnyFileEx(), PyRun_SimpleFileEx(),
Guido van Rossum <guido@python.org>
parents: 14227
diff changeset
1211 PyObject *
26601
cc05c2c8660e Constify filenames and scripts. Fixes #651362.
Martin v. Löwis <martin@v.loewis.de>
parents: 26263
diff changeset
1212 PyRun_FileEx(FILE *fp, const char *filename, int start, PyObject *globals,
17125
2054cea38754 Extend support for from __future__ import nested_scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16916
diff changeset
1213 PyObject *locals, int closeit)
14228
20377f15b96a Add three new APIs: PyRun_AnyFileEx(), PyRun_SimpleFileEx(),
Guido van Rossum <guido@python.org>
parents: 14227
diff changeset
1214 {
20377f15b96a Add three new APIs: PyRun_AnyFileEx(), PyRun_SimpleFileEx(),
Guido van Rossum <guido@python.org>
parents: 14227
diff changeset
1215 node *n = PyParser_SimpleParseFile(fp, filename, start);
20377f15b96a Add three new APIs: PyRun_AnyFileEx(), PyRun_SimpleFileEx(),
Guido van Rossum <guido@python.org>
parents: 14227
diff changeset
1216 if (closeit)
20377f15b96a Add three new APIs: PyRun_AnyFileEx(), PyRun_SimpleFileEx(),
Guido van Rossum <guido@python.org>
parents: 14227
diff changeset
1217 fclose(fp);
16916
b0456d3a0f1f Useful future statement support for the interactive interpreter
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16845
diff changeset
1218 return run_err_node(n, filename, globals, locals, NULL);
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1219 }
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1220
17125
2054cea38754 Extend support for from __future__ import nested_scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16916
diff changeset
1221 PyObject *
26601
cc05c2c8660e Constify filenames and scripts. Fixes #651362.
Martin v. Löwis <martin@v.loewis.de>
parents: 26263
diff changeset
1222 PyRun_StringFlags(const char *str, int start, PyObject *globals, PyObject *locals,
17125
2054cea38754 Extend support for from __future__ import nested_scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16916
diff changeset
1223 PyCompilerFlags *flags)
2054cea38754 Extend support for from __future__ import nested_scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16916
diff changeset
1224 {
18630
c9b4c8ae6179 PyRun_StringFlags(): forgot to pass the flags on to
Guido van Rossum <guido@python.org>
parents: 18615
diff changeset
1225 return run_err_node(PyParser_SimpleParseStringFlags(
22468
eebae4f60d05 Disable the parser hacks that enabled the "yield" keyword using a future
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 22121
diff changeset
1226 str, start, PARSER_FLAGS(flags)),
17125
2054cea38754 Extend support for from __future__ import nested_scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16916
diff changeset
1227 "<string>", globals, locals, flags);
2054cea38754 Extend support for from __future__ import nested_scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16916
diff changeset
1228 }
2054cea38754 Extend support for from __future__ import nested_scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16916
diff changeset
1229
2054cea38754 Extend support for from __future__ import nested_scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16916
diff changeset
1230 PyObject *
26601
cc05c2c8660e Constify filenames and scripts. Fixes #651362.
Martin v. Löwis <martin@v.loewis.de>
parents: 26263
diff changeset
1231 PyRun_FileFlags(FILE *fp, const char *filename, int start, PyObject *globals,
17125
2054cea38754 Extend support for from __future__ import nested_scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16916
diff changeset
1232 PyObject *locals, PyCompilerFlags *flags)
2054cea38754 Extend support for from __future__ import nested_scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16916
diff changeset
1233 {
2054cea38754 Extend support for from __future__ import nested_scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16916
diff changeset
1234 return PyRun_FileExFlags(fp, filename, start, globals, locals, 0,
28648
0e8416be208e Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 28647
diff changeset
1235 flags);
17125
2054cea38754 Extend support for from __future__ import nested_scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16916
diff changeset
1236 }
2054cea38754 Extend support for from __future__ import nested_scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16916
diff changeset
1237
2054cea38754 Extend support for from __future__ import nested_scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16916
diff changeset
1238 PyObject *
26601
cc05c2c8660e Constify filenames and scripts. Fixes #651362.
Martin v. Löwis <martin@v.loewis.de>
parents: 26263
diff changeset
1239 PyRun_FileExFlags(FILE *fp, const char *filename, int start, PyObject *globals,
17125
2054cea38754 Extend support for from __future__ import nested_scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16916
diff changeset
1240 PyObject *locals, int closeit, PyCompilerFlags *flags)
2054cea38754 Extend support for from __future__ import nested_scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16916
diff changeset
1241 {
18615
0a90e5ec61b6 Ugly. A pile of new xxxFlags() functions, to communicate to the parser
Tim Peters <tim.peters@gmail.com>
parents: 18612
diff changeset
1242 node *n = PyParser_SimpleParseFileFlags(fp, filename, start,
22468
eebae4f60d05 Disable the parser hacks that enabled the "yield" keyword using a future
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 22121
diff changeset
1243 PARSER_FLAGS(flags));
17125
2054cea38754 Extend support for from __future__ import nested_scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16916
diff changeset
1244 if (closeit)
2054cea38754 Extend support for from __future__ import nested_scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16916
diff changeset
1245 fclose(fp);
2054cea38754 Extend support for from __future__ import nested_scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16916
diff changeset
1246 return run_err_node(n, filename, globals, locals, flags);
2054cea38754 Extend support for from __future__ import nested_scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16916
diff changeset
1247 }
2054cea38754 Extend support for from __future__ import nested_scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16916
diff changeset
1248
5170
f5d707ce4223 Greatly renamed. Not a very thorough job -- I'm going to restructure
Guido van Rossum <guido@python.org>
parents: 5108
diff changeset
1249 static PyObject *
26601
cc05c2c8660e Constify filenames and scripts. Fixes #651362.
Martin v. Löwis <martin@v.loewis.de>
parents: 26263
diff changeset
1250 run_err_node(node *n, const char *filename, PyObject *globals, PyObject *locals,
16916
b0456d3a0f1f Useful future statement support for the interactive interpreter
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16845
diff changeset
1251 PyCompilerFlags *flags)
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1252 {
1839
1f4a86dbcd79 * Python/pythonrun.c (print_error): added INCREF/DECREF pair --
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
1253 if (n == NULL)
1f4a86dbcd79 * Python/pythonrun.c (print_error): added INCREF/DECREF pair --
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
1254 return NULL;
16916
b0456d3a0f1f Useful future statement support for the interactive interpreter
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16845
diff changeset
1255 return run_node(n, filename, globals, locals, flags);
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1256 }
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1257
5170
f5d707ce4223 Greatly renamed. Not a very thorough job -- I'm going to restructure
Guido van Rossum <guido@python.org>
parents: 5108
diff changeset
1258 static PyObject *
26601
cc05c2c8660e Constify filenames and scripts. Fixes #651362.
Martin v. Löwis <martin@v.loewis.de>
parents: 26263
diff changeset
1259 run_node(node *n, const char *filename, PyObject *globals, PyObject *locals,
16916
b0456d3a0f1f Useful future statement support for the interactive interpreter
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16845
diff changeset
1260 PyCompilerFlags *flags)
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1261 {
5170
f5d707ce4223 Greatly renamed. Not a very thorough job -- I'm going to restructure
Guido van Rossum <guido@python.org>
parents: 5108
diff changeset
1262 PyCodeObject *co;
f5d707ce4223 Greatly renamed. Not a very thorough job -- I'm going to restructure
Guido van Rossum <guido@python.org>
parents: 5108
diff changeset
1263 PyObject *v;
16916
b0456d3a0f1f Useful future statement support for the interactive interpreter
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16845
diff changeset
1264 co = PyNode_CompileFlags(n, filename, flags);
5170
f5d707ce4223 Greatly renamed. Not a very thorough job -- I'm going to restructure
Guido van Rossum <guido@python.org>
parents: 5108
diff changeset
1265 PyNode_Free(n);
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1266 if (co == NULL)
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1267 return NULL;
5170
f5d707ce4223 Greatly renamed. Not a very thorough job -- I'm going to restructure
Guido van Rossum <guido@python.org>
parents: 5108
diff changeset
1268 v = PyEval_EvalCode(co, globals, locals);
f5d707ce4223 Greatly renamed. Not a very thorough job -- I'm going to restructure
Guido van Rossum <guido@python.org>
parents: 5108
diff changeset
1269 Py_DECREF(co);
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1270 return v;
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1271 }
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1272
5170
f5d707ce4223 Greatly renamed. Not a very thorough job -- I'm going to restructure
Guido van Rossum <guido@python.org>
parents: 5108
diff changeset
1273 static PyObject *
26601
cc05c2c8660e Constify filenames and scripts. Fixes #651362.
Martin v. Löwis <martin@v.loewis.de>
parents: 26263
diff changeset
1274 run_pyc_file(FILE *fp, const char *filename, PyObject *globals, PyObject *locals,
17125
2054cea38754 Extend support for from __future__ import nested_scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16916
diff changeset
1275 PyCompilerFlags *flags)
1886
ac6a9f90f6fd * Import/pythonrun.h, Python/{import,pythonrun}.c,
Guido van Rossum <guido@python.org>
parents: 1870
diff changeset
1276 {
5170
f5d707ce4223 Greatly renamed. Not a very thorough job -- I'm going to restructure
Guido van Rossum <guido@python.org>
parents: 5108
diff changeset
1277 PyCodeObject *co;
f5d707ce4223 Greatly renamed. Not a very thorough job -- I'm going to restructure
Guido van Rossum <guido@python.org>
parents: 5108
diff changeset
1278 PyObject *v;
1886
ac6a9f90f6fd * Import/pythonrun.h, Python/{import,pythonrun}.c,
Guido van Rossum <guido@python.org>
parents: 1870
diff changeset
1279 long magic;
14278
fb8fb53a2eff Add a comment explaining the return value of PyOS_CheckStack().
Fred Drake <fdrake@acm.org>
parents: 14231
diff changeset
1280 long PyImport_GetMagicNumber(void);
1886
ac6a9f90f6fd * Import/pythonrun.h, Python/{import,pythonrun}.c,
Guido van Rossum <guido@python.org>
parents: 1870
diff changeset
1281
5170
f5d707ce4223 Greatly renamed. Not a very thorough job -- I'm going to restructure
Guido van Rossum <guido@python.org>
parents: 5108
diff changeset
1282 magic = PyMarshal_ReadLongFromFile(fp);
f5d707ce4223 Greatly renamed. Not a very thorough job -- I'm going to restructure
Guido van Rossum <guido@python.org>
parents: 5108
diff changeset
1283 if (magic != PyImport_GetMagicNumber()) {
f5d707ce4223 Greatly renamed. Not a very thorough job -- I'm going to restructure
Guido van Rossum <guido@python.org>
parents: 5108
diff changeset
1284 PyErr_SetString(PyExc_RuntimeError,
1886
ac6a9f90f6fd * Import/pythonrun.h, Python/{import,pythonrun}.c,
Guido van Rossum <guido@python.org>
parents: 1870
diff changeset
1285 "Bad magic number in .pyc file");
ac6a9f90f6fd * Import/pythonrun.h, Python/{import,pythonrun}.c,
Guido van Rossum <guido@python.org>
parents: 1870
diff changeset
1286 return NULL;
ac6a9f90f6fd * Import/pythonrun.h, Python/{import,pythonrun}.c,
Guido van Rossum <guido@python.org>
parents: 1870
diff changeset
1287 }
5170
f5d707ce4223 Greatly renamed. Not a very thorough job -- I'm going to restructure
Guido van Rossum <guido@python.org>
parents: 5108
diff changeset
1288 (void) PyMarshal_ReadLongFromFile(fp);
16311
8c41f3861aad It's unclear whether PyMarshal_XXX() are part of the public or private API.
Tim Peters <tim.peters@gmail.com>
parents: 16073
diff changeset
1289 v = PyMarshal_ReadLastObjectFromFile(fp);
1886
ac6a9f90f6fd * Import/pythonrun.h, Python/{import,pythonrun}.c,
Guido van Rossum <guido@python.org>
parents: 1870
diff changeset
1290 fclose(fp);
5170
f5d707ce4223 Greatly renamed. Not a very thorough job -- I'm going to restructure
Guido van Rossum <guido@python.org>
parents: 5108
diff changeset
1291 if (v == NULL || !PyCode_Check(v)) {
f5d707ce4223 Greatly renamed. Not a very thorough job -- I'm going to restructure
Guido van Rossum <guido@python.org>
parents: 5108
diff changeset
1292 Py_XDECREF(v);
f5d707ce4223 Greatly renamed. Not a very thorough job -- I'm going to restructure
Guido van Rossum <guido@python.org>
parents: 5108
diff changeset
1293 PyErr_SetString(PyExc_RuntimeError,
1886
ac6a9f90f6fd * Import/pythonrun.h, Python/{import,pythonrun}.c,
Guido van Rossum <guido@python.org>
parents: 1870
diff changeset
1294 "Bad code object in .pyc file");
ac6a9f90f6fd * Import/pythonrun.h, Python/{import,pythonrun}.c,
Guido van Rossum <guido@python.org>
parents: 1870
diff changeset
1295 return NULL;
ac6a9f90f6fd * Import/pythonrun.h, Python/{import,pythonrun}.c,
Guido van Rossum <guido@python.org>
parents: 1870
diff changeset
1296 }
5170
f5d707ce4223 Greatly renamed. Not a very thorough job -- I'm going to restructure
Guido van Rossum <guido@python.org>
parents: 5108
diff changeset
1297 co = (PyCodeObject *)v;
f5d707ce4223 Greatly renamed. Not a very thorough job -- I'm going to restructure
Guido van Rossum <guido@python.org>
parents: 5108
diff changeset
1298 v = PyEval_EvalCode(co, globals, locals);
19158
97a3d3d7029f Refactor future feature handling
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19099
diff changeset
1299 if (v && flags)
97a3d3d7029f Refactor future feature handling
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19099
diff changeset
1300 flags->cf_flags |= (co->co_flags & PyCF_MASK);
5170
f5d707ce4223 Greatly renamed. Not a very thorough job -- I'm going to restructure
Guido van Rossum <guido@python.org>
parents: 5108
diff changeset
1301 Py_DECREF(co);
1886
ac6a9f90f6fd * Import/pythonrun.h, Python/{import,pythonrun}.c,
Guido van Rossum <guido@python.org>
parents: 1870
diff changeset
1302 return v;
ac6a9f90f6fd * Import/pythonrun.h, Python/{import,pythonrun}.c,
Guido van Rossum <guido@python.org>
parents: 1870
diff changeset
1303 }
ac6a9f90f6fd * Import/pythonrun.h, Python/{import,pythonrun}.c,
Guido van Rossum <guido@python.org>
parents: 1870
diff changeset
1304
5170
f5d707ce4223 Greatly renamed. Not a very thorough job -- I'm going to restructure
Guido van Rossum <guido@python.org>
parents: 5108
diff changeset
1305 PyObject *
26601
cc05c2c8660e Constify filenames and scripts. Fixes #651362.
Martin v. Löwis <martin@v.loewis.de>
parents: 26263
diff changeset
1306 Py_CompileString(const char *str, const char *filename, int start)
1263
65a1e4e9cd54 * Fixed some subtleties with fastlocals. You can no longer access
Guido van Rossum <guido@python.org>
parents: 1262
diff changeset
1307 {
17125
2054cea38754 Extend support for from __future__ import nested_scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16916
diff changeset
1308 return Py_CompileStringFlags(str, filename, start, NULL);
2054cea38754 Extend support for from __future__ import nested_scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16916
diff changeset
1309 }
2054cea38754 Extend support for from __future__ import nested_scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16916
diff changeset
1310
2054cea38754 Extend support for from __future__ import nested_scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16916
diff changeset
1311 PyObject *
28648
0e8416be208e Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 28647
diff changeset
1312 Py_CompileStringFlags(const char *str, const char *filename, int start,
17125
2054cea38754 Extend support for from __future__ import nested_scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16916
diff changeset
1313 PyCompilerFlags *flags)
2054cea38754 Extend support for from __future__ import nested_scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16916
diff changeset
1314 {
1263
65a1e4e9cd54 * Fixed some subtleties with fastlocals. You can no longer access
Guido van Rossum <guido@python.org>
parents: 1262
diff changeset
1315 node *n;
5170
f5d707ce4223 Greatly renamed. Not a very thorough job -- I'm going to restructure
Guido van Rossum <guido@python.org>
parents: 5108
diff changeset
1316 PyCodeObject *co;
24159
cb728141ae12 Fix SF Bug 564931: compile() traceback must include filename.
Thomas Heller <theller@ctypes.org>
parents: 24155
diff changeset
1317
cb728141ae12 Fix SF Bug 564931: compile() traceback must include filename.
Thomas Heller <theller@ctypes.org>
parents: 24155
diff changeset
1318 n = PyParser_SimpleParseStringFlagsFilename(str, filename, start,
cb728141ae12 Fix SF Bug 564931: compile() traceback must include filename.
Thomas Heller <theller@ctypes.org>
parents: 24155
diff changeset
1319 PARSER_FLAGS(flags));
1839
1f4a86dbcd79 * Python/pythonrun.c (print_error): added INCREF/DECREF pair --
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
1320 if (n == NULL)
1263
65a1e4e9cd54 * Fixed some subtleties with fastlocals. You can no longer access
Guido van Rossum <guido@python.org>
parents: 1262
diff changeset
1321 return NULL;
17228
32fb6643adf1 Bug fix: compile() called from a nested-scopes-enable Python was not
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17212
diff changeset
1322 co = PyNode_CompileFlags(n, filename, flags);
5170
f5d707ce4223 Greatly renamed. Not a very thorough job -- I'm going to restructure
Guido van Rossum <guido@python.org>
parents: 5108
diff changeset
1323 PyNode_Free(n);
f5d707ce4223 Greatly renamed. Not a very thorough job -- I'm going to restructure
Guido van Rossum <guido@python.org>
parents: 5108
diff changeset
1324 return (PyObject *)co;
1263
65a1e4e9cd54 * Fixed some subtleties with fastlocals. You can no longer access
Guido van Rossum <guido@python.org>
parents: 1262
diff changeset
1325 }
65a1e4e9cd54 * Fixed some subtleties with fastlocals. You can no longer access
Guido van Rossum <guido@python.org>
parents: 1262
diff changeset
1326
16421
aa844711463a Move a bunch of definitions that were internal to compile.c to
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16311
diff changeset
1327 struct symtable *
26601
cc05c2c8660e Constify filenames and scripts. Fixes #651362.
Martin v. Löwis <martin@v.loewis.de>
parents: 26263
diff changeset
1328 Py_SymtableString(const char *str, const char *filename, int start)
16421
aa844711463a Move a bunch of definitions that were internal to compile.c to
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16311
diff changeset
1329 {
aa844711463a Move a bunch of definitions that were internal to compile.c to
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16311
diff changeset
1330 node *n;
aa844711463a Move a bunch of definitions that were internal to compile.c to
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16311
diff changeset
1331 struct symtable *st;
24159
cb728141ae12 Fix SF Bug 564931: compile() traceback must include filename.
Thomas Heller <theller@ctypes.org>
parents: 24155
diff changeset
1332 n = PyParser_SimpleParseStringFlagsFilename(str, filename,
cb728141ae12 Fix SF Bug 564931: compile() traceback must include filename.
Thomas Heller <theller@ctypes.org>
parents: 24155
diff changeset
1333 start, 0);
16421
aa844711463a Move a bunch of definitions that were internal to compile.c to
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16311
diff changeset
1334 if (n == NULL)
aa844711463a Move a bunch of definitions that were internal to compile.c to
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16311
diff changeset
1335 return NULL;
aa844711463a Move a bunch of definitions that were internal to compile.c to
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16311
diff changeset
1336 st = PyNode_CompileSymtable(n, filename);
aa844711463a Move a bunch of definitions that were internal to compile.c to
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16311
diff changeset
1337 PyNode_Free(n);
aa844711463a Move a bunch of definitions that were internal to compile.c to
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16311
diff changeset
1338 return st;
aa844711463a Move a bunch of definitions that were internal to compile.c to
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16311
diff changeset
1339 }
aa844711463a Move a bunch of definitions that were internal to compile.c to
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16311
diff changeset
1340
1839
1f4a86dbcd79 * Python/pythonrun.c (print_error): added INCREF/DECREF pair --
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
1341 /* Simplified interface to parsefile -- return node or set exception */
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1342
1839
1f4a86dbcd79 * Python/pythonrun.c (print_error): added INCREF/DECREF pair --
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
1343 node *
26601
cc05c2c8660e Constify filenames and scripts. Fixes #651362.
Martin v. Löwis <martin@v.loewis.de>
parents: 26263
diff changeset
1344 PyParser_SimpleParseFileFlags(FILE *fp, const char *filename, int start, int flags)
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1345 {
1839
1f4a86dbcd79 * Python/pythonrun.c (print_error): added INCREF/DECREF pair --
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
1346 node *n;
1f4a86dbcd79 * Python/pythonrun.c (print_error): added INCREF/DECREF pair --
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
1347 perrdetail err;
18615
0a90e5ec61b6 Ugly. A pile of new xxxFlags() functions, to communicate to the parser
Tim Peters <tim.peters@gmail.com>
parents: 18612
diff changeset
1348 n = PyParser_ParseFileFlags(fp, filename, &_PyParser_Grammar, start,
0a90e5ec61b6 Ugly. A pile of new xxxFlags() functions, to communicate to the parser
Tim Peters <tim.peters@gmail.com>
parents: 18612
diff changeset
1349 (char *)0, (char *)0, &err, flags);
1839
1f4a86dbcd79 * Python/pythonrun.c (print_error): added INCREF/DECREF pair --
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
1350 if (n == NULL)
1f4a86dbcd79 * Python/pythonrun.c (print_error): added INCREF/DECREF pair --
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
1351 err_input(&err);
1f4a86dbcd79 * Python/pythonrun.c (print_error): added INCREF/DECREF pair --
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
1352 return n;
1f4a86dbcd79 * Python/pythonrun.c (print_error): added INCREF/DECREF pair --
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
1353 }
1f4a86dbcd79 * Python/pythonrun.c (print_error): added INCREF/DECREF pair --
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
1354
18615
0a90e5ec61b6 Ugly. A pile of new xxxFlags() functions, to communicate to the parser
Tim Peters <tim.peters@gmail.com>
parents: 18612
diff changeset
1355 node *
26601
cc05c2c8660e Constify filenames and scripts. Fixes #651362.
Martin v. Löwis <martin@v.loewis.de>
parents: 26263
diff changeset
1356 PyParser_SimpleParseFile(FILE *fp, const char *filename, int start)
18615
0a90e5ec61b6 Ugly. A pile of new xxxFlags() functions, to communicate to the parser
Tim Peters <tim.peters@gmail.com>
parents: 18612
diff changeset
1357 {
0a90e5ec61b6 Ugly. A pile of new xxxFlags() functions, to communicate to the parser
Tim Peters <tim.peters@gmail.com>
parents: 18612
diff changeset
1358 return PyParser_SimpleParseFileFlags(fp, filename, start, 0);
0a90e5ec61b6 Ugly. A pile of new xxxFlags() functions, to communicate to the parser
Tim Peters <tim.peters@gmail.com>
parents: 18612
diff changeset
1359 }
0a90e5ec61b6 Ugly. A pile of new xxxFlags() functions, to communicate to the parser
Tim Peters <tim.peters@gmail.com>
parents: 18612
diff changeset
1360
1839
1f4a86dbcd79 * Python/pythonrun.c (print_error): added INCREF/DECREF pair --
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
1361 /* Simplified interface to parsestring -- return node or set exception */
1f4a86dbcd79 * Python/pythonrun.c (print_error): added INCREF/DECREF pair --
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
1362
1f4a86dbcd79 * Python/pythonrun.c (print_error): added INCREF/DECREF pair --
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
1363 node *
26601
cc05c2c8660e Constify filenames and scripts. Fixes #651362.
Martin v. Löwis <martin@v.loewis.de>
parents: 26263
diff changeset
1364 PyParser_SimpleParseStringFlags(const char *str, int start, int flags)
18615
0a90e5ec61b6 Ugly. A pile of new xxxFlags() functions, to communicate to the parser
Tim Peters <tim.peters@gmail.com>
parents: 18612
diff changeset
1365 {
0a90e5ec61b6 Ugly. A pile of new xxxFlags() functions, to communicate to the parser
Tim Peters <tim.peters@gmail.com>
parents: 18612
diff changeset
1366 node *n;
0a90e5ec61b6 Ugly. A pile of new xxxFlags() functions, to communicate to the parser
Tim Peters <tim.peters@gmail.com>
parents: 18612
diff changeset
1367 perrdetail err;
0a90e5ec61b6 Ugly. A pile of new xxxFlags() functions, to communicate to the parser
Tim Peters <tim.peters@gmail.com>
parents: 18612
diff changeset
1368 n = PyParser_ParseStringFlags(str, &_PyParser_Grammar, start, &err,
0a90e5ec61b6 Ugly. A pile of new xxxFlags() functions, to communicate to the parser
Tim Peters <tim.peters@gmail.com>
parents: 18612
diff changeset
1369 flags);
0a90e5ec61b6 Ugly. A pile of new xxxFlags() functions, to communicate to the parser
Tim Peters <tim.peters@gmail.com>
parents: 18612
diff changeset
1370 if (n == NULL)
0a90e5ec61b6 Ugly. A pile of new xxxFlags() functions, to communicate to the parser
Tim Peters <tim.peters@gmail.com>
parents: 18612
diff changeset
1371 err_input(&err);
0a90e5ec61b6 Ugly. A pile of new xxxFlags() functions, to communicate to the parser
Tim Peters <tim.peters@gmail.com>
parents: 18612
diff changeset
1372 return n;
0a90e5ec61b6 Ugly. A pile of new xxxFlags() functions, to communicate to the parser
Tim Peters <tim.peters@gmail.com>
parents: 18612
diff changeset
1373 }
0a90e5ec61b6 Ugly. A pile of new xxxFlags() functions, to communicate to the parser
Tim Peters <tim.peters@gmail.com>
parents: 18612
diff changeset
1374
0a90e5ec61b6 Ugly. A pile of new xxxFlags() functions, to communicate to the parser
Tim Peters <tim.peters@gmail.com>
parents: 18612
diff changeset
1375 node *
26601
cc05c2c8660e Constify filenames and scripts. Fixes #651362.
Martin v. Löwis <martin@v.loewis.de>
parents: 26263
diff changeset
1376 PyParser_SimpleParseString(const char *str, int start)
1839
1f4a86dbcd79 * Python/pythonrun.c (print_error): added INCREF/DECREF pair --
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
1377 {
18615
0a90e5ec61b6 Ugly. A pile of new xxxFlags() functions, to communicate to the parser
Tim Peters <tim.peters@gmail.com>
parents: 18612
diff changeset
1378 return PyParser_SimpleParseStringFlags(str, start, 0);
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1379 }
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1380
24159
cb728141ae12 Fix SF Bug 564931: compile() traceback must include filename.
Thomas Heller <theller@ctypes.org>
parents: 24155
diff changeset
1381 node *
26601
cc05c2c8660e Constify filenames and scripts. Fixes #651362.
Martin v. Löwis <martin@v.loewis.de>
parents: 26263
diff changeset
1382 PyParser_SimpleParseStringFlagsFilename(const char *str, const char *filename,
24159
cb728141ae12 Fix SF Bug 564931: compile() traceback must include filename.
Thomas Heller <theller@ctypes.org>
parents: 24155
diff changeset
1383 int start, int flags)
cb728141ae12 Fix SF Bug 564931: compile() traceback must include filename.
Thomas Heller <theller@ctypes.org>
parents: 24155
diff changeset
1384 {
cb728141ae12 Fix SF Bug 564931: compile() traceback must include filename.
Thomas Heller <theller@ctypes.org>
parents: 24155
diff changeset
1385 node *n;
cb728141ae12 Fix SF Bug 564931: compile() traceback must include filename.
Thomas Heller <theller@ctypes.org>
parents: 24155
diff changeset
1386 perrdetail err;
cb728141ae12 Fix SF Bug 564931: compile() traceback must include filename.
Thomas Heller <theller@ctypes.org>
parents: 24155
diff changeset
1387
28648
0e8416be208e Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 28647
diff changeset
1388 n = PyParser_ParseStringFlagsFilename(str, filename,
24159
cb728141ae12 Fix SF Bug 564931: compile() traceback must include filename.
Thomas Heller <theller@ctypes.org>
parents: 24155
diff changeset
1389 &_PyParser_Grammar,
cb728141ae12 Fix SF Bug 564931: compile() traceback must include filename.
Thomas Heller <theller@ctypes.org>
parents: 24155
diff changeset
1390 start, &err, flags);
cb728141ae12 Fix SF Bug 564931: compile() traceback must include filename.
Thomas Heller <theller@ctypes.org>
parents: 24155
diff changeset
1391 if (n == NULL)
cb728141ae12 Fix SF Bug 564931: compile() traceback must include filename.
Thomas Heller <theller@ctypes.org>
parents: 24155
diff changeset
1392 err_input(&err);
cb728141ae12 Fix SF Bug 564931: compile() traceback must include filename.
Thomas Heller <theller@ctypes.org>
parents: 24155
diff changeset
1393 return n;
cb728141ae12 Fix SF Bug 564931: compile() traceback must include filename.
Thomas Heller <theller@ctypes.org>
parents: 24155
diff changeset
1394 }
cb728141ae12 Fix SF Bug 564931: compile() traceback must include filename.
Thomas Heller <theller@ctypes.org>
parents: 24155
diff changeset
1395
cb728141ae12 Fix SF Bug 564931: compile() traceback must include filename.
Thomas Heller <theller@ctypes.org>
parents: 24155
diff changeset
1396 node *
26601
cc05c2c8660e Constify filenames and scripts. Fixes #651362.
Martin v. Löwis <martin@v.loewis.de>
parents: 26263
diff changeset
1397 PyParser_SimpleParseStringFilename(const char *str, const char *filename, int start)
24159
cb728141ae12 Fix SF Bug 564931: compile() traceback must include filename.
Thomas Heller <theller@ctypes.org>
parents: 24155
diff changeset
1398 {
cb728141ae12 Fix SF Bug 564931: compile() traceback must include filename.
Thomas Heller <theller@ctypes.org>
parents: 24155
diff changeset
1399 return PyParser_SimpleParseStringFlagsFilename(str, filename,
cb728141ae12 Fix SF Bug 564931: compile() traceback must include filename.
Thomas Heller <theller@ctypes.org>
parents: 24155
diff changeset
1400 start, 0);
cb728141ae12 Fix SF Bug 564931: compile() traceback must include filename.
Thomas Heller <theller@ctypes.org>
parents: 24155
diff changeset
1401 }
cb728141ae12 Fix SF Bug 564931: compile() traceback must include filename.
Thomas Heller <theller@ctypes.org>
parents: 24155
diff changeset
1402
28649
04b172843292 A missing piece of the PEP 269 patch: add PyParser_SetError(), a
Guido van Rossum <guido@python.org>
parents: 28648
diff changeset
1403 /* May want to move a more generalized form of this to parsetok.c or
04b172843292 A missing piece of the PEP 269 patch: add PyParser_SetError(), a
Guido van Rossum <guido@python.org>
parents: 28648
diff changeset
1404 even parser modules. */
04b172843292 A missing piece of the PEP 269 patch: add PyParser_SetError(), a
Guido van Rossum <guido@python.org>
parents: 28648
diff changeset
1405
04b172843292 A missing piece of the PEP 269 patch: add PyParser_SetError(), a
Guido van Rossum <guido@python.org>
parents: 28648
diff changeset
1406 void
04b172843292 A missing piece of the PEP 269 patch: add PyParser_SetError(), a
Guido van Rossum <guido@python.org>
parents: 28648
diff changeset
1407 PyParser_SetError(perrdetail *err)
04b172843292 A missing piece of the PEP 269 patch: add PyParser_SetError(), a
Guido van Rossum <guido@python.org>
parents: 28648
diff changeset
1408 {
04b172843292 A missing piece of the PEP 269 patch: add PyParser_SetError(), a
Guido van Rossum <guido@python.org>
parents: 28648
diff changeset
1409 err_input(err);
04b172843292 A missing piece of the PEP 269 patch: add PyParser_SetError(), a
Guido van Rossum <guido@python.org>
parents: 28648
diff changeset
1410 }
04b172843292 A missing piece of the PEP 269 patch: add PyParser_SetError(), a
Guido van Rossum <guido@python.org>
parents: 28648
diff changeset
1411
1839
1f4a86dbcd79 * Python/pythonrun.c (print_error): added INCREF/DECREF pair --
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
1412 /* Set the error appropriate to the given input error code (see errcode.h) */
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1413
1839
1f4a86dbcd79 * Python/pythonrun.c (print_error): added INCREF/DECREF pair --
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
1414 static void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13620
diff changeset
1415 err_input(perrdetail *err)
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1416 {
13539
feab677269e0 Create two new exceptions: IndentationError and TabError. These are
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
1417 PyObject *v, *w, *errtype;
24539
06f1e2fd05bc Patch #534304: Implement phase 1 of PEP 263.
Martin v. Löwis <martin@v.loewis.de>
parents: 24162
diff changeset
1418 PyObject* u = NULL;
1839
1f4a86dbcd79 * Python/pythonrun.c (print_error): added INCREF/DECREF pair --
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
1419 char *msg = NULL;
13539
feab677269e0 Create two new exceptions: IndentationError and TabError. These are
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
1420 errtype = PyExc_SyntaxError;
5170
f5d707ce4223 Greatly renamed. Not a very thorough job -- I'm going to restructure
Guido van Rossum <guido@python.org>
parents: 5108
diff changeset
1421 v = Py_BuildValue("(ziiz)", err->filename,
1839
1f4a86dbcd79 * Python/pythonrun.c (print_error): added INCREF/DECREF pair --
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
1422 err->lineno, err->offset, err->text);
1f4a86dbcd79 * Python/pythonrun.c (print_error): added INCREF/DECREF pair --
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
1423 if (err->text != NULL) {
12576
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 12555
diff changeset
1424 PyMem_DEL(err->text);
1839
1f4a86dbcd79 * Python/pythonrun.c (print_error): added INCREF/DECREF pair --
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
1425 err->text = NULL;
1f4a86dbcd79 * Python/pythonrun.c (print_error): added INCREF/DECREF pair --
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
1426 }
1f4a86dbcd79 * Python/pythonrun.c (print_error): added INCREF/DECREF pair --
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
1427 switch (err->error) {
1f4a86dbcd79 * Python/pythonrun.c (print_error): added INCREF/DECREF pair --
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
1428 case E_SYNTAX:
13539
feab677269e0 Create two new exceptions: IndentationError and TabError. These are
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
1429 errtype = PyExc_IndentationError;
feab677269e0 Create two new exceptions: IndentationError and TabError. These are
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
1430 if (err->expected == INDENT)
feab677269e0 Create two new exceptions: IndentationError and TabError. These are
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
1431 msg = "expected an indented block";
feab677269e0 Create two new exceptions: IndentationError and TabError. These are
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
1432 else if (err->token == INDENT)
feab677269e0 Create two new exceptions: IndentationError and TabError. These are
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
1433 msg = "unexpected indent";
feab677269e0 Create two new exceptions: IndentationError and TabError. These are
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
1434 else if (err->token == DEDENT)
feab677269e0 Create two new exceptions: IndentationError and TabError. These are
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
1435 msg = "unexpected unindent";
feab677269e0 Create two new exceptions: IndentationError and TabError. These are
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
1436 else {
feab677269e0 Create two new exceptions: IndentationError and TabError. These are
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
1437 errtype = PyExc_SyntaxError;
feab677269e0 Create two new exceptions: IndentationError and TabError. These are
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
1438 msg = "invalid syntax";
feab677269e0 Create two new exceptions: IndentationError and TabError. These are
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
1439 }
1839
1f4a86dbcd79 * Python/pythonrun.c (print_error): added INCREF/DECREF pair --
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
1440 break;
1f4a86dbcd79 * Python/pythonrun.c (print_error): added INCREF/DECREF pair --
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
1441 case E_TOKEN:
1f4a86dbcd79 * Python/pythonrun.c (print_error): added INCREF/DECREF pair --
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
1442 msg = "invalid token";
1f4a86dbcd79 * Python/pythonrun.c (print_error): added INCREF/DECREF pair --
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
1443 break;
24839
17e95cc06f69 provide less mysterious error messages when seeing end-of-line in
Skip Montanaro <skip@pobox.com>
parents: 24539
diff changeset
1444 case E_EOFS:
17e95cc06f69 provide less mysterious error messages when seeing end-of-line in
Skip Montanaro <skip@pobox.com>
parents: 24539
diff changeset
1445 msg = "EOF while scanning triple-quoted string";
17e95cc06f69 provide less mysterious error messages when seeing end-of-line in
Skip Montanaro <skip@pobox.com>
parents: 24539
diff changeset
1446 break;
17e95cc06f69 provide less mysterious error messages when seeing end-of-line in
Skip Montanaro <skip@pobox.com>
parents: 24539
diff changeset
1447 case E_EOLS:
17e95cc06f69 provide less mysterious error messages when seeing end-of-line in
Skip Montanaro <skip@pobox.com>
parents: 24539
diff changeset
1448 msg = "EOL while scanning single-quoted string";
17e95cc06f69 provide less mysterious error messages when seeing end-of-line in
Skip Montanaro <skip@pobox.com>
parents: 24539
diff changeset
1449 break;
1839
1f4a86dbcd79 * Python/pythonrun.c (print_error): added INCREF/DECREF pair --
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
1450 case E_INTR:
32526
78bd25b42d46 This closes patch:
Michael W. Hudson <mwh@python.net>
parents: 31797
diff changeset
1451 if (!PyErr_Occurred())
78bd25b42d46 This closes patch:
Michael W. Hudson <mwh@python.net>
parents: 31797
diff changeset
1452 PyErr_SetNone(PyExc_KeyboardInterrupt);
9965
c1091e82e5aa err_input(): Nailed a small memory leak. If the error is E_INTR, the
Barry Warsaw <barry@python.org>
parents: 9819
diff changeset
1453 Py_XDECREF(v);
1839
1f4a86dbcd79 * Python/pythonrun.c (print_error): added INCREF/DECREF pair --
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
1454 return;
1f4a86dbcd79 * Python/pythonrun.c (print_error): added INCREF/DECREF pair --
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
1455 case E_NOMEM:
5170
f5d707ce4223 Greatly renamed. Not a very thorough job -- I'm going to restructure
Guido van Rossum <guido@python.org>
parents: 5108
diff changeset
1456 PyErr_NoMemory();
9965
c1091e82e5aa err_input(): Nailed a small memory leak. If the error is E_INTR, the
Barry Warsaw <barry@python.org>
parents: 9819
diff changeset
1457 Py_XDECREF(v);
1839
1f4a86dbcd79 * Python/pythonrun.c (print_error): added INCREF/DECREF pair --
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
1458 return;
1f4a86dbcd79 * Python/pythonrun.c (print_error): added INCREF/DECREF pair --
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
1459 case E_EOF:
1f4a86dbcd79 * Python/pythonrun.c (print_error): added INCREF/DECREF pair --
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
1460 msg = "unexpected EOF while parsing";
1f4a86dbcd79 * Python/pythonrun.c (print_error): added INCREF/DECREF pair --
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
1461 break;
13539
feab677269e0 Create two new exceptions: IndentationError and TabError. These are
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
1462 case E_TABSPACE:
feab677269e0 Create two new exceptions: IndentationError and TabError. These are
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
1463 errtype = PyExc_TabError;
7967
bbd2b5d8d0b6 Translate E_INDENT to the clearest error message I can think of.
Guido van Rossum <guido@python.org>
parents: 7847
diff changeset
1464 msg = "inconsistent use of tabs and spaces in indentation";
bbd2b5d8d0b6 Translate E_INDENT to the clearest error message I can think of.
Guido van Rossum <guido@python.org>
parents: 7847
diff changeset
1465 break;
12970
63436719d12e Add new parser error code, E_OVERFLOW. This error is returned when
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12754
diff changeset
1466 case E_OVERFLOW:
63436719d12e Add new parser error code, E_OVERFLOW. This error is returned when
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12754
diff changeset
1467 msg = "expression too long";
63436719d12e Add new parser error code, E_OVERFLOW. This error is returned when
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12754
diff changeset
1468 break;
13539
feab677269e0 Create two new exceptions: IndentationError and TabError. These are
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
1469 case E_DEDENT:
feab677269e0 Create two new exceptions: IndentationError and TabError. These are
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
1470 errtype = PyExc_IndentationError;
feab677269e0 Create two new exceptions: IndentationError and TabError. These are
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
1471 msg = "unindent does not match any outer indentation level";
feab677269e0 Create two new exceptions: IndentationError and TabError. These are
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
1472 break;
feab677269e0 Create two new exceptions: IndentationError and TabError. These are
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
1473 case E_TOODEEP:
feab677269e0 Create two new exceptions: IndentationError and TabError. These are
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
1474 errtype = PyExc_IndentationError;
feab677269e0 Create two new exceptions: IndentationError and TabError. These are
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
1475 msg = "too many levels of indentation";
feab677269e0 Create two new exceptions: IndentationError and TabError. These are
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
1476 break;
24539
06f1e2fd05bc Patch #534304: Implement phase 1 of PEP 263.
Martin v. Löwis <martin@v.loewis.de>
parents: 24162
diff changeset
1477 case E_DECODE: { /* XXX */
31797
846b3ee7e106 Changed random calls to PyThreadState_Get() to use the macro
Nicholas Bastin <nick.bastin@gmail.com>
parents: 31771
diff changeset
1478 PyThreadState* tstate = PyThreadState_GET();
24539
06f1e2fd05bc Patch #534304: Implement phase 1 of PEP 263.
Martin v. Löwis <martin@v.loewis.de>
parents: 24162
diff changeset
1479 PyObject* value = tstate->curexc_value;
06f1e2fd05bc Patch #534304: Implement phase 1 of PEP 263.
Martin v. Löwis <martin@v.loewis.de>
parents: 24162
diff changeset
1480 if (value != NULL) {
06f1e2fd05bc Patch #534304: Implement phase 1 of PEP 263.
Martin v. Löwis <martin@v.loewis.de>
parents: 24162
diff changeset
1481 u = PyObject_Repr(value);
06f1e2fd05bc Patch #534304: Implement phase 1 of PEP 263.
Martin v. Löwis <martin@v.loewis.de>
parents: 24162
diff changeset
1482 if (u != NULL) {
06f1e2fd05bc Patch #534304: Implement phase 1 of PEP 263.
Martin v. Löwis <martin@v.loewis.de>
parents: 24162
diff changeset
1483 msg = PyString_AsString(u);
06f1e2fd05bc Patch #534304: Implement phase 1 of PEP 263.
Martin v. Löwis <martin@v.loewis.de>
parents: 24162
diff changeset
1484 break;
06f1e2fd05bc Patch #534304: Implement phase 1 of PEP 263.
Martin v. Löwis <martin@v.loewis.de>
parents: 24162
diff changeset
1485 }
06f1e2fd05bc Patch #534304: Implement phase 1 of PEP 263.
Martin v. Löwis <martin@v.loewis.de>
parents: 24162
diff changeset
1486 }
32781
caaef302f1e3 Patch #984714: Properly diagnose E_DECODE errors.
Martin v. Löwis <martin@v.loewis.de>
parents: 32526
diff changeset
1487 if (msg == NULL)
caaef302f1e3 Patch #984714: Properly diagnose E_DECODE errors.
Martin v. Löwis <martin@v.loewis.de>
parents: 32526
diff changeset
1488 msg = "unknown decode error";
caaef302f1e3 Patch #984714: Properly diagnose E_DECODE errors.
Martin v. Löwis <martin@v.loewis.de>
parents: 32526
diff changeset
1489 break;
24539
06f1e2fd05bc Patch #534304: Implement phase 1 of PEP 263.
Martin v. Löwis <martin@v.loewis.de>
parents: 24162
diff changeset
1490 }
34664
3785610ff763 Patch #802188: better parser error message for non-EOL following line cont.
Martin v. Löwis <martin@v.loewis.de>
parents: 33747
diff changeset
1491 case E_LINECONT:
3785610ff763 Patch #802188: better parser error message for non-EOL following line cont.
Martin v. Löwis <martin@v.loewis.de>
parents: 33747
diff changeset
1492 msg = "unexpected character after line continuation character";
3785610ff763 Patch #802188: better parser error message for non-EOL following line cont.
Martin v. Löwis <martin@v.loewis.de>
parents: 33747
diff changeset
1493 break;
1839
1f4a86dbcd79 * Python/pythonrun.c (print_error): added INCREF/DECREF pair --
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
1494 default:
1f4a86dbcd79 * Python/pythonrun.c (print_error): added INCREF/DECREF pair --
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
1495 fprintf(stderr, "error=%d\n", err->error);
1f4a86dbcd79 * Python/pythonrun.c (print_error): added INCREF/DECREF pair --
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
1496 msg = "unknown parsing error";
1f4a86dbcd79 * Python/pythonrun.c (print_error): added INCREF/DECREF pair --
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
1497 break;
1f4a86dbcd79 * Python/pythonrun.c (print_error): added INCREF/DECREF pair --
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
1498 }
5170
f5d707ce4223 Greatly renamed. Not a very thorough job -- I'm going to restructure
Guido van Rossum <guido@python.org>
parents: 5108
diff changeset
1499 w = Py_BuildValue("(sO)", msg, v);
24539
06f1e2fd05bc Patch #534304: Implement phase 1 of PEP 263.
Martin v. Löwis <martin@v.loewis.de>
parents: 24162
diff changeset
1500 Py_XDECREF(u);
17168
0d52358cb288 Fix memory leak with SyntaxError. (The DECREF was originally hidden
Guido van Rossum <guido@python.org>
parents: 17164
diff changeset
1501 Py_XDECREF(v);
13539
feab677269e0 Create two new exceptions: IndentationError and TabError. These are
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
1502 PyErr_SetObject(errtype, w);
5170
f5d707ce4223 Greatly renamed. Not a very thorough job -- I'm going to restructure
Guido van Rossum <guido@python.org>
parents: 5108
diff changeset
1503 Py_XDECREF(w);
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1504 }
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1505
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1506 /* Print fatal error message and abort */
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1507
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1508 void
24155
9456a2ccda61 The Py_REF_DEBUG/COUNT_ALLOCS/Py_TRACE_REFS macro minefield: added
Tim Peters <tim.peters@gmail.com>
parents: 24071
diff changeset
1509 Py_FatalError(const char *msg)
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1510 {
1911
a7f269610ec2 * Python/pythonrun.c: add string "Python" to fatal error message
Guido van Rossum <guido@python.org>
parents: 1898
diff changeset
1511 fprintf(stderr, "Fatal Python error: %s\n", msg);
24071
8142f8d668a0 Patch #569753: Remove support for WIN16.
Martin v. Löwis <martin@v.loewis.de>
parents: 23252
diff changeset
1512 #ifdef MS_WINDOWS
5593
451c7ccf36b6 Added a space in an error message
Guido van Rossum <guido@python.org>
parents: 5438
diff changeset
1513 OutputDebugString("Fatal Python error: ");
2529
16ea57c38c4f In NT, write fatal() msg to debugging device
Guido van Rossum <guido@python.org>
parents: 2318
diff changeset
1514 OutputDebugString(msg);
16ea57c38c4f In NT, write fatal() msg to debugging device
Guido van Rossum <guido@python.org>
parents: 2318
diff changeset
1515 OutputDebugString("\n");
8988
070de4d39ddd Add DebugBreak() call to Py_FatalError() for Mark Hammond (only on
Guido van Rossum <guido@python.org>
parents: 7967
diff changeset
1516 #ifdef _DEBUG
070de4d39ddd Add DebugBreak() call to Py_FatalError() for Mark Hammond (only on
Guido van Rossum <guido@python.org>
parents: 7967
diff changeset
1517 DebugBreak();
2529
16ea57c38c4f In NT, write fatal() msg to debugging device
Guido van Rossum <guido@python.org>
parents: 2318
diff changeset
1518 #endif
24071
8142f8d668a0 Patch #569753: Remove support for WIN16.
Martin v. Löwis <martin@v.loewis.de>
parents: 23252
diff changeset
1519 #endif /* MS_WINDOWS */
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1520 abort();
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1521 }
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1522
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1523 /* Clean up and exit */
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1524
1839
1f4a86dbcd79 * Python/pythonrun.c (print_error): added INCREF/DECREF pair --
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
1525 #ifdef WITH_THREAD
9203
07dae6fdae79 Renamed thread.h to pythread.h.
Guido van Rossum <guido@python.org>
parents: 9192
diff changeset
1526 #include "pythread.h"
961
a31e07847063 New thread.c from Sjoerd, supports _exit_prog(). Use this in goaway()
Guido van Rossum <guido@python.org>
parents: 941
diff changeset
1527 #endif
a31e07847063 New thread.c from Sjoerd, supports _exit_prog(). Use this in goaway()
Guido van Rossum <guido@python.org>
parents: 941
diff changeset
1528
9192
ac6c29d1f10c On second though, NEXITFUNCS should be defined here and not in
Guido van Rossum <guido@python.org>
parents: 9161
diff changeset
1529 #define NEXITFUNCS 32
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13620
diff changeset
1530 static void (*exitfuncs[NEXITFUNCS])(void);
1870
61738ed6cc10 added Py_AtExit() -- register cleanup functions for C modules
Guido van Rossum <guido@python.org>
parents: 1839
diff changeset
1531 static int nexitfuncs = 0;
61738ed6cc10 added Py_AtExit() -- register cleanup functions for C modules
Guido van Rossum <guido@python.org>
parents: 1839
diff changeset
1532
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13620
diff changeset
1533 int Py_AtExit(void (*func)(void))
1870
61738ed6cc10 added Py_AtExit() -- register cleanup functions for C modules
Guido van Rossum <guido@python.org>
parents: 1839
diff changeset
1534 {
61738ed6cc10 added Py_AtExit() -- register cleanup functions for C modules
Guido van Rossum <guido@python.org>
parents: 1839
diff changeset
1535 if (nexitfuncs >= NEXITFUNCS)
61738ed6cc10 added Py_AtExit() -- register cleanup functions for C modules
Guido van Rossum <guido@python.org>
parents: 1839
diff changeset
1536 return -1;
61738ed6cc10 added Py_AtExit() -- register cleanup functions for C modules
Guido van Rossum <guido@python.org>
parents: 1839
diff changeset
1537 exitfuncs[nexitfuncs++] = func;
61738ed6cc10 added Py_AtExit() -- register cleanup functions for C modules
Guido van Rossum <guido@python.org>
parents: 1839
diff changeset
1538 return 0;
61738ed6cc10 added Py_AtExit() -- register cleanup functions for C modules
Guido van Rossum <guido@python.org>
parents: 1839
diff changeset
1539 }
61738ed6cc10 added Py_AtExit() -- register cleanup functions for C modules
Guido van Rossum <guido@python.org>
parents: 1839
diff changeset
1540
5939
2c65bb6ab87c Merge Py_Cleanup() into Py_Finalize(). Call the various small Fini()
Guido van Rossum <guido@python.org>
parents: 5926
diff changeset
1541 static void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13620
diff changeset
1542 call_sys_exitfunc(void)
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1543 {
5170
f5d707ce4223 Greatly renamed. Not a very thorough job -- I'm going to restructure
Guido van Rossum <guido@python.org>
parents: 5108
diff changeset
1544 PyObject *exitfunc = PySys_GetObject("exitfunc");
1015
f3d5e2c3b9a3 Add sys.exitfunc feature. Make askync non-static.
Guido van Rossum <guido@python.org>
parents: 961
diff changeset
1545
f3d5e2c3b9a3 Add sys.exitfunc feature. Make askync non-static.
Guido van Rossum <guido@python.org>
parents: 961
diff changeset
1546 if (exitfunc) {
17207
16f145aac2ec call_sys_exitfunc(): Remove unused variable f.
Fred Drake <fdrake@acm.org>
parents: 17191
diff changeset
1547 PyObject *res;
5170
f5d707ce4223 Greatly renamed. Not a very thorough job -- I'm going to restructure
Guido van Rossum <guido@python.org>
parents: 5108
diff changeset
1548 Py_INCREF(exitfunc);
f5d707ce4223 Greatly renamed. Not a very thorough job -- I'm going to restructure
Guido van Rossum <guido@python.org>
parents: 5108
diff changeset
1549 PySys_SetObject("exitfunc", (PyObject *)NULL);
f5d707ce4223 Greatly renamed. Not a very thorough job -- I'm going to restructure
Guido van Rossum <guido@python.org>
parents: 5108
diff changeset
1550 res = PyEval_CallObject(exitfunc, (PyObject *)NULL);
1015
f3d5e2c3b9a3 Add sys.exitfunc feature. Make askync non-static.
Guido van Rossum <guido@python.org>
parents: 961
diff changeset
1551 if (res == NULL) {
17191
eb06a02e0507 Allow sys.excepthook and sys.exitfunc to quietly exit with a sys.exit().
Ka-Ping Yee <ping@zesty.ca>
parents: 17168
diff changeset
1552 if (!PyErr_ExceptionMatches(PyExc_SystemExit)) {
eb06a02e0507 Allow sys.excepthook and sys.exitfunc to quietly exit with a sys.exit().
Ka-Ping Yee <ping@zesty.ca>
parents: 17168
diff changeset
1553 PySys_WriteStderr("Error in sys.exitfunc:\n");
eb06a02e0507 Allow sys.excepthook and sys.exitfunc to quietly exit with a sys.exit().
Ka-Ping Yee <ping@zesty.ca>
parents: 17168
diff changeset
1554 }
5170
f5d707ce4223 Greatly renamed. Not a very thorough job -- I'm going to restructure
Guido van Rossum <guido@python.org>
parents: 5108
diff changeset
1555 PyErr_Print();
1015
f3d5e2c3b9a3 Add sys.exitfunc feature. Make askync non-static.
Guido van Rossum <guido@python.org>
parents: 961
diff changeset
1556 }
5170
f5d707ce4223 Greatly renamed. Not a very thorough job -- I'm going to restructure
Guido van Rossum <guido@python.org>
parents: 5108
diff changeset
1557 Py_DECREF(exitfunc);
1015
f3d5e2c3b9a3 Add sys.exitfunc feature. Make askync non-static.
Guido van Rossum <guido@python.org>
parents: 961
diff changeset
1558 }
f3d5e2c3b9a3 Add sys.exitfunc feature. Make askync non-static.
Guido van Rossum <guido@python.org>
parents: 961
diff changeset
1559
7509
b5d52c415b15 Fix the handling of errors in Py_FlushLine() in a few places.
Guido van Rossum <guido@python.org>
parents: 7206
diff changeset
1560 if (Py_FlushLine())
b5d52c415b15 Fix the handling of errors in Py_FlushLine() in a few places.
Guido van Rossum <guido@python.org>
parents: 7206
diff changeset
1561 PyErr_Clear();
5939
2c65bb6ab87c Merge Py_Cleanup() into Py_Finalize(). Call the various small Fini()
Guido van Rossum <guido@python.org>
parents: 5926
diff changeset
1562 }
1870
61738ed6cc10 added Py_AtExit() -- register cleanup functions for C modules
Guido van Rossum <guido@python.org>
parents: 1839
diff changeset
1563
5939
2c65bb6ab87c Merge Py_Cleanup() into Py_Finalize(). Call the various small Fini()
Guido van Rossum <guido@python.org>
parents: 5926
diff changeset
1564 static void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13620
diff changeset
1565 call_ll_exitfuncs(void)
5939
2c65bb6ab87c Merge Py_Cleanup() into Py_Finalize(). Call the various small Fini()
Guido van Rossum <guido@python.org>
parents: 5926
diff changeset
1566 {
1870
61738ed6cc10 added Py_AtExit() -- register cleanup functions for C modules
Guido van Rossum <guido@python.org>
parents: 1839
diff changeset
1567 while (nexitfuncs > 0)
61738ed6cc10 added Py_AtExit() -- register cleanup functions for C modules
Guido van Rossum <guido@python.org>
parents: 1839
diff changeset
1568 (*exitfuncs[--nexitfuncs])();
5926
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
1569
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
1570 fflush(stdout);
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
1571 fflush(stderr);
1079
e4c6a0dd5c2b * bltinmodule.c: added built-in function cmp(a, b)
Guido van Rossum <guido@python.org>
parents: 1073
diff changeset
1572 }
e4c6a0dd5c2b * bltinmodule.c: added built-in function cmp(a, b)
Guido van Rossum <guido@python.org>
parents: 1073
diff changeset
1573
e4c6a0dd5c2b * bltinmodule.c: added built-in function cmp(a, b)
Guido van Rossum <guido@python.org>
parents: 1073
diff changeset
1574 void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13620
diff changeset
1575 Py_Exit(int sts)
1079
e4c6a0dd5c2b * bltinmodule.c: added built-in function cmp(a, b)
Guido van Rossum <guido@python.org>
parents: 1073
diff changeset
1576 {
5939
2c65bb6ab87c Merge Py_Cleanup() into Py_Finalize(). Call the various small Fini()
Guido van Rossum <guido@python.org>
parents: 5926
diff changeset
1577 Py_Finalize();
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1578
3235
3df9ab03badf Moved mac-specific exit handling to macmain.c
Jack Jansen <jack.jansen@cwi.nl>
parents: 3152
diff changeset
1579 exit(sts);
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1580 }
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1581
1319
cd397266dc1c * Makefile: added all: and default: targets.
Guido van Rossum <guido@python.org>
parents: 1289
diff changeset
1582 static void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13620
diff changeset
1583 initsigs(void)
1079
e4c6a0dd5c2b * bltinmodule.c: added built-in function cmp(a, b)
Guido van Rossum <guido@python.org>
parents: 1073
diff changeset
1584 {
1839
1f4a86dbcd79 * Python/pythonrun.c (print_error): added INCREF/DECREF pair --
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
1585 #ifdef SIGPIPE
33747
b0ec4c9d595b Patch #975056 - fixes for restartable signals on *BSD. In addition,
Anthony Baxter <anthonybaxter@gmail.com>
parents: 33715
diff changeset
1586 PyOS_setsig(SIGPIPE, SIG_IGN);
1839
1f4a86dbcd79 * Python/pythonrun.c (print_error): added INCREF/DECREF pair --
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
1587 #endif
19295
157bbb896198 Bunchathings:
Guido van Rossum <guido@python.org>
parents: 19237
diff changeset
1588 #ifdef SIGXFZ
33747
b0ec4c9d595b Patch #975056 - fixes for restartable signals on *BSD. In addition,
Anthony Baxter <anthonybaxter@gmail.com>
parents: 33715
diff changeset
1589 PyOS_setsig(SIGXFZ, SIG_IGN);
19295
157bbb896198 Bunchathings:
Guido van Rossum <guido@python.org>
parents: 19237
diff changeset
1590 #endif
23252
4f335ca3f11c Ignore SIGXFSZ.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23020
diff changeset
1591 #ifdef SIGXFSZ
33747
b0ec4c9d595b Patch #975056 - fixes for restartable signals on *BSD. In addition,
Anthony Baxter <anthonybaxter@gmail.com>
parents: 33715
diff changeset
1592 PyOS_setsig(SIGXFSZ, SIG_IGN);
23252
4f335ca3f11c Ignore SIGXFSZ.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23020
diff changeset
1593 #endif
5170
f5d707ce4223 Greatly renamed. Not a very thorough job -- I'm going to restructure
Guido van Rossum <guido@python.org>
parents: 5108
diff changeset
1594 PyOS_InitInterrupts(); /* May imply initsignal() */
1079
e4c6a0dd5c2b * bltinmodule.c: added built-in function cmp(a, b)
Guido van Rossum <guido@python.org>
parents: 1073
diff changeset
1595 }
e4c6a0dd5c2b * bltinmodule.c: added built-in function cmp(a, b)
Guido van Rossum <guido@python.org>
parents: 1073
diff changeset
1596
5108
a9a6d416b945 Added new global flag variable Py_InteractiveFlag and new function
Guido van Rossum <guido@python.org>
parents: 4596
diff changeset
1597
a9a6d416b945 Added new global flag variable Py_InteractiveFlag and new function
Guido van Rossum <guido@python.org>
parents: 4596
diff changeset
1598 /*
a9a6d416b945 Added new global flag variable Py_InteractiveFlag and new function
Guido van Rossum <guido@python.org>
parents: 4596
diff changeset
1599 * The file descriptor fd is considered ``interactive'' if either
a9a6d416b945 Added new global flag variable Py_InteractiveFlag and new function
Guido van Rossum <guido@python.org>
parents: 4596
diff changeset
1600 * a) isatty(fd) is TRUE, or
a9a6d416b945 Added new global flag variable Py_InteractiveFlag and new function
Guido van Rossum <guido@python.org>
parents: 4596
diff changeset
1601 * b) the -i flag was given, and the filename associated with
a9a6d416b945 Added new global flag variable Py_InteractiveFlag and new function
Guido van Rossum <guido@python.org>
parents: 4596
diff changeset
1602 * the descriptor is NULL or "<stdin>" or "???".
a9a6d416b945 Added new global flag variable Py_InteractiveFlag and new function
Guido van Rossum <guido@python.org>
parents: 4596
diff changeset
1603 */
a9a6d416b945 Added new global flag variable Py_InteractiveFlag and new function
Guido van Rossum <guido@python.org>
parents: 4596
diff changeset
1604 int
26601
cc05c2c8660e Constify filenames and scripts. Fixes #651362.
Martin v. Löwis <martin@v.loewis.de>
parents: 26263
diff changeset
1605 Py_FdIsInteractive(FILE *fp, const char *filename)
5108
a9a6d416b945 Added new global flag variable Py_InteractiveFlag and new function
Guido van Rossum <guido@python.org>
parents: 4596
diff changeset
1606 {
a9a6d416b945 Added new global flag variable Py_InteractiveFlag and new function
Guido van Rossum <guido@python.org>
parents: 4596
diff changeset
1607 if (isatty((int)fileno(fp)))
a9a6d416b945 Added new global flag variable Py_InteractiveFlag and new function
Guido van Rossum <guido@python.org>
parents: 4596
diff changeset
1608 return 1;
a9a6d416b945 Added new global flag variable Py_InteractiveFlag and new function
Guido van Rossum <guido@python.org>
parents: 4596
diff changeset
1609 if (!Py_InteractiveFlag)
a9a6d416b945 Added new global flag variable Py_InteractiveFlag and new function
Guido van Rossum <guido@python.org>
parents: 4596
diff changeset
1610 return 0;
a9a6d416b945 Added new global flag variable Py_InteractiveFlag and new function
Guido van Rossum <guido@python.org>
parents: 4596
diff changeset
1611 return (filename == NULL) ||
a9a6d416b945 Added new global flag variable Py_InteractiveFlag and new function
Guido van Rossum <guido@python.org>
parents: 4596
diff changeset
1612 (strcmp(filename, "<stdin>") == 0) ||
a9a6d416b945 Added new global flag variable Py_InteractiveFlag and new function
Guido van Rossum <guido@python.org>
parents: 4596
diff changeset
1613 (strcmp(filename, "???") == 0);
a9a6d416b945 Added new global flag variable Py_InteractiveFlag and new function
Guido van Rossum <guido@python.org>
parents: 4596
diff changeset
1614 }
14227
9f9f3280dc68 implements PyOS_CheckStack for Windows and MSVC. this fixes a
Fredrik Lundh <fredrik@pythonware.com>
parents: 14195
diff changeset
1615
9f9f3280dc68 implements PyOS_CheckStack for Windows and MSVC. this fixes a
Fredrik Lundh <fredrik@pythonware.com>
parents: 14195
diff changeset
1616
28648
0e8416be208e Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 28647
diff changeset
1617 #if defined(USE_STACKCHECK)
14227
9f9f3280dc68 implements PyOS_CheckStack for Windows and MSVC. this fixes a
Fredrik Lundh <fredrik@pythonware.com>
parents: 14195
diff changeset
1618 #if defined(WIN32) && defined(_MSC_VER)
9f9f3280dc68 implements PyOS_CheckStack for Windows and MSVC. this fixes a
Fredrik Lundh <fredrik@pythonware.com>
parents: 14195
diff changeset
1619
9f9f3280dc68 implements PyOS_CheckStack for Windows and MSVC. this fixes a
Fredrik Lundh <fredrik@pythonware.com>
parents: 14195
diff changeset
1620 /* Stack checking for Microsoft C */
9f9f3280dc68 implements PyOS_CheckStack for Windows and MSVC. this fixes a
Fredrik Lundh <fredrik@pythonware.com>
parents: 14195
diff changeset
1621
9f9f3280dc68 implements PyOS_CheckStack for Windows and MSVC. this fixes a
Fredrik Lundh <fredrik@pythonware.com>
parents: 14195
diff changeset
1622 #include <malloc.h>
9f9f3280dc68 implements PyOS_CheckStack for Windows and MSVC. this fixes a
Fredrik Lundh <fredrik@pythonware.com>
parents: 14195
diff changeset
1623 #include <excpt.h>
9f9f3280dc68 implements PyOS_CheckStack for Windows and MSVC. this fixes a
Fredrik Lundh <fredrik@pythonware.com>
parents: 14195
diff changeset
1624
14278
fb8fb53a2eff Add a comment explaining the return value of PyOS_CheckStack().
Fred Drake <fdrake@acm.org>
parents: 14231
diff changeset
1625 /*
fb8fb53a2eff Add a comment explaining the return value of PyOS_CheckStack().
Fred Drake <fdrake@acm.org>
parents: 14231
diff changeset
1626 * Return non-zero when we run out of memory on the stack; zero otherwise.
fb8fb53a2eff Add a comment explaining the return value of PyOS_CheckStack().
Fred Drake <fdrake@acm.org>
parents: 14231
diff changeset
1627 */
14227
9f9f3280dc68 implements PyOS_CheckStack for Windows and MSVC. this fixes a
Fredrik Lundh <fredrik@pythonware.com>
parents: 14195
diff changeset
1628 int
14280
68b61aa1aa4a PyOS_CheckStack(): Better ANSI'fy this while we're at it.
Fred Drake <fdrake@acm.org>
parents: 14278
diff changeset
1629 PyOS_CheckStack(void)
14227
9f9f3280dc68 implements PyOS_CheckStack for Windows and MSVC. this fixes a
Fredrik Lundh <fredrik@pythonware.com>
parents: 14195
diff changeset
1630 {
9f9f3280dc68 implements PyOS_CheckStack for Windows and MSVC. this fixes a
Fredrik Lundh <fredrik@pythonware.com>
parents: 14195
diff changeset
1631 __try {
25573
0231cc54f87e s/_alloca/alloca/g; Windows doesn't need the former, at least not unless
Tim Peters <tim.peters@gmail.com>
parents: 24839
diff changeset
1632 /* alloca throws a stack overflow exception if there's
14227
9f9f3280dc68 implements PyOS_CheckStack for Windows and MSVC. this fixes a
Fredrik Lundh <fredrik@pythonware.com>
parents: 14195
diff changeset
1633 not enough space left on the stack */
25573
0231cc54f87e s/_alloca/alloca/g; Windows doesn't need the former, at least not unless
Tim Peters <tim.peters@gmail.com>
parents: 24839
diff changeset
1634 alloca(PYOS_STACK_MARGIN * sizeof(void*));
14227
9f9f3280dc68 implements PyOS_CheckStack for Windows and MSVC. this fixes a
Fredrik Lundh <fredrik@pythonware.com>
parents: 14195
diff changeset
1635 return 0;
9f9f3280dc68 implements PyOS_CheckStack for Windows and MSVC. this fixes a
Fredrik Lundh <fredrik@pythonware.com>
parents: 14195
diff changeset
1636 } __except (EXCEPTION_EXECUTE_HANDLER) {
9f9f3280dc68 implements PyOS_CheckStack for Windows and MSVC. this fixes a
Fredrik Lundh <fredrik@pythonware.com>
parents: 14195
diff changeset
1637 /* just ignore all errors */
9f9f3280dc68 implements PyOS_CheckStack for Windows and MSVC. this fixes a
Fredrik Lundh <fredrik@pythonware.com>
parents: 14195
diff changeset
1638 }
9f9f3280dc68 implements PyOS_CheckStack for Windows and MSVC. this fixes a
Fredrik Lundh <fredrik@pythonware.com>
parents: 14195
diff changeset
1639 return 1;
9f9f3280dc68 implements PyOS_CheckStack for Windows and MSVC. this fixes a
Fredrik Lundh <fredrik@pythonware.com>
parents: 14195
diff changeset
1640 }
9f9f3280dc68 implements PyOS_CheckStack for Windows and MSVC. this fixes a
Fredrik Lundh <fredrik@pythonware.com>
parents: 14195
diff changeset
1641
9f9f3280dc68 implements PyOS_CheckStack for Windows and MSVC. this fixes a
Fredrik Lundh <fredrik@pythonware.com>
parents: 14195
diff changeset
1642 #endif /* WIN32 && _MSC_VER */
9f9f3280dc68 implements PyOS_CheckStack for Windows and MSVC. this fixes a
Fredrik Lundh <fredrik@pythonware.com>
parents: 14195
diff changeset
1643
9f9f3280dc68 implements PyOS_CheckStack for Windows and MSVC. this fixes a
Fredrik Lundh <fredrik@pythonware.com>
parents: 14195
diff changeset
1644 /* Alternate implementations can be added here... */
9f9f3280dc68 implements PyOS_CheckStack for Windows and MSVC. this fixes a
Fredrik Lundh <fredrik@pythonware.com>
parents: 14195
diff changeset
1645
9f9f3280dc68 implements PyOS_CheckStack for Windows and MSVC. this fixes a
Fredrik Lundh <fredrik@pythonware.com>
parents: 14195
diff changeset
1646 #endif /* USE_STACKCHECK */
14637
37397bd1f8a8 Add PyOS_getsig() and PyOS_setsig() -- wrappers around signal() or
Guido van Rossum <guido@python.org>
parents: 14375
diff changeset
1647
37397bd1f8a8 Add PyOS_getsig() and PyOS_setsig() -- wrappers around signal() or
Guido van Rossum <guido@python.org>
parents: 14375
diff changeset
1648
37397bd1f8a8 Add PyOS_getsig() and PyOS_setsig() -- wrappers around signal() or
Guido van Rossum <guido@python.org>
parents: 14375
diff changeset
1649 /* Wrappers around sigaction() or signal(). */
37397bd1f8a8 Add PyOS_getsig() and PyOS_setsig() -- wrappers around signal() or
Guido van Rossum <guido@python.org>
parents: 14375
diff changeset
1650
37397bd1f8a8 Add PyOS_getsig() and PyOS_setsig() -- wrappers around signal() or
Guido van Rossum <guido@python.org>
parents: 14375
diff changeset
1651 PyOS_sighandler_t
37397bd1f8a8 Add PyOS_getsig() and PyOS_setsig() -- wrappers around signal() or
Guido van Rossum <guido@python.org>
parents: 14375
diff changeset
1652 PyOS_getsig(int sig)
37397bd1f8a8 Add PyOS_getsig() and PyOS_setsig() -- wrappers around signal() or
Guido van Rossum <guido@python.org>
parents: 14375
diff changeset
1653 {
37397bd1f8a8 Add PyOS_getsig() and PyOS_setsig() -- wrappers around signal() or
Guido van Rossum <guido@python.org>
parents: 14375
diff changeset
1654 #ifdef HAVE_SIGACTION
37397bd1f8a8 Add PyOS_getsig() and PyOS_setsig() -- wrappers around signal() or
Guido van Rossum <guido@python.org>
parents: 14375
diff changeset
1655 struct sigaction context;
33747
b0ec4c9d595b Patch #975056 - fixes for restartable signals on *BSD. In addition,
Anthony Baxter <anthonybaxter@gmail.com>
parents: 33715
diff changeset
1656 if (sigaction(sig, NULL, &context) == -1)
b0ec4c9d595b Patch #975056 - fixes for restartable signals on *BSD. In addition,
Anthony Baxter <anthonybaxter@gmail.com>
parents: 33715
diff changeset
1657 return SIG_ERR;
14637
37397bd1f8a8 Add PyOS_getsig() and PyOS_setsig() -- wrappers around signal() or
Guido van Rossum <guido@python.org>
parents: 14375
diff changeset
1658 return context.sa_handler;
37397bd1f8a8 Add PyOS_getsig() and PyOS_setsig() -- wrappers around signal() or
Guido van Rossum <guido@python.org>
parents: 14375
diff changeset
1659 #else
37397bd1f8a8 Add PyOS_getsig() and PyOS_setsig() -- wrappers around signal() or
Guido van Rossum <guido@python.org>
parents: 14375
diff changeset
1660 PyOS_sighandler_t handler;
37397bd1f8a8 Add PyOS_getsig() and PyOS_setsig() -- wrappers around signal() or
Guido van Rossum <guido@python.org>
parents: 14375
diff changeset
1661 handler = signal(sig, SIG_IGN);
33747
b0ec4c9d595b Patch #975056 - fixes for restartable signals on *BSD. In addition,
Anthony Baxter <anthonybaxter@gmail.com>
parents: 33715
diff changeset
1662 if (handler != SIG_ERR)
b0ec4c9d595b Patch #975056 - fixes for restartable signals on *BSD. In addition,
Anthony Baxter <anthonybaxter@gmail.com>
parents: 33715
diff changeset
1663 signal(sig, handler);
14637
37397bd1f8a8 Add PyOS_getsig() and PyOS_setsig() -- wrappers around signal() or
Guido van Rossum <guido@python.org>
parents: 14375
diff changeset
1664 return handler;
37397bd1f8a8 Add PyOS_getsig() and PyOS_setsig() -- wrappers around signal() or
Guido van Rossum <guido@python.org>
parents: 14375
diff changeset
1665 #endif
37397bd1f8a8 Add PyOS_getsig() and PyOS_setsig() -- wrappers around signal() or
Guido van Rossum <guido@python.org>
parents: 14375
diff changeset
1666 }
37397bd1f8a8 Add PyOS_getsig() and PyOS_setsig() -- wrappers around signal() or
Guido van Rossum <guido@python.org>
parents: 14375
diff changeset
1667
37397bd1f8a8 Add PyOS_getsig() and PyOS_setsig() -- wrappers around signal() or
Guido van Rossum <guido@python.org>
parents: 14375
diff changeset
1668 PyOS_sighandler_t
37397bd1f8a8 Add PyOS_getsig() and PyOS_setsig() -- wrappers around signal() or
Guido van Rossum <guido@python.org>
parents: 14375
diff changeset
1669 PyOS_setsig(int sig, PyOS_sighandler_t handler)
37397bd1f8a8 Add PyOS_getsig() and PyOS_setsig() -- wrappers around signal() or
Guido van Rossum <guido@python.org>
parents: 14375
diff changeset
1670 {
37397bd1f8a8 Add PyOS_getsig() and PyOS_setsig() -- wrappers around signal() or
Guido van Rossum <guido@python.org>
parents: 14375
diff changeset
1671 #ifdef HAVE_SIGACTION
33747
b0ec4c9d595b Patch #975056 - fixes for restartable signals on *BSD. In addition,
Anthony Baxter <anthonybaxter@gmail.com>
parents: 33715
diff changeset
1672 struct sigaction context, ocontext;
b0ec4c9d595b Patch #975056 - fixes for restartable signals on *BSD. In addition,
Anthony Baxter <anthonybaxter@gmail.com>
parents: 33715
diff changeset
1673 context.sa_handler = handler;
b0ec4c9d595b Patch #975056 - fixes for restartable signals on *BSD. In addition,
Anthony Baxter <anthonybaxter@gmail.com>
parents: 33715
diff changeset
1674 sigemptyset(&context.sa_mask);
b0ec4c9d595b Patch #975056 - fixes for restartable signals on *BSD. In addition,
Anthony Baxter <anthonybaxter@gmail.com>
parents: 33715
diff changeset
1675 context.sa_flags = 0;
b0ec4c9d595b Patch #975056 - fixes for restartable signals on *BSD. In addition,
Anthony Baxter <anthonybaxter@gmail.com>
parents: 33715
diff changeset
1676 if (sigaction(sig, &context, &ocontext) == -1)
b0ec4c9d595b Patch #975056 - fixes for restartable signals on *BSD. In addition,
Anthony Baxter <anthonybaxter@gmail.com>
parents: 33715
diff changeset
1677 return SIG_ERR;
b0ec4c9d595b Patch #975056 - fixes for restartable signals on *BSD. In addition,
Anthony Baxter <anthonybaxter@gmail.com>
parents: 33715
diff changeset
1678 return ocontext.sa_handler;
b0ec4c9d595b Patch #975056 - fixes for restartable signals on *BSD. In addition,
Anthony Baxter <anthonybaxter@gmail.com>
parents: 33715
diff changeset
1679 #else
14637
37397bd1f8a8 Add PyOS_getsig() and PyOS_setsig() -- wrappers around signal() or
Guido van Rossum <guido@python.org>
parents: 14375
diff changeset
1680 PyOS_sighandler_t oldhandler;
33747
b0ec4c9d595b Patch #975056 - fixes for restartable signals on *BSD. In addition,
Anthony Baxter <anthonybaxter@gmail.com>
parents: 33715
diff changeset
1681 oldhandler = signal(sig, handler);
b0ec4c9d595b Patch #975056 - fixes for restartable signals on *BSD. In addition,
Anthony Baxter <anthonybaxter@gmail.com>
parents: 33715
diff changeset
1682 #ifdef HAVE_SIGINTERRUPT
b0ec4c9d595b Patch #975056 - fixes for restartable signals on *BSD. In addition,
Anthony Baxter <anthonybaxter@gmail.com>
parents: 33715
diff changeset
1683 siginterrupt(sig, 1);
b0ec4c9d595b Patch #975056 - fixes for restartable signals on *BSD. In addition,
Anthony Baxter <anthonybaxter@gmail.com>
parents: 33715
diff changeset
1684 #endif
14637
37397bd1f8a8 Add PyOS_getsig() and PyOS_setsig() -- wrappers around signal() or
Guido van Rossum <guido@python.org>
parents: 14375
diff changeset
1685 return oldhandler;
37397bd1f8a8 Add PyOS_getsig() and PyOS_setsig() -- wrappers around signal() or
Guido van Rossum <guido@python.org>
parents: 14375
diff changeset
1686 #endif
37397bd1f8a8 Add PyOS_getsig() and PyOS_setsig() -- wrappers around signal() or
Guido van Rossum <guido@python.org>
parents: 14375
diff changeset
1687 }