Fix determinstic UUID handling when there are high-codepoint

unicode characters.  Make compatible with both python 2 and 3.
master
Michael Chisholm 2019-10-11 17:12:44 -04:00
parent 401c9ad950
commit 216b43d49e
1 changed files with 7 additions and 4 deletions

View File

@ -394,11 +394,14 @@ class _Observable(_STIXBase):
if streamlined_obj_vals: if streamlined_obj_vals:
data = canonicalize(streamlined_obj_vals, utf8=False) data = canonicalize(streamlined_obj_vals, utf8=False)
# try/except here to enable python 2 compatibility # The situation is complicated w.r.t. python 2/3 behavior, so
try: # I'd rather not rely on particular exceptions being raised to
# determine what to do. Better to just check the python version
# directly.
if six.PY3:
return required_prefix + six.text_type(uuid.uuid5(SCO_DET_ID_NAMESPACE, data)) return required_prefix + six.text_type(uuid.uuid5(SCO_DET_ID_NAMESPACE, data))
except UnicodeDecodeError: else:
return required_prefix + six.text_type(uuid.uuid5(SCO_DET_ID_NAMESPACE, six.binary_type(data))) return required_prefix + six.text_type(uuid.uuid5(SCO_DET_ID_NAMESPACE, data.encode("utf-8")))
# We return None if there are no values specified for any of the id-contributing-properties # We return None if there are no values specified for any of the id-contributing-properties
return None return None