-
Notifications
You must be signed in to change notification settings - Fork 720
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
why sys.stdout returned null #370
Comments
@yagweb can you try suggestions from here: https://mail.python.org/pipermail/pythondotnet/2016-May/001777.html http://stackoverflow.com/questions/37289082/redirect-stdout-stderr-from-python-embedded-in-net-application-to-text-box We should probably include some API to make this easier, since some people are already using this in ad-hoc ways. |
in your example make sure you acquire the GIL with dynamic sys = PythonEngine.ImportModule("sys");
dynamic stdout = sys.stdout; |
@denfromufa Yes my code was put in the curly braces of '''using (Py.GIL()) {}'''. I have tried your code snippet, but the stdout is still null. I'm not sure whether it's a problem of CPython or pythonnet. Maybe somebody else can try it with the other versions of CPython to get more clues. For redirecting stdout, 3 solutions are found according to these links, but the most elegant one (similar to the 3rd solution) provided by Cameron Hayne is not working because of the limitation of pythonnet. It needs “make any .NET object can be passed to CPython”. Here I give a briefly description, hope they can help with the API design. Solution 1: Not automatically, so not suggested.
Solution 2: Make Python call Console.Write automatically.
Solution 3
Solution 4: The best one but not working now.
Howerver, Exception is throw when assigning sys.stdout because variable output is not of type PyObject. Make the Output class inherit from PyObject does not work. A possible solution is updating the TrySetMember method of PyObject as below. Make any .NET object can be passed to CPython is very attractive. Solution 3 may provide some help.
It’s worth to mention that, after set the sys.stdout in the three solutions, the stdout can be obtained normally
|
I like that last proposal a lot. Shouldn't be too difficult. |
@denfromufa @filmor I think this proposal is different from #94 I read the source code of converter.cs and found a way to do so. The PR has be committed. Several tests have conducted for this modification. |
We don't need to keep this open, this is documented behaviour: https://docs.python.org/3/library/sys.html#sys.__stdin__
|
@filmor @denfromufa I agree with this explanation. I have a proposal based on this mechanism. When we embedded Python in a .NET Console application. Usually we want the Python print statement outputs to the screen directly. So, what about the function PythonEngine.Initialize automatically redirects the sys.stdout and sys.stderr to the Console when it detecting the current application is a Console application just like the Python does, and the Solution 4 can be used here. Or, add an argument "bool isredirect=false" to PythonEngine.Initialize, when the Initialize function is called with isredirect=true, it redirects the sys.stdout and sys.stderr to the Console using Solution 4. |
@filmor @denfromufa If the Solution 4 is chosen as the recommended way for the sys.stdout redirection. It's better to provide a .NET interface listing the methods needed. It can help us when we want the redirection in other senses. |
I agree with that, too. However, there doesn't seem to be a simple way to detect whether an application is running from a console in .NET. Also, the override should probably be done by wrapping |
For any one who maybe interested in this topic, I add my PySysIO class here. No automatically redirect, it can be used like this, or in this way.
|
Environment
Details
Python is embedded in my .NET application using pythonnet. I found the output of the print statement in a python script is not showing on the screen until a "sys.stdout.flush()" called. I don't known how to redirect sys.stdout in .NET.
In order to call the flush() method directly in .NET, I tried
and both sys.stdout and the variable stdout returned are None here.
In python, I tried
and it outputs a
_io.TextIOWrapper
object.Why sys.stdout returned null here? How to make the output of python print statement show automatically?
Thanks.
The text was updated successfully, but these errors were encountered: