Mercurial > cpython-lru_cache-weakref
annotate Lib/code.py @ 8831:00a6bb65ff29 legacy-trunk
Getting rid of this ancient stuff.
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Wed, 05 Aug 1998 21:10:09 +0000 |
| parents | e951bf2d7d93 |
| children | 0329d419e506 |
| rev | line source |
|---|---|
|
5835
b9063f6f811c
Checking in new module code.py -- utilities dealing with code objects.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1 """Utilities dealing with code objects.""" |
|
b9063f6f811c
Checking in new module code.py -- utilities dealing with code objects.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2 |
|
8543
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
3 import sys |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
4 import string |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
5 import traceback |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
6 |
|
5835
b9063f6f811c
Checking in new module code.py -- utilities dealing with code objects.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7 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
|
8 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
|
9 |
|
b9063f6f811c
Checking in new module code.py -- utilities dealing with code objects.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
10 Arguments: |
|
b9063f6f811c
Checking in new module code.py -- utilities dealing with code objects.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
11 |
|
b9063f6f811c
Checking in new module code.py -- utilities dealing with code objects.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
12 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
|
13 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
|
14 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
|
15 |
|
b9063f6f811c
Checking in new module code.py -- utilities dealing with code objects.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
16 Return value / exception raised: |
|
b9063f6f811c
Checking in new module code.py -- utilities dealing with code objects.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
17 |
|
b9063f6f811c
Checking in new module code.py -- utilities dealing with code objects.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
18 - 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
|
19 - Return None if the command is incomplete |
|
b9063f6f811c
Checking in new module code.py -- utilities dealing with code objects.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
20 - Raise SyntaxError if the command is a syntax error |
|
b9063f6f811c
Checking in new module code.py -- utilities dealing with code objects.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
21 |
|
b9063f6f811c
Checking in new module code.py -- utilities dealing with code objects.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
22 Approach: |
|
b9063f6f811c
Checking in new module code.py -- utilities dealing with code objects.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
23 |
|
b9063f6f811c
Checking in new module code.py -- utilities dealing with code objects.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
24 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
|
25 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
|
26 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
|
27 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
|
28 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
|
29 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
|
30 to hold in future releases; but this matches the compiler's |
|
b9063f6f811c
Checking in new module code.py -- utilities dealing with code objects.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
31 behavior in Python 1.4 and 1.5. |
|
b9063f6f811c
Checking in new module code.py -- utilities dealing with code objects.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
32 |
|
b9063f6f811c
Checking in new module code.py -- utilities dealing with code objects.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
33 """ |
|
b9063f6f811c
Checking in new module code.py -- utilities dealing with code objects.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
34 |
|
b9063f6f811c
Checking in new module code.py -- utilities dealing with code objects.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
35 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
|
36 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
|
37 |
|
b9063f6f811c
Checking in new module code.py -- utilities dealing with code objects.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
38 try: |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7097
diff
changeset
|
39 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
|
40 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
|
41 pass |
|
5835
b9063f6f811c
Checking in new module code.py -- utilities dealing with code objects.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
42 |
|
b9063f6f811c
Checking in new module code.py -- utilities dealing with code objects.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
43 try: |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7097
diff
changeset
|
44 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
|
45 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
|
46 pass |
|
5835
b9063f6f811c
Checking in new module code.py -- utilities dealing with code objects.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
47 |
|
b9063f6f811c
Checking in new module code.py -- utilities dealing with code objects.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
48 try: |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7097
diff
changeset
|
49 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
|
50 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
|
51 pass |
|
5835
b9063f6f811c
Checking in new module code.py -- utilities dealing with code objects.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
52 |
|
b9063f6f811c
Checking in new module code.py -- utilities dealing with code objects.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
53 if code: |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7097
diff
changeset
|
54 return code |
|
7097
00a85ab0337f
Seems I've found a way to fix this.
Guido van Rossum <guido@python.org>
parents:
6423
diff
changeset
|
55 try: |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7097
diff
changeset
|
56 e1 = err1.__dict__ |
|
7097
00a85ab0337f
Seems I've found a way to fix this.
Guido van Rossum <guido@python.org>
parents:
6423
diff
changeset
|
57 except AttributeError: |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7097
diff
changeset
|
58 e1 = err1 |
|
7097
00a85ab0337f
Seems I've found a way to fix this.
Guido van Rossum <guido@python.org>
parents:
6423
diff
changeset
|
59 try: |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7097
diff
changeset
|
60 e2 = err2.__dict__ |
|
7097
00a85ab0337f
Seems I've found a way to fix this.
Guido van Rossum <guido@python.org>
parents:
6423
diff
changeset
|
61 except AttributeError: |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7097
diff
changeset
|
62 e2 = err2 |
|
7097
00a85ab0337f
Seems I've found a way to fix this.
Guido van Rossum <guido@python.org>
parents:
6423
diff
changeset
|
63 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
|
64 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
|
65 |
|
644484e1a37b
Add Jeff Epler's interact() function. Note that it is broken.
Guido van Rossum <guido@python.org>
parents:
5835
diff
changeset
|
66 |
|
8543
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
67 class InteractiveConsole: |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
68 """Closely emulate the behavior of 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
|
69 |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
70 After code by Jeff Epler and Fredrik Lundh. |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
71 """ |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
72 |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
73 def __init__(self, filename="<console>", locals=None): |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
74 """Constructor. |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
75 |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
76 The optional filename argument specifies the (file)name of the |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
77 input stream; it will show up in tracebacks. It defaults to |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
78 '<console>'. |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
79 |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
80 """ |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
81 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
|
82 if locals is None: |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
83 locals = {} |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
84 self.locals = locals |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
85 self.resetbuffer() |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
86 |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
87 def resetbuffer(self): |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
88 """Reset the input buffer (but not the variables!).""" |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
89 self.buffer = [] |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
90 |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
91 def interact(self, banner=None): |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
92 """Closely emulate the interactive Python console.""" |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
93 try: |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
94 sys.ps1 |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
95 except AttributeError: |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
96 sys.ps1 = ">>> " |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
97 try: |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
98 sys.ps2 |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
99 except AttributeError: |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
100 sys.ps2 = "... " |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
101 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
|
102 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
|
103 (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
|
104 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
|
105 else: |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
106 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
|
107 more = 0 |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
108 while 1: |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
109 try: |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
110 if more: |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
111 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
|
112 else: |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
113 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
|
114 try: |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
115 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
|
116 except EOFError: |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
117 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
|
118 break |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
119 else: |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
120 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
|
121 except KeyboardInterrupt: |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
122 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
|
123 self.resetbuffer() |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
124 more = 0 |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
125 |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
126 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
|
127 """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
|
128 |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
129 The line should not have a trailing newline. |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
130 |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
131 One of three things will happen: |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
132 |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
133 1) The input is incorrect; compile_command() raised |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
134 SyntaxError. A syntax traceback will be printed. |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
135 |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
136 2) The input is incomplete, and more input is required; |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
137 compile_command() returned None. |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
138 |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
139 3) The input is complete; compile_command() returned a code |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
140 object. The code is executed. When an exception occurs, a |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
141 traceback is printed. All exceptions are caught except |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
142 SystemExit, which is reraised. |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
143 |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
144 The return value is 1 in case 2, 0 in the other cases. (The |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
145 return value can be used to decide whether to use sys.ps1 or |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
146 sys.ps2 to prompt the next line.) |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
147 |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
148 A note about KeyboardInterrupt: this exception may occur |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
149 elsewhere in this code, and will not always be caught. The |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
150 caller should be prepared to deal with it. |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
151 |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
152 """ |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
153 self.buffer.append(line) |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
154 |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
155 try: |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
156 x = compile_command(string.join(self.buffer, "\n"), |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
157 filename=self.filename) |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7097
diff
changeset
|
158 except SyntaxError: |
|
8543
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
159 # Case 1 |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
160 self.showsyntaxerror() |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
161 self.resetbuffer() |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
162 return 0 |
|
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 if x is None: |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
165 # Case 2 |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
166 return 1 |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
167 |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
168 # Case 3 |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
169 try: |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
170 exec x in self.locals |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
171 except SystemExit: |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
172 raise |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
173 except: |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
174 self.showtraceback() |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
175 self.resetbuffer() |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
176 return 0 |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
177 |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
178 def showsyntaxerror(self): |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
179 """Display the syntax error that just occurred. |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
180 |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
181 This doesn't display a stack trace because there isn't one. |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
182 |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
183 The output is written by self.write(), below. |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
184 |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
185 """ |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
186 type, value = sys.exc_info()[:2] |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
187 # Work hard to stuff the correct filename in the exception |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
188 try: |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
189 msg, (filename, lineno, offset, line) = value |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
190 except: |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
191 pass |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7097
diff
changeset
|
192 else: |
|
8543
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
193 try: |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
194 value = SyntaxError(msg, (self.filename, lineno, offset, line)) |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7097
diff
changeset
|
195 except: |
|
8543
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
196 value = msg, (self.filename, lineno, offset, line) |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
197 list = traceback.format_exception_only(type, value) |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
198 map(self.write, list) |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
199 |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
200 def showtraceback(self): |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
201 """Display the exception that just occurred. |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
202 |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
203 We remove the first stack item because it is our own code. |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
204 |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
205 The output is written by self.write(), below. |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
206 |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
207 """ |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
208 try: |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
209 type, value, tb = sys.exc_info() |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
210 tblist = traceback.extract_tb(tb) |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
211 del tblist[0] |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
212 list = traceback.format_list(tblist) |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
213 list[len(list):] = traceback.format_exception_only(type, value) |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
214 finally: |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
215 tblist = tb = None |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
216 map(self.write, list) |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
217 |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
218 def write(self, data): |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
219 """Write a string. |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
220 |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
221 The base implementation writes to sys.stderr; a subclass may |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
222 replace this with a different implementation. |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
223 |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
224 """ |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
225 sys.stderr.write(data) |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
226 |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
227 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
|
228 """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
|
229 |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
230 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
|
231 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
|
232 |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
233 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
|
234 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
|
235 implementation. |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
236 |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
237 """ |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
238 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
|
239 |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
240 |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
241 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
|
242 """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
|
243 |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
244 This is a backwards compatible interface to the InteractiveConsole |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
245 class. It attempts to import the readline module to enable GNU |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
246 readline if it is available. |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
247 |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
248 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
|
249 |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
250 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
|
251 readfunc -- if not None, replaces InteractiveConsole.raw_input() |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
252 locals -- passed to InteractiveConsole.__init__() |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
253 |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
254 """ |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
255 try: |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
256 import readline |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
257 except: |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
258 pass |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
259 console = InteractiveConsole(locals=locals) |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
260 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
|
261 console.raw_input = readfunc |
|
e951bf2d7d93
Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
262 console.interact(banner) |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7097
diff
changeset
|
263 |
|
6423
644484e1a37b
Add Jeff Epler's interact() function. Note that it is broken.
Guido van Rossum <guido@python.org>
parents:
5835
diff
changeset
|
264 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
|
265 interact() |
