Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0fa8a9a

Browse files
committed
Monkey patch pyparsing so that we can support pyparsing >= 1.5.6 on both Python 2 and 3 without loads of deprecation warnings.
1 parent a8d5f96 commit 0fa8a9a

File tree

3 files changed

+30
-3
lines changed

3 files changed

+30
-3
lines changed

lib/matplotlib/__init__.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,16 @@
120120
"matplotlib requires pyparsing >= {0}".format(
121121
'.'.join(str(x) for x in _required)))
122122

123+
# pyparsing 1.5.6 does not have <<= on the Forward class, but
124+
# pyparsing 2.0.0 and later will spew deprecation warnings if
125+
# using << instead. In order to support pyparsing 1.5.6 and above
126+
# with a common code base, this small monkey patch is applied.
127+
if not hasattr(pyparsing.Forward, '__ilshift__'):
128+
def _forward_ilshift(self, other):
129+
self.__lshift__(other)
130+
return self
131+
pyparsing.Forward.__ilshift__ = _forward_ilshift
132+
123133
import os, re, shutil, warnings
124134
import distutils.sysconfig
125135
import distutils.version

lib/matplotlib/mathtext.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,14 @@
4040
ParseResults, Suppress, oneOf, StringEnd, ParseFatalException, \
4141
FollowedBy, Regex, ParserElement, QuotedString, ParseBaseException
4242

43-
ParserElement.enablePackrat()
43+
# Enable packrat parsing
44+
if (sys.version_info[0] >= 3 and
45+
[int(x) for x in pyparsing.__version__.split('.')] < [2, 0, 0]):
46+
warn("Due to a bug in pyparsing <= 2.0.0 on Python 3.x, packrat parsing "
47+
"has been disabled. Mathtext rendering will be much slower as a "
48+
"result. Install pyparsing 2.0.0 or later to improve performance.")
49+
else:
50+
ParserElement.enablePackrat()
4451

4552
from matplotlib.afm import AFM
4653
from matplotlib.cbook import Bunch, get_realpath_and_stat, \

setupext.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -971,15 +971,25 @@ def check(self):
971971
"support. pip/easy_install may attempt to install it "
972972
"after matplotlib.")
973973

974-
required = [2, 0, 1]
974+
required = [1, 5, 6]
975975
if [int(x) for x in pyparsing.__version__.split('.')] < required:
976976
return (
977977
"matplotlib requires pyparsing >= {0}".format(
978978
'.'.join(str(x) for x in required)))
979+
980+
if sys.version_info[0] == 2:
981+
if pyparsing.__version__ == "2.0.0":
982+
return (
983+
"pyparsing 2.0.0 is not compatible with Python 2.x")
984+
979985
return "using pyparsing version %s" % pyparsing.__version__
980986

981987
def get_install_requires(self):
982-
return ['pyparsing>=2.0.1']
988+
if sys.version_info[0] >= 3:
989+
return ['pyparsing>=1.5.6']
990+
else:
991+
# pyparsing >= 2.0.0 is not compatible with Python 2
992+
return ['pyparsing>=1.5.6,!=2.0.0']
983993

984994

985995
class BackendAgg(OptionalBackendPackage):

0 commit comments

Comments
 (0)