annotate Lib/code.py @ 87690:66c1c9f32567 default tip

Add use_weakref option to functools.lru_cache The use_weakref option makes lru_cache create weak references to the function arguments when creating the cache key, which prevents them from staying in the memory forever even after all other references to them are gone. This is a proof of concept for http://bugs.python.org/issue19859
author Radomir Dopieralski <devel@sheep.art.pl>
date Tue, 03 Dec 2013 16:02:48 +0100
parents 2933598a7802
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
9441
913eb31b0317 Moved compile_command() to a file of its own (codeop.py).
Guido van Rossum <guido@python.org>
parents: 9405
diff changeset
1 """Utilities needed to emulate Python's interactive interpreter.
9137
0329d419e506 Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents: 8543
diff changeset
2
0329d419e506 Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents: 8543
diff changeset
3 """
5835
b9063f6f811c Checking in new module code.py -- utilities dealing with code objects.
Guido van Rossum <guido@python.org>
parents:
diff changeset
4
11870
d7eca5b0960a Simple changes by Gerrit Holl - move author acknowledgements out of
Guido van Rossum <guido@python.org>
parents: 9441
diff changeset
5 # Inspired by similar code by Jeff Epler and Fredrik Lundh.
d7eca5b0960a Simple changes by Gerrit Holl - move author acknowledgements out of
Guido van Rossum <guido@python.org>
parents: 9441
diff changeset
6
9441
913eb31b0317 Moved compile_command() to a file of its own (codeop.py).
Guido van Rossum <guido@python.org>
parents: 9405
diff changeset
7
8543
e951bf2d7d93 Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
8 import sys
e951bf2d7d93 Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
9 import traceback
19355
d5acff178ad5 A fiddled version of the rest of Michael Hudson's SF patch
Tim Peters <tim.peters@gmail.com>
parents: 17666
diff changeset
10 from codeop import CommandCompiler, compile_command
6423
644484e1a37b Add Jeff Epler's interact() function. Note that it is broken.
Guido van Rossum <guido@python.org>
parents: 5835
diff changeset
11
19355
d5acff178ad5 A fiddled version of the rest of Michael Hudson's SF patch
Tim Peters <tim.peters@gmail.com>
parents: 17666
diff changeset
12 __all__ = ["InteractiveInterpreter", "InteractiveConsole", "interact",
16070
b5345e99596b added __all__ lists to a number of Python modules
Skip Montanaro <skip@pobox.com>
parents: 15887
diff changeset
13 "compile_command"]
b5345e99596b added __all__ lists to a number of Python modules
Skip Montanaro <skip@pobox.com>
parents: 15887
diff changeset
14
9137
0329d419e506 Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents: 8543
diff changeset
15 class InteractiveInterpreter:
0329d419e506 Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents: 8543
diff changeset
16 """Base class for InteractiveConsole.
8543
e951bf2d7d93 Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
17
9137
0329d419e506 Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents: 8543
diff changeset
18 This class deals with parsing and interpreter state (the user's
0329d419e506 Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents: 8543
diff changeset
19 namespace); it doesn't deal with input buffering or prompting or
0329d419e506 Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents: 8543
diff changeset
20 input file naming (the filename is always passed in explicitly).
0329d419e506 Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents: 8543
diff changeset
21
8543
e951bf2d7d93 Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
22 """
e951bf2d7d93 Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
23
9137
0329d419e506 Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents: 8543
diff changeset
24 def __init__(self, locals=None):
8543
e951bf2d7d93 Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
25 """Constructor.
e951bf2d7d93 Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
26
9137
0329d419e506 Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents: 8543
diff changeset
27 The optional 'locals' argument specifies the dictionary in
0329d419e506 Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents: 8543
diff changeset
28 which code will be executed; it defaults to a newly created
0329d419e506 Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents: 8543
diff changeset
29 dictionary with key "__name__" set to "__console__" and key
0329d419e506 Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents: 8543
diff changeset
30 "__doc__" set to None.
0329d419e506 Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents: 8543
diff changeset
31
0329d419e506 Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents: 8543
diff changeset
32 """
0329d419e506 Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents: 8543
diff changeset
33 if locals is None:
0329d419e506 Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents: 8543
diff changeset
34 locals = {"__name__": "__console__", "__doc__": None}
0329d419e506 Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents: 8543
diff changeset
35 self.locals = locals
19355
d5acff178ad5 A fiddled version of the rest of Michael Hudson's SF patch
Tim Peters <tim.peters@gmail.com>
parents: 17666
diff changeset
36 self.compile = CommandCompiler()
9137
0329d419e506 Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents: 8543
diff changeset
37
0329d419e506 Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents: 8543
diff changeset
38 def runsource(self, source, filename="<input>", symbol="single"):
0329d419e506 Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents: 8543
diff changeset
39 """Compile and run some source in the interpreter.
0329d419e506 Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents: 8543
diff changeset
40
0329d419e506 Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents: 8543
diff changeset
41 Arguments are as for compile_command().
0329d419e506 Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents: 8543
diff changeset
42
0329d419e506 Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents: 8543
diff changeset
43 One several things can happen:
0329d419e506 Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents: 8543
diff changeset
44
0329d419e506 Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents: 8543
diff changeset
45 1) The input is incorrect; compile_command() raised an
0329d419e506 Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents: 8543
diff changeset
46 exception (SyntaxError or OverflowError). A syntax traceback
0329d419e506 Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents: 8543
diff changeset
47 will be printed by calling the showsyntaxerror() method.
0329d419e506 Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents: 8543
diff changeset
48
0329d419e506 Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents: 8543
diff changeset
49 2) The input is incomplete, and more input is required;
0329d419e506 Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents: 8543
diff changeset
50 compile_command() returned None. Nothing happens.
0329d419e506 Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents: 8543
diff changeset
51
0329d419e506 Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents: 8543
diff changeset
52 3) The input is complete; compile_command() returned a code
0329d419e506 Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents: 8543
diff changeset
53 object. The code is executed by calling self.runcode() (which
0329d419e506 Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents: 8543
diff changeset
54 also handles run-time exceptions, except for SystemExit).
0329d419e506 Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents: 8543
diff changeset
55
22830
a9dd5e6d6805 Convert a pile of obvious "yes/no" functions to return bool.
Tim Peters <tim.peters@gmail.com>
parents: 22566
diff changeset
56 The return value is True in case 2, False in the other cases (unless
9137
0329d419e506 Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents: 8543
diff changeset
57 an exception is raised). The return value can be used to
0329d419e506 Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents: 8543
diff changeset
58 decide whether to use sys.ps1 or sys.ps2 to prompt the next
0329d419e506 Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents: 8543
diff changeset
59 line.
0329d419e506 Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents: 8543
diff changeset
60
0329d419e506 Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents: 8543
diff changeset
61 """
0329d419e506 Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents: 8543
diff changeset
62 try:
19355
d5acff178ad5 A fiddled version of the rest of Michael Hudson's SF patch
Tim Peters <tim.peters@gmail.com>
parents: 17666
diff changeset
63 code = self.compile(source, filename, symbol)
15887
f83115edf16a mwh@sourceforge found that UnicodeError can be raised by compiling.
Guido van Rossum <guido@python.org>
parents: 15863
diff changeset
64 except (OverflowError, SyntaxError, ValueError):
9137
0329d419e506 Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents: 8543
diff changeset
65 # Case 1
0329d419e506 Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents: 8543
diff changeset
66 self.showsyntaxerror(filename)
22830
a9dd5e6d6805 Convert a pile of obvious "yes/no" functions to return bool.
Tim Peters <tim.peters@gmail.com>
parents: 22566
diff changeset
67 return False
9137
0329d419e506 Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents: 8543
diff changeset
68
0329d419e506 Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents: 8543
diff changeset
69 if code is None:
0329d419e506 Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents: 8543
diff changeset
70 # Case 2
22830
a9dd5e6d6805 Convert a pile of obvious "yes/no" functions to return bool.
Tim Peters <tim.peters@gmail.com>
parents: 22566
diff changeset
71 return True
9137
0329d419e506 Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents: 8543
diff changeset
72
0329d419e506 Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents: 8543
diff changeset
73 # Case 3
0329d419e506 Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents: 8543
diff changeset
74 self.runcode(code)
22830
a9dd5e6d6805 Convert a pile of obvious "yes/no" functions to return bool.
Tim Peters <tim.peters@gmail.com>
parents: 22566
diff changeset
75 return False
9137
0329d419e506 Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents: 8543
diff changeset
76
0329d419e506 Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents: 8543
diff changeset
77 def runcode(self, code):
0329d419e506 Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents: 8543
diff changeset
78 """Execute a code object.
0329d419e506 Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents: 8543
diff changeset
79
0329d419e506 Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents: 8543
diff changeset
80 When an exception occurs, self.showtraceback() is called to
0329d419e506 Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents: 8543
diff changeset
81 display a traceback. All exceptions are caught except
0329d419e506 Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents: 8543
diff changeset
82 SystemExit, which is reraised.
0329d419e506 Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents: 8543
diff changeset
83
0329d419e506 Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents: 8543
diff changeset
84 A note about KeyboardInterrupt: this exception may occur
0329d419e506 Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents: 8543
diff changeset
85 elsewhere in this code, and may not always be caught. The
0329d419e506 Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents: 8543
diff changeset
86 caller should be prepared to deal with it.
8543
e951bf2d7d93 Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
87
e951bf2d7d93 Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
88 """
9137
0329d419e506 Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents: 8543
diff changeset
89 try:
39782
c94f37054f19 Patch #1550800: make exec a function.
Georg Brandl <georg@python.org>
parents: 37045
diff changeset
90 exec(code, self.locals)
9137
0329d419e506 Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents: 8543
diff changeset
91 except SystemExit:
0329d419e506 Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents: 8543
diff changeset
92 raise
0329d419e506 Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents: 8543
diff changeset
93 except:
0329d419e506 Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents: 8543
diff changeset
94 self.showtraceback()
0329d419e506 Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents: 8543
diff changeset
95
0329d419e506 Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents: 8543
diff changeset
96 def showsyntaxerror(self, filename=None):
0329d419e506 Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents: 8543
diff changeset
97 """Display the syntax error that just occurred.
0329d419e506 Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents: 8543
diff changeset
98
0329d419e506 Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents: 8543
diff changeset
99 This doesn't display a stack trace because there isn't one.
0329d419e506 Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents: 8543
diff changeset
100
0329d419e506 Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents: 8543
diff changeset
101 If a filename is given, it is stuffed in the exception instead
0329d419e506 Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents: 8543
diff changeset
102 of what was there before (because Python's parser always uses
0329d419e506 Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents: 8543
diff changeset
103 "<string>" when reading from a string).
0329d419e506 Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents: 8543
diff changeset
104
0329d419e506 Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents: 8543
diff changeset
105 The output is written by self.write(), below.
0329d419e506 Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents: 8543
diff changeset
106
0329d419e506 Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents: 8543
diff changeset
107 """
78672
a812de69b493 Issue #12643: Respect sys.excepthook in code.InteractiveConsole
Nick Coghlan <ncoghlan@gmail.com>
parents: 57450
diff changeset
108 type, value, tb = sys.exc_info()
9137
0329d419e506 Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents: 8543
diff changeset
109 sys.last_type = type
0329d419e506 Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents: 8543
diff changeset
110 sys.last_value = value
78672
a812de69b493 Issue #12643: Respect sys.excepthook in code.InteractiveConsole
Nick Coghlan <ncoghlan@gmail.com>
parents: 57450
diff changeset
111 sys.last_traceback = tb
9137
0329d419e506 Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents: 8543
diff changeset
112 if filename and type is SyntaxError:
0329d419e506 Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents: 8543
diff changeset
113 # Work hard to stuff the correct filename in the exception
0329d419e506 Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents: 8543
diff changeset
114 try:
42455
dd74e020a132 Merged revisions 56492-56752 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40799
diff changeset
115 msg, (dummy_filename, lineno, offset, line) = value.args
dd74e020a132 Merged revisions 56492-56752 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40799
diff changeset
116 except ValueError:
9137
0329d419e506 Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents: 8543
diff changeset
117 # Not the format we expect; leave it alone
0329d419e506 Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents: 8543
diff changeset
118 pass
0329d419e506 Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents: 8543
diff changeset
119 else:
0329d419e506 Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents: 8543
diff changeset
120 # Stuff in the right filename
22565
906ad36cb7bc remove unqualified excepts - catch ImportError when trying to import
Skip Montanaro <skip@pobox.com>
parents: 19959
diff changeset
121 value = SyntaxError(msg, (filename, lineno, offset, line))
17666
12c80b570d00 InteractiveInterpreter.showsyntaxerror():
Fred Drake <fdrake@acm.org>
parents: 16495
diff changeset
122 sys.last_value = value
78672
a812de69b493 Issue #12643: Respect sys.excepthook in code.InteractiveConsole
Nick Coghlan <ncoghlan@gmail.com>
parents: 57450
diff changeset
123 if sys.excepthook is sys.__excepthook__:
a812de69b493 Issue #12643: Respect sys.excepthook in code.InteractiveConsole
Nick Coghlan <ncoghlan@gmail.com>
parents: 57450
diff changeset
124 lines = traceback.format_exception_only(type, value)
a812de69b493 Issue #12643: Respect sys.excepthook in code.InteractiveConsole
Nick Coghlan <ncoghlan@gmail.com>
parents: 57450
diff changeset
125 self.write(''.join(lines))
a812de69b493 Issue #12643: Respect sys.excepthook in code.InteractiveConsole
Nick Coghlan <ncoghlan@gmail.com>
parents: 57450
diff changeset
126 else:
a812de69b493 Issue #12643: Respect sys.excepthook in code.InteractiveConsole
Nick Coghlan <ncoghlan@gmail.com>
parents: 57450
diff changeset
127 # If someone has set sys.excepthook, we let that take precedence
a812de69b493 Issue #12643: Respect sys.excepthook in code.InteractiveConsole
Nick Coghlan <ncoghlan@gmail.com>
parents: 57450
diff changeset
128 # over self.write
a812de69b493 Issue #12643: Respect sys.excepthook in code.InteractiveConsole
Nick Coghlan <ncoghlan@gmail.com>
parents: 57450
diff changeset
129 sys.excepthook(type, value, tb)
9137
0329d419e506 Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents: 8543
diff changeset
130
0329d419e506 Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents: 8543
diff changeset
131 def showtraceback(self):
0329d419e506 Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents: 8543
diff changeset
132 """Display the exception that just occurred.
0329d419e506 Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents: 8543
diff changeset
133
0329d419e506 Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents: 8543
diff changeset
134 We remove the first stack item because it is our own code.
0329d419e506 Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents: 8543
diff changeset
135
0329d419e506 Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents: 8543
diff changeset
136 The output is written by self.write(), below.
0329d419e506 Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents: 8543
diff changeset
137
0329d419e506 Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents: 8543
diff changeset
138 """
0329d419e506 Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents: 8543
diff changeset
139 try:
0329d419e506 Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents: 8543
diff changeset
140 type, value, tb = sys.exc_info()
0329d419e506 Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents: 8543
diff changeset
141 sys.last_type = type
0329d419e506 Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents: 8543
diff changeset
142 sys.last_value = value
0329d419e506 Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents: 8543
diff changeset
143 sys.last_traceback = tb
0329d419e506 Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents: 8543
diff changeset
144 tblist = traceback.extract_tb(tb)
0329d419e506 Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents: 8543
diff changeset
145 del tblist[:1]
42455
dd74e020a132 Merged revisions 56492-56752 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40799
diff changeset
146 lines = traceback.format_list(tblist)
dd74e020a132 Merged revisions 56492-56752 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40799
diff changeset
147 if lines:
dd74e020a132 Merged revisions 56492-56752 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40799
diff changeset
148 lines.insert(0, "Traceback (most recent call last):\n")
dd74e020a132 Merged revisions 56492-56752 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40799
diff changeset
149 lines.extend(traceback.format_exception_only(type, value))
9137
0329d419e506 Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents: 8543
diff changeset
150 finally:
0329d419e506 Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents: 8543
diff changeset
151 tblist = tb = None
78672
a812de69b493 Issue #12643: Respect sys.excepthook in code.InteractiveConsole
Nick Coghlan <ncoghlan@gmail.com>
parents: 57450
diff changeset
152 if sys.excepthook is sys.__excepthook__:
a812de69b493 Issue #12643: Respect sys.excepthook in code.InteractiveConsole
Nick Coghlan <ncoghlan@gmail.com>
parents: 57450
diff changeset
153 self.write(''.join(lines))
a812de69b493 Issue #12643: Respect sys.excepthook in code.InteractiveConsole
Nick Coghlan <ncoghlan@gmail.com>
parents: 57450
diff changeset
154 else:
a812de69b493 Issue #12643: Respect sys.excepthook in code.InteractiveConsole
Nick Coghlan <ncoghlan@gmail.com>
parents: 57450
diff changeset
155 # If someone has set sys.excepthook, we let that take precedence
a812de69b493 Issue #12643: Respect sys.excepthook in code.InteractiveConsole
Nick Coghlan <ncoghlan@gmail.com>
parents: 57450
diff changeset
156 # over self.write
a812de69b493 Issue #12643: Respect sys.excepthook in code.InteractiveConsole
Nick Coghlan <ncoghlan@gmail.com>
parents: 57450
diff changeset
157 sys.excepthook(type, value, tb)
9137
0329d419e506 Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents: 8543
diff changeset
158
0329d419e506 Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents: 8543
diff changeset
159 def write(self, data):
0329d419e506 Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents: 8543
diff changeset
160 """Write a string.
0329d419e506 Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents: 8543
diff changeset
161
0329d419e506 Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents: 8543
diff changeset
162 The base implementation writes to sys.stderr; a subclass may
0329d419e506 Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents: 8543
diff changeset
163 replace this with a different implementation.
0329d419e506 Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents: 8543
diff changeset
164
0329d419e506 Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents: 8543
diff changeset
165 """
0329d419e506 Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents: 8543
diff changeset
166 sys.stderr.write(data)
0329d419e506 Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents: 8543
diff changeset
167
0329d419e506 Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents: 8543
diff changeset
168
0329d419e506 Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents: 8543
diff changeset
169 class InteractiveConsole(InteractiveInterpreter):
0329d419e506 Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents: 8543
diff changeset
170 """Closely emulate the behavior of the interactive Python interpreter.
0329d419e506 Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents: 8543
diff changeset
171
0329d419e506 Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents: 8543
diff changeset
172 This class builds on InteractiveInterpreter and adds prompting
0329d419e506 Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents: 8543
diff changeset
173 using the familiar sys.ps1 and sys.ps2, and input buffering.
0329d419e506 Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents: 8543
diff changeset
174
0329d419e506 Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents: 8543
diff changeset
175 """
0329d419e506 Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents: 8543
diff changeset
176
0329d419e506 Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents: 8543
diff changeset
177 def __init__(self, locals=None, filename="<console>"):
0329d419e506 Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents: 8543
diff changeset
178 """Constructor.
0329d419e506 Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents: 8543
diff changeset
179
0329d419e506 Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents: 8543
diff changeset
180 The optional locals argument will be passed to the
0329d419e506 Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents: 8543
diff changeset
181 InteractiveInterpreter base class.
0329d419e506 Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents: 8543
diff changeset
182
0329d419e506 Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents: 8543
diff changeset
183 The optional filename argument should specify the (file)name
0329d419e506 Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents: 8543
diff changeset
184 of the input stream; it will show up in tracebacks.
0329d419e506 Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents: 8543
diff changeset
185
0329d419e506 Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents: 8543
diff changeset
186 """
0329d419e506 Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents: 8543
diff changeset
187 InteractiveInterpreter.__init__(self, locals)
8543
e951bf2d7d93 Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
188 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
189 self.resetbuffer()
e951bf2d7d93 Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
190
e951bf2d7d93 Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
191 def resetbuffer(self):
9137
0329d419e506 Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents: 8543
diff changeset
192 """Reset the input buffer."""
8543
e951bf2d7d93 Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
193 self.buffer = []
e951bf2d7d93 Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
194
e951bf2d7d93 Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
195 def interact(self, banner=None):
9137
0329d419e506 Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents: 8543
diff changeset
196 """Closely emulate the interactive Python console.
0329d419e506 Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents: 8543
diff changeset
197
42455
dd74e020a132 Merged revisions 56492-56752 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40799
diff changeset
198 The optional banner argument specifies the banner to print
9137
0329d419e506 Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents: 8543
diff changeset
199 before the first interaction; by default it prints a banner
0329d419e506 Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents: 8543
diff changeset
200 similar to the one printed by the real Python interpreter,
0329d419e506 Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents: 8543
diff changeset
201 followed by the current class name in parentheses (so as not
0329d419e506 Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents: 8543
diff changeset
202 to confuse this with the real interpreter -- since it's so
0329d419e506 Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents: 8543
diff changeset
203 close!).
0329d419e506 Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents: 8543
diff changeset
204
0329d419e506 Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents: 8543
diff changeset
205 """
8543
e951bf2d7d93 Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
206 try:
e951bf2d7d93 Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
207 sys.ps1
e951bf2d7d93 Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
208 except AttributeError:
e951bf2d7d93 Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
209 sys.ps1 = ">>> "
e951bf2d7d93 Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
210 try:
e951bf2d7d93 Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
211 sys.ps2
e951bf2d7d93 Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
212 except AttributeError:
e951bf2d7d93 Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
213 sys.ps2 = "... "
22566
39e42d6f5da2 make default banner match what the real interpreter displays
Skip Montanaro <skip@pobox.com>
parents: 22565
diff changeset
214 cprt = 'Type "help", "copyright", "credits" or "license" for more information.'
8543
e951bf2d7d93 Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
215 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
216 self.write("Python %s on %s\n%s\n(%s)\n" %
15863
71b19d36aafa Make the copyright message the same as for the "real" interpreter.
Guido van Rossum <guido@python.org>
parents: 15847
diff changeset
217 (sys.version, sys.platform, cprt,
8543
e951bf2d7d93 Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
218 self.__class__.__name__))
86331
2933598a7802 Closes #17730: in code.interact(), when banner="", do not print anything.
Georg Brandl <georg@python.org>
parents: 84442
diff changeset
219 elif banner:
8543
e951bf2d7d93 Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
220 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
221 more = 0
e951bf2d7d93 Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
222 while 1:
e951bf2d7d93 Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
223 try:
e951bf2d7d93 Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
224 if more:
e951bf2d7d93 Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
225 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
226 else:
e951bf2d7d93 Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
227 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
228 try:
e951bf2d7d93 Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
229 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
230 except EOFError:
e951bf2d7d93 Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
231 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
232 break
e951bf2d7d93 Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
233 else:
e951bf2d7d93 Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
234 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
235 except KeyboardInterrupt:
e951bf2d7d93 Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
236 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
237 self.resetbuffer()
e951bf2d7d93 Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
238 more = 0
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 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
241 """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
242
9137
0329d419e506 Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents: 8543
diff changeset
243 The line should not have a trailing newline; it may have
0329d419e506 Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents: 8543
diff changeset
244 internal newlines. The line is appended to a buffer and the
0329d419e506 Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents: 8543
diff changeset
245 interpreter's runsource() method is called with the
0329d419e506 Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents: 8543
diff changeset
246 concatenated contents of the buffer as source. If this
0329d419e506 Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents: 8543
diff changeset
247 indicates that the command was executed or invalid, the buffer
0329d419e506 Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents: 8543
diff changeset
248 is reset; otherwise, the command is incomplete, and the buffer
0329d419e506 Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents: 8543
diff changeset
249 is left as it was after the line was appended. The return
0329d419e506 Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents: 8543
diff changeset
250 value is 1 if more input is required, 0 if the line was dealt
0329d419e506 Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents: 8543
diff changeset
251 with in some way (this is the same as runsource()).
8543
e951bf2d7d93 Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
252
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 self.buffer.append(line)
16495
69f162dcd8b4 String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents: 16070
diff changeset
255 source = "\n".join(self.buffer)
9137
0329d419e506 Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents: 8543
diff changeset
256 more = self.runsource(source, self.filename)
0329d419e506 Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents: 8543
diff changeset
257 if not more:
8543
e951bf2d7d93 Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
258 self.resetbuffer()
9137
0329d419e506 Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents: 8543
diff changeset
259 return more
8543
e951bf2d7d93 Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
260
e951bf2d7d93 Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
261 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
262 """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
263
e951bf2d7d93 Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
264 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
265 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
266
44421
f0e9c3d16766 Fix issue #1707. When raw_input() was removed, it was incorrectly replaced
Guido van Rossum <guido@python.org>
parents: 42455
diff changeset
267 The base implementation uses the built-in function
f0e9c3d16766 Fix issue #1707. When raw_input() was removed, it was incorrectly replaced
Guido van Rossum <guido@python.org>
parents: 42455
diff changeset
268 input(); a subclass may replace this with a different
f0e9c3d16766 Fix issue #1707. When raw_input() was removed, it was incorrectly replaced
Guido van Rossum <guido@python.org>
parents: 42455
diff changeset
269 implementation.
8543
e951bf2d7d93 Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
270
e951bf2d7d93 Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
271 """
44421
f0e9c3d16766 Fix issue #1707. When raw_input() was removed, it was incorrectly replaced
Guido van Rossum <guido@python.org>
parents: 42455
diff changeset
272 return input(prompt)
37045
081b4f0e81b7 Get rid of a bunch more raw_input references
Neal Norwitz <nnorwitz@gmail.com>
parents: 27878
diff changeset
273
8543
e951bf2d7d93 Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
274
e951bf2d7d93 Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
275
9405
3886165dba42 Rename 'locals' argument to top-level interact() function to 'local'
Guido van Rossum <guido@python.org>
parents: 9137
diff changeset
276 def interact(banner=None, readfunc=None, local=None):
8543
e951bf2d7d93 Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
277 """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
278
e951bf2d7d93 Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
279 This is a backwards compatible interface to the InteractiveConsole
9137
0329d419e506 Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents: 8543
diff changeset
280 class. When readfunc is not specified, it attempts to import the
0329d419e506 Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents: 8543
diff changeset
281 readline module to enable GNU readline if it is available.
8543
e951bf2d7d93 Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
282
e951bf2d7d93 Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
283 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
284
e951bf2d7d93 Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
285 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
286 readfunc -- if not None, replaces InteractiveConsole.raw_input()
9405
3886165dba42 Rename 'locals' argument to top-level interact() function to 'local'
Guido van Rossum <guido@python.org>
parents: 9137
diff changeset
287 local -- passed to InteractiveInterpreter.__init__()
8543
e951bf2d7d93 Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
288
e951bf2d7d93 Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
289 """
9405
3886165dba42 Rename 'locals' argument to top-level interact() function to 'local'
Guido van Rossum <guido@python.org>
parents: 9137
diff changeset
290 console = InteractiveConsole(local)
8543
e951bf2d7d93 Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
291 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
292 console.raw_input = readfunc
9137
0329d419e506 Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents: 8543
diff changeset
293 else:
0329d419e506 Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents: 8543
diff changeset
294 try:
0329d419e506 Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents: 8543
diff changeset
295 import readline
84442
7769c4d72806 Issue #18200: Back out usage of ModuleNotFoundError (8d28d44f3a9a)
Brett Cannon <brett@python.org>
parents: 84114
diff changeset
296 except ImportError:
9137
0329d419e506 Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents: 8543
diff changeset
297 pass
8543
e951bf2d7d93 Subsume the interact() function in a class. This should make it
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
298 console.interact(banner)
9137
0329d419e506 Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents: 8543
diff changeset
299
0329d419e506 Reworked it quite a bit. There are now two classes: a base class,
Guido van Rossum <guido@python.org>
parents: 8543
diff changeset
300
57450
0db67153d25f Merged revisions 75264,75268,75293,75318,75391-75392,75436,75478,75971,76003,76058,76140-76141,76231,76380,76428-76429 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 44421
diff changeset
301 if __name__ == "__main__":
0db67153d25f Merged revisions 75264,75268,75293,75318,75391-75392,75436,75478,75971,76003,76058,76140-76141,76231,76380,76428-76429 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 44421
diff changeset
302 interact()