Re: Better Regex and exception handling for this small code

2017-07-18 Thread Ganesh Pal
097b00 >> Note: Task completed successfully. >> Note: CRC:algo = 2, split_crc = 1, unused = 0, initiator_crc = d976d35e, >> journal_crc = a176af10 >> >> >> I have the below piece of code but would like to make this better more >> pythonic , I found regex pattern

Re: Better Regex and exception handling for this small code

2017-07-12 Thread MRAB
have the below piece of code but would like to make this better more pythonic , I found regex pattern and exception handling poor here , any quick suggestion in your spare time is welcome. #open the existing file if the flag is set and check if there is a match log_file='/file.txt' fl

Re: Better Regex and exception handling for this small code

2017-07-12 Thread Nick Mellor
; journal_crc = a176af10 > > > I have the below piece of code but would like to make this better more > pythonic , I found regex pattern and exception handling poor here , any > quick suggestion in your spare time is welcome. > > > #open the existing file if t

Re: Better Regex and exception handling for this small code

2017-07-11 Thread Cameron Simpson
regex pattern and exception handling poor here , any quick suggestion in your spare time is welcome. #open the existing file if the flag is set and check if there is a match log_file='/file.txt' flag_is_on=1 Use "True" instead of "1". A flag is a Boolean thing, a

Re: Better Regex and exception handling for this small code

2017-07-11 Thread Ganesh Pal
ompleted successfully. > Note: CRC:algo = 2, split_crc = 1, unused = 0, initiator_crc = d976d35e, > journal_crc = a176af10 > > > I have the below piece of code but would like to make this better more > pythonic , I found regex pattern and exception handling poor here , any >

Better Regex and exception handling for this small code

2017-07-11 Thread Ganesh Pal
exception handling poor here , any quick suggestion in your spare time is welcome. #open the existing file if the flag is set and check if there is a match log_file='/file.txt' flag_is_on=1 data = None with open(log_file, 'r') as f: data = f.readlines() if flag_is_o

Re: Exception handling for socket.error in Python 3.5/RStudio

2016-02-19 Thread shaunak . bangale
On Monday, February 8, 2016 at 1:05:08 AM UTC-7, shaunak...@gmail.com wrote: > On Sunday, February 7, 2016 at 1:23:32 AM UTC-7, dieter wrote: > > Shaunak Bangale writes: > > > ... > > > while (count > 0): > > > try : > > > # read line from file: > > > print(file.readline()) > >

Re: coroutine, throw, yield, call-stack and exception handling

