Path: csiph.com!news.fcku.it!peer02.fr7!news.highwinds-media.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Random832 Newsgroups: comp.lang.python Subject: Re: print() function with encoding= and errors= parameters? Date: Wed, 03 Aug 2016 10:05:12 -0400 Lines: 18 Message-ID: References: <1470227371.397715.684773553.140807A6@webmail.messagingengine.com> <1470233112.2667238.684861393.67022A98@webmail.messagingengine.com> Mime-Version: 1.0 Content-Type: text/plain Content-Transfer-Encoding: 7bit X-Trace: news.uni-berlin.de eetpa8eZEHmP+j+LOGw/qApcE3skbejU99kVttKFeKYg== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'received:internal': 0.09; 'skip:[ 30': 0.09; 'stdout': 0.09; 'stringio': 0.09; 'sys.stdout': 0.09; 'underlying': 0.09; 'python': 0.10; 'output': 0.13; 'wed,': 0.15; 'encoding': 0.15; 'classes)': 0.16; 'malcolm': 0.16; 'message-id:@webmail.messagingengine.com': 0.16; 'received:10.202': 0.16; 'received:10.202.2': 0.16; 'received:66.111': 0.16; 'received:66.111.4': 0.16; 'received:io': 0.16; 'received:messagingengine.com': 0.16; 'received:psf.io': 0.16; 'subject:parameters': 0.16; 'wrote:': 0.16; 'bytes': 0.18; 'duplicate': 0.18; 'aug': 0.20; 'libraries': 0.22; 'sends': 0.22; '(you': 0.23; 'errors': 0.23; 'long,': 0.24; 'header:In-Reply- To:1': 0.24; 'function': 0.28; 'way?': 0.29; 'print': 0.30; 'option': 0.31; 'class': 0.33; 'could': 0.35; 'configured': 0.35; "isn't": 0.35; 'but': 0.36; 'there': 0.36; 'closing': 0.36; 'to:addr:python-list': 0.36; 'subject:?': 0.36; 'subject:: ': 0.37; 'received:10': 0.37; 'being': 0.37; "won't": 0.38; 'received:66': 0.38; 'to:addr:python.org': 0.40; 'subject:with': 0.40; 'your': 0.60; 'close': 0.61; 'header:Message-Id:1': 0.61; 'concerns': 0.66; 'behavior?': 0.84; "it'd": 0.84 DKIM-Signature: v=1; a=rsa-sha1; c=relaxed/relaxed; d=fastmail.com; h= content-transfer-encoding:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to:x-sasl-enc :x-sasl-enc; s=mesmtp; bh=lJ7w2qzRUVLvF6eqizwTeEaMU5E=; b=5/fKpq /sHOB8M2sSTiOD//S3jOTLz3wvewq0mWCPVpLWMLaJFoEdxLsXAS8+69202Rxol6 z2awHqc5RXOxNrcxy12/CRnMncrMSUPt5DUGu5hQFw/OF/tEN6HZebEMO0w0q9di 15g81OL3pJ76aiRPvTgntdZtSNdg6sC3+yLfs= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed/relaxed; d= messagingengine.com; h=content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-sasl-enc:x-sasl-enc; s=smtpout; bh=lJ7w2qzRUVLvF6e qizwTeEaMU5E=; b=YLV0cisKrb3n+p3uzdhY+nZmDjR5z8AjnhVtVDTjCgz/Wxo 2gZO/9JXBACq+ZdFS4sJpo+BZ6/E6Q1n6GMAWZpN8Tzqsdck/+So0DWdoZj4PME+ T8WLroRsvYy0XJ+y9olWTrroZrhIjHp06dQZogBoehvbmgalaCIzl0uJUqOg= X-Sasl-Enc: mlIn37QeaF5wqZ4Zad05dkhVfD8VZ0alxlDiBZoDNI/Z 1470233112 X-Mailer: MessagingEngine.com Webmail Interface - ajax-25d422ed In-Reply-To: <1470227371.397715.684773553.140807A6@webmail.messagingengine.com> X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.22 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Mailman-Original-Message-ID: <1470233112.2667238.684861393.67022A98@webmail.messagingengine.com> X-Mailman-Original-References: <1470227371.397715.684773553.140807A6@webmail.messagingengine.com> X-Received-Bytes: 4712 X-Received-Body-CRC: 2548550625 Xref: csiph.com comp.lang.python:112292 On Wed, Aug 3, 2016, at 08:29, Malcolm Greene wrote: > Looking for a way to use the Python 3 print() function with encoding and > errors parameters. > > Are there any concerns with closing and re-opening sys.stdout so > sys.stdout has a specific encoding and errors behavior? Would this break > other standard libraries that depend on sys.stdout being configured a > specific way? You could duplicate stdout [open(os.dup(sys.stdout.fileno()), ...)] You could make an IO class which sends bytes output to sys.stdout.buffer but won't close it when closed (you know, it'd be nice to be able to have "not owned underlying stream" as an option of the standard IO classes) If your output isn't going to be long, you could print everything to a StringIO and then write to sys.stdout.buffer at the end.