Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Python: Python

WindowsError is not available on linux?

 

 

Python python RSS feed   Index | Next | Previous | View Threaded


pengyu.ut at gmail

Nov 17, 2009, 6:18 PM

Post #1 of 17 (592 views)
Permalink
WindowsError is not available on linux?

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
--
http://mail.python.org/mailman/listinfo/python-list


benjamin.kaplan at case

Nov 17, 2009, 6:25 PM

Post #2 of 17 (574 views)
Permalink
Re: WindowsError is not available on linux? [In reply to]

On Tue, Nov 17, 2009 at 9:18 PM, Peng Yu <pengyu.ut [at] gmail> 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


> http://mail.python.org/mailman/listinfo/python-list
>
--
http://mail.python.org/mailman/listinfo/python-list


pengyu.ut at gmail

Nov 17, 2009, 6:40 PM

Post #3 of 17 (574 views)
Permalink
Re: WindowsError is not available on linux? [In reply to]

On Tue, Nov 17, 2009 at 8:25 PM, Benjamin Kaplan
<benjamin.kaplan [at] case> wrote:
> On Tue, Nov 17, 2009 at 9:18 PM, Peng Yu <pengyu.ut [at] gmail> 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
--
http://mail.python.org/mailman/listinfo/python-list


python at mrabarnett

Nov 17, 2009, 6:44 PM

Post #4 of 17 (574 views)
Permalink
Re: WindowsError is not available on linux? [In reply to]

Peng Yu 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

WindowsError is for Windows-specific errors (hence the name!). You
shouldn't rely on it being defined on non-Windows machines.
--
http://mail.python.org/mailman/listinfo/python-list


wuwei23 at gmail

Nov 17, 2009, 7:18 PM

Post #5 of 17 (574 views)
Permalink
Re: WindowsError is not available on linux? [In reply to]

Peng Yu <pengyu...@gmail.com> wrote:
> But the document doesn't say shutil need to be imported in order to
> use WindowsError. Shall the document or the code be corrected?

Neither, it's your understanding that needs correction.

Benjamin wasn't trying to say that WindowsError is defined within
shutil, he was showing that it _isn't_ defined within shutil on a non-
Windows machine.

As you're looking in shutil.py, you should have noticed this at the
very top, just beneath the declaration of the Error exception:

try:
WindowsError
except NameError:
WindowsError = None

This looks for the existence of the WindowsError exception - present
only under Windows - and if it's not there it binds the name to None.
You'll notice that the only place it's used in shutil.py is prefixed
by the test WindowsError is not None...

I think the mention of the exception being raised when a "Windows-
specific error occurs" should make it pretty clear that this is a
Windows-only exception.
--
http://mail.python.org/mailman/listinfo/python-list


benjamin.kaplan at case

Nov 17, 2009, 7:19 PM

Post #6 of 17 (574 views)
Permalink
Re: WindowsError is not available on linux? [In reply to]

On Tue, Nov 17, 2009 at 9:40 PM, Peng Yu <pengyu.ut [at] gmail> wrote:
> On Tue, Nov 17, 2009 at 8:25 PM, Benjamin Kaplan
> <benjamin.kaplan [at] case> wrote:
>> On Tue, Nov 17, 2009 at 9:18 PM, Peng Yu <pengyu.ut [at] gmail> 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 exception WindowsError is not defined on my computer (a Mac). The
*name* is defined in shutil, but it's mapped to None, not to an
exception.

> --
> http://mail.python.org/mailman/listinfo/python-list
>
--
http://mail.python.org/mailman/listinfo/python-list


pengyu.ut at gmail

Nov 17, 2009, 7:37 PM

Post #7 of 17 (574 views)
Permalink
Re: WindowsError is not available on linux? [In reply to]

