Skip to content

Commit b413780

Browse files
committed
Improve run_eval based on using.
1 parent f1af18d commit b413780

File tree

2 files changed

+46
-16
lines changed

2 files changed

+46
-16
lines changed

trepan/api.py

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# -*- coding: utf-8 -*-
22
#
3-
# Copyright (C) 2008-2009, 2013-2017, 2019-2021, 2023-2024 Rocky
3+
# Copyright (C) 2008-2009, 2013-2017, 2019-2021, 2023-2025 Rocky
44
# Bernstein <rocky@gnu.org>
55
#
66
# This program is free software: you can redistribute it and/or modify
@@ -36,14 +36,17 @@
3636

3737
import os
3838
import sys
39+
import traceback
3940
from typing import Callable, Literal, Optional
4041

4142
from trepan.debugger import Trepan, debugger_obj
4243
from trepan.interfaces.server import ServerInterface
44+
from trepan.lib.default import DEBUGGER_SETTINGS
4345
from trepan.post_mortem import post_mortem_excepthook, uncaught_exception
4446

4547
DEFAULT_DEBUG_PORT: Literal = 1955
4648

49+
4750
def debug(
4851
dbg_opts={},
4952
start_opts=None,
@@ -194,11 +197,20 @@ def debug_for_remote_access():
194197
"""Enter the debugger in a mode that allows connection to it
195198
outside of the process being debugged.
196199
"""
197-
connection_opts = {'IO': 'TCP', 'PORT': os.getenv('TREPAN3K_TCP_PORT', DEFAULT_DEBUG_PORT)}
200+
connection_opts = {
201+
"IO": "TCP",
202+
"PORT": os.getenv("TREPAN3K_TCP_PORT", DEFAULT_DEBUG_PORT),
203+
}
198204
intf = ServerInterface(connection_opts=connection_opts)
199-
dbg_opts = {'interface': intf}
200-
print(f'Starting {connection_opts["IO"]} server listening on {connection_opts["PORT"]}.', file=sys.stderr)
201-
print(f'Use `python3 -m trepan.client --port {connection_opts["PORT"]}` to enter debugger.', file=sys.stderr)
205+
dbg_opts = {"interface": intf}
206+
print(
207+
f'Starting {connection_opts["IO"]} server listening on {connection_opts["PORT"]}.',
208+
file=sys.stderr,
209+
)
210+
print(
211+
f'Use `python3 -m trepan.client --port {connection_opts["PORT"]}` to enter debugger.',
212+
file=sys.stderr,
213+
)
202214
debug(dbg_opts=dbg_opts, step_ignore=0, level=1)
203215

204216

@@ -210,7 +222,7 @@ def debugger_on_post_mortem():
210222

211223
def run_eval(
212224
expression,
213-
debug_opts: Optional[dict] = None,
225+
debug_opts: Optional[dict] = DEBUGGER_SETTINGS,
214226
start_opts: Optional[dict] = None,
215227
globals_: Optional[dict] = None,
216228
locals_: Optional[dict] = None,
@@ -235,7 +247,7 @@ def run_eval(
235247
dbg.core.trace_hook_suspend = True
236248
if start_opts and "tb_fn" in start_opts:
237249
tb_fn = start_opts["tb_fn"]
238-
uncaught_exception(dbg, tb_fn)
250+
traceback.print_exc()
239251
finally:
240252
dbg.core.trace_hook_suspend = False
241253
return
@@ -244,7 +256,7 @@ def run_eval(
244256
def run_call(
245257
func: Callable,
246258
*args,
247-
debug_opts: Optional[dict] = None,
259+
debug_opts: Optional[dict] = DEBUGGER_SETTINGS,
248260
start_opts: Optional[dict] = None,
249261
**kwds,
250262
):
@@ -265,7 +277,13 @@ def run_call(
265277
return
266278

267279

268-
def run_exec(statement, debug_opts=None, start_opts=None, globals_=None, locals_=None):
280+
def run_exec(
281+
statement,
282+
debug_opts: Optional[dict] = DEBUGGER_SETTINGS,
283+
start_opts=None,
284+
globals_=None,
285+
locals_=None,
286+
):
269287
"""Execute the statement (given as a string) under debugger
270288
control starting with the statement subsequent to the place that
271289
this run_call appears in your program.

trepan/debugger.py

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# -*- coding: utf-8 -*-
22
#
3-
# Copyright (C) 2008-2010, 2013-2015, 2018, 2023-2024
3+
# Copyright (C) 2008-2010, 2013-2015, 2018, 2023-2025
44
# Rocky Bernstein <rocky@gnu.org>
55
#
66
# This program is free software: you can redistribute it and/or modify
@@ -31,7 +31,7 @@
3131

3232
import sys
3333
import types
34-
from typing import Any, Callable
34+
from typing import Any, Callable, Union
3535

3636
import pyficache
3737
import tracer
@@ -99,10 +99,10 @@ def completer(text: str, state):
9999

100100
# How are I/O for this debugger handled? This should
101101
# be set before calling DebuggerCore.
102-
interface_opts = opts.get("interface_opts", {
103-
"debugger_name": "trepan3k",
104-
"readline": opts.get("readline")
105-
})
102+
interface_opts = opts.get(
103+
"interface_opts",
104+
{"debugger_name": "trepan3k", "readline": opts.get("readline")},
105+
)
106106
if "complete" not in interface_opts:
107107
interface_opts["complete"] = completer
108108

@@ -239,7 +239,13 @@ def run_call(self, func: Callable, *args, start_opts=None, **kwds):
239239
self.core.stop()
240240
return res
241241

242-
def run_eval(self, expr, start_opts=None, globals_=None, locals_=None):
242+
def run_eval(
243+
self,
244+
expr: Union[str, types.CodeType],
245+
start_opts=None,
246+
globals_=None,
247+
locals_=None,
248+
):
243249
"""Run debugger on string `expr' which will executed via the
244250
built-in Python function: eval; `globals_' and `locals_' are
245251
the dictionaries to use for local and global variables. If
@@ -250,6 +256,12 @@ def run_eval(self, expr, start_opts=None, globals_=None, locals_=None):
250256
See also `run_call' if what you to debug a function call and
251257
`run' if you want to debug general Python statements.
252258
"""
259+
if not isinstance(expr, (str, types.CodeType)):
260+
self.intf[0].errmsg(
261+
"You need to pass either a string or a code type."
262+
)
263+
return
264+
253265
if globals_ is None:
254266
globals_ = globals()
255267
if locals_ is None:

0 commit comments

Comments
 (0)