Skip to content

Conversation

@biplavbarua
Copy link

Port Spitfire to Python 3

Description

This PR ports the entire spitfire project to Python 3. It addresses syntax changes, standard library reorganization, unicode/bytes handling, and C extension APIs.

Changes

Vendored Libraries

  • third_party/yapps2:
    • Converted print statements to functions.
    • Updated exception handling syntax (except E as v).
    • Removed deprecated string module functions and apply().
    • Replaced StringIO imports with io.

Core Spitfire

  • Syntax:
    • Updated print(), exec(), raise.
    • Replaced basestring with str.
    • Replaced unicode() with str().
    • Updated dictionary iteration (iteritems -> items).
  • Logic:
    • optimizer.py: Fixed iterator mutation bug by creating list copies of dictionary items during iteration.
    • util.py / codegen.py: Updated file opening modes to rb/wb to correctly handle binary data/encoding, ensuring strict bytes/str separation compliance.
    • Added missing import os in optimizer.py.

Scripts

  • scripts/spitfire-compile: Updated for Python 3 I/O and exception handling.
  • scripts/crunner.py:
    • Replaced cStringIO with io.
    • Added project root to sys.path.
    • Fixed output comparison logic (bytes vs str).

C Extensions

  • _udn.c: Ported to Python 3 C API (module initialization, PyUnicode instead of PyString).
  • _template.c: Ported to Python 3 C API.
  • _baked.c: Disabled in setup.py. This extension relies deeply on Python 2 PyStringObject internals. The Python fallback (spitfire/runtime/baked.py) is fully functional and used instead.

Tests

  • Templates: Updated tests/test-hoist-conditional-loop-optimization-3.txt to use $range() instead of $xrange().
  • Expected Outputs: Updated test data in tests/output/ to match Python 3 exception messages and repr() output (e.g., removing u prefix).

Verification

Ran make test which executes unittest discovery and spitfire-compile integration tests.

  • Result: All 124 tests passed.
  • Optimization Levels: Verified at default, -O1, -O2, and -O3.
  • Whitespace Mode: Verified --preserve-optional-whitespace tests.

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.

1 participant