10000 take over the new approach in code.py, from https://github.com/python… · pypy/pypy@1ebb72a · GitHub
[go: up one dir, main page]

Skip to content

Commit 1ebb72a

Browse files
committed
take over the new approach in code.py, from python/cpython#123062
this removes the extra keyword argument colorize in showtraceback and showsyntaxerror again, which should hopefully fix the problems in gh-5004.
1 parent f696301 commit 1ebb72a

File tree

3 files changed

+51
-102
lines changed

3 files changed

+51
-102
lines changed

extra_tests/test_pyrepl/test_functional.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ def test_sys_excepthook_is_broken():
5353
child.sendline("import sys")
5454
child.sendline("sys.excepthook = 1")
5555
child.sendline("1/0")
56-
child.expect('Error calling sys.excepthook.*object is not callable.*Traceback(.*)ZeroDivisionError: division by zero')
56+
child.expect('Error in sys.excepthook.*object is not callable.*Traceback(.*)ZeroDivisionError: division by zero')
5757
child.sendline('a = 10000000000')
5858
child.sendline('a * 5')
5959
child.expect('50000000000')

lib-python/3/code.py

Lines changed: 46 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -105,28 +105,21 @@ def showsyntaxerror(self, filename=None):
105105
The output is written by self.write(), below.
106106
107107
"""
108-
type, value, tb = sys.exc_info()
109-
sys.last_type = type
110-
sys.last_value = value
111-
sys.last_traceback = tb
112-
if filename and type is SyntaxError:
113-
# Work hard to stuff the correct filename in the exception
114-
try:
115-
msg, (dummy_filename, lineno, offset, line) = value.args
116-
except ValueError:
117-
# Not the format we expect; leave it alone
118-
pass
119-
else:
120-
# Stuff in the right filename
121-
value = SyntaxError(msg, (filename, lineno, offset, line))
122-
sys.last_value = value
123-
if sys.excepthook is sys.__excepthook__:
124-
lines = traceback.format_exception_only(type, value)
125-
self.write(''.join(lines))
126-
else:
127-
# If someone has set sys.excepthook, we let that take precedence
128-
# over self.write
129-
sys.excepthook(type, value, tb)
108+
try:
109+
typ, value, tb = sys.exc_info()
110+
if filename and typ is SyntaxError:
111+
# Work hard to stuff the correct filename in the exception
112+
try:
113+
msg, (dummy_filename, lineno, offset, line) = value.args
114+
except ValueError:
115+
# Not the format we expect; leave it alone
116+
pass
117+
else:
118+
# Stuff in the right filename
119+
value = SyntaxError(msg, (filename, lineno, offset, line))
120+
self._showtraceback(typ, value, None)
121+
finally:
122+
typ = value = tb = None
130123

131124
def showtraceback(self):
132125
"""Display the exception that just occurred.
@@ -136,18 +129,39 @@ def showtraceback(sel D7AE f):
136129
The output is written by self.write(), below.
137130
138131
"""
139-
sys.last_type, sys.last_value, last_tb = ei = sys.exc_info()
140-
sys.last_traceback = last_tb
141132
try:
142-
lines = traceback.format_exception(ei[0], ei[1], last_tb.tb_next)
143-
if sys.excepthook is sys.__excepthook__:
144-
self.write(''.join(lines))
145-
else:
146-
# If someone has set sys.excepthook, we let that take precedence
147-
# over self.write
148-
sys.excepthook(ei[0], ei[1], last_tb)
133+
typ, value, tb = sys.exc_info()
134+
self._showtraceback(typ, value, tb.tb_next)
149135
finally:
150-
last_tb = ei = None
136+
typ = value = tb = None
137+
138+
def _showtraceback(self, typ, value, tb, colorize=False, limit=None):
139+
# This method is being overwritten in
140+
# _pyrepl.console.InteractiveColoredConsole to pass different values of
141+
# colorize and limit
142+
sys.last_type = typ
143+
sys.last_traceback = tb
144+
sys.last_exc = sys.last_value = value = value.with_traceback(tb)
145+
if sys.excepthook is sys.__excepthook__:
146+
lines = traceback.format_exception(typ, value, tb,
147+
colorize=colorize,
148+
limit=limit)
149+
self.write(''.join(lines))
150+
else:
151+
# If someone has set sys.excepthook, we let that take precedence
152+
# over self.write
153+
try:
154+
sys.excepthook(typ, value, tb)
155+
except SystemExit:
156+
raise
157+
except BaseException as e:
158+
e.__context__ = None
159+
e = e.with_traceback(e.__traceback__.tb_next)
160+
print('Error in sys.excepthook:', file=sys.stderr)
161+
sys.__excepthook__(type(e), e, e.__traceback__)
162+
print(file=sys.stderr)
163+
print('Original exception was:', file=sys.stderr)
164+
sys.__excepthook__(typ, value, tb)
151165

152166
def write(self, data):
153167
"""Write a string.

