473,763 Members | 1,377 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Socket exceptions aren't in the standard exception hierarchy

Here are three network-related exceptions. These
were caught by "except" with no exception type, because
none of the more specific exceptions matched. This
is what a traceback produced:
1. File "D:\Python24\li b\socket.py", line 295, in read
data = self._sock.recv (recv_size)
timeout: timed out

2. File "D:\Python24\li b\socket.py", line 295, in read
data = self._sock.recv (recv_size)
error: (10054, 'Connection reset by peer')

3. File "D:\Python24\li b\socket.py", line 317, in readline
data = recv(1)
IOError: [Errno socket error] timed out

For 1 and 2, those are errors that aren't in the
exception hierarchy. Looking at the C code for "socketmodule.c ",
it's clear that "socket.err or" doesn't inherit from any standard
exception class. See, in "init_socket()" :

socket_error = PyErr_NewExcept ion("socket.err or", NULL, NULL);

That first NULL should be some parent exception, maybe "IOError".
As it is, "socket.err or" is outside the standard exception hierarchy.
That's not too good.

Case #3, IOError, should have been caught by this:

except IOError, message: # I/O error

But it wasn't. The "IOError" fell through, was caught by the
next outer exception block, and was logged as a generic
error.

I can't find where in the Python socket module an "IOError"
could be raised. I would have expected "socket.timeout ".

Anyway, I need to know the full set of exceptions that can
be raised by sockets. Thanks.

John Nagle
Apr 23 '07 #1
10 3581
Dennis Lee Bieber wrote:
On Sun, 22 Apr 2007 23:20:25 -0700, John Nagle <na***@animats. com>
declaimed the following in comp.lang.pytho n:

>>2. File "D:\Python24\li b\socket.py", line 295, in read
data = self._sock.recv (recv_size)
error: (10054, 'Connection reset by peer')

That looks like M$ Windows version of UNIX/Linux error number 54
(pretty much all Windows socket errors are UNIX number+10000)

Errors coming from Windows may not be mapped to specific Python
exceptions, but rather to some general error conditions. {hypothesis} As
such, the Windows errors may not match what UNIX/Linux report.
Actually, that's not what's happening. The socket module is
explicitly raising "socket.err or" in C code. It's not an OSError or
a WindowsError, although the text makes it look like one.

The problem is not that socket errors aren't entirely portable.
It's that they're not even in the Python exception hierarchy.
See "http://docs.python.org/lib/module-exceptions.html ".
They have their own hierarchy, which starts at "socket.err or".
All built-in exceptions were supposed to be converted to the
standard exception hierarchy back before Python 2.0, but these
weren't.

Either they should be under IOError, or there should be
"NetworkErr or" under EnvironmentErro r, and they should be under
that. "NetworkErr or", alongside IOError in the hierarchy,
would be useful. All the things that go wrong in networking
belong under there. Socket-level errors, SSL/TLS-level errors,
and HTTP/FTP/etc. level errors all belong under NetworkError.

This has to be fixed before PEP 352, when the exception
hierarchy is enforced, or the socket exceptions won't even work
right.

John Nagle
Apr 23 '07 #2
John Nagle wrote:
Dennis Lee Bieber wrote:
>On Sun, 22 Apr 2007 23:20:25 -0700, John Nagle <na***@animats. com>
declaimed the following in comp.lang.pytho n:

>>2. File "D:\Python24\li b\socket.py", line 295, in read
data = self._sock.recv (recv_size)
error: (10054, 'Connection reset by peer')
That looks like M$ Windows version of UNIX/Linux error number 54
(pretty much all Windows socket errors are UNIX number+10000)

Errors coming from Windows may not be mapped to specific Python
exceptions, but rather to some general error conditions. {hypothesis} As
such, the Windows errors may not match what UNIX/Linux report.

Actually, that's not what's happening. The socket module is
explicitly raising "socket.err or" in C code. It's not an OSError or
a WindowsError, although the text makes it look like one.

The problem is not that socket errors aren't entirely portable.
It's that they're not even in the Python exception hierarchy.
See "http://docs.python.org/lib/module-exceptions.html ".
They have their own hierarchy, which starts at "socket.err or".
All built-in exceptions were supposed to be converted to the
standard exception hierarchy back before Python 2.0, but these
weren't.

