Skip to content

Python: Encode text before passing it to speak_to_memory - #163

Merged
ethindp merged 1 commit into
ethindp:masterfrom
miguel-cinsfran:python-speak-to-memory-encoding
Oct 22, 2026
Merged

ethindp merged 1 commit into
ethindp:masterfrom
miguel-cinsfran:python-speak-to-memory-encoding

Conversation

@miguel-cinsfran

Copy link
Copy Markdown
Contributor

Backend.speak_to_memory in the Python bindings passes text to prism_backend_speak_to_memory as a str. cffi needs bytes for a const char *, so every call fails before it reaches the backend:

TypeError: initializer for ctype 'char *' must be a bytes or list or tuple, not str

speak, braille and output already call text.encode("utf-8"). This does the same.

Tested on Windows 11 (x64) with Python 3.11 and the prismatoid 0.18.3 wheel, whose bindings are the same as master's. With SAPI and OneCore, both raise the TypeError before the change. After it, both deliver 16 kHz mono audio to the callback, and the rate and voice set on the backend apply to it.

The regression test in tests/python/ registers a custom backend, so it doesn't need a speech engine. It fails on master with the same TypeError and passes with this change. I didn't find an existing place for Python tests, so the folder is new; if you'd rather have it somewhere else, I'll move it. Ruff flags pytest's assert as S101, so pyproject.toml ignores that rule under tests/. CI doesn't seem to run pytest, so I ran it locally. ruff check . passes.

Backend.speak_to_memory passed the str straight to cffi, which needs
bytes for a const char *, so every call raised TypeError before it
reached the backend. speak, braille and output already encode to UTF-8.

The test uses a custom backend so it runs without a speech engine.
Ruff flags pytest's assert as S101, so that rule is ignored under
tests/.

Signed-off-by: Miguel Ángel Insfrán <[email protected]>
@ethindp

ethindp commented Oct 16, 2026

Copy link
Copy Markdown
Owner

Thank you for this! We don't run pytest since we don't really have any tests (since tests for Prism is rather difficult to do even with custom backends). If you want, feel free to have pytest run given that this regression test now exists.

@miguel-cinsfran

Copy link
Copy Markdown
Contributor Author

Thanks! I'd rather keep this PR to the encoding fix only. I can open a separate PR that adds a pytest step to CI so the regression test runs. Is this one okay to merge as it is?

@ethindp

ethindp commented Oct 22, 2026

Copy link
Copy Markdown
Owner

@miguel-cinsfran I would prefer that this include a regression test, as to avoid an unnecessary PR. However, if you truly wish not to include one, then yes, this PR is ready for merge.

@ethindp
ethindp merged commit 89d2a19 into ethindp:master Oct 22, 2026
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