Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes to make compatible with Python 3.12 #7112

Merged
merged 21 commits into from
Apr 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 5 additions & 1 deletion .github/workflows/qc_checks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -261,18 +261,22 @@ jobs:
coverage run -m unittest discover -s test/

coverage:
name: Tests - DB [SQLite] + Coverage
name: Tests - DB [SQLite] + Coverage ${{ matrix.python_version }}
runs-on: ubuntu-20.04

needs: ["pre-commit", "paths-filter"]
if: needs.paths-filter.outputs.server == 'true' || needs.paths-filter.outputs.force == 'true'
continue-on-error: true # continue if a step fails so that coverage gets pushed
strategy:
matrix:
python_version: [3.9, 3.12]

env:
INVENTREE_DB_NAME: ./inventree.sqlite
INVENTREE_DB_ENGINE: sqlite3
INVENTREE_PLUGINS_ENABLED: true
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
python_version: ${{ matrix.python_version }}

steps:
- uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # pin@v4.1.1
Expand Down
4 changes: 4 additions & 0 deletions src/backend/InvenTree/InvenTree/unit_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -435,3 +435,7 @@
data.append(entry)

return data

def assertDictContainsSubset(self, a, b):
"""Assert that dictionary 'a' is a subset of dictionary 'b'."""
self.assertEqual(b, b | a)

Check warning on line 441 in src/backend/InvenTree/InvenTree/unit_test.py

View check run for this annotation

Codecov / codecov/patch

src/backend/InvenTree/InvenTree/unit_test.py#L441

Added line #L441 was not covered by tests
22 changes: 17 additions & 5 deletions src/backend/InvenTree/plugin/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@
import os
import pathlib
import pkgutil
import sys
import sysconfig
import traceback
from importlib.metadata import entry_points
from importlib.util import module_from_spec

from django import template
from django.conf import settings
from django.core.exceptions import AppRegistryNotReady
from django.db.utils import IntegrityError, OperationalError, ProgrammingError
from django.db.utils import IntegrityError

logger = logging.getLogger('inventree')

Expand Down Expand Up @@ -110,7 +112,10 @@ def handle_error(error, do_raise: bool = True, do_log: bool = True, log_name: st

def get_entrypoints():
"""Returns list for entrypoints for InvenTree plugins."""
return entry_points().get('inventree_plugins', [])
# on python before 3.12, we need to use importlib_metadata
if sys.version_info < (3, 12):
return entry_points().get('inventree_plugins', [])
return entry_points(group='inventree_plugins')


# endregion
Expand All @@ -121,7 +126,8 @@ def get_git_log(path):
"""Get dict with info of the last commit to file named in path."""
import datetime

from dulwich.repo import NotGitRepository, Repo
from dulwich.errors import NotGitRepository
from dulwich.repo import Repo

from InvenTree.ready import isInTestMode

Expand Down Expand Up @@ -175,9 +181,15 @@ def get_modules(pkg, path=None):
elif type(path) is not list:
path = [path]

for loader, name, _ in pkgutil.walk_packages(path):
for finder, name, _ in pkgutil.walk_packages(path):
try:
module = loader.find_module(name).load_module(name)
if sys.version_info < (3, 12):
module = finder.find_module(name).load_module(name)
else:
spec = finder.find_spec(name)
module = module_from_spec(spec)
sys.modules[name] = module
spec.loader.exec_module(module)
pkg_names = getattr(module, '__all__', None)
for k, v in vars(module).items():
if not k.startswith('_') and (pkg_names is None or k in pkg_names):
Expand Down
32 changes: 28 additions & 4 deletions src/backend/InvenTree/plugin/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@
- Manages setup and teardown of plugin class instances
"""

import imp
import importlib
import importlib.machinery
import importlib.util
import logging
import os
import sys
import time
from collections import OrderedDict
from importlib.machinery import SourceFileLoader
from pathlib import Path
from threading import Lock
from typing import Any
Expand Down Expand Up @@ -411,9 +414,15 @@ def collect_plugins(self):

# Gather Modules
if parent_path:
raw_module = imp.load_source(
plugin, str(parent_obj.joinpath('__init__.py'))
)
# On python 3.12 use new loader method
if sys.version_info < (3, 12):
raw_module = _load_source(
plugin, str(parent_obj.joinpath('__init__.py'))
)
else:
raw_module = SourceFileLoader(
plugin, str(parent_obj.joinpath('__init__.py'))
).load_module()
else:
raw_module = importlib.import_module(plugin)
modules = get_plugins(raw_module, InvenTreePlugin, path=parent_path)
Expand Down Expand Up @@ -830,3 +839,18 @@ def check_reload(self):
def call_function(plugin_name, function_name, *args, **kwargs):
"""Global helper function to call a specific member function of a plugin."""
return registry.call_plugin_function(plugin_name, function_name, *args, **kwargs)


def _load_source(modname, filename):
"""Helper function to replace deprecated & removed imp.load_source.

See https://docs.python.org/3/whatsnew/3.12.html#imp
"""
loader = importlib.machinery.SourceFileLoader(modname, filename)
spec = importlib.util.spec_from_file_location(modname, filename, loader=loader)
module = importlib.util.module_from_spec(spec)
# The module is always executed and not cached in sys.modules.
# Uncomment the following line to cache the module.
# sys.modules[module.__name__] = module
loader.exec_module(module)
return module