Either they should be under IOError, or there should be
"NetworkErr or" under EnvironmentErro r, and they should be under
that. "NetworkErr or", alongside IOError in the hierarchy,
would be useful. All the things that go wrong in networking
belong under there. Socket-level errors, SSL/TLS-level errors,
and HTTP/FTP/etc. level errors all belong under NetworkError.

This has to be fixed before PEP 352, when the exception
hierarchy is enforced, or the socket exceptions won't even work
right.
John:

Where did you get this information? If true it would certainly need to
be logged as a bug, but under Windows on 2,4 I see
>>issubclass(so cket.gaierror, Exception)
True
>>>
and the same under Cygwin 2.5. I am presuming most other users will see
the same thing.

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
Recent Ramblings http://holdenweb.blogspot.com

Apr 23 '07 #3
Steve Holden wrote:
John Nagle wrote:
>Dennis Lee Bieber wrote:
>>On Sun, 22 Apr 2007 23:20:25 -0700, John Nagle <na***@animats. com>
declaimed the following in comp.lang.pytho n:
2. File "D:\Python24\li b\socket.py", line 295, in read
data = self._sock.recv (recv_size)
error: (10054, 'Connection reset by peer')

That looks like M$ Windows version of UNIX/Linux error number 54
(pretty much all Windows socket errors are UNIX number+10000)

Errors coming from Windows may not be mapped to specific Python
exceptions, but rather to some general error conditions. {hypothesis} As
such, the Windows errors may not match what UNIX/Linux report.


Actually, that's not what's happening. The socket module is
explicitly raising "socket.err or" in C code. It's not an OSError or
a WindowsError, although the text makes it look like one.

The problem is not that socket errors aren't entirely portable.
It's that they're not even in the Python exception hierarchy.
See "http://docs.python.org/lib/module-exceptions.html ".
They have their own hierarchy, which starts at "socket.err or".
All built-in exceptions were supposed to be converted to the
standard exception hierarchy back before Python 2.0, but these
weren't.

Either they should be under IOError, or there should be
"NetworkErro r" under EnvironmentErro r, and they should be under
that. "NetworkErr or", alongside IOError in the hierarchy,
would be useful. All the things that go wrong in networking
belong under there. Socket-level errors, SSL/TLS-level errors,
and HTTP/FTP/etc. level errors all belong under NetworkError.

This has to be fixed before PEP 352, when the exception
hierarchy is enforced, or the socket exceptions won't even work
right.
John:

Where did you get this information? If true it would certainly need to
be logged as a bug, but under Windows on 2,4 I see
>>issubclass(so cket.gaierror, Exception)
True
>>>

and the same under Cygwin 2.5. I am presuming most other users will see
the same thing.

regards
Steve
Ah. "socket.err or" is a subclass of "Exception" , but not
of "StandardError" .

issubclass(sock et.error,Standa rdError)

is False.
John Nagle
Apr 23 '07 #4
>>2. File "D:\Python24\li b\socket.py", line 295, in read
>>data = self._sock.recv (recv_size)
error: (10054, 'Connection reset by peer')

That looks like M$ Windows version of UNIX/Linux error number 54
(pretty much all Windows socket errors are UNIX number+10000)

Errors coming from Windows may not be mapped to specific Python
exceptions, but rather to some general error conditions. {hypothesis} As
such, the Windows errors may not match what UNIX/Linux report.
Actually, that's not what's happening. The socket module is
explicitly raising "socket.err or" in C code. It's not an OSError or
a WindowsError, although the text makes it look like one.

The problem is not that socket errors aren't entirely portable.
It's that they're not even in the Python exception hierarchy.
See "http://docs.python.org/lib/module-exceptions.html ".
They have their own hierarchy, which starts at "socket.err or".
All built-in exceptions were supposed to be converted to the
standard exception hierarchy back before Python 2.0, but these
weren't.

Either they should be under IOError, or there should be
"NetworkErr or" under EnvironmentErro r, and they should be under
that. "NetworkErr or", alongside IOError in the hierarchy,
would be useful. All the things that go wrong in networking
belong under there. Socket-level errors, SSL/TLS-level errors,
and HTTP/FTP/etc. level errors all belong under NetworkError.

