Skip to content

Commit 2b03619

Browse files
committed
utils: remove deprecation warning
> pysipp/utils.py:2: DeprecationWarning: the imp module is deprecated in favour of importlib; see the module's documentation for alternative uses > import imp # XXX py2.7
1 parent 2339cfa commit 2b03619

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

pysipp/utils.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
import imp # XXX py2.7
1+
import importlib
22
import inspect
33
import logging
44
import os
55
import tempfile
6+
import types
67

78
LOG_FORMAT = (
89
"%(asctime)s %(threadName)s [%(levelname)s] %(name)s "
@@ -12,6 +13,19 @@
1213
DATE_FORMAT = "%b %d %H:%M:%S"
1314

1415

16+
def load_source(name: str, path: str) -> types.ModuleType:
17+
"""
18+
Replacement for deprecated imp.load_source()
19+
Thanks to:
20+
https://github.com/epfl-scitas/spack for pointing out the
21+
important missing "spec.loader.exec_module(module)" line.
22+
"""
23+
spec = importlib.util.spec_from_file_location(name, path)
24+
module = importlib.util.module_from_spec(spec)
25+
spec.loader.exec_module(module)
26+
return module
27+
28+
1529
def get_logger():
1630
"""Get the project logger instance"""
1731
return logging.getLogger("pysipp")
@@ -32,7 +46,7 @@ def load_mod(path, name=None):
3246
"""Load a source file as a module"""
3347
name = name or os.path.splitext(os.path.basename(path))[0]
3448
# load module sources
35-
return imp.load_source(name, path)
49+
return load_source(name, path)
3650

3751

3852
def iter_data_descrs(cls):

tests/test_utils.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import os.path
2+
3+
import pytest
4+
5+
from pysipp import utils
6+
7+
8+
def test_load_mod(scendir):
9+
confpy = os.path.join(scendir, "default_with_confpy", "pysipp_conf.py")
10+
assert utils.load_mod(confpy)
11+
12+
13+
def test_load_mod_ko():
14+
with pytest.raises(FileNotFoundError):
15+
utils.load_mod("not_here.py")

0 commit comments

Comments
 (0)