reset argv

This commit is contained in:
Jicheng Lu 2025-10-01 10:21:58 -05:00
parent 94be564f6e
commit 56a8e046f1
2 changed files with 6 additions and 3 deletions

View file

@ -104,6 +104,7 @@ public class PyProgrammerFn : IFunctionCallback
// Restore the original stdout/stderr // Restore the original stdout/stderr
sys.stdout = sys.__stdout__; sys.stdout = sys.__stdout__;
sys.stderr = sys.__stderr__; sys.stderr = sys.__stderr__;
sys.argv = new PyList();
} }
} }
catch (Exception ex) catch (Exception ex)

View file

@ -1,4 +1,3 @@
using BotSharp.Abstraction.Models;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Python.Runtime; using Python.Runtime;
using System.Threading.Tasks; using System.Threading.Tasks;
@ -35,6 +34,8 @@ public class PyInterpretService : ICodeInterpretService
sys.stdout = stringIO; sys.stdout = stringIO;
sys.stderr = stringIO; sys.stderr = stringIO;
var org = sys.argv;
// Set global items // Set global items
using var globals = new PyDict(); using var globals = new PyDict();
if (codeScript.Contains("__main__") == true) if (codeScript.Contains("__main__") == true)
@ -43,9 +44,9 @@ public class PyInterpretService : ICodeInterpretService
} }
// Set arguments // Set arguments
var list = new PyList();
if (options?.Arguments?.Any() == true) if (options?.Arguments?.Any() == true)
{ {
var list = new PyList();
list.Append(new PyString("code.py")); list.Append(new PyString("code.py"));
foreach (var arg in options.Arguments) foreach (var arg in options.Arguments)
@ -56,8 +57,8 @@ public class PyInterpretService : ICodeInterpretService
list.Append(new PyString($"{arg.Value}")); list.Append(new PyString($"{arg.Value}"));
} }
} }
sys.argv = list;
} }
sys.argv = list;
// Execute Python script // Execute Python script
PythonEngine.Exec(codeScript, globals); PythonEngine.Exec(codeScript, globals);
@ -68,6 +69,7 @@ public class PyInterpretService : ICodeInterpretService
// Restore the original stdout/stderr // Restore the original stdout/stderr
sys.stdout = sys.__stdout__; sys.stdout = sys.__stdout__;
sys.stderr = sys.__stderr__; sys.stderr = sys.__stderr__;
sys.argv = new PyList();
return new CodeInterpretResult return new CodeInterpretResult
{ {