space added before printed line from except block

Bjorn Pettersen BPettersen at NAREX.com
Tue Dec 24 13:49:58 EST 2002


> From: v.wehren [mailto:v.wehren at home.nl] 
> 
> In order to quickly view a "legacy" encoding and its 
> respective Unicode mapping etc. in Idle, I wrote a simple 
> script printing a table-like overview, which works fine. What 
> puzzles me however, is that when running it from the win 
> console or from within the text-based interpreter ,  an 
> additional space is placed in front of each printed line 
> coming from the except UnicodeError block, which is 
> unexpected (that the print statement in the try block isn't 
> very useful in the console - as it generates an UnicodeError 
> itself - is, of course, expected and a different matter). 
> Where does the additional space come from (if it is an 
> additional space)? Anybody?

First a little background... The reason

  print 'a', 'b'

prints out "a b" (notice the space), is because sys.stdout keeps a
'softspace' attribute indicating whether it needs to output a space
before the next item or not.

When compiled to bytecode this turns into:

  PRINT_ITEM 'a'
  PRINT_ITEM 'b'
  PRINT_NEWLINE

The PRINT_ITEM bytecode will cause an extra space to be printed if the
softspace attribute is set, then print the item, and finally re-set the
softspace attribute. PRINT_NEWLINE clears the softspace attribute so
that following print statements will not have a leading space...

Since your code throws during PRINT_ITEM, PRINT_NEWLINE never executes
and thus never gets a chance to clear the softspace attribute, resulting
in the extra space you're seeing. A simple way to make it work like you
want it would be to insert:

  sys.stdout.softspace = 0

before printing in the except clause.

On a different note, I'm wondering why str.join expects a 'list' as an
argument:

>     print "|".join(["Decimal".center(10),
>                     "Hex".center(10),
>                     "Current".center(10),
>                     "Uni Hex".center(10),
>                     "Uni Char".center(10)])

while os.path.join requires separate arguments... Seems rather
inconsistent? Anyone?

-- bjorn




More information about the Python-list mailing list