This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Shutil cannot import WindowsError on windows
Type: behavior Stage:
Components: Extension Modules, Library (Lib), Windows Versions: Python 3.3, Python 2.7, Python 2.6
process
Status: closed Resolution: wont fix
Dependencies: Superseder:
Assigned To: Nosy List: christian.heimes, ronaldoussoren, saurabhgupta2u, tim.golden
Priority: normal Keywords:

Created on 2013-07-22 05:02 by saurabhgupta2u, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Messages (10)
msg193497 - (view) Author: Saurabh Gupta (saurabhgupta2u) Date: 2013-07-22 05:02
I've observed that shutil fails to import WindowsError on our windows 7 systems:

    from shutil import WindowsError

    File <file>, line <no>, in <module>
        from shutil import WindowsError
    ImportError: cannot import name WindowsError

The same statement works absolutely fine on linux. Has anyone else come across it too? Do you know how I could fix it?

Python version: 2.6.7
Linux OS: Centos 6.3
Windows OS: Windows 7 Professional x64
msg193510 - (view) Author: Christian Heimes (christian.heimes) * (Python committer) Date: 2013-07-22 07:34
Please open an interactive interpreter shell and show us the output of

import shutil
print shutil.__file__
print dir(shutil)
msg193511 - (view) Author: Saurabh Gupta (saurabhgupta2u) Date: 2013-07-22 07:45
>>> import shutil
>>> shutil.__file__
'C:\\Python26\\lib\\shutil.pyc'
>>> dir(shutil)
['Error', '__all__', '__builtins__', '__doc__', '__file__', '__name__', '__package__', '_basename', '_samefile', 'abspath', 'copy', 'copy2', 'copyfile', 'copyfileobj', 'copymode', 'copystat', 'copytree', 'destinsrc', 'errno', 'fnmatch', 'ignore_patterns', 'move', 'os', 'rmtree', 'stat', 'sys']
msg193512 - (view) Author: Saurabh Gupta (saurabhgupta2u) Date: 2013-07-22 07:47
And, the same thing on linux:

>>> import shutil
>>> print shutil.__file__
/software/package/linux64_centos6/python/2.6.7/lib/python2.6/shutil.pyc
>>> print dir(shutil)
['Error', 'WindowsError', '__all__', '__builtins__', '__doc__', '__file__', '__n
ame__', '__package__', '_basename', '_samefile', 'abspath', 'copy', 'copy2', 'co
pyfile', 'copyfileobj', 'copymode', 'copystat', 'copytree', 'destinsrc', 'errno'
, 'fnmatch', 'ignore_patterns', 'move', 'os', 'rmtree', 'stat', 'sys']
msg193513 - (view) Author: Christian Heimes (christian.heimes) * (Python committer) Date: 2013-07-22 07:52
Seems legit ... I don't have access to a Windows system right now so I can't test it.

WindowsError is globally available on Windows. You don't have to import it from shutil. Portable application should use OSError instead of WindowsError. WindowsError is a subclass of OSError with a Windows specific extra field. "try: ... except OSError: ..." catches WindowsError, too.
msg193514 - (view) Author: Saurabh Gupta (saurabhgupta2u) Date: 2013-07-22 08:17
I have a legacy software that has things like:

    try:
        some stuff
    except OSError, why:
        if WindowsError is not None and isinstance(why, WindowsError):
            do something
        else:
            do something else

So I'd ideally like to able to run this software unchanged on Windows systems too. It would be best for me if I could import WindowsError on windows.
msg193515 - (view) Author: Ronald Oussoren (ronaldoussoren) * (Python committer) Date: 2013-07-22 08:21
WindowsError is not part of the documented interface of shutil, but is an implementation detail.

"from shutil import WindowsUtil" works on Unix platforms because shutil contains a compatibility definition:

try:
    WindowsError
except NameError:
    WindowsError = None

shutil.copytree uses this to ignore some errors on Windows (that is, uses "if WindowsError is not None and isinstance(exc, WindowsError): ...").

Note that in 3.4 shutil does not export WindowsError at all, it uses a different way to suppress errors in copytree.

In a perfect world the code would have used:

try:
    _WindowsError = WindowsError
except NameError:
    _WindowsError = None

This would have avoided accidently exporting WindowsError on Unix platforms. I don't think it is worthwhile to do this change in a bugfix release though.
msg193516 - (view) Author: Tim Golden (tim.golden) * (Python committer) Date: 2013-07-22 08:24
Really this should be a wont-fix: the fact that it's possible to import WindowsError from shutil (on Linux) is an accident of its internal implementation. It's certainly not documented as such.

Saurabh: WindowsError is a builtin on Windows. If you want to mimic shutil which uses it as a proxy for determining the platform, you can  put a conditional import at the head of your own code:

try:
    WindowsError
except NameError:
    WindowsError = None


Don't bother trying to import it from shutil where it's an implementation artefact.
msg193517 - (view) Author: Ronald Oussoren (ronaldoussoren) * (Python committer) Date: 2013-07-22 08:25
I agree that this should be closed.
msg193523 - (view) Author: Saurabh Gupta (saurabhgupta2u) Date: 2013-07-22 10:06
Thank you all for your inputs!
Now I understand what the issue really is & would be fixing it by removing the imports in the legacy software.

I agree with the won't fix resolution of the ticket.
History
Date User Action Args
2022-04-11 14:57:48adminsetgithub: 62725
2013-07-22 10:06:02saurabhgupta2usetmessages: + msg193523
2013-07-22 08:25:38ronaldoussorensetstatus: open -> closed
resolution: wont fix
messages: + msg193517

versions: + Python 2.7, Python 3.3
2013-07-22 08:24:21tim.goldensetnosy: + tim.golden
messages: + msg193516
2013-07-22 08:21:08ronaldoussorensetnosy: + ronaldoussoren
messages: + msg193515
2013-07-22 08:17:41saurabhgupta2usetmessages: + msg193514
2013-07-22 07:52:26christian.heimessetmessages: + msg193513
2013-07-22 07:47:07saurabhgupta2usetmessages: + msg193512
2013-07-22 07:45:07saurabhgupta2usetmessages: + msg193511
2013-07-22 07:34:26christian.heimessetnosy: + christian.heimes
messages: + msg193510
2013-07-22 05:02:21saurabhgupta2ucreate