This has to be fixed before PEP 352, when the exception
hierarchy is enforced, or the socket exceptions won't even work
right.
John:

Where did you get this information? If true it would certainly need to
be logged as a bug, but under Windows on 2,4 I see
>>issubclass(so cket.gaierror, Exception)
True
>>>
and the same under Cygwin 2.5. I am presuming most other users will see
the same thing.

regards
Steve

Ah. "socket.err or" is a subclass of "Exception" , but not
of "StandardError" .

issubclass(sock et.error,Standa rdError)

is False.

On linux, python 2.5:
>>import socket
issubclass(so cket.error,Exce ption)
True
Apr 23 '07 #5
John Nagle wrote:
[socket.error bug report]
>>
Where did you get this information? If true it would certainly need to
be logged as a bug, but under Windows on 2,4 I see
> >>issubclass(so cket.gaierror, Exception)
True
> >>>

and the same under Cygwin 2.5. I am presuming most other users will see
the same thing.

regards
Steve

Ah. "socket.err or" is a subclass of "Exception" , but not
of "StandardError" .

issubclass(sock et.error,Standa rdError)

is False.
Right, so this *is* a big, as long as Brett Cannon's upcoming (further)
reorganization of the standard exception hierarchy doesn't stamp on it.
It probably *was* overlooked in the reorganization of the hierarchy, and
this implies there may be other extensions that also make the same error.

It should be logged as a bug in the tracker - the fix is probably pretty
simple, but it'll need some consideration of the forward compatibility
considerations.

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
Recent Ramblings http://holdenweb.blogspot.com

Apr 23 '07 #6
Steve Holden wrote:
John Nagle wrote:
[socket.error bug report]
>>>
Where did you get this information? If true it would certainly need
to be logged as a bug, but under Windows on 2,4 I see

>>issubclass(so cket.gaierror, Exception)
True
>>>

and the same under Cygwin 2.5. I am presuming most other users will
see the same thing.

regards
Steve


Ah. "socket.err or" is a subclass of "Exception" , but not
of "StandardError" .

issubclass(sock et.error,Standa rdError)

is False.
Right, so this *is* a bug, as long as Brett Cannon's upcoming (further)
reorganization of the standard exception hierarchy doesn't stamp on it.
It probably *was* overlooked in the reorganization of the hierarchy, and
this implies there may be other extensions that also make the same error.

It should be logged as a bug in the tracker - the fix is probably pretty
simple, but it'll need some consideration of the forward compatibility
considerations.

regards
Steve
The bug tracker shows quite a number of problems related to
socket exceptions. Searching for "socket AND exception" returns
45 results. These seem to be the most relevant ones:

[ 805194 ] Inappropriate error received using socket timeout
[ 1019808 ] wrong socket error returned
[ 1571878 ] Improvements to socket module exceptions
[ 708927 ] socket timeouts produce wrong errors in win32

None of them quite cover this new issue, but it's clear
that the area needs some work. Should this new issue
be added as a new bug or as an added comment to one of the
above?

Just figuring out what exceptions can be raised from
the socket module is tough. I've seen exceptions
derived from "socket.err or", exceptions from IOError,
and exceptions from the SSL layer, which patches the
sockets module when loaded. These are non-bug
exceptions; that is, the problem is out in the network,
external to the program. I'm still not sure I have all
the possibilities covered.

Retrying on unknown exceptions isn't the answer;
that leads to exception loops if there's a program bug.
The most important distinction with sockets is "external
network problem" vs. "local program program". I'd like
to see a "NetworkExcepti on" in the exception hierarchy,
with all the things that can go wrong due to conditions
external to the local machine under that exception.

I'd suggest the following:

1. Add "NetworkErr or" under "IOError" in
the exception hierarchy.

2. Put the existing "socket.err or" under "NetworkErr or".
Since "socket.err or" needs to be reparented anyway (it's currently
a direct descendant of "Exception" ) this provides a good place for it.

3. Find any places where the socket module can raise IOError
or OSError due to an external network condition, and make them raise
something under NetworkError instead. Code that catches IOError
will still work.

