Fix Python reserved keyword 'class' in auto-generated Kaitai parser #3432
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The auto-generated Kaitai Struct parser
polyfile/kaitai/parsers/openpgp_message.py
contains invalid Python syntax. It usesself.class = ...
which causes aSyntaxError
becauseclass
is a reserved keyword in Python.Error
Root Cause
The Kaitai Struct compiler generates Python code without escaping reserved keywords. This affects lines 361, 369, 370, and 371 in
openpgp_message.py
.Upstream Fix Status
A fix has been submitted to the Kaitai Struct compiler upstream to address the root cause:
Solution
This PR provides an interim fix that:
fix_class_keyword.py
) for development/debuggingThe fix replaces all occurrences of
self.class
withself.class_
(following PEP 8 conventions).Changes
polyfile/__init__.py
: Added_fix_class_keyword_if_needed()
auto-fix functionpolyfile/kaitai/parsers/openpgp_message.py
: Auto-fixed by the import hookfix_class_keyword.py
: Standalone script for manual fixing if neededREADME.md
: Added Known Issues section documenting the problem and fix.gitignore
: Added*.egg-info/
to ignore build artifactsTesting
The fix successfully:
self.class
withself.class_
(line 370)SEQ_FIELDS
from"class"
to"class_"
(line 361)['class']
to['class_']
(lines 369, 371)Note
Once the upstream fix in the Kaitai Struct compiler is merged and new parsers are generated, this workaround will no longer be necessary.