Skip to content

Commit eb84290

Browse files
committed
Go over options processing
1 parent 520b0e1 commit eb84290

4 files changed

Lines changed: 135 additions & 104 deletions

File tree

test/unit/test_options.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
"""Unit test for trepan.options"""
2+
3+
from trepan.options import process_options
4+
5+
6+
def test_options():
7+
option_key_set = {
8+
"annotate",
9+
"basename",
10+
"cd",
11+
"client",
12+
"command",
13+
"confirm",
14+
"dbg_trepan",
15+
"different",
16+
"execute",
17+
"fntrace",
18+
"from_ipython",
19+
"highlight",
20+
"host",
21+
"linetrace",
22+
"main",
23+
"noexecute",
24+
"output",
25+
"port",
26+
"post_mortem",
27+
"private",
28+
"server",
29+
"sigcheck",
30+
"style",
31+
"target",
32+
}
33+
dbg_opts_set = {"proc_opts", "from_ipython"}
34+
35+
opts, dbg_opts, sys_argv = process_options("testing", "1.0", [__file__])
36+
diff_set = option_key_set - set(vars(opts).keys())
37+
assert diff_set == set(), "expecting at least these options keys set"
38+
assert (
39+
dbg_opts_set - set(dbg_opts.keys())
40+
) == set(), "expecting at least these processor keys set"
41+
42+
arg_str = f"{__file__} --fntrace --cd=/tmp"
43+
opts, dbg_opts, sys_argv = process_options("testing", "1.1", arg_str.split())
44+
assert opts.cd == "/tmp"
45+
46+
arg_str = f"{__file__} --style=emacs"
47+
opts, dbg_opts, sys_argv = process_options("testing", "1.2", arg_str.split())
48+
assert opts.style == "emacs"

trepan/__main__.py

Lines changed: 35 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env python3
22
# -*- coding: iso-8859-1 -*-
3-
# Copyright (C) 2008-2010, 2013-2018, 2020-2023 Rocky Bernstein
3+
# Copyright (C) 2008-2010, 2013-2018, 2020-2024 Rocky Bernstein
44
# <rocky@gnu.org>
55
#
66
# This program is free software: you can redistribute it and/or modify
@@ -24,57 +24,53 @@
2424

2525
import pyficache
2626

27-
package = "trepan"
27+
from trepan.client import run
28+
from trepan.clifns import whence_file
29+
from trepan.debugger import Trepan
30+
from trepan.exception import DebuggerQuit, DebuggerRestart
31+
from trepan.interfaces.server import ServerInterface
32+
from trepan.lib.file import is_compiled_py, readable
33+
from trepan.misc import wrapped_lines
34+
from trepan.options import postprocess_options, process_options
35+
from trepan.version import __version__
2836

29-
# Our local modules
30-
import trepan.client as Mclient
31-
import trepan.clifns as Mclifns
32-
import trepan.debugger as Mdebugger
33-
import trepan.exception as Mexcept
34-
import trepan.interfaces.server as Mserver
35-
import trepan.lib.file as Mfile
36-
import trepan.misc as Mmisc
37-
import trepan.options as Moptions
37+
package = "trepan"
3838

3939
# The name of the debugger we are currently going by.
4040
__title__ = package
4141

42-
from trepan.version import __version__
43-
4442

4543
def main(dbg=None, sys_argv=list(sys.argv)):
4644
"""Routine which gets run if we were invoked directly"""
4745
global __title__
4846

4947
# Save the original just for use in the restart that works via exec.
5048
orig_sys_argv = list(sys_argv)
51-
opts, dbg_opts, sys_argv = Moptions.process_options(
52-
__title__, __version__, sys_argv
53-
)
49+
opts, dbg_opts, sys_argv = process_options(__title__, __version__, sys_argv)
5450