On Tue, Nov 17, 2009 at 9:18 PM, alex23 <wuwei23 [at] gmail> wrote:
> Peng Yu <pengyu...@gmail.com> wrote:
>> But the document doesn't say shutil need to be imported in order to
>> use WindowsError. Shall the document or the code be corrected?
>
> Neither, it's your understanding that needs correction.
>
> Benjamin wasn't trying to say that WindowsError is defined within
> shutil, he was showing that it _isn't_ defined within shutil on a non-
> Windows machine.
>
> As you're looking in shutil.py, you should have noticed this at the
> very top, just beneath the declaration of the Error exception:
>
>    try:
>        WindowsError
>    except NameError:
>        WindowsError = None
>
> This looks for the existence of the WindowsError exception - present
> only under Windows - and if it's not there it binds the name to None.
> You'll notice that the only place it's used in shutil.py is prefixed
> by the test WindowsError is not None...
>
> I think the mention of the exception being raised when a "Windows-
> specific error occurs" should make it pretty clear that this is a
> Windows-only exception.

I don't know about others. The wording "Windows-specific error occurs"
was ambiguous to me. It could refers to some errors resulted from
copying (on a linux machine) some files from linux file systems to
windows files systems (via samba, maybe). I recommend to revise the
document a little bit to avoid confusion.
--
http://mail.python.org/mailman/listinfo/python-list


davea at ieee

Nov 17, 2009, 7:57 PM

Post #8 of 17 (574 views)
Permalink
Re: WindowsError is not available on linux? [In reply to]

Peng Yu wrote:
> On Tue, Nov 17, 2009 at 8:25 PM, Benjamin Kaplan
> <benjamin.kaplan [at] case> wrote:
>
>> On Tue, Nov 17, 2009 at 9:18 PM, Peng Yu <pengyu.ut [at] gmail> 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

--
http://mail.python.org/mailman/listinfo/python-list


wuwei23 at gmail

Nov 17, 2009, 8:11 PM

Post #9 of 17 (564 views)
Permalink
Re: WindowsError is not available on linux? [In reply to]

Peng Yu <pengyu...@gmail.com> wrote:
> I don't know about others. The wording "Windows-specific error occurs"
> was ambiguous to me. It could refers to some errors resulted from
> copying (on a linux machine) some files from linux file systems to
> windows files systems (via samba, maybe). I recommend to revise the
> document a little bit to avoid confusion.

So file a bug: http://docs.python.org/bugs.html
--
http://mail.python.org/mailman/listinfo/python-list


clp2 at rebertia

Nov 17, 2009, 8:18 PM

Post #10 of 17 (564 views)
Permalink
Re: WindowsError is not available on linux? [In reply to]

On Tue, Nov 17, 2009 at 7:37 PM, Peng Yu <pengyu.ut [at] gmail> wrote:
> On Tue, Nov 17, 2009 at 9:18 PM, alex23 <wuwei23 [at] gmail> wrote:
>> Peng Yu <pengyu...@gmail.com> wrote:
>>> But the document doesn't say shutil need to be imported in order to
>>> use WindowsError. Shall the document or the code be corrected?
>>
>> Neither, it's your understanding that needs correction.
>>
>> Benjamin wasn't trying to say that WindowsError is defined within
>> shutil, he was showing that it _isn't_ defined within shutil on a non-
>> Windows machine.
>>
>> As you're looking in shutil.py, you should have noticed this at the
>> very top, just beneath the declaration of the Error exception:
>>
>>    try:
>>        WindowsError
>>    except NameError:
>>        WindowsError = None
>>
>> This looks for the existence of the WindowsError exception - present
>> only under Windows - and if it's not there it binds the name to None.
>> You'll notice that the only place it's used in shutil.py is prefixed
>> by the test WindowsError is not None...
>>
>> I think the mention of the exception being raised when a "Windows-
>> specific error occurs" should make it pretty clear that this is a
>> Windows-only exception.
>
> I don't know about others. The wording "Windows-specific error occurs"
> was ambiguous to me. It could refers to some errors resulted from
> copying (on a linux machine) some files from linux file systems to
> windows files systems (via samba, maybe). I recommend to revise the
> document a little bit to avoid confusion.

But your example isn't even Windows-specific because Samba server
exists; any error would network-related or specific to the SMB
protocol rather than Windows-related.

Cheers,
Chris
--
http://blog.rebertia.com
--
http://mail.python.org/mailman/listinfo/python-list


davea at ieee

Nov 17, 2009, 9:08 PM

