polyfile icon indicating copy to clipboard operation
polyfile copied to clipboard

Python SyntaxError in auto-generated Kaitai parser due to reserved keyword 'class'

Open andrewmonostate opened this issue 4 months ago • 0 comments

Problem

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

Impact

This prevents polyfile from being imported in Python, breaking functionality for parsing OpenPGP messages.

Root Cause

The Kaitai Struct compiler generates Python code without escaping reserved keywords. This affects line 361, 369, 370, and 371 in openpgp_message.py.

Upstream Fix

A fix has been submitted to the Kaitai Struct compiler upstream:

Once that PR is merged and new parsers are generated, this issue will be resolved at the source.

Interim Solution

Until the upstream fix is available, I've prepared a PR 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 for reserved keywords).

Testing

The fix has been tested and 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

andrewmonostate avatar Aug 18 '25 04:08 andrewmonostate