5551
if opts.server is not None:
5652
if opts.server == "tcp":
5753
connection_opts = {"IO": "TCP", "PORT": opts.port}
5854
else:
5955
connection_opts = {"IO": "FIFO"}
60-
intf = Mserver.ServerInterface(connection_opts=connection_opts)
56+
intf = ServerInterface(connection_opts=connection_opts)
6157
dbg_opts["interface"] = intf
6258
if "FIFO" == intf.server_type:
63-
print("Starting FIFO server for process %s." % os.getpid())
59+
print(f"Starting FIFO server for process {os.getpid()}.")
6460
elif "TCP" == intf.server_type:
65-
print("Starting TCP server listening on port %s." % intf.inout.PORT)
61+
print(f"Starting TCP server listening on port {intf.inout.PORT}.")
6662
pass
6763
elif opts.client:
68-
Mclient.run(opts, sys_argv)
64+
run(opts, sys_argv)
6965
return
7066

7167
dbg_opts["orig_sys_argv"] = orig_sys_argv
7268

7369
if dbg is None:
74-
dbg = Mdebugger.Trepan(dbg_opts)
70+
dbg = Trepan(dbg_opts)
7571
dbg.core.add_ignore(main)
76-
pass
77-
Moptions._postprocess_options(dbg, opts)
72+
73+
postprocess_options(dbg, opts)
7874

7975
# process_options has munged sys.argv to remove any options that
8076
# options that belong to this debugger. The original options to
@@ -87,29 +83,17 @@ def main(dbg=None, sys_argv=list(sys.argv)):
8783
else:
8884
mainpyfile = sys_argv[0] # Get script filename.
8985
if not osp.isfile(mainpyfile):
90-
mainpyfile = Mclifns.whence_file(mainpyfile)
91-
is_readable = Mfile.readable(mainpyfile)
86+
mainpyfile = whence_file(mainpyfile)
87+
is_readable = readable(mainpyfile)
9288
if is_readable is None:
93-
print(
94-
"%s: Python script file '%s' does not exist"
95-
% (
96-
__title__,
97-
mainpyfile,
98-
)
99-
)
89+
print(f"{__title__}: Python script file '{mainpyfile}' does not exist")
10090
sys.exit(1)
10191
elif not is_readable:
102-
print(
103-
"%s: Can't read Python script file '%s'"
104-
% (
105-
__title__,
106-
mainpyfile,
107-
)
108-
)
92+
print(f"{__title__}: Can't read Python script file '{mainpyfile}'")
10993
sys.exit(1)
11094
return
11195

112-
if Mfile.is_compiled_py(mainpyfile):
96+
if is_compiled_py(mainpyfile):
11397
try:
11498
from xdis import IS_PYPY, PYTHON_VERSION_TRIPLE, load_module
11599
from xdis.version_info import version_tuple_to_str
@@ -124,7 +108,7 @@ def main(dbg=None, sys_argv=list(sys.argv)):
124108
sip_hash,
125109
) = load_module(mainpyfile, code_objects=None, fast_load=False)
126110
if is_pypy != IS_PYPY:
127-
print("Bytecode is pypy %s, but we are %s." % (is_pypy, IS_PYPY))
111+
print(f"Bytecode is pypy {is_pypy}, but we are {IS_PYPY}.")
128112
print("For a cross-version debugger, use trepan-xpy with x-python.")
129113
sys.exit(2)
130114
if python_version[:2] != PYTHON_VERSION_TRIPLE[:2]:
@@ -146,15 +130,15 @@ def main(dbg=None, sys_argv=list(sys.argv)):
146130
+ os.environ["PATH"].split(osp.pathsep)
147131
+ ["."]
148132
)
149-
try_file = Mclifns.whence_file(py_file, dirnames)
133+
try_file = whence_file(py_file, dirnames)
150134

