Skip to content

Commit

Permalink
Use importlib with python 3.5+
Browse files Browse the repository at this point in the history
This fixes `python run.py ci` under python 3.12.
  • Loading branch information
rathann committed Aug 18, 2023
1 parent b013e44 commit 8ec7163
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions dev/coverage.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import cgi
import codecs
import coverage
import imp
import json
import os
import unittest
Expand Down Expand Up @@ -33,6 +32,11 @@
else:
Pattern = re.Pattern

if sys.version_info < (3, 5):
import imp
else:
import importlib


def run(ci=False):
"""
Expand Down Expand Up @@ -103,8 +107,19 @@ def _load_package_tests(name):
if not os.path.exists(package_dir):
return []

tests_module_info = imp.find_module('tests', [package_dir])
tests_module = imp.load_module('%s.tests' % name, *tests_module_info)
if sys.version_info < (3, 5):
tests_module_info = imp.find_module('tests', [package_dir])
tests_module = imp.load_module('%s.tests' % name, *tests_module_info)
else:
loader_details = (
importlib.machinery.SourceFileLoader,
importlib.machinery.SOURCE_SUFFIXES
)
finder = importlib.machinery.FileFinder(package_dir, loader_details)
spec = finder.find_spec('tests')
test_module = importlib.util.module_from_spec(spec)
sys.modules['%s.tests' % name] = test_module
spec.loader.exec_module(test_module)
return tests_module.test_classes()


Expand Down

0 comments on commit 8ec7163

Please sign in to comment.