4. Put all errors in the various SSL modules (SSLError, etc.)
which can be raised due to external network conditions under "NetworkErr or".

5. Move "urllib2.URLErr or", which is currently under IOError, to be
under NetworkError.

6. Move the misc. errors from "urllib", like "ContentTooShor tError",
which are currently under IOError, down a level under NetworkError.

Then, programs that catch NetworkError could be sure of catching
all network trouble conditions, but not local code bugs.

John Nagle
Apr 24 '07 #7
John Nagle wrote:
Steve Holden wrote:
>John Nagle wrote:
[socket.error bug report]
>>>Where did you get this information? If true it would certainly need
to be logged as a bug, but under Windows on 2,4 I see

>>issubclass(so cket.gaierror, Exception)
True
>>>

and the same under Cygwin 2.5. I am presuming most other users will
see the same thing.

regards
Steve

Ah. "socket.err or" is a subclass of "Exception" , but not
of "StandardError" .

issubclass(sock et.error,Standa rdError)

is False.
Right, so this *is* a bug, as long as Brett Cannon's upcoming (further)
reorganizati on of the standard exception hierarchy doesn't stamp on it.
It probably *was* overlooked in the reorganization of the hierarchy, and
this implies there may be other extensions that also make the same error.

It should be logged as a bug in the tracker - the fix is probably pretty
simple, but it'll need some consideration of the forward compatibility
considerations .

regards
Steve

The bug tracker shows quite a number of problems related to
socket exceptions. Searching for "socket AND exception" returns
45 results. These seem to be the most relevant ones:

[ 805194 ] Inappropriate error received using socket timeout
[ 1019808 ] wrong socket error returned
[ 1571878 ] Improvements to socket module exceptions
[ 708927 ] socket timeouts produce wrong errors in win32

None of them quite cover this new issue, but it's clear
that the area needs some work. Should this new issue
be added as a new bug or as an added comment to one of the
above?

Just figuring out what exceptions can be raised from
the socket module is tough. I've seen exceptions
derived from "socket.err or", exceptions from IOError,
and exceptions from the SSL layer, which patches the
sockets module when loaded. These are non-bug
exceptions; that is, the problem is out in the network,
external to the program. I'm still not sure I have all
the possibilities covered.

Retrying on unknown exceptions isn't the answer;
that leads to exception loops if there's a program bug.
The most important distinction with sockets is "external
network problem" vs. "local program program". I'd like
to see a "NetworkExcepti on" in the exception hierarchy,
with all the things that can go wrong due to conditions
external to the local machine under that exception.

I'd suggest the following:

1. Add "NetworkErr or" under "IOError" in
the exception hierarchy.

2. Put the existing "socket.err or" under "NetworkErr or".
Since "socket.err or" needs to be reparented anyway (it's currently
a direct descendant of "Exception" ) this provides a good place for it.

3. Find any places where the socket module can raise IOError
or OSError due to an external network condition, and make them raise
something under NetworkError instead. Code that catches IOError
will still work.

4. Put all errors in the various SSL modules (SSLError, etc.)
which can be raised due to external network conditions under "NetworkErr or".

5. Move "urllib2.URLErr or", which is currently under IOError, to be
under NetworkError.

6. Move the misc. errors from "urllib", like "ContentTooShor tError",
which are currently under IOError, down a level under NetworkError.

Then, programs that catch NetworkError could be sure of catching
all network trouble conditions, but not local code bugs.

John Nagle
All these notes should be included in the bug report, as I suspect the
module would benefit from additional clarity. I believe socket just grew
up as a clever way of making as much use of the platform socket library
as possible, and that the approach's deficiencies have become more
obvious as time has gone by and Python has matured.

The work you describe is not insignificant, so it should be fully
described if it's to stand any chance of being implemented. I *do* think
it would rationalize things considerably, but that's only me ...

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
Recent Ramblings http://holdenweb.blogspot.com

Apr 24 '07 #8
Steve Holden wrote:
John Nagle wrote:
>Steve Holden wrote:
>>John Nagle wrote:
[socket.error bug report]
All these notes should be included in the bug report, as I suspect the
module would benefit from additional clarity.
Done. See

