try...except...finally problem in Python 2.5

2007-02-14 Thread redawgts
I keep getting this error local variable 'f' referenced before assignment in the finally block when I run the following code. try: f = file(self.filename, 'rb') f.seek(DATA_OFFSET) self.__data = f.read(DATA_SIZE) self.isDataLoaded = True

Re: try...except...finally problem in Python 2.5

2007-02-14 Thread Jean-Paul Calderone
On 14 Feb 2007 11:41:29 -0800, redawgts [EMAIL PROTECTED] wrote: I keep getting this error local variable 'f' referenced before assignment in the finally block when I run the following code. try: f = file(self.filename, 'rb') f.seek(DATA_OFFSET)

Re: try...except...finally problem in Python 2.5

2007-02-14 Thread Robert Kern
redawgts wrote: I keep getting this error local variable 'f' referenced before assignment in the finally block when I run the following code. try: f = file(self.filename, 'rb') f.seek(DATA_OFFSET) self.__data = f.read(DATA_SIZE)

Re: try...except...finally problem in Python 2.5

2007-02-14 Thread Andrew Koenig
redawgts [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] I keep getting this error local variable 'f' referenced before assignment in the finally block when I run the following code. try: f = file(self.filename, 'rb') f.seek(DATA_OFFSET)

Re: try...except...finally problem in Python 2.5

2007-02-14 Thread Larry Bates
redawgts wrote: I keep getting this error local variable 'f' referenced before assignment in the finally block when I run the following code. try: f = file(self.filename, 'rb') f.seek(DATA_OFFSET) self.__data = f.read(DATA_SIZE)

Re: try...except...finally problem in Python 2.5

2007-02-14 Thread Paul Rubin
redawgts [EMAIL PROTECTED] writes: try: f = file(self.filename, 'rb') ... Can someone tell me what's wrong with the code? Various people have explained the error: if the file open attempt fails, f is never assigned. Doing it the right way (i.e. handling the potential

Re: try...except...finally problem in Python 2.5

2007-02-14 Thread redawgts
Thanks everybody, that helped alot. -- http://mail.python.org/mailman/listinfo/python-list

Re: try...except...finally problem in Python 2.5

2007-02-14 Thread Steven D'Aprano
On Wed, 14 Feb 2007 12:09:34 -0800, Paul Rubin wrote: redawgts [EMAIL PROTECTED] writes: try: f = file(self.filename, 'rb') ... Can someone tell me what's wrong with the code? Various people have explained the error: if the file open attempt fails, f is never

Re: try...except...finally problem in Python 2.5

2007-02-14 Thread Paul Rubin
Steven D'Aprano [EMAIL PROTECTED] writes: self.isDataLoaded = False try: f = open(self.filename, 'rb') f.seek(DATA_OFFSET) self.__data = f.read(DATA_SIZE) self.isDataLoaded = True except: pass else: pass (apart from being four lines shorter) Your version never

Re: try...except...finally problem in Python 2.5

2007-02-14 Thread Steven D'Aprano
On Wed, 14 Feb 2007 18:03:19 -0800, Paul Rubin wrote: Steven D'Aprano [EMAIL PROTECTED] writes: self.isDataLoaded = False try: f = open(self.filename, 'rb') f.seek(DATA_OFFSET) self.__data = f.read(DATA_SIZE) self.isDataLoaded = True except: pass else: pass

Re: try...except...finally problem in Python 2.5

2007-02-14 Thread Steven D'Aprano
Replying to my own question... On Thu, 15 Feb 2007 13:17:00 +1100, Steven D'Aprano wrote: I don't see where the with version closes the file either. How does it know that I want to call the f's close() method, rather than, say, f.exit() or f.do_something_else()? Ah, I *think* I see... file

Re: try...except...finally problem in Python 2.5

2007-02-14 Thread Paul Rubin
Steven D'Aprano [EMAIL PROTECTED] writes: Yes it does. Eventually f goes out of scope and is closed automatically. Oh right, however you can't really predict when the closure occurs, unless you're relying on current CPython artifacts. Re your other post: yes, PEP 363 explains how the with

Re: try...except...finally problem in Python 2.5

2007-02-14 Thread Paul Rubin
Paul Rubin http://[EMAIL PROTECTED] writes: Re your other post: yes, PEP 363 explains how the with statement Whoops, 343. -- http://mail.python.org/mailman/listinfo/python-list