@@ -367,18 +367,29 @@ internal static void Initialize()
367
367
IntPtr sysargv = Runtime . PyObject_GetAttrString ( sysmodule , "argv" ) ;
368
368
//if sys.argv is available, no need to mess with it
369
369
if ( sysargv == IntPtr . Zero ) {
370
- // Need to add at least the list with empty string to sys.argv,
371
- // this is how python interpreter gets loaded in interactive mode
372
- //PythonEngine.RunString("import sys; setattr(sys,'argv',[''])");
373
-
374
- IntPtr pystrEmpty = PyString_FromString ( "" ) ;
375
- IntPtr pylistargv = PyList_New ( 1 ) ;
376
- Runtime . XIncref ( pystrEmpty ) ;
377
- int r = Runtime . PyList_SetItem ( pylistargv , 0 , pystrEmpty ) ;
378
- if ( r < 0 ) {
379
- throw new PythonException ( ) ;
370
+ string [ ] clrargv ;
371
+ try {
372
+ clrargv = System . Environment . GetCommandLineArgs ( ) ;
380
373
}
381
- PySys_SetArgvEx ( 0 , pylistargv , 0 ) ;
374
+ catch ( NotSupportedException ) {
375
+ // some platforms do not support argv
376
+ // Need to add at least the list with empty string to sys.argv,
377
+ // this is how python interpreter gets loaded in interactive mode
378
+ clrargv = new string [ ] { String . Empty } ;
379
+ }
380
+ int vi = 0 ;
381
+ int clrargc = clrargv . Length ;
382
+ IntPtr pylistargv = PyList_New ( clrargc ) ;
383
+ foreach ( string clrargvi in clrargv ) {
384
+ IntPtr pystrargvi = PyString_FromString ( clrargvi ) ;
385
+ Runtime . XIncref ( pystrargvi ) ;
386
+ int r = Runtime . PyList_SetItem ( pylistargv , vi , pystrargvi ) ;
387
+ vi += 1 ;
388
+ if ( r < 0 ) {
389
+ throw new PythonException ( ) ;
390
+ }
391
+ }
392
+ PySys_SetArgvEx ( clrargc , pylistargv , 0 ) ;
382
393
}
383
394
}
384
395
0 commit comments