pydub icon indicating copy to clipboard operation
pydub copied to clipboard

SyntaxWarnings for invalid escape sequences in regular expressions

Open k-brahma opened this issue 1 year ago • 4 comments

SyntaxWarnings for invalid escape sequences in regular expressions

Description

When running tests or using the library with Python 3.12, several SyntaxWarnings may occur due to invalid escape sequences in regular expressions in the pydub/utils.py file.

Warnings

The following warnings are displayed:

pydub/utils.py:300: SyntaxWarning: invalid escape sequence '\('
  m = re.match('([su]([0-9]{1,2})p?) \(([0-9]{1,2}) bit\)$', token)
pydub/utils.py:301: SyntaxWarning: invalid escape sequence '\('
  m2 = re.match('([su]([0-9]{1,2})p?)( \(default\))?$', token)
pydub/utils.py:310: SyntaxWarning: invalid escape sequence '\('
  elif re.match('(flt)p?( \(default\))?$', token):
pydub/utils.py:314: SyntaxWarning: invalid escape sequence '\('
  elif re.match('(dbl)p?( \(default\))?$', token):

Affected code

The warnings appear in the get_audio_properties function in pydub/utils.py.

Possible solution

Adding an r prefix to the regular expression patterns to treat them as raw strings could resolve these warnings:

m = re.match(r'([su]([0-9]{1,2})p?) \(([0-9]{1,2}) bit\)$', token)
m2 = re.match(r'([su]([0-9]{1,2})p?)( \(default\))?$', token)
elif re.match(r'(flt)p?( \(default\))?$', token):
elif re.match(r'(dbl)p?( \(default\))?$', token):

So finally all the lines in for structure wold be:

    for token in extra_info[stream['index']]:
        m = re.match(r'([su]([0-9]{1,2})p?) \(([0-9]{1,2}) bit\)$', token)
        m2 = re.match(r'([su]([0-9]{1,2})p?)( \(default\))?$', token)
        if m:
            set_property(stream, 'sample_fmt', m.group(1))
            set_property(stream, 'bits_per_sample', int(m.group(2)))
            set_property(stream, 'bits_per_raw_sample', int(m.group(3)))
        elif m2:
            set_property(stream, 'sample_fmt', m2.group(1))
            set_property(stream, 'bits_per_sample', int(m2.group(2)))
            set_property(stream, 'bits_per_raw_sample', int(m2.group(2)))
        elif re.match(r'(flt)p?( \(default\))?$', token):
            set_property(stream, 'sample_fmt', token)
            set_property(stream, 'bits_per_sample', 32)
            set_property(stream, 'bits_per_raw_sample', 32)
        elif re.match(r'(dbl)p?( \(default\))?$', token):
            set_property(stream, 'sample_fmt', token)
            set_property(stream, 'bits_per_sample', 64)
            set_property(stream, 'bits_per_raw_sample', 64)
    return info

Additional information

  • Python version: 3.12.5
  • pydub version: pydub==0.25.1

It seems these warnings don't affect the current functionality but may become errors in future Python versions. Addressing them would improve code quality and ensure future compatibility.

k-brahma avatar Aug 22 '24 08:08 k-brahma

have the same problem. I got this issue in manim 0.19.0 with pydub 0.25.1 on Windows 11 and Python 3.13.6

BCircleTech avatar Aug 10 '25 13:08 BCircleTech

have the same problem. I got this issue in manim 0.19.0 with pydub 0.25.1 on Windows 11 and Python 3.13.6

I got this warning only at the first time i installed manim and used it. then the warning disappeared in the following using.

BCircleTech avatar Aug 10 '25 13:08 BCircleTech

same issue here

lennardkorte avatar Sep 03 '25 19:09 lennardkorte

I fixed the warning issue using pozalabs-pydub==0.37.0

pydub package is not maintained anymore since 2021

lennardkorte avatar Sep 03 '25 20:09 lennardkorte