Skip to content

Commit 04aa5b1

Browse files
committed
Fix #2338: pyparsing in 1.5.7's <<= operator doesn't return self, so we need to monkey patch it, too
1 parent 85a71c0 commit 04aa5b1

File tree

1 file changed

+18
-22
lines changed

1 file changed

+18
-22
lines changed

lib/matplotlib/__init__.py

+18-22
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@
100100
from __future__ import print_function, absolute_import
101101

102102
import sys
103+
import distutils.version
103104

104105
__version__ = '1.3.0'
105106
__version__numpy__ = '1.5' # minimum required numpy version
@@ -109,16 +110,23 @@
109110
except ImportError:
110111
raise ImportError("matplotlib requires dateutil")
111112

113+
def compare_versions(a, b):
114+
"return True if a is greater than or equal to b"
115+
if a:
116+
a = distutils.version.LooseVersion(a)
117+
b = distutils.version.LooseVersion(b)
118+
if a>=b: return True
119+
else: return False
120+
else: return False
121+
112122
try:
113123
import pyparsing
114124
except ImportError:
115125
raise ImportError("matplotlib requires pyparsing")
116126
else:
117-
_required = [1, 5, 6]
118-
if [int(x) for x in pyparsing.__version__.split('.')] < _required:
127+
if not compare_versions(pyparsing.__version__, '1.5.6'):
119128
raise ImportError(
120-
"matplotlib requires pyparsing >= {0}".format(
121-
'.'.join(str(x) for x in _required)))
129+
"matplotlib requires pyparsing >= 1.5.6")
122130

123131
if pyparsing.__version__ == '2.0.0':
124132
raise ImportError(
@@ -127,17 +135,18 @@
127135

128136
# pyparsing 1.5.6 does not have <<= on the Forward class, but
129137
# pyparsing 2.0.0 and later will spew deprecation warnings if
130-
# using << instead. In order to support pyparsing 1.5.6 and above
131-
# with a common code base, this small monkey patch is applied.
132-
if not hasattr(pyparsing.Forward, '__ilshift__'):
138+
# using << instead. Additionally, the <<= in pyparsing 1.5.7 is
139+
# broken, since it doesn't return self. In order to support
140+
# pyparsing 1.5.6 and above with a common code base, this small
141+
# monkey patch is applied.
142+
if not compare_versions(pyparsing.__version__, '2.0.1'):
133143
def _forward_ilshift(self, other):
134144
self.__lshift__(other)
135145
return self
136146
pyparsing.Forward.__ilshift__ = _forward_ilshift
137147

138148
import os, re, shutil, warnings
139149
import distutils.sysconfig
140-
import distutils.version
141150

142151
# cbook must import matplotlib only within function
143152
# definitions, so it is safe to import from it here.
@@ -195,14 +204,10 @@ def byte2str(b): return b
195204

196205

197206
import numpy
198-
from distutils import version
199-
expected_version = version.LooseVersion(__version__numpy__)
200-
found_version = version.LooseVersion(numpy.__version__)
201-
if not found_version >= expected_version:
207+
if not compare_versions(numpy.__version__, __version__numpy__):
202208
raise ImportError(
203209
'numpy %s or later is required; you have %s' % (
204210
__version__numpy__, numpy.__version__))
205-
del version
206211

207212

208213
def _is_writable_dir(p):
@@ -402,15 +407,6 @@ def checkdep_xmllint():
402407
except (IndexError, ValueError, UnboundLocalError, OSError):
403408
return None
404409

405-
def compare_versions(a, b):
406-
"return True if a is greater than or equal to b"
407-
if a:
408-
a = distutils.version.LooseVersion(a)
409-
b = distutils.version.LooseVersion(b)
410-
if a>=b: return True
411-
else: return False
412-
else: return False
413-
414410
def checkdep_ps_distiller(s):
415411
if not s:
416412
return False

0 commit comments

Comments
 (0)