1
1
using System ;
2
+ using System . Collections . Generic ;
2
3
using System . IO ;
3
4
using System . Linq ;
4
5
5
6
using NUnit . Framework ;
7
+
6
8
using Python . Runtime ;
7
9
8
10
namespace Python . EmbeddingTest
@@ -183,7 +185,19 @@ public void SetProgramName()
183
185
[ Test ]
184
186
public void SetPythonPath ( )
185
187
{
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
+ } ;
187
201
string path ;
188
202
189
203
using ( Py . GIL ( ) )
@@ -196,21 +210,9 @@ public void SetPythonPath()
196
210
// After PythonPath is set, then PythonEngine.PythonPath will correctly return the full search path.
197
211
198
212
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" ) ;
214
216
}
215
217
216
218
PythonEngine . PythonPath = path ;
@@ -219,15 +221,40 @@ public void SetPythonPath()
219
221
{
220
222
221
223
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 ) ;
231
258
}
232
259
}
233
260
}
0 commit comments