Re: WindowsError is not available on linux?
In article , wrote: >On 07:53 pm, a...@pythoncraft.com wrote: >>In article , >>Peng Yu 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 (a...@pythoncraft.com) <*> 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
Re: WindowsError is not available on linux?
Benjamin Kaplan wrote: On Wed, Nov 18, 2009 at 2:53 PM, Aahz wrote: In article , Peng Yu 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
Re: WindowsError is not available on linux?
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
Re: WindowsError is not available on linux?
On 07:53 pm, a...@pythoncraft.com wrote: In article , Peng Yu 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
Re: WindowsError is not available on linux?
On Wed, Nov 18, 2009 at 2:53 PM, Aahz wrote: > In article , > Peng Yu 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 (a...@pythoncraft.com) <*> 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
Re: WindowsError is not available on linux?
In article , Peng Yu 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 (a...@pythoncraft.com) <*> 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
Re: WindowsError is not available on linux?
Peng Yu wrote: On Tue, Nov 17, 2009 at 9:18 PM, alex23 wrote: Peng Yu 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
Re: WindowsError is not available on linux?
On Tue, Nov 17, 2009 at 7:37 PM, Peng Yu wrote: > On Tue, Nov 17, 2009 at 9:18 PM, alex23 wrote: >> Peng Yu 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
Re: WindowsError is not available on linux?
Peng Yu 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
Re: WindowsError is not available on linux?
Peng Yu wrote: On Tue, Nov 17, 2009 at 8:25 PM, Benjamin Kaplan wrote: On Tue, Nov 17, 2009 at 9:18 PM, 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 "", line 3, in 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
Re: WindowsError is not available on linux?
On Tue, Nov 17, 2009 at 9:18 PM, alex23 wrote: > Peng Yu 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
Re: WindowsError is not available on linux?
Peng Yu 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
Re: WindowsError is not available on linux?
On Tue, Nov 17, 2009 at 9:40 PM, Peng Yu wrote: > On Tue, Nov 17, 2009 at 8:25 PM, Benjamin Kaplan > wrote: >> On Tue, Nov 17, 2009 at 9:18 PM, 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 "", line 3, in >>> 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
Re: WindowsError is not available on linux?
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 "", line 3, in 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
Re: WindowsError is not available on linux?
On Tue, Nov 17, 2009 at 8:25 PM, Benjamin Kaplan wrote: > On Tue, Nov 17, 2009 at 9:18 PM, 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 "", line 3, in >> 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
Re: WindowsError is not available on linux?
On Tue, Nov 17, 2009 at 9:18 PM, 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 "", line 3, in > 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
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 "", line 3, in NameError: name 'WindowsError' is not defined -- http://mail.python.org/mailman/listinfo/python-list