lib_pypy/pyrepl/console.py

Lines changed: 4 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -161,76 +161,11 @@ def __init__(
161161
super().__init__(locals=locals, filename=filename) # type: ignore[call-arg]
162162
self.can_colorize = _colorize.can_colorize()
163163

164-
def showsyntaxerror(self, filename=None):
165-
"""Display the syntax error that just occurred.
166-
167-
This doesn't display a stack trace because there isn't one.
168-
169-
If a filename is given, it is stuffed in the exception instead
170-
of what was there before (because Python's parser always uses
171-
"<string>" when reading from a string).
172-
173-
The output is written by self.write(), below.
174-
175-
"""
176-
import traceback
177-
# pypy modification: rewrite this function to a) support positions and
178-
# b) pass self.can_colorize
179-
type, value, tb = sys.exc_info()
180-
sys.last_exc = value
181-
sys.last_type = type
182-
sys.last_value = value
183-
sys.last_traceback = tb
184-
if filename and type is SyntaxError:
185-
# Work hard to stuff the correct filename in the exception
186-
try:
187-
msg, (dummy_filename, lineno, offset, line) = value.args
188-
except ValueError:
189-
# Not the format we expect; leave it alone
190-
pass
191-
else:
192-
# Stuff in the right filename
193-
value = SyntaxError(msg, (filename, lineno, offset, line))
194-
sys.last_exc = sys.last_value = value
195-
if sys.excepthook is sys.__excepthook__:
196-
lines = traceback.format_exception_only(type, value, colorize=self.can_colorize)
197-
self.write(''.join(lines))
198-
else:
199-
self._call_excepthook(type, value, tb)
200-
201-
def showtraceback(self):
202-
"""Display the exception that just occurred.
203-
204-
We remove the first stack item because it is our own code.
205-
206-
The output is written by self.write(), below.
207-
208-
"""
209-
# pypy modification: rewrite this function to:
210-
# - support positions
211-
# - pass self.can_colorize
212-
# - don't crash with wrong sys.excepthook
213-
# - use the correct tracebacklimit
164+
def _showtraceback(self, typ, value, tb, colorize=False, limit=None):
214165
import traceback
215-
sys.last_type, sys.last_value, last_tb = ei = sys.exc_info()
216-
sys.last_traceback = last_tb
217-
try:
218-
if sys.excepthook is sys.__excepthook__:
219-
tb_exc = traceback.TracebackException(
220-
ei[0],
221-
ei[1],
222-
last_tb.tb_next,
223-
limit=traceback.BUILTIN_EXCEPTION_LIMIT,
224-
_frame_constructor=traceback._construct_positionful_frame
225-
)
226-
lines = tb_exc.format(colorize=self.can_colorize)
227-
self.write(''.join(lines))
228-
else:
229-
# If someone has set sys.excepthook, we let that take precedence
230-
# over self.write
231-
self._call_excepthook(ei[0], ei[1], last_tb)
232-
finally:
233-
last_tb = ei = None
166+
return super()._showtraceback(
167+
typ, value, tb, colorize=self.can_colorize,
168+
limit=traceback.BUILTIN_EXCEPTION_LIMIT)
234169

235170
def _call_excepthook(self, typ, value, tb):
236171
try:

0 commit comments

Comments
 (0)
0