151135
if osp.isfile(try_file):
152136
mainpyfile = try_file
153137
pass
154138
else:
155139
# Move onto the except branch
156140
raise IOError(
157-
"Python file name embedded in code %s not found" % try_file
141+
f"Python file name embedded in code {try_file} not found"
158142
)
159143
except IOError:
160144
decompiler = "uncompyle6"
@@ -199,7 +183,7 @@ def write_wrapper(*args, **kwargs):
199183
decompile_file(mainpyfile, fd.file, mapstream=fd)
200184
except Exception:
201185
print(
202-
"%s: error decompiling '%s'" % (__title__, mainpyfile),
186+
f"{__title__}: error decompiling '{mainpyfile}'",
203187
file=sys.stderr,
204188
)
205189
sys.exit(1)
@@ -230,15 +214,9 @@ def write_wrapper(*args, **kwargs):
230214
# If mainpyfile is an optimized Python script try to find and
231215
# use non-optimized alternative.
232216
mainpyfile_noopt = pyficache.resolve_name_to_path(mainpyfile)
233-
if mainpyfile != mainpyfile_noopt and Mfile.readable(mainpyfile_noopt):
234-
print("%s: Compiled Python script given and we can't use that." % __title__)
235-
print(
236-
"%s: Substituting non-compiled name: %s"
237-
% (
238-
__title__,
239-
mainpyfile_noopt,
240-
)
241-
)
217+
if mainpyfile != mainpyfile_noopt and readable(mainpyfile_noopt):
218+
print(f"{__title__}: Compiled Python script given and we can't use that.")
219+
print(f"{__title__}: Substituting non-compiled name: {mainpyfile_noopt}")
242220
mainpyfile = mainpyfile_noopt
243221
pass
244222

@@ -272,17 +250,15 @@ def write_wrapper(*args, **kwargs):
272250
dbg.core.execution_status = "Terminated"
273251
dbg.intf[-1].msg("The program finished - quit or restart")
274252
dbg.core.processor.process_commands()
275-
except Mexcept.DebuggerQuit:
253+
except DebuggerQuit:
276254
break
277-
except Mexcept.DebuggerRestart:
255+
except DebuggerRestart:
278256
dbg.core.execution_status = "Restart requested"
279257
if dbg.program_sys_argv:
280258
sys.argv = list(dbg.program_sys_argv)
281-
part1 = "Restarting %s with arguments:" % dbg.core.filename(mainpyfile)
259+
part1 = f"Restarting {dbg.core.filename(mainpyfile)} with arguments:"
282260
args = " ".join(dbg.program_sys_argv[1:])
283-
dbg.intf[-1].msg(
284-
Mmisc.wrapped_lines(part1, args, dbg.settings["width"])
285-
)
261+
dbg.intf[-1].msg(wrapped_lines(part1, args, dbg.settings["width"]))
286262
else:
287263
break
288264
except SystemExit:
@@ -298,4 +274,3 @@ def write_wrapper(*args, **kwargs):
298274
# When invoked as main program, invoke the debugger on a script
299275
if __name__ == "__main__":
300276
main()
301-
pass

trepan/bwcli.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ def process_options(debugger_name, pkg_version, sys_argv, option_list=None):
8989
return opts, dbg_opts, sys.argv
9090

9191

92-
def _postprocess_options(dbg, opts):
92+
def postprocess_options(dbg, opts):
9393
"""Handle options (`opts') that feed into the debugger (`dbg')"""
9494
# Set dbg.settings['printset']
9595
print_events = []
@@ -128,7 +128,7 @@ def main(dbg=None, sys_argv=list(sys.argv)):
128128
dbg = Mdebugger.Trepan(dbg_opts)
129129
dbg.core.add_ignore(main)
130130
pass
131-
_postprocess_options(dbg, opts)
131+
postprocess_options(dbg, opts)
132132

133133
# process_options has munged sys.argv to remove any options that
134134
# options that belong to this debugger. The original options to
@@ -191,7 +191,6 @@ def main(dbg=None, sys_argv=list(sys.argv)):
191191
# pass
192192

193193
while True:
194-
195194
# Run the debugged script over and over again until we get it
196195
# right.
197196

0 commit comments

Comments
 (0)