WindowsError is not available on linux?

Dave Angel davea at ieee.org
Tue Nov 17 22:57:53 EST 2009


Peng Yu wrote:
> On Tue, Nov 17, 2009 at 8:25 PM, Benjamin Kaplan
> <benjamin.kaplan at case.edu> wrote:
>   
>> On Tue, Nov 17, 2009 at 9:18 PM, Peng Yu <pengyu.ut at gmail.com> wrote:
>>     
>>> It's not clear to me whether WindowsError is available on linux or
>>> not, after I read the document. But I see WindowsError in shutil.py.
>>> Could you somebody let me know what cause the following error?
>>>
>>>       
>>>>>> try:
>>>>>>             
>>> ...   raise WindowsError('WindowsError')
>>> ... except WindowsError as e:
>>> ...   print e
>>> ...
>>> Traceback (most recent call last):
>>>  File "<stdin>", line 3, in <module>
>>> NameError: name 'WindowsError' is not defined
>>> --
>>>       
>> does this answer your question?
>>
>> Python 2.6.4 (r264:75706, Oct 28 2009, 23:01:00)
>> [GCC 4.2.1 (Apple Inc. build 5646) (dot 1)] on darwin
>> Type "help", "copyright", "credits" or "license" for more information.
>>     
>>>>> import shutil
>>>>> print shutil.WindowsError
>>>>>           
>> None
>>     
>
> But the document doesn't say shutil need to be imported in order to
> use WindowsError. Shall the document or the code be corrected?
>
> http://docs.python.org/library/exceptions.html
>
>   
The WindowsError is available in a Windows build, and I don't directly 
know if it's available on Linux.

I think shutil is a red-herring here, however.  The docs show the 
implementation of copyTree(), and that function uses WindowsError.  
However, earlier in the shutil.py file, there is the following trick:

try:
    WindowsError
except NameError:
    WindowsError = None

This has the effect of defining a dummy attribute "WindowsError"  WITHIN 
THIS ONE MODULE, if it's not already in the global namespace.  This lets 
the code
in function copytree() deal with an OSError differently on Windows than 
in other systems.

I do not expect that the name WindowsError of that module was intended 
to be accessed by user's code.  And because some of you see a None 
value, that tells me that it is indeed not defined for some systems.

I think that fact should be documented in the URL you mention,  
exceptions.html

But in the meantime, if you're not on a Windows system, you won't see 
that exception, and if you need to be portable, you may pull the same 
trick that shutil did.

DaveA




More information about the Python-list mailing list