Post #11 of 17 (564 views)
Permalink
Re: WindowsError is not available on linux? [In reply to]

Peng Yu wrote:
> On Tue, Nov 17, 2009 at 9:18 PM, alex23 <wuwei23 [at] gmail> wrote:
>
>> Peng Yu <pengyu...@gmail.com> wrote:
>>
>>> But the document doesn't say shutil need to be imported in order to
>>> use WindowsError. Shall the document or the code be corrected?
>>>
>> Neither, it's your understanding that needs correction.
>>
>> Benjamin wasn't trying to say that WindowsError is defined within
>> shutil, he was showing that it _isn't_ defined within shutil on a non-
>> Windows machine.
>>
>> As you're looking in shutil.py, you should have noticed this at the
>> very top, just beneath the declaration of the Error exception:
>>
>> try:
>> WindowsError
>> except NameError:
>> WindowsError =one
>>
>> This looks for the existence of the WindowsError exception - present
>> only under Windows - and if it's not there it binds the name to None.
>> You'll notice that the only place it's used in shutil.py is prefixed
>> by the test WindowsError is not None...
>>
>> I think the mention of the exception being raised when a "Windows-
>> specific error occurs" should make it pretty clear that this is a
>> Windows-only exception.
>>
>
> I don't know about others. The wording "Windows-specific error occurs"
> was ambiguous to me. It could refers to some errors resulted from
> copying (on a linux machine) some files from linux file systems to
> windows files systems (via samba, maybe). I recommend to revise the
> document a little bit to avoid confusion.
>
>
Worse, even if the exception cannot be thrown on a non-Windows
environment, leaving it undefined makes it very awkward to write
portable code. An except clause that can never happen in a particular
environment is pretty innocent. Or somebody can use a base class for
his except clause, to catch this error and other related ones. But it
blows up if you explicitly use this exception. I think that needs
documentation, at a minimum.

DaveA
--
http://mail.python.org/mailman/listinfo/python-list


aahz at pythoncraft

Nov 18, 2009, 11:53 AM

Post #12 of 17 (535 views)
Permalink
Re: WindowsError is not available on linux? [In reply to]

In article <mailman.599.1258510702.2873.python-list [at] python>,
Peng Yu <pengyu.ut [at] gmail> wrote:
>
>It's not clear to me whether WindowsError is available on linux or
>not, after I read the document.

Here's what I told a co-worker to do yesterday:

if os.name == 'nt':
DiskError = (OSError, WindowsError)
else:
DiskError = WindowsError

try:
disk_operation()
except DiskError:
logit()
--
Aahz (aahz [at] pythoncraft) <*> http://www.pythoncraft.com/

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are, by
definition, not smart enough to debug it." --Brian W. Kernighan
--
http://mail.python.org/mailman/listinfo/python-list


benjamin.kaplan at case

Nov 18, 2009, 12:38 PM

Post #13 of 17 (531 views)
Permalink
Re: WindowsError is not available on linux? [In reply to]

On Wed, Nov 18, 2009 at 2:53 PM, Aahz <aahz [at] pythoncraft> wrote:
> In article <mailman.599.1258510702.2873.python-list [at] python>,
> Peng Yu  <pengyu.ut [at] gmail> wrote:
>>
>>It's not clear to me whether WindowsError is available on linux or
>>not, after I read the document.
>
> Here's what I told a co-worker to do yesterday:
>
> if os.name == 'nt':
>    DiskError = (OSError, WindowsError)
> else:
>    DiskError = WindowsError
>
> try:
>    disk_operation()
> except DiskError:
>    logit()
> --

Shouldn't that be the other way?
if os.name == 'nt':
DiskError = OSError, WindowsError
else :
DiskError = OSError


> Aahz (aahz [at] pythoncraft)           <*>         http://www.pythoncraft.com/
>
> "Debugging is twice as hard as writing the code in the first place.
> Therefore, if you write the code as cleverly as possible, you are, by
> definition, not smart enough to debug it."  --Brian W. Kernighan
> --
> http://mail.python.org/mailman/listinfo/python-list
>
--
http://mail.python.org/mailman/listinfo/python-list


