Fix Python reserved keyword 'class' in auto-generated Kaitai parser
The auto-generated Kaitai Struct parser polyfile/kaitai/parsers/openpgp_message.py contains invalid Python syntax. It uses self.class = ... which causes a SyntaxError because class is a reserved keyword in Python.
Error
File "polyfile/kaitai/parsers/openpgp_message.py", line 370
self.class = self._io.read_u1()
^^^^^
SyntaxError: invalid syntax
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:
- Automatically patches the issue on import - The fix is applied transparently when polyfile is imported
- Includes a manual fix script (
fix_class_keyword.py) for development/debugging - Updates documentation with a Known Issues section
The fix replaces all occurrences of self.class with self.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 artifacts
Testing
The fix successfully:
- Replaces
self.classwithself.class_(line 370) - Updates
SEQ_FIELDSfrom"class"to"class_"(line 361) - Updates debug references from
['class']to['class_'](lines 369, 371) - Allows polyfile to be imported without errors
Note
Once the upstream fix in the Kaitai Struct compiler is merged and new parsers are generated, this workaround will no longer be necessary.
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.