annotate Lib/code.py @ 7097:00a85ab0337f legacy-trunk

Seems I've found a way to fix this.
author Guido van Rossum <guido@python.org>
date Wed, 14 Jan 1998 15:40:30 +0000
parents 644484e1a37b
children fb4b4d069cc7
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
b9063f6f811c Checking in new module code.py -- utilities dealing with code objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
3 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
4 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
5
b9063f6f811c Checking in new module code.py -- utilities dealing with code objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
6 Arguments:
b9063f6f811c Checking in new module code.py -- utilities dealing with code objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
7
b9063f6f811c Checking in new module code.py -- utilities dealing with code objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
8 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
9 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
10 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
11
b9063f6f811c Checking in new module code.py -- utilities dealing with code objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
12 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
13
b9063f6f811c Checking in new module code.py -- utilities dealing with code objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
14 - 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
15 - 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
16 - 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
17
b9063f6f811c Checking in new module code.py -- utilities dealing with code objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
18 Approach:
b9063f6f811c Checking in new module code.py -- utilities dealing with code objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
19
b9063f6f811c Checking in new module code.py -- utilities dealing with code objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
20 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
21 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
22 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
23 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
24 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
25 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
26 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
27 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
28
b9063f6f811c Checking in new module code.py -- utilities dealing with code objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
29 """
b9063f6f811c Checking in new module code.py -- utilities dealing with code objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
30
b9063f6f811c Checking in new module code.py -- utilities dealing with code objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
31 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
32 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
33
b9063f6f811c Checking in new module code.py -- utilities dealing with code objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
34 try:
b9063f6f811c Checking in new module code.py -- utilities dealing with code objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
35 code = compile(source, filename, symbol)
b9063f6f811c Checking in new module code.py -- utilities dealing with code objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
36 except SyntaxError, err:
b9063f6f811c Checking in new module code.py -- utilities dealing with code objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
37 pass
b9063f6f811c Checking in new module code.py -- utilities dealing with code objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
38
b9063f6f811c Checking in new module code.py -- utilities dealing with code objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
39 try:
b9063f6f811c Checking in new module code.py -- utilities dealing with code objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
40 code1 = compile(source + "\n", filename, symbol)
b9063f6f811c Checking in new module code.py -- utilities dealing with code objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
41 except SyntaxError, err1:
b9063f6f811c Checking in new module code.py -- utilities dealing with code objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
42 pass
b9063f6f811c Checking in new module code.py -- utilities dealing with code objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
43
b9063f6f811c Checking in new module code.py -- utilities dealing with code objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
44 try:
b9063f6f811c Checking in new module code.py -- utilities dealing with code objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
45 code2 = compile(source + "\n\n", filename, symbol)
b9063f6f811c Checking in new module code.py -- utilities dealing with code objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
46 except SyntaxError, err2:
b9063f6f811c Checking in new module code.py -- utilities dealing with code objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
47 pass
b9063f6f811c Checking in new module code.py -- utilities dealing with code objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
48
b9063f6f811c Checking in new module code.py -- utilities dealing with code objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
49 if code:
b9063f6f811c Checking in new module code.py -- utilities dealing with code objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
50 return code
7097
00a85ab0337f Seems I've found a way to fix this.
Guido van Rossum <guido@python.org>
parents: 6423
diff changeset
51 try:
00a85ab0337f Seems I've found a way to fix this.
Guido van Rossum <guido@python.org>
parents: 6423
diff changeset
52 e1 = err1.__dict__
00a85ab0337f Seems I've found a way to fix this.
Guido van Rossum <guido@python.org>
parents: 6423
diff changeset
53 except AttributeError:
00a85ab0337f Seems I've found a way to fix this.
Guido van Rossum <guido@python.org>
parents: 6423
diff changeset
54 e1 = err1
00a85ab0337f Seems I've found a way to fix this.
Guido van Rossum <guido@python.org>
parents: 6423
diff changeset
55 try:
00a85ab0337f Seems I've found a way to fix this.
Guido van Rossum <guido@python.org>
parents: 6423
diff changeset
56 e2 = err2.__dict__
00a85ab0337f Seems I've found a way to fix this.
Guido van Rossum <guido@python.org>
parents: 6423
diff changeset
57 except AttributeError:
00a85ab0337f Seems I've found a way to fix this.
Guido van Rossum <guido@python.org>
parents: 6423
diff changeset
58 e2 = err2
00a85ab0337f Seems I've found a way to fix this.
Guido van Rossum <guido@python.org>
parents: 6423
diff changeset
59 if not code1 and e1 == e2:
5835
b9063f6f811c Checking in new module code.py -- utilities dealing with code objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
60 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
61
644484e1a37b Add Jeff Epler's interact() function. Note that it is broken.
Guido van Rossum <guido@python.org>
parents: 5835
diff changeset
62
644484e1a37b Add Jeff Epler's interact() function. Note that it is broken.
Guido van Rossum <guido@python.org>
parents: 5835
diff changeset
63 def interact(banner=None, readfunc=raw_input, local=None):
644484e1a37b Add Jeff Epler's interact() function. Note that it is broken.
Guido van Rossum <guido@python.org>
parents: 5835
diff changeset
64 # Due to Jeff Epler, with changes by Guido:
644484e1a37b Add Jeff Epler's interact() function. Note that it is broken.
Guido van Rossum <guido@python.org>
parents: 5835
diff changeset
65 """Closely emulate the interactive Python console."""
644484e1a37b Add Jeff Epler's interact() function. Note that it is broken.
Guido van Rossum <guido@python.org>
parents: 5835
diff changeset
66 try: import readline # Enable GNU readline if available
644484e1a37b Add Jeff Epler's interact() function. Note that it is broken.
Guido van Rossum <guido@python.org>
parents: 5835
diff changeset
67 except: pass
644484e1a37b Add Jeff Epler's interact() function. Note that it is broken.
Guido van Rossum <guido@python.org>
parents: 5835
diff changeset
68 local = local or {}
644484e1a37b Add Jeff Epler's interact() function. Note that it is broken.
Guido van Rossum <guido@python.org>
parents: 5835
diff changeset
69 import sys, string, traceback
644484e1a37b Add Jeff Epler's interact() function. Note that it is broken.
Guido van Rossum <guido@python.org>
parents: 5835
diff changeset
70 sys.ps1 = '>>> '
644484e1a37b Add Jeff Epler's interact() function. Note that it is broken.
Guido van Rossum <guido@python.org>
parents: 5835
diff changeset
71 sys.ps2 = '... '
644484e1a37b Add Jeff Epler's interact() function. Note that it is broken.
Guido van Rossum <guido@python.org>
parents: 5835
diff changeset
72 if banner:
644484e1a37b Add Jeff Epler's interact() function. Note that it is broken.
Guido van Rossum <guido@python.org>
parents: 5835
diff changeset
73 print banner
644484e1a37b Add Jeff Epler's interact() function. Note that it is broken.
Guido van Rossum <guido@python.org>
parents: 5835
diff changeset
74 else:
644484e1a37b Add Jeff Epler's interact() function. Note that it is broken.
Guido van Rossum <guido@python.org>
parents: 5835
diff changeset
75 print "Python Interactive Console", sys.version
644484e1a37b Add Jeff Epler's interact() function. Note that it is broken.
Guido van Rossum <guido@python.org>
parents: 5835
diff changeset
76 print sys.copyright
644484e1a37b Add Jeff Epler's interact() function. Note that it is broken.
Guido van Rossum <guido@python.org>
parents: 5835
diff changeset
77 buf = []
644484e1a37b Add Jeff Epler's interact() function. Note that it is broken.
Guido van Rossum <guido@python.org>
parents: 5835
diff changeset
78 while 1:
644484e1a37b Add Jeff Epler's interact() function. Note that it is broken.
Guido van Rossum <guido@python.org>
parents: 5835
diff changeset
79 if buf: prompt = sys.ps2
644484e1a37b Add Jeff Epler's interact() function. Note that it is broken.
Guido van Rossum <guido@python.org>
parents: 5835
diff changeset
80 else: prompt = sys.ps1
644484e1a37b Add Jeff Epler's interact() function. Note that it is broken.
Guido van Rossum <guido@python.org>
parents: 5835
diff changeset
81 try: line = readfunc(prompt)
644484e1a37b Add Jeff Epler's interact() function. Note that it is broken.
Guido van Rossum <guido@python.org>
parents: 5835
diff changeset
82 except KeyboardInterrupt:
644484e1a37b Add Jeff Epler's interact() function. Note that it is broken.
Guido van Rossum <guido@python.org>
parents: 5835
diff changeset
83 print "\nKeyboardInterrupt"
644484e1a37b Add Jeff Epler's interact() function. Note that it is broken.
Guido van Rossum <guido@python.org>
parents: 5835
diff changeset
84 buf = []
644484e1a37b Add Jeff Epler's interact() function. Note that it is broken.
Guido van Rossum <guido@python.org>
parents: 5835
diff changeset
85 continue
644484e1a37b Add Jeff Epler's interact() function. Note that it is broken.
Guido van Rossum <guido@python.org>
parents: 5835
diff changeset
86 except EOFError: break
644484e1a37b Add Jeff Epler's interact() function. Note that it is broken.
Guido van Rossum <guido@python.org>
parents: 5835
diff changeset
87 buf.append(line)
644484e1a37b Add Jeff Epler's interact() function. Note that it is broken.
Guido van Rossum <guido@python.org>
parents: 5835
diff changeset
88 try: x = compile_command(string.join(buf, "\n"))
644484e1a37b Add Jeff Epler's interact() function. Note that it is broken.
Guido van Rossum <guido@python.org>
parents: 5835
diff changeset
89 except SyntaxError:
644484e1a37b Add Jeff Epler's interact() function. Note that it is broken.
Guido van Rossum <guido@python.org>
parents: 5835
diff changeset
90 traceback.print_exc(0)
644484e1a37b Add Jeff Epler's interact() function. Note that it is broken.
Guido van Rossum <guido@python.org>
parents: 5835
diff changeset
91 buf = []
644484e1a37b Add Jeff Epler's interact() function. Note that it is broken.
Guido van Rossum <guido@python.org>
parents: 5835
diff changeset
92 continue
644484e1a37b Add Jeff Epler's interact() function. Note that it is broken.
Guido van Rossum <guido@python.org>
parents: 5835
diff changeset
93 if x == None: continue
644484e1a37b Add Jeff Epler's interact() function. Note that it is broken.
Guido van Rossum <guido@python.org>
parents: 5835
diff changeset
94 else:
644484e1a37b Add Jeff Epler's interact() function. Note that it is broken.
Guido van Rossum <guido@python.org>
parents: 5835
diff changeset
95 try: exec x in local
644484e1a37b Add Jeff Epler's interact() function. Note that it is broken.
Guido van Rossum <guido@python.org>
parents: 5835
diff changeset
96 except:
644484e1a37b Add Jeff Epler's interact() function. Note that it is broken.
Guido van Rossum <guido@python.org>
parents: 5835
diff changeset
97 exc_type, exc_value, exc_traceback = \
644484e1a37b Add Jeff Epler's interact() function. Note that it is broken.
Guido van Rossum <guido@python.org>
parents: 5835
diff changeset
98 sys.exc_type, sys.exc_value, \
644484e1a37b Add Jeff Epler's interact() function. Note that it is broken.
Guido van Rossum <guido@python.org>
parents: 5835
diff changeset
99 sys.exc_traceback
644484e1a37b Add Jeff Epler's interact() function. Note that it is broken.
Guido van Rossum <guido@python.org>
parents: 5835
diff changeset
100 l = len(traceback.extract_tb(sys.exc_traceback))
644484e1a37b Add Jeff Epler's interact() function. Note that it is broken.
Guido van Rossum <guido@python.org>
parents: 5835
diff changeset
101 try: 1/0
644484e1a37b Add Jeff Epler's interact() function. Note that it is broken.
Guido van Rossum <guido@python.org>
parents: 5835
diff changeset
102 except:
644484e1a37b Add Jeff Epler's interact() function. Note that it is broken.
Guido van Rossum <guido@python.org>
parents: 5835
diff changeset
103 m = len(traceback.extract_tb(
644484e1a37b Add Jeff Epler's interact() function. Note that it is broken.
Guido van Rossum <guido@python.org>
parents: 5835
diff changeset
104 sys.exc_traceback))
644484e1a37b Add Jeff Epler's interact() function. Note that it is broken.
Guido van Rossum <guido@python.org>
parents: 5835
diff changeset
105 traceback.print_exception(exc_type,
644484e1a37b Add Jeff Epler's interact() function. Note that it is broken.
Guido van Rossum <guido@python.org>
parents: 5835
diff changeset
106 exc_value, exc_traceback, l-m)
644484e1a37b Add Jeff Epler's interact() function. Note that it is broken.
Guido van Rossum <guido@python.org>
parents: 5835
diff changeset
107 buf = []
644484e1a37b Add Jeff Epler's interact() function. Note that it is broken.
Guido van Rossum <guido@python.org>
parents: 5835
diff changeset
108
644484e1a37b Add Jeff Epler's interact() function. Note that it is broken.
Guido van Rossum <guido@python.org>
parents: 5835
diff changeset
109 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
110 interact()