SyntaxWarnings for invalid escape sequences in regular expressions
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.
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
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.
same issue here
I fixed the warning issue using pozalabs-pydub==0.37.0
pydub package is not maintained anymore since 2021