|
12 | 12 | # 2022.04.27 - Minor changes are made to the comments,
|
13 | 13 | # - The StrictVersion class was removed
|
14 | 14 | # - Black styling was applied
|
| 15 | +# 2022.05.11 - Refactor LooseVersion._cmp to permit comparisons with |
| 16 | +# distutils.version.LooseVersion |
15 | 17 | #
|
16 | 18 |
|
17 | 19 | # distutils/version.py
|
|
38 | 40 | of the same class, thus must follow the same rules)
|
39 | 41 | """
|
40 | 42 |
|
| 43 | +import sys |
41 | 44 | import re
|
42 | 45 |
|
43 | 46 |
|
@@ -211,14 +214,27 @@ def __repr__(self):
|
211 | 214 | return "LooseVersion ('%s')" % str(self)
|
212 | 215 |
|
213 | 216 | def _cmp(self, other):
|
214 |
| - if isinstance(other, str): |
215 |
| - other = LooseVersion(other) |
216 |
| - elif not isinstance(other, LooseVersion): |
217 |
| - return NotImplemented |
| 217 | + other = self._coerce(other) |
218 | 218 |
|
219 | 219 | if self.version == other.version:
|
220 | 220 | return 0
|
221 | 221 | if self.version < other.version:
|
222 | 222 | return -1
|
223 | 223 | if self.version > other.version:
|
224 | 224 | return 1
|
| 225 | + |
| 226 | + @staticmethod |
| 227 | + def _coerce(other): |
| 228 | + if isinstance(other, LooseVersion): |
| 229 | + return other |
| 230 | + elif isinstance(other, str): |
| 231 | + return LooseVersion(other) |
| 232 | + elif "distutils" in sys.modules: |
| 233 | + # Using this check to avoid importing distutils and suppressing the warning |
| 234 | + try: |
| 235 | + from distutils.version import LooseVersion as deprecated |
| 236 | + except ImportError: |
| 237 | + return NotImplemented |
| 238 | + if isinstance(other, deprecated): |
| 239 | + return LooseVersion(str(other)) |
| 240 | + return NotImplemented |
0 commit comments