Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #53024 > unrolled thread

Re: Missing something on exception handling in Python 3

Started byMRAB <python@mrabarnett.plus.com>
First post2013-08-27 03:13 +0100
Last post2013-08-27 03:13 +0100
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: Missing something on exception handling in Python 3 MRAB <python@mrabarnett.plus.com> - 2013-08-27 03:13 +0100

#53024 — Re: Missing something on exception handling in Python 3

FromMRAB <python@mrabarnett.plus.com>
Date2013-08-27 03:13 +0100
SubjectRe: Missing something on exception handling in Python 3
Message-ID<mailman.251.1377569630.19984.python-list@python.org>
On 27/08/2013 02:48, Skip Montanaro wrote:
> I have code in the pylockfile package that looks roughly like this
> (apologies for the indentation - I'm growing to dislike Gmail for
> typing code fragments):
>
> try:
>      write_pid_to_lockfile(somefile)
> except OSError as exc:
>      if conditions_i_can_handle:
>         do_other_stuff...
>      else:
>          raise LockFailed("Failed to create %s" % self.path)
>
> Thinking I would just get the final exception (not the one I caught
> originally), I was surprised to see this output on my screen:
>
>>>> lock.acquire()
> Traceback (most recent call last):
>    File "lockfile/pidlockfile.py", line 80, in acquire
>      write_pid_to_pidfile(self.path)
>    File "lockfile/pidlockfile.py", line 163, in write_pid_to_pidfile
>      pidfile_fd = os.open(pidfile_path, open_flags, open_mode)
> OSError: [Errno 13] Permission denied: '/tmp/skip/lock'
>
> During handling of the above exception, another exception occurred:
>
> Traceback (most recent call last):
>    File "<stdin>", line 1, in <module>
>    File "lockfile/pidlockfile.py", line 94, in acquire
>      raise LockFailed("failed to create %s" % self.path)
> lockfile.LockFailed: 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 "<stdin>", line 1, in <module>
>    File "lockfile/pidlockfile.py", line 94, in acquire
>      raise LockFailed("failed to create %s" % self.path)
> LockFailed: failed to create /tmp/skip/lock
>
> It appears exception handling changed in Python 3.  How do I suppress
> the lower level OSError?
>
Do this:

     raise LockFailed("Failed to create %s" % self.path) from None

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web