sys.stderr.write and sys.exit

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • GinTon

    #1

    sys.stderr.write and sys.exit

    Is the same use _sys.stderr.wri te('error message'); sys.exit(1)_ than
    _sys.exit('erro r message')_ ?

    Note: help(sys.exit)
    If the status is omitted or None, it defaults to zero (i.e., success).
    If the status is numeric, it will be used as the system exit status.
    If it is another kind of object, it will be printed and the system exit
    status will be one (i.e., failure).

  • Ben Finney

    #2
    Re: sys.stderr.writ e and sys.exit

    "GinTon" <jonas.esp@goog lemail.comwrite s:
    Is the same use _sys.stderr.wri te('error message'); sys.exit(1)_
    than _sys.exit('erro r message')_ ?
    (Note: The underscore '_' is a valid character in Python code, so I
    was quite confused by what you wrote and had to read it several times
    to see that you were intending the underscores not to be part of the
    code. Better to show a line of code on its own line in your message.)

    Code that wants to catch SystemExit will get a different exception
    object in each case::
    >>import sys
    >>from StringIO import StringIO
    >>sys.stderr = StringIO()
    >>try:
    ... sys.stderr.writ e('error message')
    ... sys.exit(1)
    ... except SystemExit, e:
    ... print "stderr contains:", sys.stderr.getv alue()
    ... print "e.code is:", e.code
    ...
    stderr contains: error message
    e.code is: 1
    >>try:
    ... sys.exit('error message')
    ... except SystemExit, e:
    ... print "stderr contains:", sys.stderr.getv alue()
    ... print "e.code is:", e.code
    ...
    stderr contains: error message
    e.code is: error message

    I quite often catch SystemExit in unit tests, or other code that is
    inspecting a program module.

    --
    \ "I have a large seashell collection, which I keep scattered on |
    `\ the beaches all over the world. Maybe you've seen it." -- |
    _o__) Steven Wright |
    Ben Finney

    Comment

    • GinTon

      #3
      Re: sys.stderr.writ e and sys.exit

      Thanks Ben Finney. So it's understood very well.

      Ben Finney ha escrito:
      "GinTon" <jonas.esp@goog lemail.comwrite s:
      >
      Is the same use
      > >>sys.stderr.wr ite('error message'); sys.exit(1)
      than
      > >>sys.exit('err or message') ?
      >
      Code that wants to catch SystemExit will get a different exception
      object in each case::
      >
      >>import sys
      >>from StringIO import StringIO
      >>sys.stderr = StringIO()
      >
      >>try:
      ... sys.stderr.writ e('error message')
      ... sys.exit(1)
      ... except SystemExit, e:
      ... print "stderr contains:", sys.stderr.getv alue()
      ... print "e.code is:", e.code
      ...
      stderr contains: error message
      e.code is: 1
      >
      >>try:
      ... sys.exit('error message')
      ... except SystemExit, e:
      ... print "stderr contains:", sys.stderr.getv alue()
      ... print "e.code is:", e.code
      ...
      stderr contains: error message
      e.code is: error message
      >
      I quite often catch SystemExit in unit tests, or other code that is
      inspecting a program module.

      Comment

      • Ben Finney

        #4
        Text markup conventions (was: sys.stderr.writ e and sys.exit)

        Dennis Lee Bieber <wlfraed@ix.net com.comwrites:
        Ben Finney wrote:
        (Note: The underscore '_' is a valid character in Python code, so
        I was quite confused by what you wrote and had to read it several
        times to see that you were intending the underscores not to be
        part of the code. Better to show a line of code on its own line in
        your message.)
        >
        Old "rich text" email format effectors... *word* =bolded, /word/
        =italic, _word_ =underscored.
        Yes, I use those (or similar) myself, but only in natural language
        where the context makes it clear that the punctuation characters are
        not intended to be part of the content. When showing program code,
        it's a poor choice to use punctuation characters for such markup,
        since it's not clear which ones are part of the code.

        --
        \ "It's a good thing we have gravity or else when birds died |
        `\ they'd just stay right up there. Hunters would be all |
        _o__) confused." -- Steven Wright |
        Ben Finney

        Comment

        Working...