Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PyAudio.write SystemError: PY_SSIZE_T_CLEAN macro must be defined for '#' formats

When I was trying to play a wave file with PyAudio-0.2.11 in a python-3.10.1 virtualenv(created with python-3.9.0 venv and upgraded), I received an exception and nothing is played, others just go well.

Exception in thread WavPlayer:rsrc/sound.wav:
Traceback (most recent call last):
  File "/usr/lib/python3.10/threading.py", line 1009, in _bootstrap_inner
    self.run()
  File "/Code/py/tetris/tetris.py", line 39, in run
    self.play()
  File "/Code/py/tetris/tetris.py", line 35, in play
    self.stream.write(data)
  File "/Code/py/tetris/.venv/lib/python3.10/site-packages/pyaudio.py", line 584, in write
    pa.write_stream(self._stream, frames, num_frames,
SystemError: PY_SSIZE_T_CLEAN macro must be defined for '#' formats

I do not know what '#' format stands for, and I am sure that my code has nothing to do with C API, but it run well under python-3.9.x, is that a problem with the PyAudio library itself? How can I solve that?

like image 316
gynamics Avatar asked Nov 16 '25 03:11

gynamics


2 Answers

Unfortunately, a breaking change was introduced in Python 3.10 (see bpo-40943 and PEP-0353), and thus PyAudio needs updated.

EDIT: This has been fixed upstream since PyAudio 0.2.12! Leaving the interim below for posterity, but a simple version bump is all that is needed now.


I've submitted a fix upstream, but as the project has not been updated since 2017, I'm not quite sure how quick it will get reviewed.

In the meantime, you are free to build my fork from source and use that :)

like image 63
skeh Avatar answered Nov 18 '25 16:11

skeh


Necroposting.

As @skeh indicated, starting with Python 3.10 some (backward compatible) functionality was disabled on purpose. That means that some extension modules will no longer work OOTB.
Check [GitHub]: python/cpython - PEP 353: Drop support for PyArg_ParseTuple() "#" formats when PY_SSIZE_T_CLEAN is not defined for more details.

PyAudio (v0.2.11) is one such module.
Things are fixed in v0.2.12 (which was released in the meantime).

Python 3.11 .whls were not published on [PyPI]: PyAudio 0.2.12 - Download files at answer time (they were added afterwards).
I built and placed them at [GitHub]: CristiFati/Prebuilt-Binaries - (master) Prebuilt-Binaries/PyAudio/v0.2.12.

Also forked the (original) PyAudio repo at: [GitHub]: CristiFati/pyaudio - PyAudio

Similar problem on another package: [SO]: Can't install pyo using pip (@CristiFati's answer).

like image 39
CristiFati Avatar answered Nov 18 '25 18:11

CristiFati



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!