2016-02-09 Thread Veek. M
Ian Kelly wrote: > On Mon, Feb 8, 2016 at 2:17 AM, Veek. M wrote: >> >> Exceptions can be raised inside a coroutine using the throw( >> >> Exceptions raised in this manner will originate at the currently >> executing yield state-ment in the coroutine.A coroutine can e

Re: coroutine, throw, yield, call-stack and exception handling

2016-02-08 Thread Ian Kelly
On Mon, Feb 8, 2016 at 2:17 AM, Veek. M wrote: > > Exceptions can be raised inside a coroutine using the throw( > > Exceptions raised in this manner will originate at the currently > executing yield state-ment in the coroutine.A coroutine can elect to > catch exception

Re: coroutine, throw, yield, call-stack and exception handling

2016-02-08 Thread Veek. M
Veek. M wrote: > > Exceptions can be raised inside a coroutine using the throw( > > Exceptions raised in this manner will originate at the currently > executing yield state-ment in the coroutine.A coroutine can elect to > catch exceptions and handle them as appropriat

coroutine, throw, yield, call-stack and exception handling

2016-02-08 Thread Veek. M
Exceptions can be raised inside a coroutine using the throw( Exceptions raised in this manner will originate at the currently executing yield state-ment in the coroutine.A coroutine can elect to catch exceptions and handle them as appropriate. It is not safe to use

Re: Exception handling for socket.error in Python 3.5/RStudio

2016-02-08 Thread shaunak . bangale
On Sunday, February 7, 2016 at 1:23:32 AM UTC-7, dieter wrote: > Shaunak Bangale writes: > > ... > > while (count > 0): > > try : > > # read line from file: > > print(file.readline()) > > # parse > > parse_json(file.readline()) > > count = count - 1 > >

Re: Exception handling for socket.error in Python 3.5/RStudio

2016-02-07 Thread dieter
Shaunak Bangale writes: > ... > while (count > 0): > try : > # read line from file: > print(file.readline()) > # parse > parse_json(file.readline()) > count = count - 1 > except socket.error as e: > print('Connection fail', e) > print

Re: Exception handling for socket.error in Python 3.5/RStudio

2016-02-06 Thread Shaunak Bangale
I do have the initiation command defined. Just that I am not allowed to make the username, pwd public. I am absolutely sure I am running the same code. Now opened the same file with Python 3.5 shell and I get following error: from _ssl import RAND_status, RAND_egd, RAND_add ImportError: cannot

Re: Exception handling for socket.error in Python 3.5/RStudio

2016-02-06 Thread Shaunak Bangale
Hi Martin, Answering your questions below: On Fri, Feb 5, 2016 at 1:50 PM, Shaunak Bangale wrote: > Hi Martin, > > Thanks for the detailed reply. I edited, saved and opened the file again. > Still I am getting exactly the same error. > > Putting bigger chunk of code and the error again: > > >

Re: Exception handling for socket.error in Python 3.5/RStudio

2016-02-06 Thread Shaunak Bangale
Hi Martin, Thanks for the detailed reply. I edited, saved and opened the file again. Still I am getting exactly the same error. Putting bigger chunk of code and the error again: # create socket s = socket.socket(socket.AF_INET) #create a SSL context with the recommended security settings for c

Re: Exception handling for socket.error in Python 3.5/RStudio

2016-02-05 Thread Chris Angelico
On Sat, Feb 6, 2016 at 9:35 AM, wrote: > On Friday, February 5, 2016 at 12:58:37 PM UTC-7, shaunak...@gmail.com wrote: >> I am running this python script on R-studio. I have Python 3.5 installed on >> my system. >> >> count = 10 >> while (count > 0): >> try : >> # read line from file

Re: Exception handling for socket.error in Python 3.5/RStudio

2016-02-05 Thread shaunak . bangale
On Friday, February 5, 2016 at 12:58:37 PM UTC-7, shaunak...@gmail.com wrote: > I am running this python script on R-studio. I have Python 3.5 installed on > my system. > > count = 10 > while (count > 0): > try : > # read line from file: > print(file.readline()) > # pa

Re: Exception handling for socket.error in Python 3.5/RStudio

2016-02-05 Thread shaunak . bangale
On Friday, February 5, 2016 at 12:58:37 PM UTC-7, shaunak...@gmail.com wrote: > I am running this python script on R-studio. I have Python 3.5 installed on > my system. > > count = 10 > while (count > 0): > try : > # read line from file: > print(file.readline()) > # pa

Re: Exception handling for socket.error in Python 3.5/RStudio

2016-02-05 Thread Chris Angelico
On Sat, Feb 6, 2016 at 8:08 AM, Bernardo Sulzbach wrote: > On 02/05/2016 07:01 PM, Chris Angelico wrote: >> >> On Sat, Feb 6, 2016 at 6:58 AM, wrote: >>> >>> I am running this python script on R-studio. I have Python 3.5 installed >>> on my system. >>> >> >> Let's just try a quick smoke test. Ru

Re: Exception handling for socket.error in Python 3.5/RStudio

2016-02-05 Thread Bernardo Sulzbach
On 02/05/2016 07:26 PM, shaunak.bang...@gmail.com wrote: from _ssl import RAND_status, RAND_egd, RAND_add ImportError: cannot import name 'RAND_egd' I believe I've already seen this issue myself. It has to do with LibreSSL not having RAND_egd for some reason I can't recall. This seems

Re: Exception handling for socket.error in Python 3.5/RStudio

2016-02-05 Thread shaunak . bangale
On Friday, February 5, 2016 at 2:09:11 PM UTC-7, Bernardo Sulzbach wrote: > On 02/05/2016 07:01 PM, Chris Angelico wrote: > > On Sat, Feb 6, 2016 at 6:58 AM, wrote: > >> I am running this python script on R-studio. I have Python 3.5 installed > >> on my system. > >> > > > > Let's just try a quic

Re: Exception handling for socket.error in Python 3.5/RStudio

2016-02-05 Thread Bernardo Sulzbach
On 02/05/2016 07:01 PM, Chris Angelico wrote: On Sat, Feb 6, 2016 at 6:58 AM, wrote: I am running this python script on R-studio. I have Python 3.5 installed on my system. Let's just try a quick smoke test. Run this script: import sys print(sys.version) input("Press Enter to exit...") Th

Re: Exception handling for socket.error in Python 3.5/RStudio

2016-02-05 Thread Martin A. Brown
Hi there, >Thanks for the detailed reply. I edited, saved and opened the file >again. Still I am getting exactly the same error. > >Putting bigger chunk of code and the error again: [snipped; thanks for the larger chunk] >Error: >except socket.error as e: > ^ >SyntaxErr

Re: Exception handling for socket.error in Python 3.5/RStudio

2016-02-05 Thread Chris Angelico
On Sat, Feb 6, 2016 at 6:58 AM, wrote: > I am running this python script on R-studio. I have Python 3.5 installed on > my system. > Let's just try a quick smoke test. Run this script: import sys print(sys.version) input("Press Enter to exit...") That'll tell you a few things about how your sy

Re: Exception handling for socket.error in Python 3.5/RStudio

2016-02-05 Thread Martin A. Brown
Hi there Shaunak, I saw your few replies to my (and Nathan's) quick identification of syntax error. More comments follow, here. >I am running this python script on R-studio. I have Python 3.5 installed on my >system. > >count = 10 >while (count > 0): >try : ># read line from file:

Re: Exception handling for socket.error in Python 3.5/RStudio

2016-02-05 Thread shaunak . bangale
On Friday, February 5, 2016 at 1:09:35 PM UTC-7, Martin A. Brown wrote: > >except socket.error as e > > >line 53 except socket.error as e ^ SyntaxError: invalid syntax > > > >I tried changing socket.error to ConnectionRefusedError. and still > >got the same error. > > >Please tell me if the

Re: Exception handling for socket.error in Python 3.5/RStudio

2016-02-05 Thread shaunak . bangale
On Friday, February 5, 2016 at 1:11:19 PM UTC-7, shaunak...@gmail.com wrote: > On Friday, February 5, 2016 at 1:08:11 PM UTC-7, Nathan Hilterbrand wrote: > > On Feb 5, 2016 15:01, wrote: > > > > > > I am running this python script on R-studio. I have Python 3.5 installed > > on my system. > > > >

Re: Exception handling for socket.error in Python 3.5/RStudio

2016-02-05 Thread shaunak . bangale
On Friday, February 5, 2016 at 1:08:11 PM UTC-7, Nathan Hilterbrand wrote: > On Feb 5, 2016 15:01, wrote: > > > > I am running this python script on R-studio. I have Python 3.5 installed > on my system. > > > > count = 10 > > while (count > 0): > > try : > > # read line from file: > >

Re: Exception handling for socket.error in Python 3.5/RStudio

2016-02-05 Thread Martin A. Brown
>except socket.error as e >line 53 except socket.error as e ^ SyntaxError: invalid syntax > >I tried changing socket.error to ConnectionRefusedError. and still >got the same error. >Please tell me if the problem is with Rstudio, Python version or >the syntax. Syntax. Your code has, unfor

Re: Exception handling for socket.error in Python 3.5/RStudio

2016-02-05 Thread Nathan Hilterbrand
On Feb 5, 2016 15:01, wrote: > > I am running this python script on R-studio. I have Python 3.5 installed on my system. > > count = 10 > while (count > 0): > try : > # read line from file: > print(file.readline()) > # parse > parse_json(file.readline()) >

Exception handling for socket.error in Python 3.5/RStudio

2016-02-05 Thread shaunak . bangale
I am running this python script on R-studio. I have Python 3.5 installed on my system. count = 10 while (count > 0): try : # read line from file: print(file.readline()) # parse parse_json(file.readline()) count = count - 1 except socket.error as e

Re: Modern recommended exception handling practices?

2015-11-03 Thread Paul Rubin
Chris Angelico writes: > Aside from string exceptions and the "except Type, e:" syntax, I would > agree with you. Actually, I can't think of any "obsolete > exception-handling practices" in any language. I'd say that context managers are a big recent im

Re: Modern recommended exception handling practices?

2015-11-02 Thread Chris Angelico
On Tue, Nov 3, 2015 at 6:22 PM, Steven D'Aprano wrote: > A lot can happen in the few microseconds between > checking for the existence of the file and actually opening it -- the file > could be renamed or deleted. And a lot of microseconds can happen between two opcodes, too. Even inside a Python

Re: Modern recommended exception handling practices?

2015-11-02 Thread Steven D'Aprano
On Tue, 3 Nov 2015 06:16 pm, Chris Angelico wrote: > Not to mention having race condition possibilities. Arrggghhh! I knew there was something else I wanted to say. You're right. Sometimes you *have* to use exception handling code. Take this for example: if os.path.exists(pathname

Re: Modern recommended exception handling practices?

2015-11-02 Thread Chris Angelico
On Tue, Nov 3, 2015 at 5:47 PM, Steven D'Aprano wrote: > On Fri, 30 Oct 2015 04:43 am, vasudevram wrote: > >> Are there any modern (i.e. for current versions of Python 2 and 3) >> recommended exception handling practices? > > When you say "modern", it sou

Re: Modern recommended exception handling practices?

2015-11-02 Thread Steven D'Aprano
Hi Vasudev, and welcome. Sorry for the delay in replying to your questions. My answers inline, below. On Fri, 30 Oct 2015 04:43 am, vasudevram wrote: > Are there any modern (i.e. for current versions of Python 2 and 3) > recommended exception handling practices? When you say "

Re: Exception Handling

2015-04-10 Thread dieter
Palpandi writes: > Thanks for your responses. And I have one more question. > This is the situation. A python application which reads data from one .xml > file and generates some other file. Here what kind of exceptions occur? Ideally, you would not get any exceptions. In the not ideal case, y

Re: Exception Handling

2015-04-09 Thread Steven D'Aprano
On Thu, 9 Apr 2015 07:31 pm, Palpandi wrote: > Hi all, > > Is there any way to roll back or undo changes which are all done before > exception occurs. Not automatically. You have to program it yourself. -- Steven -- https://mail.python.org/mailman/listinfo/python-list

Re: Exception Handling

2015-04-09 Thread Chris Angelico
On Thu, Apr 9, 2015 at 7:31 PM, Palpandi wrote: > Hi all, > > Is there any way to roll back or undo changes which are all done before > exception occurs. In Python itself? Not directly; there are no facilities for undoing Python code. But if you're serious about integrity, you probably want to b

Exception Handling

2015-04-09 Thread Palpandi
Hi all, Is there any way to roll back or undo changes which are all done before exception occurs. -- https://mail.python.org/mailman/listinfo/python-list

Re: Python 3.x (beazley): __context__ vs __cause__ attributes in exception handling

2014-11-14 Thread Veek M
It's been answered here: http://stackoverflow.com/questions/26924045/python-3-x-beazley-context-vs- cause-attributes-in-exception-handling?noredirect=1#comment42403467_26924045 -- https://mail.python.org/mailman/listinfo/python-list

Python 3.x (beazley): __context__ vs __cause__ attributes in exception handling

2014-11-13 Thread Veek M
In 'Chained Exceptions' - Beazley pg:626 try: pass except ValueError as e: raise SyntaxError('foo bar') from e - Here, if ValueError is raised and SyntaxError is then raised.. 'e' contains __cause__ which points to the ValueError Traceback. He goes on to say: --

Re: Missing something on exception handling in Python 3

2013-08-27 Thread Steven D'Aprano
On Tue, 27 Aug 2013 00:00:36 -0500, Skip Montanaro wrote: > I found this question/answer on Stack Overflow: > > http://stackoverflow.com/questions/15123137 > > but after fiddling around with it, I can't find a solution that works > for Python 3.2 and 3.3, let alone 2.x. In 3.2, exceptions have

Re: Missing something on exception handling in Python 3

2013-08-26 Thread Skip Montanaro
I found this question/answer on Stack Overflow: http://stackoverflow.com/questions/15123137 but after fiddling around with it, I can't find a solution that works for Python 3.2 and 3.3, let alone 2.x. In 3.2, exceptions have both __cause__ and __context__ attributes. I tried setting both to Non

Re: Missing something on exception handling in Python 3

2013-08-26 Thread Ethan Furman
On 08/26/2013 07:49 PM, Skip Montanaro wrote: Do this: raise LockFailed("Failed to create %s" % self.path) from None Thanks. Is there some construct which will work in 2.x and 3.x? Something like this (untested): exc = None try: write_pid_to_lockfile(somefile) excep

Re: Missing something on exception handling in Python 3

2013-08-26 Thread Skip Montanaro
> Do this: > > raise LockFailed("Failed to create %s" % self.path) from None Thanks. Is there some construct which will work in 2.x and 3.x? Skip -- http://mail.python.org/mailman/listinfo/python-list

Re: Missing something on exception handling in Python 3

2013-08-26 Thread MRAB
led: failed to create /tmp/skip/lock When I rung the same code with Python 2.7, I get the exception output I anticipate: lock.acquire() Traceback (most recent call last): File "", line 1, in File "lockfile/pidlockfile.py", line 94, in acquire raise LockFailed("

Missing something on exception handling in Python 3

2013-08-26 Thread Skip Montanaro
When I rung the same code with Python 2.7, I get the exception output I anticipate: >>> lock.acquire() Traceback (most recent call last): File "", line 1, in File "lockfile/pidlockfile.py", line 94, in acquire raise LockFailed("failed to create %s" % self.p

Re: Exception Handling Practices / Patterns

2013-08-24 Thread frank . ruiz
gt; > > statements seem to be used more within modules, main functions, wrapper > > > scripts. > > > > It depends on whose code you are reading. I don't write a lot of classes, > > but when I do, I often use try...except inside them. > > > >

Re: Exception Handling Practices / Patterns

2013-08-24 Thread frank . ruiz
if there is a > > good approach for exception handling within classes. > > > > > > From my readings and gatherings - it seems I have found a common theme, but > > I am trying to solicit from the experts. > > > > > > Here is wh

Re: Exception Handling Practices / Patterns

2013-08-24 Thread Steven D'Aprano
On Sat, 24 Aug 2013 15:57:55 -0400, Terry Reedy wrote: >> # Worst >> except: >> >> Don't use the last one, except maybe in the interactive interpreter, > > Stick with never. "except:" means the same thing as "except > BaseException:", except that the latter indicates a deliberate choice > rather

Re: Exception Handling Practices / Patterns

2013-08-24 Thread Terry Reedy
On 8/24/2013 6:27 AM, Steven D'Aprano wrote: On Fri, 23 Aug 2013 22:25:55 -0700, snarf wrote: [...] * Seems like exception handing within Classes is largely avoided and is typically only used when calling external libraries. There is certainly no rule "avoid exceptions inside classes". Method

Re: Exception Handling Practices / Patterns

2013-08-24 Thread MRAB
On 24/08/2013 11:27, Steven D'Aprano wrote: On Fri, 23 Aug 2013 22:25:55 -0700, snarf wrote: [snip] * Using Exception is typically a bad. More specific the better. Yes, you should always try to catch the specific exceptions you care about: # Best except ValueError, OverflowError, ZeroDivisi

Re: Exception Handling Practices / Patterns

2013-08-24 Thread Steven D'Aprano
sort of things that you do at the top level often needs exception handling. For example, you might have a fallback module: try: import this_module except ImportError: import that_module as this_module You will very rarely see that inside a class, since you very rarely import modules insi

Re: Exception Handling Practices / Patterns

2013-08-24 Thread Dave Angel
snarf wrote: > Greetings, > > As I tread through my journey of OO I am trying to determine if there is a > good approach for exception handling within classes. > > From my readings and gatherings - it seems I have found a common theme, but I > am trying to solicit from the

Exception Handling Practices / Patterns

2013-08-23 Thread snarf
Greetings, As I tread through my journey of OO I am trying to determine if there is a good approach for exception handling within classes. >From my readings and gatherings - it seems I have found a common theme, but I >am trying to solicit from the experts. Here is what I have found (I

RE: newbie question about confusing exception handling in urllib

2013-04-12 Thread Prasad, Ramit
Steven D'Aprano wrote: > try: > main() > except Exception as err: > log(err) > print("Sorry, an unexpected error has occurred.") > print("Please contact support for assistance.") > sys.exit(-1) > > I like the traceback[0] module for logging last exception thrown. See tracebac

Re: newbie question about confusing exception handling in urllib

2013-04-09 Thread Ian Kelly
On Tue, Apr 9, 2013 at 5:41 AM, wrote: > try: > response = urllib.request.urlopen(request) > content = response.read() > except BaseException as ue: > if (isinstance(ue, socket.timeout) or (hasattr(ue, "reason") and > isinstance(ue.reason, sock

Re: newbie question about confusing exception handling in urllib

2013-04-09 Thread Chris Angelico
On Wed, Apr 10, 2013 at 1:05 AM, Steven D'Aprano wrote: > One exception to this rule (no pun intended) is that sometimes you want > to hide the details of unexpected tracebacks from your users. In that > case, it may be acceptable to wrap your application's main function in a > try block, catch an

Re: newbie question about confusing exception handling in urllib

2013-04-09 Thread Steven D'Aprano
On Tue, 09 Apr 2013 06:19:09 -0700, cabbar wrote: > How do I > handle all other exceptions, just say Exception: and handle them? I want > to silently ignore them. Please don't. That is normally poor practice, since it simply hides bugs in your code. As a general rule, you should only catch exce

Re: newbie question about confusing exception handling in urllib

2013-04-09 Thread Peter Otten
cab...@gmail.com wrote: > Ah, looks better. > > But, 2 questions: > > 1. I should also catch ConnectionResetError I am guessing. Does it need a special reaction? If so give it its own except suite. > 2. How do I handle all other exceptions, just say Exception: and handle > them? I want to sile

Re: newbie question about confusing exception handling in urllib

2013-04-09 Thread Terry Jan Reedy
On 4/9/2013 7:41 AM, cab...@gmail.com wrote: Hi, I have been using Java/Perl professionally for many years and have been trying to learn python3 recently. As my first program, I tried writing a class for a small project, and I am having really hard time understanding exception handling in

Re: newbie question about confusing exception handling in urllib

2013-04-09 Thread cabbar
Hi, > > > > I have been using Java/Perl professionally for many years and have been > trying to learn python3 recently. As my first program, I tried writing a > class for a small project, and I am having really hard time understanding > exception handling in urllib

Re: newbie question about confusing exception handling in urllib

2013-04-09 Thread Peter Otten
cab...@gmail.com wrote: > Hi, > > I have been using Java/Perl professionally for many years and have been > trying to learn python3 recently. As my first program, I tried writing a > class for a small project, and I am having really hard time understanding > exception handlin

newbie question about confusing exception handling in urllib

2013-04-09 Thread cabbar
Hi, I have been using Java/Perl professionally for many years and have been trying to learn python3 recently. As my first program, I tried writing a class for a small project, and I am having really hard time understanding exception handling in urllib and in python in general... Basically

Re: Weird exception handling behavior -- late evaluation in except clause

2012-12-02 Thread Chris Angelico
On Mon, Dec 3, 2012 at 6:30 PM, Steven D'Aprano wrote: > Yeah, in hindsight it was a pretty crappy example. But this sort of > dynamism really is useful: > > def testRaises(exc, func, *args): > try: > result = func(*args) > except exc: > return > raise AssertionError("e

Re: Weird exception handling behavior -- late evaluation in except clause

2012-12-02 Thread Steven D'Aprano
On Mon, 03 Dec 2012 16:24:50 +1100, Chris Angelico wrote: > On Mon, Dec 3, 2012 at 8:31 AM, Steven D'Aprano > wrote: >> Consider this piece of legal Python code: >> >> Err = None >> if condition(x) > 100: >> Err = OneException >> elif another_condition(x): >> Err = AnotherException >> try

Re: Weird exception handling behavior -- late evaluation in except clause

2012-12-02 Thread Chris Angelico
On Mon, Dec 3, 2012 at 8:31 AM, Steven D'Aprano wrote: > Consider this piece of legal Python code: > > Err = None > if condition(x) > 100: > Err = OneException > elif another_condition(x): > Err = AnotherException > try: > spam(a, b, c) > except Err: > recover() Legal it may be, b

Re: Weird exception handling behavior -- late evaluation in except clause

2012-12-02 Thread Steven D'Aprano
On Sun, 02 Dec 2012 12:25:22 -0500, Roy Smith wrote: > This is kind of weird (Python 2.7.3): > > try: > print "hello" > except foo: > print "foo" > > prints "hello". The problem (IMHO) is that apparently the except clause > doesn't get evaluated until after some exception is caught. Wh

Re: Weird exception handling behavior -- late evaluation in except clause

2012-12-02 Thread Terry Reedy
On 12/2/2012 12:25 PM, Roy Smith wrote: This is kind of weird (Python 2.7.3): try: print "hello" except foo: print "foo" prints "hello". The problem (IMHO) is that apparently the except clause doesn't get evaluated until after some exception is caught. Which means it never notices t

Re: Weird exception handling behavior -- late evaluation in except clause

2012-12-02 Thread Hans Mulder
On 2/12/12 18:25:22, Roy Smith wrote: > This is kind of weird (Python 2.7.3): > > try: > print "hello" > except foo: > print "foo" > > prints "hello". The problem (IMHO) is that apparently the except clause > doesn't get evaluated until after some exception is caught. Which means > it

Weird exception handling behavior -- late evaluation in except clause

2012-12-02 Thread Roy Smith
This is kind of weird (Python 2.7.3): try: print "hello" except foo: print "foo" prints "hello". The problem (IMHO) is that apparently the except clause doesn't get evaluated until after some exception is caught. Which means it never notices that foo is not defined until it's too late

Re: Exception Handling (C - extending python)

2011-10-25 Thread Stefan Behnel
Ulrich Eckhardt, 25.10.2011 08:49: Am 23.10.2011 14:41, schrieb Stefan Behnel: That's just fine. If you are interested in the inner mechanics of the CPython runtime, reading the source is a very good way to start getting involved with the project. However, many extension module authors don't ca

Re: Exception Handling (C - extending python)

2011-10-25 Thread Ulrich Eckhardt
Am 23.10.2011 14:41, schrieb Stefan Behnel: That's just fine. If you are interested in the inner mechanics of the CPython runtime, reading the source is a very good way to start getting involved with the project. However, many extension module authors don't care about these inner mechanics and j

Re: Exception Handling (C - extending python)

2011-10-23 Thread Lee
For a moment, back to the basics... I am using the example provided by docs at 2.1.2 "Providing finer control...". Using say: mynoddy = noddy2.Noddy() mynoddy.first = "a" mynoddy.last = 0 the last line causes an ugly crash (on python 2.6.5 on winxp). No way to catch the exception. As I understa

Re: Exception Handling (C - extending python)

2011-10-23 Thread Stefan Behnel
Hi, note that I reformatted your posting to get the replies back into order. Lee, 23.10.2011 13:32: On Oct 23, 10:06 pm, Stefan Behnel wrote: Lee, 23.10.2011 06:09: Where does PyExc_TypeError (and alike) points to? I can see its declaration - PyAPI_DATA(PyObject *) PyExc_TypeError; - in pyerr

Re: Exception Handling (C - extending python)

2011-10-23 Thread Christian Heimes
Am 23.10.2011 06:09, schrieb Lee: > Hi all, > > Where does PyExc_TypeError (and alike) points to? I can see its > declaration - PyAPI_DATA(PyObject *) PyExc_TypeError; - in pyerrors.h > but I cannot figure out what it is its value, where it is > initialized. It's initialized in Objects/exceptions

Re: Exception Handling (C - extending python)

2011-10-23 Thread Lee
Thanks Stefan, I am just interested to understand the mechanism inside python. If it points to an object that means I can defered it (through ob_type). >From there, how a function like PyErr_SetString knows what exception is? Where its value is kept? Lee On Oct 23, 10:06 pm, Stefan Behnel wro

Re: Exception Handling (C - extending python)

2011-10-23 Thread Stefan Behnel
Lee, 23.10.2011 06:09: Where does PyExc_TypeError (and alike) points to? I can see its declaration - PyAPI_DATA(PyObject *) PyExc_TypeError; - in pyerrors.h but I cannot figure out what it is its value, where it is initialized. It gets initialised inside of the interpreter core and then points

Exception Handling (C - extending python)

2011-10-22 Thread Lee
Hi all, Where does PyExc_TypeError (and alike) points to? I can see its declaration - PyAPI_DATA(PyObject *) PyExc_TypeError; - in pyerrors.h but I cannot figure out what it is its value, where it is initialized. Any help is greatly appreciated. Lee -- http://mail.python.org/mailman/listinfo/py

Re: Exception handling in Python 3.x

2010-12-13 Thread Arnaud Delobelle
Ethan Furman writes: > Ethan Furman wrote: >> Arnaud Delobelle wrote: >>> >>> I missed the start of this discussion but there are two simpler ways: >>> >>> def func(iterable): >>> for x in iterable: >>> print(x) >>> return >>> raise ValueError("... empty iterable") >> >> >

Re: Exception handling in Python 3.x

2010-12-13 Thread Ethan Furman
Ethan Furman wrote: Arnaud Delobelle wrote: I missed the start of this discussion but there are two simpler ways: def func(iterable): for x in iterable: print(x) return raise ValueError("... empty iterable") For the immediate case this is a cool solution. Drat --

Re: Exception handling in Python 3.x

2010-12-13 Thread Ethan Furman
Ethan Furman wrote: Please don't top-post. Rob Richardson wrote: -Original Message- I missed the start of this discussion but there are two simpler ways: def func(iterable): for x in iterable: print(x) return raise ValueError("... empty iterable") Or using 3

Re: Exception handling in Python 3.x

2010-12-13 Thread Ethan Furman
Arnaud Delobelle wrote: I missed the start of this discussion but there are two simpler ways: def func(iterable): for x in iterable: print(x) return raise ValueError("... empty iterable") For the immediate case this is a cool solution. Unfortunately, it doesn't fix t

Re: Exception handling in Python 3.x

2010-12-13 Thread Arnaud Delobelle
"Rob Richardson" writes: You shouldn't top-post! > Arnaud, > > Wouldn't your first suggestion exit after the first element in iterable? Yes, after printing that element, which is what the code I quoted did. > And would your second suggestion throw an exception after normal > processing of all

Re: Exception handling in Python 3.x

2010-12-13 Thread Ethan Furman
Please don't top-post. Rob Richardson wrote: -Original Message- I missed the start of this discussion but there are two simpler ways: def func(iterable): for x in iterable: print(x) return raise ValueError("... empty iterable") Or using 3.x's next's optional

RE: Exception handling in Python 3.x

2010-12-13 Thread Rob Richardson
Arnaud, Wouldn't your first suggestion exit after the first element in iterable? And would your second suggestion throw an exception after normal processing of all elements in the interator? RobR -Original Message- I missed the start of this discussion but there are two simpler ways:

Re: Exception handling in Python 3.x

2010-12-13 Thread Arnaud Delobelle
Paul Rubin writes: > Steven D'Aprano writes: >> Apart from this horrible idiom: >> >> def func(iterable): >> it = iter(iterable) >> failed = False >> try: >> x = next(it) >> except StopIteration: >> failed = True >> if failed: >> raise ValueError("can'

Re: Exception handling in Python 3.x

2010-12-07 Thread Steve Holden
On 12/7/2010 1:48 AM, MRAB wrote: > Perhaps Python could use Guido's time machine to check whether the > sequence will yield another object in the future. :-) Since there's only one time machine that would effectively be a lock across all Python interpreters. regards Steve -- Steve Holden

Re: Exception handling in Python 3.x

2010-12-07 Thread Steve Holden
On 12/7/2010 5:58 AM, John Nagle wrote: >PEP 255, like too much Python literature, doesn't distinguish clearly > between the language definition and implementation detail. It says > "The mechanics of StopIteration are low-level details, much like the > mechanics of IndexError in Python 2.1". A

Re: Exception handling in Python 3.x

2010-12-07 Thread Mark Wooding
John Nagle writes: >PEP 255, like too much Python literature, doesn't distinguish > clearly between the language definition and implementation detail. It > says "The mechanics of StopIteration are low-level details, much like > the mechanics of IndexError in Python 2.1". Applications should

Re: Exception handling in Python 3.x

2010-12-06 Thread John Nagle
On 12/6/2010 4:23 PM, Steven D'Aprano wrote: On Mon, 06 Dec 2010 13:13:40 -0800, Paul Rubin wrote: It's really unfortunate, though, that Python 3 didn't offer a way to peek at the next element of an iterable and test emptiness directly. This idea of peekable iterables just won't die, despite

Re: Exception handling in Python 3.x

2010-12-06 Thread John Nagle
On 12/6/2010 2:24 PM, Mark Wooding wrote: John Nagle writes: Right. You're not entitled to assume that StopIteration is how a generator exits. That's a CPyton thing; generators were a retrofit, and that's how they were hacked in. Other implementations may do generators differently. This i

Re: Exception handling in Python 3.x

2010-12-06 Thread MRAB
On 07/12/2010 00:23, Steven D'Aprano wrote: On Mon, 06 Dec 2010 13:13:40 -0800, Paul Rubin wrote: It's really unfortunate, though, that Python 3 didn't offer a way to peek at the next element of an iterable and test emptiness directly. This idea of peekable iterables just won't die, despite t

Re: Exception handling in Python 3.x

2010-12-06 Thread Steven D'Aprano
On Mon, 06 Dec 2010 13:13:40 -0800, Paul Rubin wrote: > It's really unfortunate, though, that Python 3 didn't offer a way to > peek at the next element of an iterable and test emptiness directly. This idea of peekable iterables just won't die, despite the obvious flaws in the idea. There's no g

Re: Exception handling in Python 3.x

2010-12-06 Thread Mark Wooding
John Nagle writes: > Right. You're not entitled to assume that StopIteration is how a > generator exits. That's a CPyton thing; generators were a retrofit, > and that's how they were hacked in. Other implementations may do > generators differently. This is simply wrong. The StopIteration exc

Re: Exception handling in Python 3.x

2010-12-06 Thread Paul Rubin
Steven D'Aprano writes: > Apart from this horrible idiom: > > def func(iterable): > it = iter(iterable) > failed = False > try: > x = next(it) > except StopIteration: > failed = True > if failed: > raise ValueError("can't process empty iterable") > p

Re: Exception handling in Python 3.x

2010-12-06 Thread John Nagle
On 12/3/2010 5:04 AM, Steven D'Aprano wrote: Consider the following common exception handling idiom: def func(iterable): it = iter(iterable) try: x = next(it) except StopIteration: raise ValueError("can't process empty iterable") print(

Re: Exception handling in Python 3.x

2010-12-03 Thread Steven D'Aprano
On Fri, 03 Dec 2010 17:08:38 +0100, Peter Otten wrote: > After rereading the original post I still don't get why the workarounds > provided in those links aren't worth considering. The first work-around: http://mail.python.org/pipermail/python-list/2010-October/1258606.html is unsuitable beca

  1   2   3   4   >