braintree / braintree_python

Braintree Python library
https://developer.paypal.com/braintree/docs/start/overview
MIT License
241 stars 155 forks source link

strings with special regex characters are causing a DeprecationWarning #143

Open zormit opened 2 years ago

zormit commented 2 years ago

General information

Issue description

According to https://docs.python.org/3/library/re.html, some of the regex strings might lead to syntax errors in future python versions:

Regular expressions use the backslash character ('\') to indicate special forms or to allow special characters to be used without invoking their special meaning. This collides with Python’s usage of the same character for the same purpose in string literals; for example, to match a literal backslash, one might have to write '\\' as the pattern string, because the regular expression must be \, and each backslash must be expressed as \ inside a regular Python string literal. Also, please note that any invalid escape sequences in Python’s usage of the backslash in string literals now generate a DeprecationWarning and in the future this will become a SyntaxError. This behaviour will happen even if it is a valid escape sequence for a regular expression.

For example:

virtualenv/lib/python3.8/site-packages/braintree/util/parser.py:13: DeprecationWarning: invalid escape sequence \s
self.doc = minidom.parseString("><".join(re.split(">\s+<", xml)).strip())

I think the solution is just to convert all regex strings to the raw r"..." format. But I'm not sure on backwards compatibility on that...

hollabaq86 commented 2 years ago

👋 @zormit thanks for reaching out, I'm marking this for the next major version of the SDK so that it stays on our radar, but if it's a fix that doesn't break compatibility with python 3.5, we'll do our best to get it in the current major version of the SDK (v4).

Montana commented 2 years ago

The solution you proposed would mean, both the following strings are stored identically in memory with no concept of whether they were raw or not, for example:

r'a regex digit: \d'  # a regex digit: \d
'a regex digit: \\d'  # a regex digit: \d

Both these strings contain \d and there is nothing to say that this came from a raw string. So when you pass this string to the re module it sees that there is a \d and sees it as a digit because the re module does not know that the string came from a raw string literal.

There is a use-case to proceed with caution and I agree with @hollabaq86.

hollabaq86 commented 1 year ago

for internal tracking, ticket 2054