python3-pyaudio fails with: PY_SSIZE_T_CLEAN macro must be defined for '#' formats

Bug #1970912 reported by Nick Craig-Wood
14
This bug affects 2 people
Affects Status Importance Assigned to Milestone
python-pyaudio (Debian)
Fix Released
Unknown
python-pyaudio (Ubuntu)
Fix Released
Undecided
Unassigned
Jammy
New
Undecided
Unassigned
Kinetic
New
Undecided
Unassigned
Lunar
Fix Released
Undecided
Unassigned

Bug Description

I recently upgraded my system from 21.10 to 22.04 and a previous working python program stopped working.

It uses the python3-pyaudio package, and its initialisation fails like this:

    SystemError: PY_SSIZE_T_CLEAN macro must be defined for '#' formats

A bit of searching came up with this stack overflow answer: https://stackoverflow.com/a/70358664/164234

> Unfortunately, a breaking change was introduced in Python 3.10 (see bpo-40943 and PEP-0353), and thus PyAudio needs updated.
>
> 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 :)

The fix looks quite simple: https://git.skeh.site/skeh/pyaudio/commit/2ee560056ec889ea7cd3ce1801b796b0939dd540

So might be something that you could carry as a fix.

Tags: patch
Revision history for this message
Ronald J. Wright (logiconcepts819) wrote :

It seems this patch did not completely fix the problem. I was using nrsc5-gui from https://github.com/cmnybo/nrsc5-gui, and with the proposed fix, I encountered a segfault on a call to pyaudio.Stream.write(). This segfault appears to be related to incorrect initialization of memory due to the wrong type of argument being passed in to PyArg_ParseTuple() in _portaudiomodule.c:

   2037 /*************************************************************
   2038 * Stream Read/Write
   2039 *************************************************************/
   2040
   2041 static PyObject *pa_write_stream(PyObject *self, PyObject *args) {
   2042 const char *data;
   2043 int total_size; //<--- wrong type, should be Py_ssize_t, not int
   2044 int total_frames;
   2045 int err;
   2046 int should_throw_exception = 0;
   2047
   2048 PyObject *stream_arg;
   2049 _pyAudio_Stream *streamObject;
   2050
   2051 // clang-format off
   2052 if (!PyArg_ParseTuple(args, "O!s#i|i",
   2053 &_pyAudio_StreamType,
   2054 &stream_arg, //<--- incorrectly initialized
   2055 &data,
   2056 &total_size, //<--- total_size passed here
   2057 &total_frames,
   2058 &should_throw_exception)) {
   2059 return NULL;
   2060 }
   2061 // clang-format on
   2062
   2063 if (total_frames < 0) {
   2064 PyErr_SetString(PyExc_ValueError, "Invalid number of frames");
   2065 return NULL;
   2066 }
   2067
   2068 streamObject = (_pyAudio_Stream *)stream_arg;
   2069
   2070 if (!_is_open(streamObject)) { //<--- segfault here
   2071 PyErr_SetObject(PyExc_IOError,
   2072 Py_BuildValue("(i,s)", paBadStreamPtr, "Stream closed"));
   2073 return NULL;
   2074 }

After changing the type of the "total_size" variable from int to Py_ssize_t, the segfault no longer occurred, and the audio stream played normally.

A modified patch that fixes the segfault is attached.

Revision history for this message
Ubuntu Foundations Team Bug Bot (crichton) wrote :

The attachment "Modified patch for fixing the segfault introduced by the original fix" seems to be a patch. If it isn't, please remove the "patch" flag from the attachment, remove the "patch" tag, and if you are a member of the ~ubuntu-reviewers, unsubscribe the team.

[This is an automated message performed by a Launchpad user owned by ~brian-murray, for any issues please contact him.]

tags: added: patch
Changed in python-pyaudio (Debian):
status: Unknown → New
Changed in python-pyaudio (Debian):
status: New → Fix Released
Revision history for this message
Ronald J. Wright (logiconcepts819) wrote :

Looking at the official PyAudio Git repository (i.e., https://people.csail.mit.edu/hubert/git/pyaudio.git), the official fix appears to be in commit a0edea31bb2af47f54d85da0912133ac1ac180f9. The only difference I see compared to the fix I attached here is some additional minor refactoring; otherwise, it is functionally similar.

Revision history for this message
Launchpad Janitor (janitor) wrote :

Status changed to 'Confirmed' because the bug affects multiple users.

Changed in python-pyaudio (Ubuntu):
status: New → Confirmed
Revision history for this message
Andrea C G Mennucci (am70xk) wrote :

for the record: I download the Debian binary package from https://packages.debian.org/bookworm/amd64/python3-pyaudio/download and installed it, it works fine.

Revision history for this message
Stefano Rivera (stefanor) wrote :

To get pyaudio updated in Ubuntu 22.04, the next step is to prepare a stable release update: https://wiki.ubuntu.com/StableReleaseUpdates

I would sponsor the update, but in a few minutes of experimentation, I couldn't figure out how to reproduce the bug inside a container. If someone could complete the SRU Bug template, including minimal reproduction steps, that would be very helpful.

Changed in python-pyaudio (Ubuntu Lunar):
status: Confirmed → Fix Released
Revision history for this message
Antonio Bono (bnontn89) wrote (last edit ):

Hello everyone! I have the same problem with ubuntu 22.04 and python 3.10.

I am a beginner with python package management so I could use some help about how to fix it.

At the moment the only solution to me seems the one suggested by @Andrea C G Mennucci.

I tried to download the package but it is available only for python >=3.11. I need the fix for python 3.10 (default python version for ubu 22.04)

Do you have any other suggestions? Thanks

To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.