Rocksolid Light

Welcome to RetroBBS

mail  files  register  newsreader  groups  login

Message-ID:  

Have you reconsidered a computer career?


devel / comp.lang.python / Re: First two bytes of 'stdout' are lost

SubjectAuthor
o Re: First two bytes of 'stdout' are lostThomas Passin

1
Re: First two bytes of 'stdout' are lost

<mailman.98.1712841560.3468.python-list@python.org>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=29847&group=comp.lang.python#29847

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!fu-berlin.de!uni-berlin.de!not-for-mail
From: list1@tompassin.net (Thomas Passin)
Newsgroups: comp.lang.python
Subject: Re: First two bytes of 'stdout' are lost
Date: Thu, 11 Apr 2024 09:19:09 -0400
Lines: 51
Message-ID: <mailman.98.1712841560.3468.python-list@python.org>
References: <CA+cSArh=U-H6nW7nYBBD5v=ZUAvGpE1M_nB9BUoYzvca9XsKfA@mail.gmail.com>
<804f3151-f662-4419-b1d2-9b0788a6585b@tompassin.net>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
X-Trace: news.uni-berlin.de Qf39A4xOXEkJECC8Sz6LIww7Ao01y5Xi3s0bXjaIzClQ==
Cancel-Lock: sha1:NbPqJjyfSUXoNNNBChhe/f4InZc= sha256:ztPq05rVeh3alOQJ/phMVaJa5eaeiCAJmM2k0NAtO5M=
Return-Path: <list1@tompassin.net>
X-Original-To: python-list@python.org
Delivered-To: python-list@mail.python.org
Authentication-Results: mail.python.org; dkim=pass
reason="2048-bit key; unprotected key"
header.d=tompassin.net header.i=@tompassin.net header.b=b0NjdFwC;
dkim-adsp=pass; dkim-atps=neutral
X-Spam-Status: OK 0.003
X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'looks': 0.02; 'this:':
0.03; 'def': 0.04; 'hopefully': 0.07; 'string': 0.07; 'api,':
0.09; 'subject:two': 0.09; 'import': 0.15; 'api.': 0.16; 'c++':
0.16; 'changed:': 0.16; 'characters.': 0.16; 'manipulating': 0.16;
'naively': 0.16; 'print(': 0.16; 'received:10.0.0': 0.16;
'received:64.90': 0.16; 'received:64.90.62': 0.16;
'received:64.90.62.162': 0.16; 'received:dreamhost.com': 0.16;
'stdout': 0.16; 'stdout.': 0.16; 'variable,': 0.16; 'wrote:':
0.16; 'python': 0.16; 'api': 0.17; 'instead': 0.17; 'to:addr
:python-list': 0.20; 'written': 0.22; "i've": 0.22; 'code': 0.23;
'run': 0.23; 'seems': 0.26; 'ideas': 0.28; 'wrong': 0.28; 'asked':
0.29; 'header:User-Agent:1': 0.30; 'am,': 0.31; 'extract': 0.32;
'point,': 0.32; 'python-list': 0.32; 'received:10.0': 0.32;
'received:mailchannels.net': 0.32;
'received:relay.mailchannels.net': 0.32; 'but': 0.32; 'someone':
0.34; 'header:In-Reply-To:1': 0.34; 'running': 0.34; 'trying':
0.35; 'using': 0.37; 'class': 0.37; 'two': 0.39; 'quite': 0.39;
'this,': 0.39; 'use': 0.39; 'something': 0.40; 'method': 0.61;
'point.': 0.62; 'limited': 0.62; 'skip:m 20': 0.63; 'now,': 0.67;
'worked': 0.67; 'header:Received:6': 0.67; 'received:64': 0.67;
'right': 0.68; 'exactly': 0.68; 'end,': 0.69; 'compared': 0.71;
'content': 0.72; 'capture': 0.84; 'characters': 0.84; 'clue':
0.84; 'lose': 0.84; 'ruled': 0.84; 'subject:First': 0.84
X-Sender-Id: dreamhost|x-authsender|tpassin@tompassin.net
ARC-Seal: i=1; s=arc-2022; d=mailchannels.net; t=1712841550; a=rsa-sha256;
cv=none;
b=ZQ2H0uoEpS4ZkMC/u/wi7WQ8QEMjJmvKVcOtThXXyXWfk7nhLcBU2A2G29Yp8OfFqICg4L
YoUDIQjM7PBkf2GZY8VbIgJ2yPma+4hsPKxGMhRjg9My5sSScB5N/BPqZGYg2/aYXu270s
w6oDgXMdpR6lTxtSBXiLK0jEMg0YIctkWNUZJD/Aro1rpbxHDQzshu2ho2xeNn9qP9dluc
tqhQnQ0lIypsuz44RZvIUmqaBn6h1lkpXzd8rOmwUM6tfhenkEyyibOpGcZPrHLt8WHbux
U/eIIzgiSz7kRnh28juTVj3H9UiSB2A+WrsT0F2yv7crOdN0ur8DAZohDKKXUw==
ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed;
d=mailchannels.net; s=arc-2022; t=1712841549;
h=from:from:reply-to:subject:subject:date:date:message-id:message-id:
to:to:cc:mime-version:mime-version:content-type:content-type:
content-transfer-encoding:content-transfer-encoding:
in-reply-to:in-reply-to:references:references:dkim-signature;
bh=q0TBuydf0VByyOYnIfmNnLE+Z3wzsU7XqIgOWiRRHb8=;
b=VoX13KAzfyITGSoIlHYbUZ+4amac5LYkUbWxy/6sZPfKAHjiVXUafYlyjDYH1mU0bcbEKk
j4DGyXfOqFRQfJlCgW3iPlWNUFpTv/Kx/XBRzaSnphgqoB2cWP0FjzfyTjOmHmGKvjo/TN
LKfLzQtklirIF16LfHdVb6FsgRHCMQz4NrwP39rxFITiUxxpe+0toa15E/DhHSgtNwuipT
EA+vHCJscvyxYMJB3G7/ODKsnehw6ZVFfccqqNCckkvxAj8v6PRl40Cidz1SIpt+7lD2aM
Fb5kUJd9ifeI6jFws5BB7oulKQtXtcLYMyDPTZfZcptNuNZvJ50kMtTOlwAfVA==
ARC-Authentication-Results: i=1; rspamd-878bcf566-p9cz5;
auth=pass smtp.auth=dreamhost smtp.mailfrom=list1@tompassin.net
X-Sender-Id: dreamhost|x-authsender|tpassin@tompassin.net
X-MC-Relay: Neutral
X-MailChannels-SenderId: dreamhost|x-authsender|tpassin@tompassin.net
X-MailChannels-Auth-Id: dreamhost
X-Plucky-Bottle: 4360a89e5b3d1a01_1712841550213_75022736
X-MC-Loop-Signature: 1712841550213:2332776762
X-MC-Ingress-Time: 1712841550213
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=tompassin.net;
s=dreamhost; t=1712841549;
bh=q0TBuydf0VByyOYnIfmNnLE+Z3wzsU7XqIgOWiRRHb8=;
h=Date:Subject:To:From:Content-Type:Content-Transfer-Encoding;
b=b0NjdFwCGzOgEnZktbmhAXBIPWrxT1OoxFZKRKuTkIdkIAd6sojBVdLsS/EOic04n
8qY7+rmGD/5ko3gmoWAKg+P83gcqZ+26vr4G8I/bEJaWfeg4DVINByByUBIIXC200s
ORe4WIrgDXjeNR7SmlNQ5khXL5vhYbjhEQEWY8uwa3VQ37EPkzPC/KM9k7tJ4+5suE
T1X4SdIl1pdvQT9o5tRwp/kYKQggLXtUSo5nat2tMceDtmcR2//20fuyfIH+SWJzVP
5S8si8y4wK40QN+GbUU/BzDe4NT9OeVEJ8B7M8WcR2gfKolqv7Vo0YnEHn5nDhCP05
r752UlYmyX5aA==
User-Agent: Mozilla Thunderbird
Content-Language: en-US
In-Reply-To: <CA+cSArh=U-H6nW7nYBBD5v=ZUAvGpE1M_nB9BUoYzvca9XsKfA@mail.gmail.com>
X-BeenThere: python-list@python.org
X-Mailman-Version: 2.1.39
Precedence: list
List-Id: General discussion list for the Python programming language
<python-list.python.org>
List-Unsubscribe: <https://mail.python.org/mailman/options/python-list>,
<mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive: <https://mail.python.org/pipermail/python-list/>
List-Post: <mailto:python-list@python.org>
List-Help: <mailto:python-list-request@python.org?subject=help>
List-Subscribe: <https://mail.python.org/mailman/listinfo/python-list>,
<mailto:python-list-request@python.org?subject=subscribe>
X-Mailman-Original-Message-ID: <804f3151-f662-4419-b1d2-9b0788a6585b@tompassin.net>
X-Mailman-Original-References: <CA+cSArh=U-H6nW7nYBBD5v=ZUAvGpE1M_nB9BUoYzvca9XsKfA@mail.gmail.com>
 by: Thomas Passin - Thu, 11 Apr 2024 13:19 UTC

On 4/11/2024 8:42 AM, Olivier B. via Python-list wrote:
> 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?

I've not used the C API, so just for fun I asked ChatGPT about this and
it suggested that a flush after writing to StringIO might do it. It
suggested using a custom class for this purpose:

class MyStringIO(StringIO):
def write(self, s):
# Override write method to ensure all characters are written
correctly
super().write(s)
self.flush()

You would use it like this:

sys.stdout = mystdout = MyStringIO()

I haven't tested it but it seems reasonable, although I would have
naively expected to lose bytes from the end, not the beginning.

1
server_pubkey.txt

rocksolid light 0.9.8
clearnet tor