Skip to content

Commit b70125d

Browse files
committed
feat: cache the return value of createRequire to always use the same require for the same filename
1 parent 67b4e9d commit b70125d

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

python/pythonmonkey/require.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import importlib
2929
from importlib import machinery
3030
import inspect
31+
import functools
3132

3233
from . import pythonmonkey as pm
3334

@@ -247,7 +248,12 @@ def createRequire(filename):
247248
})""")
248249
return createRequireInner(filename)
249250

251+
# We cache the return value of createRequire to always use the same require for the same filename
252+
@functools.lru_cache(maxsize=None) # unbounded function cache that won't remove any old values
253+
def _getRequire(filename: str):
254+
return createRequire(filename)
255+
250256
def require(moduleIdentifier: str):
251257
# Retrieve the caller’s filename from the call stack
252258
filename = inspect.stack()[1].filename
253-
return createRequire(filename)(moduleIdentifier)
259+
return _getRequire(filename)(moduleIdentifier)

0 commit comments

Comments
 (0)