One function

String in construct/construct

The author described this change as String building from unicode string without encoding raises StringError. It counts as a record because the check below fails on the code as it stood at 0daf84869 and passes on b65413e5d, with nothing else changed between the two runs.

Fix saved2016-09-02
Sharing licenceMIT · LICENSE
Change size+5 0

What the code was meant to do, written into the code itself as a docstring

A configurable, variable-length string field. When parsing, the byte string is stripped of pad character (as specified) from the direction (as specified) then decoded (as specified). Length is a constant integer or a function of the context. When building, the string is encoded (as specified) then padded (as specified) from the direction (as specified) or trimmed as bytes (as specified). The padding character and direction must be specified for padding to work. The trim direction must be specified for trimming to work. :param name: name :param length: length in bytes (not unicode characters), as int or function :param encoding: encoding (e.g. "utf8") or None for bytes :param padchar: optional byte or unicode character to pad out strings :param paddir: direction to pad out strings (one of: right left both) :param trimdir: direction to trim strings (one of: right left) Example:: String("string", 5) .parse(b"hello") -> b"hello" String("string", 12, encoding="utf8") .parse(b"hello joh\xd4\x83n") -> u'hello joh\u0503n' String("string", 10, padchar="X", paddir="right") .parse(b"helloXXXXX") -> b"hello" .build(u"hello") -> b"helloXXXXX" String("string", 5, trimdir="right") .build(u"hello12345") -> b"hello"

The change

1818
1919 String("string", 5)
2020 .parse(b"hello") -> b"hello"
21+ .build(u"hello") raises StringError
2122
2223 String("string", 12, encoding="utf8")
2324 .parse(b"hello joh\xd4\x83n") -> u'hello joh\u0503n'
25+ .build(u'abc') -> b'abc\x00\x00\x00\x00\x00\x00\x00\x00\x00'
2426
2527 String("string", 10, padchar="X", paddir="right")
2628 .parse(b"helloXXXXX") -> b"hello"
7577 obj = obj.encode(self.encoding)
7678 else:
7779 obj = self.encoding.encode(obj)
80+ else:
81+ if not isinstance(obj, bytes):
82+ raise StringError("no encoding provided but building from unicode string?")
7883 if self.paddir == "right":
7984 obj = obj.ljust(length, padchar)
8085 elif self.paddir == "left":

The check that tells the two apart

failpass·tests/test_strings.py::TestString::test_build_missing_encoding

Check file tests/test_strings.py, taken without changes from the fix and copied onto the older code, so the exact same check runs against both versions.

Origin and history

The code before it0daf8486917420ee158ffc827dd40728426155cd
Broken version dated2016-09-02
Moduleconstruct.core
Units changedString
Fingerprint8f506552b62790b5
Checked2026-08-18 by goldset/0.1

Every field above is generated by our program. None of it is written by hand.

Other bugs found in construct/construct