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


Groups > comp.lang.python > #8061

Re: those darn exceptions

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <rosuav@gmail.com>
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; 'else:': 0.03; 'arguments': 0.05; 'instance': 0.05; 'instance,': 0.05; 'interpreter': 0.07; 'raised': 0.07; 'python': 0.08; '21,': 0.09; 'exceptions': 0.09; 'false,': 0.09; 'inclined': 0.09; 'throw': 0.09; 'url:linux': 0.09; 'this:': 0.10; 'examples': 0.12; 'am,': 0.14; 'wrote:': 0.14; '"real"': 0.16; 'angelico': 0.16; 'did,': 0.16; 'exceptions.': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'next?': 0.16; 'obvious.': 0.16; 'oserror': 0.16; 'propagate': 0.16; 'subject:those': 0.16; 'throws': 0.16; '\xa0>>>': 0.16; '\xa0for': 0.16; 'tue,': 0.17; 'rewrite': 0.19; 'slightly': 0.19; 'header:In-Reply-To:1': 0.21; '(like': 0.21; 'ignore': 0.21; 'column': 0.22; 'trying': 0.23; 'integer': 0.23; 'received:209.85.210.174': 0.23; 'received:mail- iy0-f174.google.com': 0.23; 'code': 0.24; "doesn't": 0.25; 'function': 0.25; 'modules': 0.26; 'statement': 0.26; "i'm": 0.27; 'work.': 0.28; 'message-id:@mail.gmail.com': 0.28; 'raise': 0.28; 'keeps': 0.28; 'mode': 0.29; 'import': 0.29; 'acceptable': 0.29; 'all,': 0.30; 'responded': 0.30; 'typeerror:': 0.30; "can't": 0.32; 'wanting': 0.32; 'someone': 0.33; 'does': 0.33; 'to:addr :python-list': 0.33; 'comment': 0.33; 'error': 0.33; 'rather': 0.34; 'chris': 0.34; '(with': 0.36; 'table': 0.37; 'received:google.com': 0.37; 'received:209.85': 0.37; 'pretty': 0.37; 'put': 0.37; 'could': 0.38; 'but': 0.38; 'returning': 0.38; 'subject:: ': 0.38; 'comments': 0.39; 'perhaps': 0.39; "i'd": 0.39; 'received:209': 0.39; 'windows.': 0.39; 'to:addr:python.org': 0.39; 'skip:z 10': 0.40; 'more': 0.60; 'your': 0.60; 'you.': 0.62; 'back': 0.63; 'url:net': 0.63; 'here': 0.66; 'concept': 0.73; 'attempted': 0.73; '11:43': 0.84; 'above),': 0.84; 'bottom,': 0.84; 'kills': 0.84; 'point:': 0.84; 'quote,': 0.84; 'here...': 0.91; 'killed': 0.91; 'penalty': 0.91; 'death': 0.97
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:in-reply-to:references:date :message-id:subject:from:to:content-type:content-transfer-encoding; bh=guUca1OQhg/TU45veqozvZZaQ6Xk/bjU1iE6ms78HLw=; b=feyHV2l83q76SU61BUTsgBwugHeNIpw1pq9KOB1JbkOCVa+3Opi/lKbocauT+J1sMu CVNhDkMCE9Vv57UoCor8O85KrmYTj9pKaslvO3vqKwTBpe0cX3yWLeQtr0REQ2ZBpC78 KGO8VvuiBwDpxnm3kkQ60itSM++zu1z6gxjKg=
DomainKey-Signature a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type:content-transfer-encoding; b=P43Lor5jw3i8/fexn9gILInTfGm9WxhFie7SZJ0SOKHSZWngoU0H1thrfgg07XI/wk eyLCTvamt6Qj+L5ZvFo2Dq7uKDnHPnTQU+BGqLs6M2YxY+SUUzdxEe1f/J91QPJrnnRg GYkmkLGQDTKvgs0qzvAnWe3GtUZ5dUNAa5OUE=
MIME-Version 1.0
In-Reply-To <itot0b022i@news4.newsguy.com>
References <itot0b022i@news4.newsguy.com>
Date Tue, 21 Jun 2011 13:19:09 +1000
Subject Re: those darn exceptions
From Chris Angelico <rosuav@gmail.com>
To python-list@python.org
Content-Type text/plain; charset=ISO-8859-1
Content-Transfer-Encoding quoted-printable
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.211.1308626356.1164.python-list@python.org> (permalink)
Lines 57
NNTP-Posting-Host 82.94.164.166
X-Trace 1308626356 news.xs4all.nl 49044 [::ffff:82.94.164.166]:54828
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:8061

