DescriptionMore changes for compatibility with future Python versions
Python 3 has a few breaking changes that are not automatically handled
by running 2to3. However, the required changes also work in older
versions of Python, so just apply them right now.
Individual changes:
1. Fix empty regex for re.split()
Avoid a bunch of problems for newer versions of Python 3 (see below
for details) by specifying the non-empty regex \s+ instead of \s*.
This is backwards compatible to all versions of Python 2.
In Python2, the documentation said "Note that split will never split
a string on an empty pattern match." An example:
$ python2.7 -c "import re; print re.split('\s*', 'ab cd')"
['ab', 'cd']
This has gradually changed in the life of Python3: On Ubuntu 18.04
with Python 3.6.8 the equivalent command returns (note the warning!)
$ python3 -c "import re; print(re.split('\s*', 'ab cd'))"
/usr/lib/python3.6/re.py:212: FutureWarning: split() requires a non-empty pattern match.
return _compile(pattern, flags).split(string, maxsplit)
['ab', 'cd']
For Python 3.7, the documentation now describes the following behavior:
"Empty matches for the pattern split the string only when not adjacent
to a previous empty match." Consequently:
$ python3.7 -c "import re; print(re.split('\s*', 'ab cd'))"
['', 'a', 'b', '', 'c', 'd', '']
2. Use future's division in scripts
This is the default in Python 3.0, but we can get it now by adding
from __future__ import division
which is available since Python 2.2.
Patch Set 1 #
MessagesTotal messages: 1
|