exarkun at twistedmatrix

Nov 18, 2009, 12:58 PM

Post #14 of 17 (531 views)
Permalink
Re: WindowsError is not available on linux? [In reply to]

On 07:53 pm, aahz [at] pythoncraft wrote:
>In article <mailman.599.1258510702.2873.python-list [at] python>,
>Peng Yu <pengyu.ut [at] gmail> wrote:
>>
>>It's not clear to me whether WindowsError is available on linux or
>>not, after I read the document.
>
>Here's what I told a co-worker to do yesterday:
>
>if os.name == 'nt':
> DiskError = (OSError, WindowsError)
>else:
> DiskError = WindowsError
>
>try:
> disk_operation()
>except DiskError:
> logit()

This isn't necessary. WindowsError subclasses OSError.

Jean-Paul
--
http://mail.python.org/mailman/listinfo/python-list


lists at cheimes

Nov 18, 2009, 1:09 PM

Post #15 of 17 (532 views)
Permalink
Re: WindowsError is not available on linux? [In reply to]

Dave Angel wrote:
> Worse, even if the exception cannot be thrown on a non-Windows
> environment, leaving it undefined makes it very awkward to write
> portable code. An except clause that can never happen in a particular
> environment is pretty innocent. Or somebody can use a base class for
> his except clause, to catch this error and other related ones. But it
> blows up if you explicitly use this exception. I think that needs
> documentation, at a minimum.

WindowsError is a subclass of OSError that contains additional Windows
specific error information. Just catch OSError and you are on the safe
side.

Christian

--
http://mail.python.org/mailman/listinfo/python-list


davea at ieee

Nov 18, 2009, 2:00 PM

Post #16 of 17 (527 views)
Permalink
Re: WindowsError is not available on linux? [In reply to]

Benjamin Kaplan wrote:
> On Wed, Nov 18, 2009 at 2:53 PM, Aahz <aahz [at] pythoncraft> wrote:
>
>> In article <mailman.599.1258510702.2873.python-list [at] python>,
>> Peng Yu <pengyu.ut [at] gmail> wrote:
>>
>>> It's not clear to me whether WindowsError is available on linux or
>>> not, after I read the document.
>>>
>> Here's what I told a co-worker to do yesterday:
>>
>> if os.name ='nt':
>> DiskError =OSError, WindowsError)
>> else:
>> DiskError =indowsError
>>
>> try:
>> disk_operation()
>> except DiskError:
>> logit()
>> --
>>
>
> Shouldn't that be the other way?
> if os.name ='nt':
> DiskError =SError, WindowsError
> else :
> DiskError =SError
>
>
>
Doesn't matter. It's not needed anyway, since WindowsError is derived
from OSError. So just use OSError in the except clause.

DaveA

--
http://mail.python.org/mailman/listinfo/python-list


aahz at pythoncraft

Nov 28, 2009, 9:40 AM

Post #17 of 17 (378 views)
Permalink
Re: WindowsError is not available on linux? [In reply to]

In article <mailman.656.1258577952.2873.python-list [at] python>,
<exarkun [at] twistedmatrix> wrote:
>On 07:53 pm, aahz [at] pythoncraft wrote:
>>In article <mailman.599.1258510702.2873.python-list [at] python>,
>>Peng Yu <pengyu.ut [at] gmail> wrote:
>>>
>>>It's not clear to me whether WindowsError is available on linux or
>>>not, after I read the document.
>>
>>Here's what I told a co-worker to do yesterday:
>>
>>if os.name =3D=3D 'nt':
>> DiskError =3D (OSError, WindowsError)
>>else:
>> DiskError =3D WindowsError
>>
>>try:
>> disk_operation()
>>except DiskError:
>> logit()
>
>This isn't necessary. WindowsError subclasses OSError.

Thanks! Much appreciated! (I haven't done much Windows programming in
the past -- and would have preferred to keep it that way. ;-)
--
Aahz (aahz [at] pythoncraft) <*> http://www.pythoncraft.com/

The best way to get information on Usenet is not to ask a question, but
to post the wrong information.
--
http://mail.python.org/mailman/listinfo/python-list

Python python RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.