One function
Struct in construct/construct
The author described this change as “Struct sizeof proper context nesting (bug #266)”. It counts as a record because the check below fails on the code as it stood at 3c9410d68 and passes on 647e59590, with nothing else changed between the two runs.
Projectconstruct/construct
Fix saved2017-04-04
Sharing licenceMIT · LICENSE
Change size+10 −13
What the code was meant to do, written into the code itself as a docstring
A sequence of usually named constructs, similar to structs in C. The elements are parsed and built in the order they are defined. Some fields do not need to be named, since they are built from None anyway. See Const Padding Pass Terminated. .. seealso:: Can be nested easily, and embedded using :func:`~construct.core.Embedded` wrapper that merges members into parent's members. :param subcons: a sequence of subconstructs that make up this structure Example:: Note that this syntax works ONLY on python 3.6 due to unordered keyword arguments:
The change
| 67 | 67 | return context | |
| 68 | 68 | def _sizeof(self, context, path): | |
| 69 | 69 | try: | |
| 70 | - | # def isStruct(sc): | |
| 71 | - | # return isStruct(sc.subcon) if isinstance(sc, Renamed) else isinstance(sc.subcon, Struct) | |
| 72 | - | # def nest(context, sc): | |
| 73 | - | # if isStruct(sc) and sc.name in context: | |
| 74 | - | # # if isinstance(sc, Renamed) and isinstance(sc.subcon, Struct) and sc.name in context: | |
| 75 | - | # # if isinstance(sc, Struct) and sc.name in context: | |
| 76 | - | # context2 = context[sc.name] | |
| 77 | - | # context2._ = context | |
| 78 | - | # return context2 | |
| 79 | - | # else: | |
| 80 | - | # return Container() | |
| 81 | - | # return sum(sc._sizeof(nest(context, sc), path) for sc in self.subcons) | |
| 82 | - | return sum(sc._sizeof(context, path) for sc in self.subcons) | |
| 70 | + | def isStruct(sc): | |
| 71 | + | return isStruct(sc.subcon) if isinstance(sc, Renamed) else isinstance(sc, Struct) | |
| 72 | + | def nest(context, sc): | |
| 73 | + | if isStruct(sc) and not sc.flagembedded and sc.name in context: | |
| 74 | + | context2 = context[sc.name] | |
| 75 | + | context2["_"] = context | |
| 76 | + | return context2 | |
| 77 | + | else: | |
| 78 | + | return context | |
| 79 | + | return sum(sc._sizeof(nest(context, sc), path) for sc in self.subcons) | |
| 83 | 80 | except (KeyError, AttributeError): | |
| 84 | 81 | raise SizeofError("cannot calculate size, key not found in context") |
The check that tells the two apart
fail→pass·tests/test_all.py::TestCore::test_struct_sizeof_context_nesting
Check file tests/test_all.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 it3c9410d6825a151b745a6abe80b3a27407883db5
Broken version dated2017-04-04
Moduleconstruct.core
Units changedStruct
Fingerprint93c7fce26879099f
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
- 2022-09-18Lazy
- 2018-05-05Fix issue #709: Hex() might not always work.
- 2018-04-02VarInt
- 2018-03-03TransformData
- 2017-10-08FIX: Left-shift operator (#406)
- 2017-08-29Switch