Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Malcolm Greene Newsgroups: comp.lang.python Subject: Re: Possible to capture cgitb style output in a try/except section? Date: Tue, 26 Jul 2016 11:52:47 -0400 Lines: 25 Message-ID: References: <1469527864.2996118.676987281.0EE8893F@webmail.messagingengine.com> <1469548367.1451657.677295577.18F0D7FD@webmail.messagingengine.com> Mime-Version: 1.0 Content-Type: text/plain Content-Transfer-Encoding: 7bit X-Trace: news.uni-berlin.de Uoug316imTGSYBSGOSA1eAZfNaIzF2vWU7P2fkT6ojxQ== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.009 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'handler': 0.04; '(python': 0.05; 'parameter.': 0.09; 'received:internal': 0.09; 'stringio': 0.09; 'output': 0.13; 'from:addr:python': 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; 'appears': 0.23; 'import': 0.24; 'header:In-Reply-To:1': 0.24; 'helpful': 0.27; 'subject:/': 0.30; 'help!': 0.30; 'shorter': 0.33; 'steven': 0.33; 'to:addr:python-list': 0.36; 'subject:?': 0.36; 'subject:: ': 0.37; 'received:10': 0.37; 'difference': 0.38; 'received:66': 0.38; 'thank': 0.38; 'to:addr:python.org': 0.40; 'your': 0.60; 'header:Message-Id:1': 0.61; 'default': 0.61; 'yourself': 0.73; 'peter,': 0.84; 'subject:Possible': 0.84; 'subject:try': 0.84; 'technique': 0.93 DKIM-Signature: v=1; a=rsa-sha1; c=relaxed/relaxed; d=bdurham.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=zrLupNfeFTzU84wDfn+3bBESkK0=; b=V3WfD1 8kdeAyCwisYssjcF4pvw1u1/hKEcZDBAH2chgboD32k70yeLbE21bzry3/qcGSg8 /FiXfiDNDJX3b17p3vgN2X5oUc4exrl8GO93aHHwmOGe1K7YIWFVEg63ElEfQDc4 DqidRDQatxdSzcFqY0Jea70asAJSgm5u5U/M0= 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=zrLupNfeFTzU84w Dfn+3bBESkK0=; b=RwkxAN0UDZZziJgEnkKKKAB7Ql6BpHH0nkanYNCIC2OzH/l 0L5M/cQv0Vh5qu2LsEBWkRQPXUxQuhqosfNN/d/dCxBh2KzFwCo4JVCZLdCALWmL arUjXPIoXqd1GI6kyrTh3rHOXTlo+MR2vrejucq2h5+AD/PHZ7SJ5Qqrq77U= X-Sasl-Enc: ExsfEAvYEnm/JrHmonl+K1IcimwEsqnntUu3osamf1PY 1469548367 X-Mailer: MessagingEngine.com Webmail Interface - ajax-3c2832c2 In-Reply-To: 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: <1469548367.1451657.677295577.18F0D7FD@webmail.messagingengine.com> X-Mailman-Original-References: <1469527864.2996118.676987281.0EE8893F@webmail.messagingengine.com> Xref: csiph.com comp.lang.python:111899 Hi Steven and Peter, Steven: Interestingly (oddly???) enough, the output captured by hooking the cgitb handler on my system appears to be shorter than the default cgitb output. You can see this yourself via this tiny script: import cgitb cgitb.enable(format='text') x = 1/0 The solution I came up with (Python 3.5.1) was to use your StringIO technique with the Hook's 'file=' parameter. import io cgitb_buffer = io.StringIO() cgitb.Hook(file=cgitb_buffer, format='text) return cgitb_buffer.getvalue() Peter: Your output was helpful in seeing the difference I mentioned above. Thank you both for your help! Malcolm