Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Print Output Redirection not fully works #1863

Open
bugtesterdani opened this issue Jan 5, 2025 · 1 comment
Open

Print Output Redirection not fully works #1863

bugtesterdani opened this issue Jan 5, 2025 · 1 comment

Comments

@bugtesterdani
Copy link

Prerequisites

The issue tracker is used to report bugs and request new features, NOT to ask questions.

Questions should be posted in Discussions or to the users mailing list which can be accessed at
https://ironpython.groups.io/g/users.

  • [ YES ] Are you running the latest version?
  • [ YES ] Are you reporting to the correct repository?
  • [ YES ] Did you perform a cursory search?

Description

I have a Python Script running from C#, by calling a def function inside a class of a file.
The Console prints out the print(...) values, but the Function i want to be called (StreamWriter custom from GPT) is not being called. By C# Code Running with Console.WriteLine it calls the function properly.

Steps to Reproduce

Code i used (Runner class C#):

internal Runner(StreamWriter? writer = null, MemoryStream? msPython = null, Action<object?, MyEvtArgs<string>>? tboxwritepython = null)
{
    _verbose = Statics._variables.verbose;
    if (writer != null)
    {
        _writer = writer;
        _verbose = false;
    }
    if (msPython != null || tboxwritepython != null)
    {
        if (msPython == null)
            msPython = new();
        if (tboxwritepython == null)
            tboxwritepython = new((obj1, tar1) => { Console.WriteLine(tar1.ToString()); });
        _pywriterms = msPython;
        _pywriter = new(msPython);
        _pywriter.StringWritten += new EventHandler<MyEvtArgs<string>>(tboxwritepython);
    }
}

internal object? InvokePythonMethod(string classname, string methodname, params object[] args)
{
    if (_pythonScripts.Count == 0)
        throw new InvalidOperationException("No Python script added.");

    var runtime = Python.CreateRuntime();

    if (_pywriter != null)
    {
        runtime.IO.SetOutput(_pywriterms, _pywriter);
        runtime.IO.SetErrorOutput(_pywriterms, _pywriter);
    }

    runtime.IO.RedirectToConsole();
    var engine = Python.GetEngine(runtime);

    var searchPaths = engine.GetSearchPaths();
    if (!searchPaths.Contains(Environment.CurrentDirectory))
        searchPaths.Add(Environment.CurrentDirectory);
    engine.SetSearchPaths(searchPaths);

    var scope = engine.CreateScope();
    foreach (var script in _pythonScripts)
    {
        engine.ExecuteFile(script, scope);
    }

    dynamic pyClass = scope.GetVariable(classname);
    dynamic pyInstance = engine.Operations.CreateInstance(pyClass);
    dynamic pyMethod = engine.Operations.GetMember(pyInstance, methodname);
    
    var result = pyMethod.Call(args);
    return result;
}

My ViewModel Code is this:

public ICommand RunScript => new cmdHandler(() =>
{
    PreRun();
    Task.Run(() =>
    {
        for (int i = 0; i < ParallelCount; i++)
        {
            Runner run = new(
                writer: new ObservableStreamWriter(new MemoryStream(), ((str) => UpdateOutput(str, i))),
                tboxwritepython: new(((obj1, tar1) => UpdateOutput(tar1.Value, i))));
            Running(run, i);
        }
    });
}, () => true);

private void UpdateOutput(string outp, int index, Results result = Results.INFO, string DrawRef = "LOG MESSAGE")
{
    UpdateLineEntry(index, new()
    {
        Message = outp,
        Drawref = DrawRef,
        Result = result
    });
}

The Python Code is this:

class testing:
    def __init__(self):
        print("Hello Init")

    def submit(self):
        print("entered text were " + self.entry.get())

    def outputing(self, Name):
        print(f"Hello {Name}")
        return "Whatsup"

Expected behavior:
Actual behavior:

And by calling then: InvokePythonMethod("testing", "outputing", "ABC");
The Function (UpdateOutput) is not being called. But with a C# Code call, it works fine.

Version Information

If you are using the ipy console program, provide output of executing ipy -VV. If it is a local build, provide also the git hash of the commit used to build IronPython. In either case, provide the type of the operating system used.

If you are using the IronPython engine embedded in a .NET application, provide the version number of the NuGet package used (or if it is a local build, the git hash of the commit used to build IronPython), and the following info:

  • .NET platform used (choice from: .NET, .NET Core, .NET Framework, Mono, Unity, Xamarin),

  • Version of the .NET platform used,

  • Operating system used,

  • Value of sys.version, from imported module sys.

  • .NET Core 8.0.11

  • Windows 11 10.0.22631

  • IronPython Version 3.4.1 (NuGet)

  • Visual Studio 2022 Version 17.12.3

@bugtesterdani
Copy link
Author

Already tested issues:

1204

1210

961

1311

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant