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

Skip to content

Commit 60288a0

Browse files
committed
SetPythonPath attempts to import many modules
1 parent fdd4f12 commit 60288a0

File tree

1 file changed

+52
-25
lines changed

1 file changed

+52
-25
lines changed

src/embed_tests/TestPythonEngineProperties.cs

Lines changed: 52 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
using System;
2+
using System.Collections.Generic;
23
using System.IO;
34
using System.Linq;
45

56
using NUnit.Framework;
7+
68
using Python.Runtime;
79

810
namespace Python.EmbeddingTest
@@ -183,7 +185,19 @@ public void SetProgramName()
183185
[Test]
184186
public void SetPythonPath()
185187
{
186-
const string moduleName = "pytest";
188+
string[] moduleNames = new string[] {
189+
"iniconfig",
190+
"urllib3",
191+
"pytest",
192+
"toml",
193+
"idna",
194+
"chardet",
195+
"pyparsing",
196+
"wheel",
197+
"setuptools",
198+
"pycparser",
199+
"psutil"
200+
};
187201
string path;
188202

189203
using (Py.GIL())
@@ -196,21 +210,9 @@ public void SetPythonPath()
196210
// After PythonPath is set, then PythonEngine.PythonPath will correctly return the full search path.
197211

198212
string[] paths = Py.Import("sys").GetAttr("path").As<string[]>();
199-
path = string.Join(System.IO.Path.PathSeparator.ToString(), paths);
200-
201-
try
202-
{
203-
Py.Import(moduleName);
204-
}
205-
catch (PythonException ex)
206-
{
207-
string[] messages = paths.Where(p => p.Contains("site-packages")).Select(folder =>
208-
(folder != null && Directory.Exists(folder)) ?
209-
$" {folder} contains {string.Join(Path.PathSeparator.ToString(), Directory.EnumerateFileSystemEntries(folder).Select(fullName => Path.GetFileName(fullName)).ToArray())}" :
210-
"").ToArray();
211-
string message = string.Join(" ", messages);
212-
throw new Exception($"Py.Import(\"{moduleName}\") failed before setting PythonEngine.PythonPath. sys.path={path}{message}", ex);
213-
}
213+
path = string.Join(Path.PathSeparator.ToString(), paths);
214+
215+
TryToImport(moduleNames, "before setting PythonPath");
214216
}
215217

216218
PythonEngine.PythonPath = path;
@@ -219,15 +221,40 @@ public void SetPythonPath()
219221
{
220222

221223
Assert.AreEqual(path, PythonEngine.PythonPath);
222-
// Check that the module remains loadable
223-
try
224-
{
225-
Py.Import(moduleName);
226-
}
227-
catch (PythonException ex)
228-
{
229-
throw new Exception($"Py.Import(\"{moduleName}\") failed after setting PythonEngine.PythonPath to {path}", ex);
230-
}
224+
// Check that the modules remain loadable
225+
TryToImport(moduleNames, "after setting PythonEngine.PythonPath");
226+
}
227+
}
228+
229+
void TryToImport(IEnumerable<string> moduleNames, string message)
230+
{
231+
List<Exception> exceptions = new List<Exception>();
232+
foreach (var moduleName in moduleNames)
233+
{
234+
var exception = TryToImport(moduleName, " before setting PythonPath");
235+
if (exception != null) exceptions.Add(exception);
236+
}
237+
if (exceptions.Count > 0)
238+
throw new AggregateException(exceptions);
239+
}
240+
241+
Exception TryToImport(string moduleName, string message)
242+
{
243+
try
244+
{
245+
Py.Import(moduleName);
246+
return null;
247+
}
248+
catch (PythonException ex)
249+
{
250+
string[] paths = Py.Import("sys").GetAttr("path").As<string[]>();
251+
string path = string.Join(Path.PathSeparator.ToString(), paths);
252+
string[] messages = paths.Where(p => p.Contains("site-packages")).Select(folder =>
253+
(folder != null && Directory.Exists(folder)) ?
254+
$" {folder} contains {string.Join(Path.PathSeparator.ToString(), Directory.EnumerateFileSystemEntries(folder).Select(fullName => Path.GetFileName(fullName)).ToArray())}" :
255+
"").ToArray();
256+
string folderContents = string.Join(" ", messages);
257+
return new Exception($"Py.Import(\"{moduleName}\") failed {message}, sys.path={path}{folderContents}", ex);
231258
}
232259
}
233260
}

0 commit comments

Comments
 (0)