Mercurial > cpython-lru_cache-weakref
annotate Lib/code.py @ 9383:2b34461ba4a3 legacy-trunk
PyMapping_SetItemString() returns int, not an object.
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Fri, 16 Oct 1998 17:43:53 +0000 |
| parents | 0329d419e506 |
| children | 3886165dba42 |
| rev | line source |
|---|---|
|
9137
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
1 """Utilities dealing with code objects. |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
2 |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
3 Inspired by similar code by Jeff Epler and Fredrik Lundh. |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
4 """ |
|
5835
b9063f6f811c
Checking in new module code.py -- utilities dealing with code objects.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5 |
|
8543
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
6 import sys |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
7 import string |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
8 import traceback |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
9 |
|
5835
b9063f6f811c
Checking in new module code.py -- utilities dealing with code objects.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
10 def compile_command(source, filename="<input>", symbol="single"): |
|
b9063f6f811c
Checking in new module code.py -- utilities dealing with code objects.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
11 r"""Compile a command and determine whether it is incomplete. |
|
b9063f6f811c
Checking in new module code.py -- utilities dealing with code objects.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
12 |
|
b9063f6f811c
Checking in new module code.py -- utilities dealing with code objects.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
13 Arguments: |
|
b9063f6f811c
Checking in new module code.py -- utilities dealing with code objects.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
14 |
|
b9063f6f811c
Checking in new module code.py -- utilities dealing with code objects.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
15 source -- the source string; may contain \n characters |
|
b9063f6f811c
Checking in new module code.py -- utilities dealing with code objects.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
16 filename -- optional filename from which source was read; default "<input>" |
|
b9063f6f811c
Checking in new module code.py -- utilities dealing with code objects.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
17 symbol -- optional grammar start symbol; "single" (default) or "eval" |
|
b9063f6f811c
Checking in new module code.py -- utilities dealing with code objects.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
18 |
|
9137
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
19 Return value / exceptions raised: |
|
5835
b9063f6f811c
Checking in new module code.py -- utilities dealing with code objects.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
20 |
|
b9063f6f811c
Checking in new module code.py -- utilities dealing with code objects.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
21 - Return a code object if the command is complete and valid |
|
b9063f6f811c
Checking in new module code.py -- utilities dealing with code objects.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
22 - Return None if the command is incomplete |
|
9137
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
23 - Raise SyntaxError or OverflowError if the command is a syntax error |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
24 (OverflowError if the error is in a numeric constant) |
|
5835
b9063f6f811c
Checking in new module code.py -- utilities dealing with code objects.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
25 |
|
b9063f6f811c
Checking in new module code.py -- utilities dealing with code objects.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
26 Approach: |
|
9137
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
27 |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
28 First, check if the source consists entirely of blank lines and |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
29 comments; if so, replace it with 'pass', because the built-in |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
30 parser doesn't always do the right thing for these. |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
31 |
|
5835
b9063f6f811c
Checking in new module code.py -- utilities dealing with code objects.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
32 Compile three times: as is, with \n, and with \n\n appended. If |
|
b9063f6f811c
Checking in new module code.py -- utilities dealing with code objects.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
33 it compiles as is, it's complete. If it compiles with one \n |
|
b9063f6f811c
Checking in new module code.py -- utilities dealing with code objects.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
34 appended, we expect more. If it doesn't compile either way, we |
|
b9063f6f811c
Checking in new module code.py -- utilities dealing with code objects.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
35 compare the error we get when compiling with \n or \n\n appended. |
|
b9063f6f811c
Checking in new module code.py -- utilities dealing with code objects.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
36 If the errors are the same, the code is broken. But if the errors |
|
b9063f6f811c
Checking in new module code.py -- utilities dealing with code objects.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
37 are different, we expect more. Not intuitive; not even guaranteed |
|
b9063f6f811c
Checking in new module code.py -- utilities dealing with code objects.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
38 to hold in future releases; but this matches the compiler's |
|
9137
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
39 behavior from Python 1.4 through 1.5.2, at least. |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
40 |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
41 Caveat: |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
42 |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
43 It is possible (but not likely) that the parser stops parsing |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
44 with a successful outcome before reaching the end of the source; |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
45 in this case, trailing symbols may be ignored instead of causing an |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
46 error. For example, a backslash followed by two newlines may be |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
47 followed by arbitrary garbage. This will be fixed once the API |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
48 for the parser is better. |
|
5835
b9063f6f811c
Checking in new module code.py -- utilities dealing with code objects.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
49 |
|
b9063f6f811c
Checking in new module code.py -- utilities dealing with code objects.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
50 """ |
|
b9063f6f811c
Checking in new module code.py -- utilities dealing with code objects.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
51 |
|
9137
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
52 # Check for source consisting of only blank lines and comments |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
53 for line in string.split(source, "\n"): |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
54 line = string.strip(line) |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
55 if line and line[0] != '#': |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
56 break # Leave it alone |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
57 else: |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
58 source = "pass" # Replace it with a 'pass' statement |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
59 |
|
5835
b9063f6f811c
Checking in new module code.py -- utilities dealing with code objects.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
60 err = err1 = err2 = None |
|
b9063f6f811c
Checking in new module code.py -- utilities dealing with code objects.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
61 code = code1 = code2 = None |
|
b9063f6f811c
Checking in new module code.py -- utilities dealing with code objects.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
62 |
|
b9063f6f811c
Checking in new module code.py -- utilities dealing with code objects.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
63 try: |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7097
diff
changeset
|
64 code = compile(source, filename, symbol) |
|
5835
b9063f6f811c
Checking in new module code.py -- utilities dealing with code objects.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
65 except SyntaxError, err: |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7097
diff
changeset
|
66 pass |
|
5835
b9063f6f811c
Checking in new module code.py -- utilities dealing with code objects.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
67 |
|
b9063f6f811c
Checking in new module code.py -- utilities dealing with code objects.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
68 try: |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7097
diff
changeset
|
69 code1 = compile(source + "\n", filename, symbol) |
|
5835
b9063f6f811c
Checking in new module code.py -- utilities dealing with code objects.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
70 except SyntaxError, err1: |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7097
diff
changeset
|
71 pass |
|
5835
b9063f6f811c
Checking in new module code.py -- utilities dealing with code objects.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
72 |
|
b9063f6f811c
Checking in new module code.py -- utilities dealing with code objects.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
73 try: |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7097
diff
changeset
|
74 code2 = compile(source + "\n\n", filename, symbol) |
|
5835
b9063f6f811c
Checking in new module code.py -- utilities dealing with code objects.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
75 except SyntaxError, err2: |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7097
diff
changeset
|
76 pass |
|
5835
b9063f6f811c
Checking in new module code.py -- utilities dealing with code objects.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
77 |
|
b9063f6f811c
Checking in new module code.py -- utilities dealing with code objects.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
78 if code: |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7097
diff
changeset
|
79 return code |
|
7097
00a85ab0337f
Seems I've found a way to fix this.
Guido van Rossum <guido@python.org>
parents:
6423
diff
changeset
|
80 try: |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7097
diff
changeset
|
81 e1 = err1.__dict__ |
|
7097
00a85ab0337f
Seems I've found a way to fix this.
Guido van Rossum <guido@python.org>
parents:
6423
diff
changeset
|
82 except AttributeError: |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7097
diff
changeset
|
83 e1 = err1 |
|
7097
00a85ab0337f
Seems I've found a way to fix this.
Guido van Rossum <guido@python.org>
parents:
6423
diff
changeset
|
84 try: |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7097
diff
changeset
|
85 e2 = err2.__dict__ |
|
7097
00a85ab0337f
Seems I've found a way to fix this.
Guido van Rossum <guido@python.org>
parents:
6423
diff
changeset
|
86 except AttributeError: |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7097
diff
changeset
|
87 e2 = err2 |
|
7097
00a85ab0337f
Seems I've found a way to fix this.
Guido van Rossum <guido@python.org>
parents:
6423
diff
changeset
|
88 if not code1 and e1 == e2: |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7097
diff
changeset
|
89 raise SyntaxError, err1 |
|
6423
644484e1a37b
Add Jeff Epler's interact() function. Note that it is broken.
Guido van Rossum <guido@python.org>
parents:
5835
diff
changeset
|
90 |
|
644484e1a37b
Add Jeff Epler's interact() function. Note that it is broken.
Guido van Rossum <guido@python.org>
parents:
5835
diff
changeset
|
91 |
|
9137
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
92 class InteractiveInterpreter: |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
93 """Base class for InteractiveConsole. |
|
8543
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
94 |
|
9137
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
95 This class deals with parsing and interpreter state (the user's |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
96 namespace); it doesn't deal with input buffering or prompting or |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
97 input file naming (the filename is always passed in explicitly). |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
98 |
|
8543
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
99 """ |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
100 |
|
9137
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
101 def __init__(self, locals=None): |
|
8543
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
102 """Constructor. |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
103 |
|
9137
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
104 The optional 'locals' argument specifies the dictionary in |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
105 which code will be executed; it defaults to a newly created |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
106 dictionary with key "__name__" set to "__console__" and key |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
107 "__doc__" set to None. |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
108 |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
109 """ |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
110 if locals is None: |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
111 locals = {"__name__": "__console__", "__doc__": None} |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
112 self.locals = locals |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
113 |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
114 def runsource(self, source, filename="<input>", symbol="single"): |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
115 """Compile and run some source in the interpreter. |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
116 |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
117 Arguments are as for compile_command(). |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
118 |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
119 One several things can happen: |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
120 |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
121 1) The input is incorrect; compile_command() raised an |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
122 exception (SyntaxError or OverflowError). A syntax traceback |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
123 will be printed by calling the showsyntaxerror() method. |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
124 |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
125 2) The input is incomplete, and more input is required; |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
126 compile_command() returned None. Nothing happens. |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
127 |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
128 3) The input is complete; compile_command() returned a code |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
129 object. The code is executed by calling self.runcode() (which |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
130 also handles run-time exceptions, except for SystemExit). |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
131 |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
132 The return value is 1 in case 2, 0 in the other cases (unless |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
133 an exception is raised). The return value can be used to |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
134 decide whether to use sys.ps1 or sys.ps2 to prompt the next |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
135 line. |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
136 |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
137 """ |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
138 try: |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
139 code = compile_command(source, filename, symbol) |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
140 except (OverflowError, SyntaxError): |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
141 # Case 1 |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
142 self.showsyntaxerror(filename) |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
143 return 0 |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
144 |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
145 if code is None: |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
146 # Case 2 |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
147 return 1 |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
148 |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
149 # Case 3 |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
150 self.runcode(code) |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
151 return 0 |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
152 |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
153 def runcode(self, code): |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
154 """Execute a code object. |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
155 |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
156 When an exception occurs, self.showtraceback() is called to |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
157 display a traceback. All exceptions are caught except |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
158 SystemExit, which is reraised. |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
159 |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
160 A note about KeyboardInterrupt: this exception may occur |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
161 elsewhere in this code, and may not always be caught. The |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
162 caller should be prepared to deal with it. |
|
8543
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
163 |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
164 """ |
|
9137
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
165 try: |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
166 exec code in self.locals |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
167 except SystemExit: |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
168 raise |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
169 except: |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
170 self.showtraceback() |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
171 |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
172 def showsyntaxerror(self, filename=None): |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
173 """Display the syntax error that just occurred. |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
174 |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
175 This doesn't display a stack trace because there isn't one. |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
176 |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
177 If a filename is given, it is stuffed in the exception instead |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
178 of what was there before (because Python's parser always uses |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
179 "<string>" when reading from a string). |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
180 |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
181 The output is written by self.write(), below. |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
182 |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
183 """ |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
184 type, value, sys.last_traceback = sys.exc_info() |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
185 sys.last_type = type |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
186 sys.last_value = value |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
187 if filename and type is SyntaxError: |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
188 # Work hard to stuff the correct filename in the exception |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
189 try: |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
190 msg, (dummy_filename, lineno, offset, line) = value |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
191 except: |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
192 # Not the format we expect; leave it alone |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
193 pass |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
194 else: |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
195 # Stuff in the right filename |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
196 try: |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
197 # Assume SyntaxError is a class exception |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
198 value = SyntaxError(msg, (filename, lineno, offset, line)) |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
199 except: |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
200 # If that failed, assume SyntaxError is a string |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
201 value = msg, (filename, lineno, offset, line) |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
202 list = traceback.format_exception_only(type, value) |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
203 map(self.write, list) |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
204 |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
205 def showtraceback(self): |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
206 """Display the exception that just occurred. |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
207 |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
208 We remove the first stack item because it is our own code. |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
209 |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
210 The output is written by self.write(), below. |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
211 |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
212 """ |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
213 try: |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
214 type, value, tb = sys.exc_info() |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
215 sys.last_type = type |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
216 sys.last_value = value |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
217 sys.last_traceback = tb |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
218 tblist = traceback.extract_tb(tb) |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
219 del tblist[:1] |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
220 list = traceback.format_list(tblist) |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
221 if list: |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
222 list.insert(0, "Traceback (innermost last):\n") |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
223 list[len(list):] = traceback.format_exception_only(type, value) |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
224 finally: |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
225 tblist = tb = None |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
226 map(self.write, list) |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
227 |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
228 def write(self, data): |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
229 """Write a string. |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
230 |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
231 The base implementation writes to sys.stderr; a subclass may |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
232 replace this with a different implementation. |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
233 |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
234 """ |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
235 sys.stderr.write(data) |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
236 |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
237 |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
238 class InteractiveConsole(InteractiveInterpreter): |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
239 """Closely emulate the behavior of the interactive Python interpreter. |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
240 |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
241 This class builds on InteractiveInterpreter and adds prompting |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
242 using the familiar sys.ps1 and sys.ps2, and input buffering. |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
243 |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
244 """ |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
245 |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
246 def __init__(self, locals=None, filename="<console>"): |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
247 """Constructor. |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
248 |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
249 The optional locals argument will be passed to the |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
250 InteractiveInterpreter base class. |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
251 |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
252 The optional filename argument should specify the (file)name |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
253 of the input stream; it will show up in tracebacks. |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
254 |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
255 """ |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
256 InteractiveInterpreter.__init__(self, locals) |
|
8543
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
257 self.filename = filename |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
258 self.resetbuffer() |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
259 |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
260 def resetbuffer(self): |
|
9137
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
261 """Reset the input buffer.""" |
|
8543
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
262 self.buffer = [] |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
263 |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
264 def interact(self, banner=None): |
|
9137
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
265 """Closely emulate the interactive Python console. |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
266 |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
267 The optional banner argument specify the banner to print |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
268 before the first interaction; by default it prints a banner |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
269 similar to the one printed by the real Python interpreter, |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
270 followed by the current class name in parentheses (so as not |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
271 to confuse this with the real interpreter -- since it's so |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
272 close!). |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
273 |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
274 """ |
|
8543
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
275 try: |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
276 sys.ps1 |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
277 except AttributeError: |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
278 sys.ps1 = ">>> " |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
279 try: |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
280 sys.ps2 |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
281 except AttributeError: |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
282 sys.ps2 = "... " |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
283 if banner is None: |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
284 self.write("Python %s on %s\n%s\n(%s)\n" % |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
285 (sys.version, sys.platform, sys.copyright, |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
286 self.__class__.__name__)) |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
287 else: |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
288 self.write("%s\n" % str(banner)) |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
289 more = 0 |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
290 while 1: |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
291 try: |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
292 if more: |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
293 prompt = sys.ps2 |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
294 else: |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
295 prompt = sys.ps1 |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
296 try: |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
297 line = self.raw_input(prompt) |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
298 except EOFError: |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
299 self.write("\n") |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
300 break |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
301 else: |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
302 more = self.push(line) |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
303 except KeyboardInterrupt: |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
304 self.write("\nKeyboardInterrupt\n") |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
305 self.resetbuffer() |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
306 more = 0 |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
307 |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
308 def push(self, line): |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
309 """Push a line to the interpreter. |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
310 |
|
9137
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
311 The line should not have a trailing newline; it may have |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
312 internal newlines. The line is appended to a buffer and the |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
313 interpreter's runsource() method is called with the |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
314 concatenated contents of the buffer as source. If this |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
315 indicates that the command was executed or invalid, the buffer |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
316 is reset; otherwise, the command is incomplete, and the buffer |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
317 is left as it was after the line was appended. The return |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
318 value is 1 if more input is required, 0 if the line was dealt |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
319 with in some way (this is the same as runsource()). |
|
8543
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
320 |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
321 """ |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
322 self.buffer.append(line) |
|
9137
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
323 source = string.join(self.buffer, "\n") |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
324 more = self.runsource(source, self.filename) |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
325 if not more: |
|
8543
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
326 self.resetbuffer() |
|
9137
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
327 return more |
|
8543
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
328 |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
329 def raw_input(self, prompt=""): |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
330 """Write a prompt and read a line. |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
331 |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
332 The returned line does not include the trailing newline. |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
333 When the user enters the EOF key sequence, EOFError is raised. |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
334 |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
335 The base implementation uses the built-in function |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
336 raw_input(); a subclass may replace this with a different |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
337 implementation. |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
338 |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
339 """ |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
340 return raw_input(prompt) |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
341 |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
342 |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
343 def interact(banner=None, readfunc=None, locals=None): |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
344 """Closely emulate the interactive Python interpreter. |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
345 |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
346 This is a backwards compatible interface to the InteractiveConsole |
|
9137
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
347 class. When readfunc is not specified, it attempts to import the |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
348 readline module to enable GNU readline if it is available. |
|
8543
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
349 |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
350 Arguments (all optional, all default to None): |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
351 |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
352 banner -- passed to InteractiveConsole.interact() |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
353 readfunc -- if not None, replaces InteractiveConsole.raw_input() |
|
9137
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
354 locals -- passed to InteractiveInterpreter.__init__() |
|
8543
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
355 |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
356 """ |
|
9137
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
357 console = InteractiveConsole(locals) |
|
8543
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
358 if readfunc is not None: |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
359 console.raw_input = readfunc |
|
9137
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
360 else: |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
361 try: |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
362 import readline |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
363 except: |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
364 pass |
|
8543
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
365 console.interact(banner) |
|
9137
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
366 |
|
0329d419e506
Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents:
8543
diff
changeset
|
367 |
|
6423
644484e1a37b
Add Jeff Epler's interact() function. Note that it is broken.
Guido van Rossum <guido@python.org>
parents:
5835
diff
changeset
|
368 if __name__ == '__main__': |
|
644484e1a37b
Add Jeff Epler's interact() function. Note that it is broken.
Guido van Rossum <guido@python.org>
parents:
5835
diff
changeset
|
369 interact() |
