Sujet : Re: First two bytes of 'stdout' are lost
De : perso.olivier.barthelemy (at) *nospam* gmail.com (Olivier B.)
Groupes : comp.lang.pythonDate : 11. Apr 2024, 15:09:32
Autres entêtes
Message-ID : <mailman.97.1712840985.3468.python-list@python.org>
References : 1 2
Partly answering myself:
For some reason, right after mystdout has been created, i now have to
do mystdout.seek(0) and this solves the issue.
No idea why though..
Le jeu. 11 avr. 2024 à 14:42, Olivier B.
<
perso.olivier.barthelemy@gmail.com> a écrit :
>
I am trying to use StringIO to capture stdout, in code that looks like this:
>
import sys
from io import StringIO
old_stdout = sys.stdout
sys.stdout = mystdout = StringIO()
print( "patate")
mystdout.seek(0)
sys.stdout = old_stdout
print(mystdout.read())
>
Well, it is not exactly like this, since this works properly
>
This code is actually run from C++ using the C Python API.
This worked quite well, so the code was right at some point. But now,
two things changed:
- Now using python 3.11.7 instead of 3.7.12
- Now using only the python limited C API
>
And it seems that now, mystdout.read() always misses the first two
characters that have been written to stdout.
>
My first ideas was something related to the BOM improperly truncated
at some point, but i am manipulating UTF-8, so the bom would be 3
bytes, not 2.
>
I ruled out wrong C++ code to extract the string from the python
variable, since running a python print of the content of mystdout in
the real stdout also misses the two first characters.
>
Hopefully someone has a clue on what would have changed in Python for
this to stop working compared to python 3.7?