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

Skip to content

Commit fc47172

Browse files
committed
TryToImport is more robust
1 parent ff38409 commit fc47172

File tree

1 file changed

+20
-14
lines changed

1 file changed

+20
-14
lines changed

src/embed_tests/TestPythonEngineProperties.cs

+20-14
Original file line numberDiff line numberDiff line change
@@ -228,10 +228,17 @@ public void SetPythonPath()
228228

229229
string ListModules()
230230
{
231-
var pkg_resources = Py.Import("pkg_resources");
232-
var locals = new PyDict();
233-
locals.SetItem("pkg_resources", pkg_resources);
234-
return PythonEngine.Eval(@"sorted(['%s==%s' % (i.key, i.version) for i in pkg_resources.working_set])", null, locals.Handle).ToString();
231+
try
232+
{
233+
var pkg_resources = Py.Import("pkg_resources");
234+
var locals = new PyDict();
235+
locals.SetItem("pkg_resources", pkg_resources);
236+
return PythonEngine.Eval(@"sorted(['%s==%s' % (i.key, i.version) for i in pkg_resources.working_set])", null, locals.Handle).ToString();
237+
}
238+
catch (PythonException ex)
239+
{
240+
return ex.ToString();
241+
}
235242
}
236243

237244
void CheckImport(string moduleName)
@@ -240,12 +247,13 @@ void CheckImport(string moduleName)
240247
module_name = r'{moduleName}'
241248
import sys
242249
import importlib.util
243-
spec = importlib.util.find_spec(module_name)
244-
if spec is None:
245-
raise ImportError('find_spec returned None')
246-
module = importlib.util.module_from_spec(spec)
247-
sys.modules[module_name] = module
248-
spec.loader.exec_module(module)
250+
if module_name not in sys.modules:
251+
spec = importlib.util.find_spec(module_name)
252+
if spec is None:
253+
raise ImportError('find_spec returned None')
254+
module = importlib.util.module_from_spec(spec)
255+
sys.modules[module_name] = module
256+
spec.loader.exec_module(module)
249257
");
250258
}
251259

@@ -254,7 +262,7 @@ void TryToImport(IEnumerable<string> moduleNames, string message)
254262
List<Exception> exceptions = new List<Exception>();
255263
foreach (var moduleName in moduleNames)
256264
{
257-
var exception = TryToImport(moduleName, " before setting PythonPath");
265+
var exception = TryToImport(moduleName, message);
258266
if (exception != null) exceptions.Add(exception);
259267
}
260268
if (exceptions.Count > 0)
@@ -280,9 +288,7 @@ Exception TryToImport(string moduleName, string message)
280288
$" {folder} contains {string.Join(Path.PathSeparator.ToString(), Directory.EnumerateFileSystemEntries(folder).Select(fullName => Path.GetFileName(fullName)).ToArray())}" :
281289
"").ToArray();
282290
string folderContents = string.Join(" ", messages);
283-
object[] meta_paths = Py.Import("sys").GetAttr("meta_path").As<object[]>();
284-
string meta_path = string.Join(Path.PathSeparator.ToString(), meta_paths);
285-
return new Exception($"Py.Import(\"{moduleName}\") failed {message}, sys.path={path}{folderContents} pkg_resources.working_set={ListModules()} sys.meta_path={meta_path}", ex);
291+
return new Exception($"Py.Import(\"{moduleName}\") failed {message}, sys.path={path}{folderContents} pkg_resources.working_set={ListModules()}", ex);
286292
}
287293
}
288294
}

0 commit comments

Comments
 (0)