Author mjpieters
Recipients mjpieters
Date 2017-11-22.10:52:36
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1511347956.68.0.213398074469.issue32112@psf.upfronthosting.co.za>
In-reply-to
Content
When someone accidentally passes in an existing uuid.UUID() instance into uuid.UUID(), an attribute error is thrown because it is not a hex string:

>>> import uuid
>>> value = uuid.uuid4()
>>> uuid.UUID(value)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/mjpieters/Development/Library/buildout.python/parts/opt/lib/python2.7/uuid.py", line 133, in __init__
    hex = hex.replace('urn:', '').replace('uuid:', '')
AttributeError: 'UUID' object has no attribute 'replace'

This happened in the Stack Overflow question at https://stackoverflow.com/q/47429929/100297, because the code there didn't take into account that some database drivers may already have mapped the PostgreSQL UUID column to a Python uuid.UUID() object.

The fix could be as simple as:

    if hex is not None:
        if isinstance(hex, uuid.UUID):
            int = hex.int
        else:
            hex = hex.replace('urn:', '').replace('uuid:', '')
            hex = hex.strip('{}').replace('-', '')
            if len(hex) != 32:
                raise ValueError('badly formed hexadecimal UUID string')
            int = int_(hex, 16)

Or we could add a uuid=None keyword argument, and use 

    if hex is not None:
        if isinstance(hex, uuid.UUID):
            uuid = hex
        else:
            hex = hex.replace('urn:', '').replace('uuid:', '')
            hex = hex.strip('{}').replace('-', '')
            if len(hex) != 32:
                raise ValueError('badly formed hexadecimal UUID string')
            int = int_(hex, 16)
    if uuid is not None:
        int = uuid.int
History
Date User Action Args
2017-11-22 10:52:36mjpieterssetrecipients: + mjpieters
2017-11-22 10:52:36mjpieterssetmessageid: <1511347956.68.0.213398074469.issue32112@psf.upfronthosting.co.za>
2017-11-22 10:52:36mjpieterslinkissue32112 messages
2017-11-22 10:52:36mjpieterscreate