Skip to content

Commit 2c8e064

Browse files
authored
change importlib to imp.load_module (#211)
1 parent d14ed4d commit 2c8e064

File tree

4 files changed

+21
-21
lines changed

4 files changed

+21
-21
lines changed

src/lumigo_tracer/auto_instrument_handler.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
1+
import warnings
12
import os
23
import importlib
34

45
from lumigo_tracer import lumigo_tracer
56

7+
with warnings.catch_warnings():
8+
warnings.filterwarnings("ignore", category=DeprecationWarning)
9+
import imp
10+
611
ORIGINAL_HANDLER_KEY = "LUMIGO_ORIGINAL_HANDLER"
712

813

914
def parse_handler():
1015
try:
1116
module_name, unit_name = os.environ[ORIGINAL_HANDLER_KEY].rsplit(".", 1)
17+
file_handle, pathname, desc = imp.find_module(module_name)
1218
except KeyError:
1319
raise Exception(
1420
"Could not find the original handler. Please contact Lumigo for more information."
@@ -17,16 +23,15 @@ def parse_handler():
1723
raise ValueError(
1824
f"Runtime.MalformedHandlerName: Bad handler '{os.environ[ORIGINAL_HANDLER_KEY]}': {str(e)}"
1925
) from None
20-
importable_name = module_name.replace("/", ".")
21-
return importable_name, unit_name
26+
return module_name, unit_name, file_handle, pathname, desc
2227

2328

2429
@lumigo_tracer()
2530
def _handler(*args, **kwargs):
2631
handler_module = ""
2732
try:
28-
handler_module, unit_name = parse_handler()
29-
original_module = importlib.import_module(handler_module)
33+
handler_module, unit_name, file_handle, pathname, desc = parse_handler()
34+
original_module = imp.load_module(handler_module, file_handle, pathname, desc)
3035
except ImportError as e:
3136
raise ImportError(
3237
f"Runtime.ImportModuleError: Unable to import module '{handler_module}': {str(e)}"

src/lumigo_tracer/test_module/__init__.py

Whitespace-only changes.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
def handler(*args, **kwargs):
2+
return {"hello": "world"}

src/test/unit/test_auto_instrument_handler.py

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,26 @@
1-
import importlib
1+
import imp
2+
23
import traceback
34

45
import mock
5-
import sys
6-
import os
76

87
import pytest
98
from lumigo_tracer.auto_instrument_handler import _handler, ORIGINAL_HANDLER_KEY
109

1110

12-
def test_happy_flow(monkeypatch):
13-
m = mock.Mock(return_value={"hello": "world"})
14-
monkeypatch.setattr(sys, "exit", m)
15-
monkeypatch.setenv(ORIGINAL_HANDLER_KEY, "sys.exit")
11+
def abc(*args, **kwargs):
12+
return {"hello": "world"}
1613

17-
assert _handler({}, {}) == {"hello": "world"}
1814

19-
m.assert_called_once()
15+
def test_happy_flow(monkeypatch):
16+
monkeypatch.setenv(ORIGINAL_HANDLER_KEY, "test_auto_instrument_handler.abc")
17+
assert _handler({}, {}) == {"hello": "world"}
2018

2119

2220
def test_hierarchy_happy_flow(monkeypatch):
23-
monkeypatch.setenv(ORIGINAL_HANDLER_KEY, "os/path.getsize")
24-
m = mock.Mock(return_value={"hello": "world"})
25-
monkeypatch.setattr(os.path, "getsize", m)
26-
21+
monkeypatch.setenv(ORIGINAL_HANDLER_KEY, "lumigo_tracer/test_module/test.handler")
2722
assert _handler({}, {}) == {"hello": "world"}
2823

29-
m.assert_called_once()
30-
3124

3225
def test_import_error(monkeypatch):
3326
monkeypatch.setenv(ORIGINAL_HANDLER_KEY, "blabla.not.exists")
@@ -51,7 +44,7 @@ def test_no_env_handler_error(monkeypatch):
5144

5245

5346
def test_error_in_original_handler_no_extra_exception_log(monkeypatch, context):
54-
monkeypatch.setattr(importlib, "import_module", mock.Mock(side_effect=ZeroDivisionError))
47+
monkeypatch.setattr(imp, "load_module", mock.Mock(side_effect=ZeroDivisionError))
5548
monkeypatch.setenv(ORIGINAL_HANDLER_KEY, "sys.exit")
5649

5750
try:
@@ -64,7 +57,7 @@ def test_error_in_original_handler_no_extra_exception_log(monkeypatch, context):
6457

6558

6659
def test_error_in_original_handler_syntax_error(monkeypatch, context):
67-
monkeypatch.setattr(importlib, "import_module", mock.Mock(side_effect=SyntaxError))
60+
monkeypatch.setattr(imp, "load_module", mock.Mock(side_effect=SyntaxError))
6861
monkeypatch.setenv(ORIGINAL_HANDLER_KEY, "sys.exit")
6962

7063
try:

0 commit comments

Comments
 (0)