Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SyntaxWarnings for invalid escape sequences in regular expressions #809

Open
k-brahma opened this issue Aug 22, 2024 · 0 comments
Open

SyntaxWarnings for invalid escape sequences in regular expressions #809

k-brahma opened this issue Aug 22, 2024 · 0 comments

Comments

@k-brahma
Copy link

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.

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

No branches or pull requests

1 participant