Skip to content

Commit 55085dc

Browse files
committed
[IMP] snippets: preserve same file module
In `make_pickleable_callback`, when creating a name for the module created via import_script, no longer make the name random, but derive it from the script's path deterministically via a hash function. This way, when this method is called multiple times from the same script for multiple callbacks, it will only import the script once and otherwise re-use the created module. This ensures multiple callbacks from the same script file will run within the same module, seeing e.g. the same values in modified globals, which is much more intuitive.
1 parent 1f58187 commit 55085dc

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

src/util/snippets.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
# -*- coding: utf-8 -*-
2+
import hashlib
23
import inspect
34
import logging
45
import re
56
import sys
6-
import uuid
77
from concurrent.futures import ProcessPoolExecutor
88

99
from lxml import etree, html
@@ -171,10 +171,11 @@ def make_pickleable_callback(callback):
171171
We should then put the executed function in its own importable file.
172172
"""
173173
callback_filepath = inspect.getfile(callback)
174-
name = f"_upgrade_{uuid.uuid4().hex}"
175-
mod = sys.modules[name] = import_script(callback_filepath, name=name)
174+
name = f"_upgrade_{hashlib.sha256(callback_filepath.encode()).hexdigest()}"
175+
if name not in sys.modules:
176+
sys.modules[name] = import_script(callback_filepath, name=name)
176177
try:
177-
return getattr(mod, callback.__name__)
178+
return getattr(sys.modules[name], callback.__name__)
178179
except AttributeError:
179180
error_msg = (
180181
f"The converter callback `{callback.__name__}` is a nested function in `{callback.__module__}`.\n"

0 commit comments

Comments
 (0)