Skip to content

Add MicroPython/CircuitPython compatibility layer#640

Open
gijzelaerr wants to merge 2 commits intomasterfrom
feature/micropython-compat
Open

Add MicroPython/CircuitPython compatibility layer#640
gijzelaerr wants to merge 2 commits intomasterfrom
feature/micropython-compat

Conversation

@gijzelaerr
Copy link
Owner

Summary

  • Add snap7/compat.py with runtime detection (MICROPYTHON flag), a functools.cache fallback for MicroPython, and a HAS_CTYPES availability flag
  • Update snap7/error.py to import cache from the new compat module instead of functools directly
  • Add tests/test_micropython_compat.py with 29 tests verifying that core protocol modules work using only struct/bytearray operations (no ctypes dependency)

Compatibility audit results

Module Status Notes
connection.py OK socket, struct, enum, logging -- all in MicroPython
s7protocol.py OK struct, enum, logging, datetime
datatypes.py OK struct, enum only
error.py Shimmed functools.cache replaced with compat shim
client.py Partial Core read/write works; async helpers need ctypes
type.py Blocked Heavy ctypes.Structure usage (BlocksList, S7CpuInfo, etc.)
util/getters.py OK struct, datetime only
util/setters.py OK struct, datetime, re only
util/db.py OK Pure Python data handling

What works on MicroPython today

The core protocol stack (connection.py, s7protocol.py, datatypes.py) and the utility getters/setters can run on MicroPython as-is. The snap7/compat.py module provides the MICROPYTHON flag and shims needed by error.py.

Known blockers for full MicroPython support

  1. type.py uses ctypes.Structure extensively -- needs pure-Python dataclass replacements for MicroPython
  2. client.py async helpers use ctypes.memmove and ctypes.Array -- core sync operations work fine
  3. importlib.metadata in __init__.py -- not available in MicroPython

Test plan

  • All 29 new compat tests pass
  • All 476 existing tests pass (48 skipped -- need PLC)
  • mypy clean (Success: no issues found in 29 source files)
  • ruff clean (All checks passed!)

Closes #624

🤖 Generated with Claude Code

gijzelaerr and others added 2 commits March 20, 2026 12:47
Create snap7/compat.py with runtime detection and shims:
- MICROPYTHON flag for runtime detection
- functools.cache fallback (no-op on MicroPython)
- HAS_CTYPES flag for conditional ctypes usage

Update snap7/error.py to use the compat cache shim instead of
importing functools.cache directly.

Add tests/test_micropython_compat.py verifying that core protocol
modules (connection, s7protocol, datatypes, util) work with only
struct/bytearray operations -- no ctypes dependency.

Closes #624

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@gijzelaerr gijzelaerr force-pushed the feature/micropython-compat branch from e018b2b to a7a8d74 Compare March 20, 2026 10:47
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.

MicroPython / CircuitPython compatibility

1 participant