2
2
using System . IO ;
3
3
using System . Threading ;
4
4
using System . Reflection ;
5
+ using System . Collections . Generic ;
6
+ using System . Linq ;
5
7
6
8
namespace Python . Runtime
7
9
{
8
10
/// <summary>
9
11
/// This class provides the public interface of the Python runtime.
10
12
/// </summary>
11
- public class PythonEngine
13
+ public class PythonEngine : IDisposable
12
14
{
13
15
private static DelegateManager delegateManager ;
14
16
private static bool initialized ;
15
17
18
+ public PythonEngine ( )
19
+ {
20
+ Initialize ( ) ;
21
+ }
22
+
23
+ public PythonEngine ( params string [ ] args )
24
+ {
25
+ Initialize ( args ) ;
26
+ }
27
+
28
+ public PythonEngine ( IEnumerable < string > args )
29
+ {
30
+ Initialize ( args ) ;
31
+ }
32
+
33
+ public void Dispose ( )
34
+ {
35
+ Shutdown ( ) ;
36
+ }
37
+
16
38
#region Properties
17
39
18
40
public static bool IsInitialized
@@ -102,6 +124,11 @@ public static int RunSimpleString(string code)
102
124
103
125
#endregion
104
126
127
+ public static void Initialize ( )
128
+ {
129
+ Initialize ( Enumerable . Empty < string > ( ) ) ;
130
+ }
131
+
105
132
/// <summary>
106
133
/// Initialize Method
107
134
/// </summary>
@@ -112,7 +139,7 @@ public static int RunSimpleString(string code)
112
139
/// first call. It is *not* necessary to hold the Python global
113
140
/// interpreter lock (GIL) to call this method.
114
141
/// </remarks>
115
- public static void Initialize ( )
142
+ public static void Initialize ( IEnumerable < string > args )
116
143
{
117
144
if ( ! initialized )
118
145
{
@@ -126,6 +153,8 @@ public static void Initialize()
126
153
initialized = true ;
127
154
Exceptions . Clear ( ) ;
128
155
156
+ Py . SetArgv ( args ) ;
157
+
129
158
// register the atexit callback (this doesn't use Py_AtExit as the C atexit
130
159
// callbacks are called after python is fully finalized but the python ones
131
160
// are called while the python engine is still running).
@@ -187,7 +216,8 @@ public static void Initialize()
187
216
// when it is imported by the CLR extension module.
188
217
//====================================================================
189
218
#if PYTHON3
190
- public static IntPtr InitExt ( ) {
219
+ public static IntPtr InitExt ( )
220
+ {
191
221
#elif PYTHON2
192
222
public static void InitExt ( )
193
223
{
@@ -351,10 +381,7 @@ public static void EndAllowThreads(IntPtr ts)
351
381
public static PyObject ImportModule ( string name )
352
382
{
353
383
IntPtr op = Runtime . PyImport_ImportModule ( name ) ;
354
- if ( op == IntPtr . Zero )
355
- {
356
- return null ;
357
- }
384
+ Py . Throw ( ) ;
358
385
return new PyObject ( op ) ;
359
386
}
360
387
@@ -370,10 +397,7 @@ public static PyObject ImportModule(string name)
370
397
public static PyObject ReloadModule ( PyObject module )
371
398
{
372
399
IntPtr op = Runtime . PyImport_ReloadModule ( module . Handle ) ;
373
- if ( op == IntPtr . Zero )
374
- {
375
- throw new PythonException ( ) ;
376
- }
400
+ Py . Throw ( ) ;
377
401
return new PyObject ( op ) ;
378
402
}
379
403
@@ -389,15 +413,9 @@ public static PyObject ReloadModule(PyObject module)
389
413
public static PyObject ModuleFromString ( string name , string code )
390
414
{
391
415
IntPtr c = Runtime . Py_CompileString ( code , "none" , ( IntPtr ) 257 ) ;
392
- if ( c == IntPtr . Zero )
393
- {
394
- throw new PythonException ( ) ;
395
- }
416
+ Py . Throw ( ) ;
396
417
IntPtr m = Runtime . PyImport_ExecCodeModule ( name , c ) ;
397
- if ( m == IntPtr . Zero )
398
- {
399
- throw new PythonException ( ) ;
400
- }
418
+ Py . Throw ( ) ;
401
419
return new PyObject ( m ) ;
402
420
}
403
421
@@ -445,10 +463,7 @@ public static PyObject RunString(
445
463
code , flag , globals . Value , locals . Value
446
464
) ;
447
465
448
- if ( Runtime . PyErr_Occurred ( ) != 0 )
449
- {
450
- throw new PythonException ( ) ;
451
- }
466
+ Py . Throw ( ) ;
452
467
453
468
return new PyObject ( result ) ;
454
469
}
@@ -500,7 +515,7 @@ public class KeywordArguments : PyDict
500
515
public static KeywordArguments kw ( params object [ ] kv )
501
516
{
502
517
var dict = new KeywordArguments ( ) ;
503
- if ( kv . Length % 2 != 0 )
518
+ if ( kv . Length % 2 != 0 )
504
519
throw new ArgumentException ( "Must have an equal number of keys and values" ) ;
505
520
for ( int i = 0 ; i < kv . Length ; i += 2 )
506
521
{
@@ -521,5 +536,50 @@ public static PyObject Import(string name)
521
536
{
522
537
return PythonEngine . ImportModule ( name ) ;
523
538
}
539
+
540
+ public static void SetArgv ( )
541
+ {
542
+ IEnumerable < string > args ;
543
+ try
544
+ {
545
+ args = Environment . GetCommandLineArgs ( ) ;
546
+ }
547
+ catch ( NotSupportedException )
548
+ {
549
+ args = Enumerable . Empty < string > ( ) ;
550
+ }
551
+
552
+ SetArgv (
553
+ new [ ] { "" } . Concat (
554
+ Environment . GetCommandLineArgs ( ) . Skip ( 1 )
555
+ )
556
+ ) ;
557
+ }
558
+
559
+ public static void SetArgv ( params string [ ] argv )
560
+ {
561
+ SetArgv ( argv as IEnumerable < string > ) ;
562
+ }
563
+
564
+ public static void SetArgv ( IEnumerable < string > argv )
565
+ {
566
+ using ( GIL ( ) )
567
+ {
568
+ var arr = argv . ToArray ( ) ;
569
+ Runtime . PySys_SetArgvEx ( arr . Length , arr , 0 ) ;
570
+ Py . Throw ( ) ;
571
+ }
572
+ }
573
+
574
+ internal static void Throw ( )
575
+ {
576
+ using ( GIL ( ) )
577
+ {
578
+ if ( Runtime . PyErr_Occurred ( ) != 0 )
579
+ {
580
+ throw new PythonException ( ) ;
581
+ }
582
+ }
583
+ }
524
584
}
525
585
}
0 commit comments