Skip to content

Commit

Permalink
Fix determinstic UUID handling when there are high-codepoint
Browse files Browse the repository at this point in the history
unicode characters.  Make compatible with both python 2 and 3.
  • Loading branch information
chisholm committed Oct 11, 2019
1 parent 401c9ad commit 216b43d
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions stix2/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -394,11 +394,14 @@ def _generate_id(self, kwargs):
if streamlined_obj_vals:
data = canonicalize(streamlined_obj_vals, utf8=False)

# try/except here to enable python 2 compatibility
try:
# The situation is complicated w.r.t. python 2/3 behavior, so
# 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))
except UnicodeDecodeError:
return required_prefix + six.text_type(uuid.uuid5(SCO_DET_ID_NAMESPACE, six.binary_type(data)))
else:
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
return None
Expand Down

0 comments on commit 216b43d

Please sign in to comment.