annotate Lib/code.py @ 6719:faebc1940432 legacy-trunk

Understand -Xlinker option.
author Guido van Rossum <guido@python.org>
date Tue, 02 Dec 1997 20:38:11 +0000
parents 644484e1a37b
children 00a85ab0337f
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
6423
644484e1a37b Add Jeff Epler's interact() function. Note that it is broken.
Guido van Rossum <guido@python.org>
parents: 5835
diff changeset
1 # XXX This is broken with class exceptions
644484e1a37b Add Jeff Epler's interact() function. Note that it is broken.
Guido van Rossum <guido@python.org>
parents: 5835
diff changeset
2
5835
b9063f6f811c Checking in new module code.py -- utilities dealing with code objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
3 """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
4
b9063f6f811c Checking in new module code.py -- utilities dealing with code objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
5 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
6 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
7
b9063f6f811c Checking in new module code.py -- utilities dealing with code objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
8 Arguments:
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 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
11 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
12 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
13
b9063f6f811c Checking in new module code.py -- utilities dealing with code objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
14 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
15
b9063f6f811c Checking in new module code.py -- utilities dealing with code objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
16 - 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
17 - 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
18 - 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
19
b9063f6f811c Checking in new module code.py -- utilities dealing with code objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
20 Approach:
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 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
23 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
24 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
25 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
26 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
27 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
28 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
29 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
30
b9063f6f811c Checking in new module code.py -- utilities dealing with code objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
31 """
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 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
34 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
35
b9063f6f811c Checking in new module code.py -- utilities dealing with code objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
36 try:
b9063f6f811c Checking in new module code.py -- utilities dealing with code objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
37 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
38 except SyntaxError, err:
b9063f6f811c Checking in new module code.py -- utilities dealing with code objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
39 pass
b9063f6f811c Checking in new module code.py -- utilities dealing with code objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
40
b9063f6f811c Checking in new module code.py -- utilities dealing with code objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
41 try:
b9063f6f811c Checking in new module code.py -- utilities dealing with code objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
42 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
43 except SyntaxError, err1:
b9063f6f811c Checking in new module code.py -- utilities dealing with code objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
44 pass
b9063f6f811c Checking in new module code.py -- utilities dealing with code objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
45
b9063f6f811c Checking in new module code.py -- utilities dealing with code objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
46 try:
b9063f6f811c Checking in new module code.py -- utilities dealing with code objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
47 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
48 except SyntaxError, err2:
b9063f6f811c Checking in new module code.py -- utilities dealing with code objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
49 pass
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 if code:
b9063f6f811c Checking in new module code.py -- utilities dealing with code objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
52 return code
b9063f6f811c Checking in new module code.py -- utilities dealing with code objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
53 if not code1 and err1 == err2:
b9063f6f811c Checking in new module code.py -- utilities dealing with code objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
54 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
55
644484e1a37b Add Jeff Epler's interact() function. Note that it is broken.
Guido van Rossum <guido@python.org>
parents: 5835
diff changeset
56
644484e1a37b Add Jeff Epler's interact() function. Note that it is broken.
Guido van Rossum <guido@python.org>
parents: 5835
diff changeset
57 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
58 # 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
59 """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
60 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
61 except: pass
644484e1a37b Add Jeff Epler's interact() function. Note that it is broken.
Guido van Rossum <guido@python.org>
parents: 5835
diff changeset
62 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
63 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
64 sys.ps1 = '>>> '
644484e1a37b Add Jeff Epler's interact() function. Note that it is broken.
Guido van Rossum <guido@python.org>
parents: 5835
diff changeset
65 sys.ps2 = '... '
644484e1a37b Add Jeff Epler's interact() function. Note that it is broken.
Guido van Rossum <guido@python.org>
parents: 5835
diff changeset
66 if banner:
644484e1a37b Add Jeff Epler's interact() function. Note that it is broken.
Guido van Rossum <guido@python.org>
parents: 5835
diff changeset
67 print banner
644484e1a37b Add Jeff Epler's interact() function. Note that it is broken.
Guido van Rossum <guido@python.org>
parents: 5835
diff changeset
68 else:
644484e1a37b Add Jeff Epler's interact() function. Note that it is broken.
Guido van Rossum <guido@python.org>
parents: 5835
diff changeset
69 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
70 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
71 buf = []
644484e1a37b Add Jeff Epler's interact() function. Note that it is broken.
Guido van Rossum <guido@python.org>
parents: 5835
diff changeset
72 while 1:
644484e1a37b Add Jeff Epler's interact() function. Note that it is broken.
Guido van Rossum <guido@python.org>
parents: 5835
diff changeset
73 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
74 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
75 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
76 except KeyboardInterrupt:
644484e1a37b Add Jeff Epler's interact() function. Note that it is broken.
Guido van Rossum <guido@python.org>
parents: 5835
diff changeset
77 print "\nKeyboardInterrupt"
644484e1a37b Add Jeff Epler's interact() function. Note that it is broken.
Guido van Rossum <guido@python.org>
parents: 5835
diff changeset
78 buf = []
644484e1a37b Add Jeff Epler's interact() function. Note that it is broken.
Guido van Rossum <guido@python.org>
parents: 5835
diff changeset
79 continue
644484e1a37b Add Jeff Epler's interact() function. Note that it is broken.
Guido van Rossum <guido@python.org>
parents: 5835
diff changeset
80 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
81 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
82 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
83 except SyntaxError:
644484e1a37b Add Jeff Epler's interact() function. Note that it is broken.
Guido van Rossum <guido@python.org>
parents: 5835
diff changeset
84 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
85 buf = []
644484e1a37b Add Jeff Epler's interact() function. Note that it is broken.
Guido van Rossum <guido@python.org>
parents: 5835
diff changeset
86 continue
644484e1a37b Add Jeff Epler's interact() function. Note that it is broken.
Guido van Rossum <guido@python.org>
parents: 5835
diff changeset
87 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
88 else:
644484e1a37b Add Jeff Epler's interact() function. Note that it is broken.
Guido van Rossum <guido@python.org>
parents: 5835
diff changeset
89 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
90 except:
644484e1a37b Add Jeff Epler's interact() function. Note that it is broken.
Guido van Rossum <guido@python.org>
parents: 5835
diff changeset
91 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
92 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
93 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
94 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
95 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
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 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
98 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
99 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
100 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
101 buf = []
644484e1a37b Add Jeff Epler's interact() function. Note that it is broken.
Guido van Rossum <guido@python.org>
parents: 5835
diff changeset
102
644484e1a37b Add Jeff Epler's interact() function. Note that it is broken.
Guido van Rossum <guido@python.org>
parents: 5835
diff changeset
103 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
104 interact()