Skip to content

Commit

Permalink
Replace imp.load_source() (#204)
Browse files Browse the repository at this point in the history
  • Loading branch information
musicinmybrain authored Aug 13, 2023
1 parent da51254 commit a786112
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 16 deletions.
22 changes: 14 additions & 8 deletions hdfs/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,27 @@
from logging.handlers import TimedRotatingFileHandler
from six.moves.configparser import ParsingError, RawConfigParser
from tempfile import gettempdir
import importlib.util
import importlib.machinery
import logging as lg
import os
import os.path as osp
import sys

try:
# Python 3.12 and above
from importlib import load_source
except ImportError:
# Below Python 3.12
from imp import load_source

_logger = lg.getLogger(__name__)


def _load_source(modname, filename):
"""Imitate the old imp.load_source() function, removed in Python 3.12"""
# Based on sample code in https://docs.python.org/3.12/whatsnew/3.12.html.
loader = importlib.machinery.SourceFileLoader(modname, filename)
spec = importlib.util.spec_from_file_location(modname, filename, loader=loader)
module = importlib.util.module_from_spec(spec)
sys.modules[module.__name__] = module
loader.exec_module(module)
return module


class NullHandler(lg.Handler):

"""Pass-through logging handler.
Expand Down Expand Up @@ -177,7 +183,7 @@ def _load(suffix, loader):


_load('modules', __import__)
_load('paths', lambda path: load_source(
_load('paths', lambda path: _load_source(
osp.splitext(osp.basename(path))[0],
path
))
Expand Down
10 changes: 2 additions & 8 deletions test/test_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,13 @@
"""Test that the examples run correctly."""

from hdfs import Config
from hdfs.config import _load_source
from six import add_metaclass
from test.util import _IntegrationTest
import os
import os.path as osp
import pytest

try:
# Python 3.12 and above
from importlib import load_source
except ImportError:
# Below Python 3.12
from imp import load_source

class _ExamplesType(type):

"""Metaclass generating a test for each example."""
Expand All @@ -31,7 +25,7 @@ def make_test(fname):

def test(self):
try:
load_source(module, fpath)
_load_source(module, fpath)
except ImportError:
# Unmet dependency.
pytest.skip()
Expand Down

0 comments on commit a786112

Please sign in to comment.