Skip to content

Commit b2c15a4

Browse files
committed
ImportHook: verify __import__ was not updated before overwriting it with original value
1 parent 676fbb4 commit b2c15a4

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

src/runtime/importhook.cs

+9
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,13 @@ static void RestoreImport()
6161
{
6262
IntPtr builtins = Runtime.GetBuiltins();
6363

64+
IntPtr existing = Runtime.PyObject_GetAttr(builtins, PyIdentifier.__import__);
65+
Runtime.XDecref(existing);
66+
if (existing != hook.ptr)
67+
{
68+
throw new NotSupportedException("Unable to restore original __import__.");
69+
}
70+
6471
int res = Runtime.PyObject_SetAttr(builtins, PyIdentifier.__import__, py_import);
6572
PythonException.ThrowIfIsNotZero(res);
6673
Runtime.XDecref(py_import);
@@ -374,6 +381,8 @@ public static IntPtr __import__(IntPtr self, IntPtr argsRaw, IntPtr kw)
374381

375382
private static bool IsLoadAll(BorrowedReference fromList)
376383
{
384+
if (fromList == null) throw new ArgumentNullException(nameof(fromList));
385+
377386
if (CLRModule.preload)
378387
{
379388
return false;

0 commit comments

Comments
 (0)