string interpolation mystery in Python 2.6

Alan G Isaac alan.isaac at gmail.com
Fri Sep 11 12:02:42 EDT 2009


MYSTERY: how can "%s"%error be different from "%s"%str(error) in Python 2.6?

APOLOGY: I tried to strip this down, but could not find a simple way to
reproduce the problem.  This way works, however.  (There is a discussion on
the docutils-develop list.)  Although there are several steps, we are talking
about less than 5 minutes to document the puzzle.  Please use the specific
revision (or earlier) in the directions, as later revisions implement a
work around.

1. Check out revision 6121 from docutils
http://docutils.sourceforge.net/docs/dev/repository.html

2. Install docutils under Python 2.6

3. Process the file below [1]_ using the rst2html.py script
(found in Python26/Scripts on Windows platforms)

4. Note the IOError::

         temp.rst:: (SEVERE/4) Problems with "include" directive path:
         IOError: (2, 'No such file or directory').
         Exiting due to level-4 (SEVERE) system message.

5. Make the following change and *no other* changes:
In docutils/parsers/rst/directives/misc.py (line 66-67) change ::

             raise self.severe('Problems with "%s" directive path:\n%s: %s.'
                               % (self.name, error.__class__.__name__, error))

to ::

             raise self.severe('Problems with "%s" directive path:\n%s: %s.'
                               % (self.name, error.__class__.__name__, str(error)))

6. Process the same file the same way. Note the change in the IOError::

         temp.rst:: (SEVERE/4) Problems with "include" directive path:
         IOError: [Errno 2] No such file or directory: 'doesnotexist.rst'.
         Exiting due to level-4 (SEVERE) system message.

7. Try this again in Python 2.5.  The correct (filename reported) error report
is produced both times.  So this is a Python 2.6 change.


Clues?  Bug or feature?

I'm going to hazard a guess that there was an undocumented (in What's New) change
to the __unicode__ method of BaseException.

Thanks,
Alan Isaac

.. [1] Following is the rst file to process:


Test
====

This is just a test.

.. include:: doesnotexist.rst




More information about the Python-list mailing list