Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Stephen Hansen Newsgroups: comp.lang.python Subject: Re: Most probably a stupid question, but I still want to ask Date: Sun, 10 Apr 2016 22:42:23 -0700 Lines: 37 Message-ID: References: <570b02db$0$1605$c3e8da3$5496439d@news.astraweb.com> <570b2000$0$22142$c3e8da3$5496439d@news.astraweb.com> <1460350018.3507765.574780001.5531F1C0@webmail.messagingengine.com> <45c1ee67-16c5-45b3-bf3c-f33efb42d3c3@googlegroups.com> <1460353343.3517647.574804705.5CF46A82@webmail.messagingengine.com> Mime-Version: 1.0 Content-Type: text/plain Content-Transfer-Encoding: 7bit X-Trace: news.uni-berlin.de c4aiqrflCWJQSVxk3HY1WAdToB8g6bzAroHUGC6dmmWw== 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; 'exception.': 0.07; 'puts': 0.07; 'so?': 0.07; 'valueerror:': 0.07; 'subject:question': 0.08; 'ast': 0.09; 'received:internal': 0.09; 'reliably': 0.09; 'subject:still': 0.09; 'throws': 0.09; 'valueerror': 0.09; '(either': 0.16; '--s': 0.16; '2016': 0.16; '63,': 0.16; 'malformed': 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:66.111.4.27': 0.16; 'received:io': 0.16; 'received:messagingengine.com': 0.16; 'received:out3-smtp.messagingengine.com': 0.16; 'received:psf.io': 0.16; 'worst': 0.16; 'wrote:': 0.16; 'string': 0.17; 'input': 0.18; '>>>': 0.20; 'fix': 0.21; '"",': 0.22; 'stephen': 0.22; 'wrote': 0.23; 'import': 0.24; '(most': 0.24; 'header:In-Reply- To:1': 0.24; 'skip:" 20': 0.26; 'skip:_ 20': 0.26; 'error': 0.27; 'raise': 0.29; 'reporting': 0.29; 'traceback': 0.33; 'file': 0.34; 'could': 0.35; "isn't": 0.35; 'but': 0.36; 'monday,': 0.36; 'to:addr:python-list': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'received:10': 0.37; 'skip:v 20': 0.38; 'skip:z 10': 0.38; 'received:66': 0.38; 'log': 0.38; 'someone': 0.38; 'why': 0.39; 'data': 0.39; 'to:addr:python.org': 0.40; 'your': 0.60; 'avoid': 0.61; 'header:Message-Id:1': 0.61; 'skip:n 10': 0.62; 'more': 0.63; 'safe': 0.63; 'utc+5:30,': 0.84; 'maybe,': 0.91; 'subject:want': 0.93 DKIM-Signature: v=1; a=rsa-sha1; c=relaxed/relaxed; d=ixokai.io; 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=UFts6byMRqXNu8IBIHfHXpcFbQI=; b=WyNY7n nSshkK+GRW9XYmD/ruLp04wLeDA/y/K9HMfDxTNESePJ4coXClkA3Bt384pzIrXz vj5QncA+K5Ai5JJrBvoltEE1fLTocFa5S9LXRLDJ9yZyL5+3KAJcpwyAhV/k4rGa zv5x9crs+u2aqcuEm+cFabMCmYrE3ajdPizX4= 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=UFts6byMRqXNu8I BIHfHXpcFbQI=; b=Hz2NjKapytlfGTgRCghNI8ts8i6AylQLScg5O+77aZyh9qy CgAYnMNaGrl0KASShraNtnq2ruvQKz1uQgK5Lu0clbxPd5Gp881WIzRrjz+VZjUa t90VA5/PVUc92oYxScxyAUxK8BU5Y7yVzr8Hh6UAtFJSA/piFVP2NiW3nZe4= X-Sasl-Enc: dbo11lzApafCPbAE8rmUIQDLcfwj/Wzmwf06VeRt7mkT 1460353343 X-Mailer: MessagingEngine.com Webmail Interface - ajax-82e6ff7b In-Reply-To: <45c1ee67-16c5-45b3-bf3c-f33efb42d3c3@googlegroups.com> X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.21 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: <1460353343.3517647.574804705.5CF46A82@webmail.messagingengine.com> X-Mailman-Original-References: <570b02db$0$1605$c3e8da3$5496439d@news.astraweb.com> <570b2000$0$22142$c3e8da3$5496439d@news.astraweb.com> <1460350018.3507765.574780001.5531F1C0@webmail.messagingengine.com> <45c1ee67-16c5-45b3-bf3c-f33efb42d3c3@googlegroups.com> Xref: csiph.com comp.lang.python:106861 On Sun, Apr 10, 2016, at 10:18 PM, Rustom Mody wrote: > On Monday, April 11, 2016 at 10:17:13 AM UTC+5:30, Stephen Hansen wrote: > > On Sun, Apr 10, 2016, at 09:03 PM, Fillmore wrote: > > > and the (almost always to be avoided) use of eval() > > > > FWIW, there's ast.literal_eval which is safe and there's no reason to > > avoid it. > > Its error reporting is clunky: > > >>> from ast import literal_eval as le > >>> le("(1)") > 1 > >>> le("(1,)") > (1,) > >>> le("1") > 1 > >>> le("{1:x}") > Traceback (most recent call last): > File "", line 1, in > File "/usr/lib/python2.7/ast.py", line 80, in literal_eval > return _convert(node_or_string) > File "/usr/lib/python2.7/ast.py", line 63, in _convert > in zip(node.keys, node.values)) > File "/usr/lib/python2.7/ast.py", line 62, in > return dict((_convert(k), _convert(v)) for k, v > File "/usr/lib/python2.7/ast.py", line 79, in _convert > raise ValueError('malformed string') > ValueError: malformed string So? Worst case scenario, someone puts invalid data into the file and it throws an exception. Could it be more specific? Maybe, but I don't see why it needs to be. If your input isn't reliably formatted, catch ValueError and log it and fix (either what wrote it or change how you read it). --S