What happens when an invalid argument is fed to uuid.UUID()?

exception, python, uuid

Solution

The `UUID()` constructor either raises a `TypeError` or a `ValueError`, depending on what was passed in.

Not passing in any of the `hex`, `bytes`, `bytes_le`, `fields`, or `int` options raises a `TypeError`, passing in a value that is invalid raises a `ValueError`:

>>> uuid.UUID()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/uuid.py", line 129, in __init__
    raise TypeError('need one of hex, bytes, bytes_le, fields, or int')
TypeError: need one of hex, bytes, bytes_le, fields, or int
>>> uuid.UUID('abcd')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/uuid.py", line 134, in __init__
    raise ValueError('badly formed hexadecimal UUID string')
ValueError: badly formed hexadecimal UUID string
>>> uuid.UUID(bytes='abcd')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/uuid.py", line 144, in __init__
    raise ValueError('bytes is not a 16-char string')
ValueError: bytes is not a 16-char string

etc.

It will not fail silently. It'll certainly never return `None`. Either `myUUID` is set to a `UUID` instance, or an exception is raised.

Problem

Will an exception get thrown? Does UUID() ever silently fail? Is there ANY circumstance in which 'myStatus' from ``` myStatus = True myUUID = uuid.UUID( someWeirdValue ) if myUUID == None: myStatus = False ``` would equal False?

Original source