[ 1706815 ] socket.error exceptions not subclass of StandardError

Also see

[ 805194 ] Inappropriate error received using socket timeout
[ 1019808 ] wrong socket error returned
[ 1571878 ] Improvements to socket module exceptions
[ 708927 ] socket timeouts produce wrong errors in win32

for related but not identical problems in that area.

John Nagle
Apr 24 '07 #9
John Nagle wrote:
Steve Holden wrote:
>John Nagle wrote:
>>Steve Holden wrote:

John Nagle wrote:
[socket.error bug report]
>All these notes should be included in the bug report, as I suspect the
module would benefit from additional clarity.

Done. See

[ 1706815 ] socket.error exceptions not subclass of StandardError

Also see

[ 805194 ] Inappropriate error received using socket timeout
[ 1019808 ] wrong socket error returned
[ 1571878 ] Improvements to socket module exceptions
[ 708927 ] socket timeouts produce wrong errors in win32

for related but not identical problems in that area.
Thanks. At least this is less likely to be overlooked now.

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
Recent Ramblings http://holdenweb.blogspot.com

Apr 25 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
1864
by: cody | last post by:
I have a method that gets called if the user presses a certain button. If the object is in a state that doesn't allow the calling of that method, what should I do? Should I better throw an InvalidOperationException or should I derive my own exceptions from ApplicationException? In general, it is not clear to me when to use the predefined exceptions and when to create new ones by deriving from ApplicationException. And a last question:...
4
2273
by: faktujaa | last post by:
Hi, I am having some problem with callback used in socket implementation. private static void Connect(string strPrtrIPAddr, int intPrtrPort, ref Socket rsocClient) { try { // Create remote end point. System.Net.IPAddress IPAddress = System.Net.IPAddress.Parse(strPrtrIPAddr); System.Net.IPEndPoint IPEndPoint = new System.Net.IPEndPoint(IPAddress,
24
2500
by: mag31 | last post by:
Is there any way to find out if a particular .net function will throw an exception without first generating the exception? I am using structured exception handling i.e. try catch finally blocks with a top level catch all for Exception. However, I would like to be able to catch most .net exceptions when they are generated. I would then be able to generate a valuable exception message and do something about it!!! Hence the question above....
14
2296
by: Noah Roberts | last post by:
I am a bit confused about how inheritance works with regard to exceptions apparently. class ParentEx : public std::exception { }; class SubEx : public ParentEx,std::runtime_error { };
822
29704
by: Turamnvia Suouriviaskimatta | last post by:
I 'm following various posting in "comp.lang.ada, comp.lang.c++ , comp.realtime, comp.software-eng" groups regarding selection of a programming language of C, C++ or Ada for safety critical real-time applications. The majority of expert/people recommend Ada for safety critical real-time applications. I've many years of experience in C/C++ (and Delphi) but no Ada knowledge. May I ask if it is too difficult to move from C/C++ to Ada?...
16
2176
by: Einar Høst | last post by:
Hi, I'm getting into the Trace-functionality in .NET, using it to provide some much-needed logging across dlls in the project we're working on. However, being a newbie, I'm wondering if some more experienced loggers can provide me with some ideas as to how to log in a simple yet flexible manner. For instance, I'd like the code to be as uncluttered as possible by Trace statements. As an example of basic logging functionality, I've come...
14
3481
by: dcassar | last post by:
I have had a lively discussion with some coworkers and decided to get some general feedback on an issue that I could find very little guidance on. Why is it considered bad practice to define a public member with a return type that is derived from System.Exception? I understand the importance of having clean, concise code that follows widely-accepted patterns and practices, but in this case, I find it hard to blindly follow a standard...
10
5186
by: ThunderMusic | last post by:
Hi, I'm currently working with sockets. I accept connections using m_mySocket.Listen(BackLogCount); But when I want to stop listening, I shutdown all my clients and call m_mySocket.Close(), but it always raise a OnConnect event (actually, it calls the callback function as if there was a new connection attempt) and I receive a ObjectDisposedException as soon as I do m_mySocket.EndAccept. Does anyone have any idea of what I could do about...
0
9563
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9386
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10145
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9938
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7366
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6642
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5406
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3917
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2793
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.