annotate Lib/code.py @ 8291:f8a389b104f0 legacy-trunk

Changed some comments.... no longer mention FrameMaker.
author Fred Drake <fdrake@acm.org>
date Mon, 11 May 1998 16:31:32 +0000
parents fb4b4d069cc7
children e951bf2d7d93
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:
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7097
diff changeset
35 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
36 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
37 pass
5835
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:
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7097
diff changeset
40 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
41 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
42 pass
5835
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:
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7097
diff changeset
45 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
46 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
47 pass
5835
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:
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7097
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:
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7097
diff changeset
52 e1 = err1.__dict__
7097
00a85ab0337f Seems I've found a way to fix this.
Guido van Rossum <guido@python.org>
parents: 6423
diff changeset
53 except AttributeError:
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7097
diff changeset
54 e1 = err1
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 e2 = err2.__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 e2 = err2
7097
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:
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7097
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:
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7097
diff changeset
73 print banner
6423
644484e1a37b Add Jeff Epler's interact() function. Note that it is broken.
Guido van Rossum <guido@python.org>
parents: 5835
diff changeset
74 else:
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7097
diff changeset
75 print "Python Interactive Console", sys.version
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7097
diff changeset
76 print sys.copyright
6423
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:
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7097
diff changeset
79 if buf: prompt = sys.ps2
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7097
diff changeset
80 else: prompt = sys.ps1
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7097
diff changeset
81 try: line = readfunc(prompt)
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7097
diff changeset
82 except KeyboardInterrupt:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7097
diff changeset
83 print "\nKeyboardInterrupt"
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7097
diff changeset
84 buf = []
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7097
diff changeset
85 continue
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7097
diff changeset
86 except EOFError: break
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7097
diff changeset
87 buf.append(line)
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7097
diff changeset
88 try: x = compile_command(string.join(buf, "\n"))
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7097
diff changeset
89 except SyntaxError:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7097
diff changeset
90 traceback.print_exc(0)
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7097
diff changeset
91 buf = []
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7097
diff changeset
92 continue
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7097
diff changeset
93 if x == None: continue
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7097
diff changeset
94 else:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7097
diff changeset
95 try: exec x in local
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7097
diff changeset
96 except:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7097
diff changeset
97 exc_type, exc_value, exc_traceback = \
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7097
diff changeset
98 sys.exc_type, sys.exc_value, \
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7097
diff changeset
99 sys.exc_traceback
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7097
diff changeset
100 l = len(traceback.extract_tb(sys.exc_traceback))
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7097
diff changeset
101 try: 1/0
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7097
diff changeset
102 except:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7097
diff changeset
103 m = len(traceback.extract_tb(
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7097
diff changeset
104 sys.exc_traceback))
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7097
diff changeset
105 traceback.print_exception(exc_type,
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7097
diff changeset
106 exc_value, exc_traceback, l-m)
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7097
diff changeset
107 buf = []
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7097
diff changeset
108
6423
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()