Skip to content
Open
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
46 changes: 46 additions & 0 deletions Lib/test/test_uuid.py
Original file line number Diff line number Diff line change
Expand Up @@ -1223,6 +1223,29 @@ def test_cli_uuid3_ouputted_with_valid_namespace_and_name(self):
self.assertEqual(output, str(uuid_output))
self.assertEqual(uuid_output.version, 3)

@mock.patch.object(sys, "argv", ["", "-u", "uuid3", "-n", "any UUID", "-N", "python.org"])
@mock.patch('sys.stderr', new_callable=io.StringIO)
def test_cli_uuid3_with_invalid_namespace(self, mock_err):
with self.assertRaises(SystemExit) as cm:
self.uuid.main()
# Check that exception code is the same as argparse.ArgumentParser.error
self.assertEqual(cm.exception.code, 2)
self.assertIn("error: badly formed hexadecimal UUID string", mock_err.getvalue())

@mock.patch.object(sys, "argv",
["", "-u", "uuid3", "-n", "0d6a16cc-34a7-47d8-b660-214d0ae184d2", "-N", "some.user"])
def test_cli_uuid3_ouputted_with_user_provided_namespace_and_name(self):
stdout = io.StringIO()
with contextlib.redirect_stdout(stdout):
self.uuid.main()

output = stdout.getvalue().strip()
uuid_output = self.uuid.UUID(output)

# Output should be in the form of uuid5
self.assertEqual(output, str(uuid_output))
self.assertEqual(uuid_output.version, 3)

@mock.patch.object(sys, "argv",
["", "-u", "uuid5", "-n", "@dns", "-N", "python.org"])
def test_cli_uuid5_ouputted_with_valid_namespace_and_name(self):
Expand All @@ -1237,6 +1260,29 @@ def test_cli_uuid5_ouputted_with_valid_namespace_and_name(self):
self.assertEqual(output, str(uuid_output))
self.assertEqual(uuid_output.version, 5)

@mock.patch.object(sys, "argv", ["", "-u", "uuid5", "-n", "any UUID", "-N", "python.org"])
@mock.patch('sys.stderr', new_callable=io.StringIO)
def test_cli_uuid5_with_invalid_namespace(self, mock_err):
with self.assertRaises(SystemExit) as cm:
self.uuid.main()
# Check that exception code is the same as argparse.ArgumentParser.error
self.assertEqual(cm.exception.code, 2)
self.assertIn("error: badly formed hexadecimal UUID string", mock_err.getvalue())

@mock.patch.object(sys, "argv",
["", "-u", "uuid5", "-n", "0d6a16cc-34a7-47d8-b660-214d0ae184d2", "-N", "some.user"])
def test_cli_uuid5_ouputted_with_user_provided_namespace_and_name(self):
stdout = io.StringIO()
with contextlib.redirect_stdout(stdout):
self.uuid.main()

output = stdout.getvalue().strip()
uuid_output = self.uuid.UUID(output)

# Output should be in the form of uuid5
self.assertEqual(output, str(uuid_output))
self.assertEqual(uuid_output.version, 5)

@mock.patch.object(sys, "argv", ["", "-u", "uuid6"])
def test_cli_uuid6(self):
self.do_test_standalone_uuid(6)
Expand Down
12 changes: 10 additions & 2 deletions Lib/uuid.py
Original file line number Diff line number Diff line change
Expand Up @@ -962,7 +962,7 @@ def main():
default="uuid4",
help="function to generate the UUID")
parser.add_argument("-n", "--namespace",
choices=["any UUID", *namespaces.keys()],
metavar=f"{{any UUID,{','.join(namespaces.keys())}}}",
help="uuid3/uuid5 only: "
"a UUID, or a well-known predefined UUID addressed "
"by namespace name")
Expand All @@ -984,7 +984,15 @@ def main():
f"{args.uuid} requires a namespace and a name. "
"Run 'python -m uuid -h' for more information."
)
namespace = namespaces[namespace] if namespace in namespaces else UUID(namespace)
if namespace in namespaces:
namespace = namespaces[namespace]
else:
try:
namespace = UUID(namespace)
except ValueError as e:
parser.error(
f"{str(e)}: '{args.namespace}'."
)
for _ in range(args.count):
print(uuid_func(namespace, name))
else:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix usage for :mod:`uuid` command line interface to support a custom namespace be
provided for uuid3 and uuid5.
Loading