Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!goblin2!goblin.stu.neva.ru!newsfeed.xs4all.nl!newsfeed2a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; '"this': 0.03; 'value,': 0.04; 'interpreter': 0.05; 'subject:skip:s 10': 0.07; 'variables': 0.07; '%s"': 0.09; 'msg': 0.09; 'cc:addr:python-list': 0.11; 'python': 0.11; 'def': 0.12; '24,': 0.16; 'called.': 0.16; 'closure,': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'globals': 0.16; "handler')": 0.16; 'handler.': 0.16; 'held?': 0.16; 'none.': 0.16; 'one-way': 0.16; 'script?': 0.16; 'stderr': 0.16; 'exception': 0.16; 'wrote:': 0.18; 'variable': 0.18; 'thu,': 0.19; 'seems': 0.21; 'saying': 0.22; 'cc:addr:python.org': 0.22; 'print': 0.22; 'install': 0.23; 'error': 0.23; 'cc:2**0': 0.24; 'order.': 0.26; 'header:In-Reply- To:1': 0.27; 'idea': 0.28; 'function': 0.29; 'am,': 0.29; 'sets': 0.30; 'message-id:@mail.gmail.com': 0.30; 'work.': 0.31; "d'aprano": 0.31; 'steven': 0.31; 'critical': 0.32; 'problem': 0.35; 'something': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'words,': 0.36; 'possible': 0.36; 'changed': 0.39; 'called': 0.40; 'how': 0.40; 'bottom': 0.67; 'jul': 0.74; 'guaranteed': 0.75; 'shutdown': 0.84; 'snapshot': 0.84; 'to:none': 0.92; 'connection,': 0.95 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=N/k/1GbdIqM/JYyO+WGpy/idZwJMIYkitweGXQFHiJI=; b=jvHxvED/RX5rnJJ/hffoo/tQrFVLEDaU5iYt+X8BB8NkBv5+icc/qeRaBtW9Rdr68Z vFmkTKWHu25wyydkEDTBwkja5SJfQwgQn76kv1C/CF0YUV4IOS7pN4b3Xk4bwvauxyxj GOLH7WAWeN28T3/L87skgcx9Wh/wl5/s8JN0mj91wiizXJGeWQ0pO74OGfzRvZFXuhI2 S6cbpAUifinhHPzWUeQo1VyglNGdtrRLUTpJw/DDHaehyB2j3gjf/7ZzqnI170DUH6ZB DCieBB9wTfpQ9dcOl7Jn+ZUij/ejlX+kMrMzahLrcoF8Sl4kgWh3ij0tN4+VxV2GBBXJ XhsQ== MIME-Version: 1.0 X-Received: by 10.220.164.198 with SMTP id f6mr7382043vcy.51.1406166647907; Wed, 23 Jul 2014 18:50:47 -0700 (PDT) In-Reply-To: <53d061c1$0$29966$c3e8da3$5496439d@news.astraweb.com> References: <53cf60d3$0$29897$c3e8da3$5496439d@news.astraweb.com> <53cf6867$0$29897$c3e8da3$5496439d@news.astraweb.com> <53d061c1$0$29966$c3e8da3$5496439d@news.astraweb.com> Date: Thu, 24 Jul 2014 11:50:47 +1000 Subject: Re: My sys.excepthook dies painfully From: Chris Angelico Cc: "python-list@python.org" Content-Type: text/plain; charset=UTF-8 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 29 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1406167091 news.xs4all.nl 2872 [2001:888:2000:d::a6]:36105 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:75112 On Thu, Jul 24, 2014 at 11:30 AM, Steven D'Aprano wrote: > However, I think I have a glimmer of an idea for how the global variable > might be set to None. When the Python interpreter shuts down, it sets > global variables to None in some arbitrary order. If the excepthook > function isn't called until after the shutdown process begins, then > depending on the phase of the moon, it's possible that ``mylogger`` may > have been set to None by the time it is called. In other words, the problem changed when you added the NameError trigger at the bottom of the script? Would it be possible to snapshot all critical globals with a closure, to force them to be held? Something like: def handler_gen(mylogger, sys): def my_error_handler(type, value, tb): msg = "Uncaught %s: %s" % (type, value) mylogger.exception(msg) sys.__excepthook__(type, value, tb) # print the traceback to stderr # Install exception handler. mylogger.info('installing error handler') sys.excepthook = handler_gen(mylogger, sys) It seems crazy, but it might work. It's a guaranteed one-way connection, saying "this function NEEDS these objects". ChrisA