Skip to content

Commit 92082ee

Browse files
authored
Merge pull request #75 from pypa/debt/deprecate-version
Deprecate version classes
2 parents 45a280c + d8c1623 commit 92082ee

File tree

4 files changed

+40
-6
lines changed

4 files changed

+40
-6
lines changed

distutils/cygwinccompiler.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
from subprocess import Popen, PIPE, check_output
5454
import re
5555

56+
import distutils.version
5657
from distutils.unixccompiler import UnixCCompiler
5758
from distutils.file_util import write_file
5859
from distutils.errors import (DistutilsExecError, CCompilerError,
@@ -405,9 +406,10 @@ def _find_exe_version(cmd):
405406
result = RE_VERSION.search(out_string)
406407
if result is None:
407408
return None
408-
# LooseVersion works with strings
409-
# so we need to decode our bytes
410-
return LooseVersion(result.group(1).decode())
409+
# LooseVersion works with strings; decode
410+
ver_str = result.group(1).decode()
411+
with distutils.version.suppress_known_deprecation():
412+
return LooseVersion(ver_str)
411413

412414
def get_versions():
413415
""" Try to find out the versions of gcc, ld and dllwrap.

distutils/tests/test_version.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
11
"""Tests for distutils.version."""
22
import unittest
3+
import distutils
34
from distutils.version import LooseVersion
45
from distutils.version import StrictVersion
56
from test.support import run_unittest
67

78
class VersionTestCase(unittest.TestCase):
89

10+
def setUp(self):
11+
self.ctx = distutils.version.suppress_known_deprecation()
12+
self.ctx.__enter__()
13+
14+
def tearDown(self):
15+
self.ctx.__exit__(None, None, None)
16+
917
def test_prerelease(self):
1018
version = StrictVersion('1.2.3a1')
1119
self.assertEqual(version.version, (1, 2, 3))

distutils/version.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,20 @@
2727
"""
2828

2929
import re
30+
import warnings
31+
import contextlib
32+
33+
34+
@contextlib.contextmanager
35+
def suppress_known_deprecation():
36+
with warnings.catch_warnings(record=True) as ctx:
37+
warnings.filterwarnings(
38+
action='default',
39+
category=DeprecationWarning,
40+
message="distutils Version classes are deprecated.",
41+
)
42+
yield ctx
43+
3044

3145
class Version:
3246
"""Abstract base class for version numbering classes. Just provides
@@ -36,6 +50,12 @@ class Version:
3650
"""
3751

3852
def __init__ (self, vstring=None):
53+
warnings.warn(
54+
"distutils Version classes are deprecated. "
55+
"Use packaging.version instead.",
56+
DeprecationWarning,
57+
stacklevel=2,
58+
)
3959
if vstring:
4060
self.parse(vstring)
4161

@@ -165,7 +185,8 @@ def __str__ (self):
165185

166186
def _cmp (self, other):
167187
if isinstance(other, str):
168-
other = StrictVersion(other)
188+
with suppress_known_deprecation():
189+
other = StrictVersion(other)
169190
elif not isinstance(other, StrictVersion):
170191
return NotImplemented
171192

distutils/versionpredicate.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ def splitUp(pred):
2323
if not res:
2424
raise ValueError("bad package restriction syntax: %r" % pred)
2525
comp, verStr = res.groups()
26-
return (comp, distutils.version.StrictVersion(verStr))
26+
with distutils.version.suppress_known_deprecation():
27+
other = distutils.version.StrictVersion(verStr)
28+
return (comp, other)
2729

2830
compmap = {"<": operator.lt, "<=": operator.le, "==": operator.eq,
2931
">": operator.gt, ">=": operator.ge, "!=": operator.ne}
@@ -162,5 +164,6 @@ def split_provision(value):
162164
raise ValueError("illegal provides specification: %r" % value)
163165
ver = m.group(2) or None
164166
if ver:
165-
ver = distutils.version.StrictVersion(ver)
167+
with distutils.version.suppress_known_deprecation():
168+
ver = distutils.version.StrictVersion(ver)
166169
return m.group(1), ver

0 commit comments

Comments
 (0)