summaryrefslogtreecommitdiff
path: root/python
diff options
context:
space:
mode:
authorRobert Collins <robertc@robertcollins.net>2013-08-25 12:40:00 +1200
committerRobert Collins <robertc@robertcollins.net>2013-08-25 12:40:00 +1200
commiteb3444332823e1bb726ce61b17939d99a757c643 (patch)
tree5478acd3702a04d47f6d8f63ac177de433a67403 /python
parentf407275f50750b08d38b681b3889d96720ba4f78 (diff)
downloadsubunit-git-eb3444332823e1bb726ce61b17939d99a757c643.tar.gz
* Python 3.1 and 3.2 have an inconsistent memoryview implementation which
required a workaround for NUL byte detection. (Robert Collins, #1216246)
Diffstat (limited to 'python')
-rw-r--r--python/subunit/v2.py20
1 files changed, 19 insertions, 1 deletions
diff --git a/python/subunit/v2.py b/python/subunit/v2.py
index b4e1e38..057f65c 100644
--- a/python/subunit/v2.py
+++ b/python/subunit/v2.py
@@ -50,6 +50,24 @@ FLAG_EOF = 0x0010
FLAG_FILE_CONTENT = 0x0040
EPOCH = datetime.datetime.utcfromtimestamp(0).replace(tzinfo=iso8601.Utc())
NUL_ELEMENT = b'\0'[0]
+# Contains True for types for which 'nul in thing' falsely returns false.
+_nul_test_broken = {}
+
+
+def has_nul(buffer_or_bytes):
+ """Return True if a null byte is present in buffer_or_bytes."""
+ # Simple "if NUL_ELEMENT in utf8_bytes:" fails on Python 3.1 and 3.2 with
+ # memoryviews. See https://bugs.launchpad.net/subunit/+bug/1216246
+ buffer_type = type(buffer_or_bytes)
+ broken = _nul_test_broken.get(buffer_type)
+ if broken is None:
+ reference = buffer_type(b'\0')
+ broken = not NUL_ELEMENT in reference
+ _nul_test_broken[buffer_type] = broken
+ if broken:
+ return b'\0' in buffer_or_bytes
+ else:
+ return NUL_ELEMENT in buffer_or_bytes
class ParseError(Exception):
@@ -462,7 +480,7 @@ class ByteStreamToStreamResult(object):
'UTF8 string at offset %d extends past end of packet: '
'claimed %d bytes, %d available' % (pos - 2, length,
len(utf8_bytes)))
- if NUL_ELEMENT in utf8_bytes:
+ if has_nul(utf8_bytes):
raise ParseError('UTF8 string at offset %d contains NUL byte' % (
pos-2,))
try: