[Zope3-checkins] CVS: Zope3/src/zope/app/pythonpage -__init__.py:1.7

Tim Peters tim at zope.com
Fri Apr 2 12:22:56 EST 2004


[Stephan Richter]
> Modified Files:
> 	__init__.py
> Log Message:
> Updated documentation.
>
> Finally fixed test failure in Windows. It turns out that Windows did
> the right thing, while the Linux Python version seems to have a bug.
> I changed the test enough, so that it does not depend on the string
> representation of the SyntaxError.

Thanks for digging into this!  The test works on Windows now, but I'm afraid
it probably shouldn't -- and it turns out the original Linux output was
actually correct all along.

str(some_SyntaxError_object) in Python shows the *basename* of the filename
attribute's value, not the entire filename attribute.  That's why, on Linux,
a filename '/pp' got changed to its basename, 'pp'.  On Windows it did not,
because '/' isn't the (primary) Windows filepath separator character.

Here's a little interactive session on Windows that may make this clearer:

>>> e = SyntaxError('ga', ('/abc/def', 2, 5, ''))
>>> print e  # the forward slash isn't recognized as special on Windows
ga (/abc/def, line 2)
>>> e = SyntaxError('ga', (r'\abc\def', 2, 5, ''))
>>> print e  # but backslashes are, and basename is 'def'
ga (def, line 2)
>>>

Showing the basename is intentional (this is obvious from the Python
implementation code, where it goes to a lot of trouble to extract the
basename).  It's arguably a Python bug that Python didn't remove the forward
slash on Windows to begin with, because Python's os.path.basename() on
Windows treats / and \ the same way (it's the SyntaxError object's __str__
method that doesn't recognize / as a separator on Windows).

The test now verifies:

  >>> try:
  ...     pp.setSource(u"prin 'hello'")
  ... except SyntaxError, err:
  ...     err_dict = err.__dict__
  ...     print '%(filename)s, line %(lineno)i, offset %(offset)i' %err_dict
  /pp, line 1, offset 12

and while that does work on Windows, it probably shouldn't:  the /pp
pathname is unnatural on Windows (\pp would be natural there).




More information about the Zope3-Checkins mailing list