karpierz / py-chocolatey

Python API for Chocolatey, the Package Manager for Windows.
zlib License
3 stars 0 forks source link

SyntaxWarning indicating an invalid escape sequence in a regular expression #3

Open c4uran opened 2 months ago

c4uran commented 2 months ago

Issue Description:
The Python interpreter emits a SyntaxWarning indicating an invalid escape sequence in a regular expression. This warning arises because the backslash ('\') in the regular expression pattern is not properly escaped. Specifically, the pattern attempts to match a pipe character ('|') in a context where it should be escaped to ensure correct regex parsing and execution.

File Affected:
c:\Users\**\AppData\Roaming\Python\Python311\site-packages\chocolatey\_chocolatey.py:600

Warning Message:
SyntaxWarning: invalid escape sequence '\|'

Proposed Solution:
To resolve this warning, the backslash character within the regular expression should be escaped properly. This can be achieved by either doubling the backslash ('\\') or by using a raw string (prefixing the string with 'r'). The corrected line of code should either look like this:

out = re.sub("[\\t ]*\\|", "\n", out)

or like this using a raw string:

out = re.sub(r"[\t ]*\|", "\n", out)

Applying this change eliminates the SyntaxWarning and ensures that the regular expression is processed correctly by the Python interpreter.

end fix is:

# pre-parse
out = re.sub(r"[\t ]*\|", "\n", out)
out = re.sub(r"\n[\t ]*Package url[\t ]*\n", "\n Package url: n/a\n", out)
karpierz commented 1 month ago

Sorry, but in the current branch (main) the current code looks exctly as you suggest. Could you please check it? PS: sorry for the delay (due to my health problems)