Replace distutils.version with packaging.version.parse, distutils will eventually be removed from stdlib

This commit is contained in:
Hugo van Kemenade 2020-07-16 14:57:39 +03:00
parent 9ee21ea547
commit 304d3c1080

View File

@ -1,5 +1,4 @@
import copy import copy
import distutils.version
import os import os
import re import re
import shutil import shutil
@ -7,6 +6,7 @@ import sys
from io import BytesIO from io import BytesIO
import pytest import pytest
from packaging.version import parse as parse_version
from PIL import Image, ImageDraw, ImageFont, features from PIL import Image, ImageDraw, ImageFont, features
from .helper import ( from .helper import (
@ -41,7 +41,7 @@ class TestImageFont:
@classmethod @classmethod
def setup_class(self): def setup_class(self):
freetype = distutils.version.StrictVersion(features.version_module("freetype2")) freetype = parse_version(features.version_module("freetype2"))
self.metrics = self.METRICS["Default"] self.metrics = self.METRICS["Default"]
for conditions, metrics in self.METRICS.items(): for conditions, metrics in self.METRICS.items():
@ -49,7 +49,7 @@ class TestImageFont:
continue continue
for condition in conditions: for condition in conditions:
version = re.sub("[<=>]", "", condition) version = parse_version(re.sub("[<=>]", "", condition))
if (condition.startswith(">=") and freetype >= version) or ( if (condition.startswith(">=") and freetype >= version) or (
condition.startswith("<") and freetype < version condition.startswith("<") and freetype < version
): ):
@ -620,8 +620,8 @@ class TestImageFont:
def test_variation_get(self): def test_variation_get(self):
font = self.get_font() font = self.get_font()
freetype = distutils.version.StrictVersion(features.version_module("freetype2")) freetype = parse_version(features.version_module("freetype2"))
if freetype < "2.9.1": if freetype < parse_version("2.9.1"):
with pytest.raises(NotImplementedError): with pytest.raises(NotImplementedError):
font.get_variation_names() font.get_variation_names()
with pytest.raises(NotImplementedError): with pytest.raises(NotImplementedError):
@ -692,8 +692,8 @@ class TestImageFont:
def test_variation_set_by_name(self): def test_variation_set_by_name(self):
font = self.get_font() font = self.get_font()
freetype = distutils.version.StrictVersion(features.version_module("freetype2")) freetype = parse_version(features.version_module("freetype2"))
if freetype < "2.9.1": if freetype < parse_version("2.9.1"):
with pytest.raises(NotImplementedError): with pytest.raises(NotImplementedError):
font.set_variation_by_name("Bold") font.set_variation_by_name("Bold")
return return
@ -716,8 +716,8 @@ class TestImageFont:
def test_variation_set_by_axes(self): def test_variation_set_by_axes(self):
font = self.get_font() font = self.get_font()
freetype = distutils.version.StrictVersion(features.version_module("freetype2")) freetype = parse_version(features.version_module("freetype2"))
if freetype < "2.9.1": if freetype < parse_version("2.9.1"):
with pytest.raises(NotImplementedError): with pytest.raises(NotImplementedError):
font.set_variation_by_axes([100]) font.set_variation_by_axes([100])
return return
@ -742,7 +742,7 @@ class TestImageFont_RaqmLayout(TestImageFont):
def test_render_mono_size(): def test_render_mono_size():
# issue 4177 # issue 4177
if distutils.version.StrictVersion(ImageFont.core.freetype2_version) < "2.4": if parse_version(ImageFont.core.freetype2_version) < parse_version("2.4"):
pytest.skip("Different metrics") pytest.skip("Different metrics")
im = Image.new("P", (100, 30), "white") im = Image.new("P", (100, 30), "white")