Skip to content

Commit

Permalink
Prevent the registration of duplicate subparser
Browse files Browse the repository at this point in the history
- Addition of a duplicate subparser will result in an exception under
  Python 3.11

Fixes: #64
  • Loading branch information
metatoaster committed Mar 2, 2023
1 parent 92e5da9 commit c6309a9
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
8 changes: 8 additions & 0 deletions src/calmjs/runtime.py
Expand Up @@ -487,6 +487,14 @@ def to_module_attr(ep):
return '%s:%s' % (ep.module_name, '.'.join(ep.attrs))

def register(name, runtime, entry_point):
if name in subparsers:
logger.critical(
"another subparser was already registered as %r; "
"aborting the registration of its associated runtime",
name
)
return

subparser = commands.add_parser(
name, help=inst.description,
)
Expand Down
7 changes: 6 additions & 1 deletion src/calmjs/tests/test_runtime.py
Expand Up @@ -1883,9 +1883,14 @@ def cleanup():

# EXPLOSION
msg = stderr.getvalue()
self.assertIn("CRITICAL", msg)
self.assertIn(
"Runtime instance has been used or initialized improperly.", msg)
# The actual duplicated runtime may be non-deterministic as the
# order of which the registration are run that will result in
# the duplicate is derived from an unordered set.
self.assertIn(
"CRITICAL calmjs.runtime another subparser was already registered "
"as", msg)
# Naisu Bakuretsu - Megumin.

@unittest.skipIf(currentframe() is None, 'stack frame not supported')
Expand Down

0 comments on commit c6309a9

Please sign in to comment.