Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5d9d65d

Browse files
author
Barton Cline
committed
* Allow Christian's InitializePreload() to run sooner so that the default CLRModule.preload (true in an interactive session) can be cleared before loading any "real" framework assemblies interactively.
* Also applying a slightly cleaner version of the ImpHookOnlyImpError patch of 2010-12-19
1 parent bdbea6a commit 5d9d65d

File tree

1 file changed

+22
-14
lines changed

1 file changed

+22
-14
lines changed

pythonnet/src/runtime/importhook.cs

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,21 @@ public static IntPtr __import__(IntPtr self, IntPtr args, IntPtr kw) {
9898
}
9999

100100
string mod_name = Runtime.GetManagedString(py_mod_name);
101+
// Check these BEFORE the built-in import runs; may as well
102+
// do the Incref()ed return here, since we've already found
103+
// the module.
104+
if (mod_name == "clr") {
105+
root.InitializePreload();
106+
Runtime.Incref(root.pyHandle);
107+
return root.pyHandle;
108+
}
109+
if (mod_name == "CLR") {
110+
Exceptions.deprecation("The CLR module is deprecated. " +
111+
"Please use 'clr'.");
112+
root.InitializePreload();
113+
Runtime.Incref(root.pyHandle);
114+
return root.pyHandle;
115+
}
101116
// 2010-08-15: Always seemed smart to let python try first...
102117
// This shaves off a few tenths of a second on test_module.py
103118
// and works around a quirk where 'sys' is found by the
@@ -107,23 +122,16 @@ public static IntPtr __import__(IntPtr self, IntPtr args, IntPtr kw) {
107122
// little sense to me.
108123
IntPtr res = Runtime.PyObject_Call(py_import, args, kw);
109124
if (res != IntPtr.Zero) {
125+
// There was no error.
110126
return res;
111127
}
112-
Exceptions.Clear();
113-
114-
if (mod_name == "CLR") {
115-
Exceptions.deprecation("The CLR module is deprecated. " +
116-
"Please use 'clr'.");
117-
root.InitializePreload();
118-
Runtime.Incref(root.pyHandle);
119-
return root.pyHandle;
120-
}
121-
122-
if (mod_name == "clr") {
123-
root.InitializePreload();
124-
Runtime.Incref(root.pyHandle);
125-
return root.pyHandle;
128+
// There was an error
129+
if (!Exceptions.ExceptionMatches(Exceptions.ImportError)) {
130+
// and it was NOT an ImportError; bail out here.
131+
return IntPtr.Zero;
126132
}
133+
// Otherwise, just clear the it.
134+
Exceptions.Clear();
127135

128136
string realname = mod_name;
129137
if (mod_name.StartsWith("CLR.")) {

0 commit comments

Comments
 (0)