10000 Fix argument issue with coredumpy run by gaogaotiantian · Pull Request #81 · gaogaotiantian/coredumpy · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Fix argument issue with coredumpy run #81

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions 8 src/coredumpy/coredumpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -333,18 +333,18 @@ def peek(cls, path: str):
print(textwrap.indent(data["description"], " "))

@classmethod
def run(cls, options):
def run(cls, options, args):
if options.module:
file = options.module
target = _ModuleTarget(file)
else:
if not options.args:
if not args:
print("Error: no script specified")
sys.exit(1)
file = options.args.pop(0)
file = args.pop(0)
target = _ScriptTarget(file)

sys.argv[:] = [file] + options.args
sys.argv[:] = [file] + args

import __main__
__main__.__dict__.clear()
Expand Down
27 changes: 13 additions & 14 deletions src/coredumpy/main.py 8000
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ def main():
subparsers_run.add_argument("--path", help="The path of dump file", default=None)
subparsers_run.add_argument("--directory", help="The directory of dump file", default=None)
subparsers_run.add_argument("--conf", help="The startup configuration file to run", default=None)
subparsers_run.add_argument("args", nargs="*")

subparsers_load = subparsers.add_parser("load", help="Load a dump file.")
subparsers_load.add_argument("file", type=str, help="The dump file to load.")
Expand All @@ -31,19 +30,19 @@ def main():
subparsers_host = subparsers.add_parser("host", help="Host a DAP server.")
subparsers_host.add_argument("--conf", help="The startup configuration file to run", default=None)

args = parser.parse_args()
options, args = parser.parse_known_args()

if hasattr(args, "conf") and args.conf and os.path.exists(args.conf):
runpy.run_path(args.conf)
if hasattr(options, "conf") and options.conf and os.path.exists(options.conf):
runpy.run_path(options.conf)

if args.command == "load":
if os.path.exists(args.file):
debugger = "ipdb" if args.ipdb else "pdb"
load(args.file, debugger=debugger)
if options.command == "load":
if os.path.exists(options.file):
debugger = "ipdb" if options.ipdb else "pdb"
load(options.file, debugger=debugger)
else:
print(f"File {args.file} not found.")
elif args.command == "peek":
for file in args.files:
print(f"File {options.file} not found.")
elif options.command == "peek":
for file in options.files:
if os.path.exists(file):
if os.path.isdir(file):
for f in os.listdir(file):
Expand All @@ -59,7 +58,7 @@ def main():
pass
else:
print(f"File {file} not found.")
elif args.command == "run":
run(args)
elif args.command == "host":
elif options.command == "run":
run(options, args)
elif options.command == "host":
host()
20 changes: 20 additions & 0 deletions tests/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import io
import os
import tempfile
import textwrap

from .base import TestBase

Expand Down Expand Up @@ -280,6 +281,25 @@ def test_peek(self):
stdout, _ = self.run_peek([os.path.join(tmpdir, "nosuchfile")])
self.assertIn("not found", stdout)

def test_script_with_options(self):
# Test script with options
script = textwrap.dedent("""
import sys
print(sys.argv[1:])
""")
with tempfile.TemporaryDirectory() as tmpdir:
with open(f"{tmpdir}/script.py", "w", encoding="utf-8") as f:
f.write(script)

stdout, _ = self.run_run([f"{tmpdir}/script.py", "arg1", "arg2"])
self.assertIn("['arg1', 'arg2']", stdout)

stdout, _ = self.run_run([f"{tmpdir}/script.py", "--test", "-k"])
self.assertIn("['--test', '-k']", stdout)

stdout, _ = self.run_run([f"{tmpdir}/script.py", "--", "-m", "module", "--path", "path"])
self.assertIn("['--', '-m', 'module', '--path', 'path']", stdout)

def test_nonexist_file(self):
stdout, stderr = self.run_test("", "nonexist_dump", [])
self.assertIn("File nonexist_dump not found", stdout)
0