Show key headers only | View raw


On Tue, Jun 21, 2011 at 11:43 AM, Chris Torek <nospam@torek.net> wrote:
> It can be pretty obvious.  For instance, the os.* modules raise
> OSError on errors.  The examples here are slightly silly until
> I reach the "real" code at the bottom, but perhaps one will get
> the point:
>
>    >>> import os
>    >>> os.kill(getpid(), 0) # am I alive?
>    >>> # yep, I am alive.
>    ...
>
> [I'm not sure why the interpreter wants more after my comment here.]
>

It's not wanting more. It responded to your statement "yep, I am
alive" by boggling at you. It said, and I quote, "...". What next?
Reading the obituaries column in search of your own PID?

Yep, slightly silly. And very amusing. But back to the serious:

os.kill(pid,0) doesn't work on Windows, but that just means this whole
function can't be used on Windows. (Actually, the kill call DOES work.
It just doesn't do what you want here... it kills the process.)

os.kill("asdf",0) --> TypeError: an integer is required
os.kill(-1,0) --> no error raised - not sure if you want to propagate
os.kill()'s behaviour on negative PIDs or not - see for instance
http://linux.die.net/man/2/kill

I'm not sure if it's possible to put Python into "secure computing"
mode (with prctl(PR_SET_SECCOMP) on Linux), but if you did, then
there'd be an additional possible result from this: No return at all,
because your process has just been killed for trying to kill someone
else. (Secure Computing: The death penalty for attempted murder.)

Interesting concept of pulling out all possible exceptions. Would be
theoretically possible to build a table that keeps track of them, but
automated tools may have problems:

a=5; b=7; c=12
d=1/(a+b-c) # This could throw ZeroDivisionError

if a+b>c:
  d=1/(a+b-c) # This can't, because it's guarded.
else:
  d=2

And don't tell me to rewrite that with try/except, because it's not the same :)

I'd be inclined to have comments about the exceptions that this can
itself produce, but if there's exceptions that come from illogical
arguments (like the TypeError above), then just ignore them and let
them propagate. If is_process("asdf") throws TypeError rather than
returning False, I would call that acceptable behaviour.

Chris Angelico

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

those darn exceptions Chris Torek <nospam@torek.net> - 2011-06-21 01:43 +0000
  Re: those darn exceptions Chris Angelico <rosuav@gmail.com> - 2011-06-21 13:19 +1000
    Re: those darn exceptions Chris Torek <nospam@torek.net> - 2011-06-21 04:40 +0000
  Re: those darn exceptions Ben Finney <ben+python@benfinney.id.au> - 2011-06-21 14:04 +1000
  Re: those darn exceptions Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-06-21 09:49 +0000
    Re: those darn exceptions Chris Torek <nospam@torek.net> - 2011-06-21 21:51 +0000
      Re: those darn exceptions John Nagle <nagle@animats.com> - 2011-06-27 11:52 -0700
  Re: those darn exceptions Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2011-06-23 20:16 +1200
    Re: those darn exceptions Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-06-23 18:40 +1000
    Re: those darn exceptions Chris Torek <nospam@torek.net> - 2011-06-23 18:33 +0000
      Re: those darn exceptions Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2011-06-24 19:50 +1200
        Re: those darn exceptions Chris Torek <nospam@torek.net> - 2011-06-24 18:21 +0000
          Re: those darn exceptions Ben Finney <ben+python@benfinney.id.au> - 2011-06-25 10:25 +1000
            Re: those darn exceptions Chris Angelico <rosuav@gmail.com> - 2011-06-25 13:55 +1000
              Re: those darn exceptions steve+comp.lang.python@pearwood.info - 2011-06-26 00:28 +1000
                Re: those darn exceptions Chris Angelico <rosuav@gmail.com> - 2011-06-26 01:52 +1000

csiph-web