Skip to content

Conversation

andrewmonostate
Copy link

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:

  1. Automatically patches the issue on import - The fix is applied transparently when polyfile is imported
  2. Includes a manual fix script (fix_class_keyword.py) for development/debugging
  3. 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 function
  • polyfile/kaitai/parsers/openpgp_message.py: Auto-fixed by the import hook
  • fix_class_keyword.py: Standalone script for manual fixing if needed
  • README.md: Added Known Issues section documenting the problem and fix
  • .gitignore: Added *.egg-info/ to ignore build artifacts

Testing

The fix successfully:

  • Replaces self.class with self.class_ (line 370)
  • Updates SEQ_FIELDS from "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.

Problem: The auto-generated openpgp_message.py uses 'self.class = ...' which
causes SyntaxError since 'class' is a reserved keyword in Python.

Solution: Added automatic patching in polyfile/__init__.py that:
- Detects if openpgp_message.py uses the reserved keyword
- Automatically replaces self.class with self.class_ before module imports
- Runs silently without breaking imports if any issues occur
- Ensures the package works out-of-the-box without manual intervention

Changes:
- polyfile/__init__.py: Added _fix_class_keyword_if_needed() auto-fix
- polyfile/kaitai/parsers/openpgp_message.py: Auto-fixed by import hook
- fix_class_keyword.py: Standalone script for manual fixing if needed
- README.md: Added Known Issues section documenting the problem and fix
- .gitignore: Added *.egg-info/ to ignore build artifacts

Note: A fix has been submitted upstream to the Kaitai Struct compiler.
Once merged, this workaround will no longer be necessary.
@CLAassistant
Copy link

CLA assistant check
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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants