Raising OSError concrete classes from errno code
Currently we have exception tree of classes inherited from OSError When we use C API we can call PyErr_SetFromErrno and PyErr_SetFromErrnoWithFilename[Object] functions. This ones raise concrete exception class (FileNotFoundError for example) looking on implicit errno value. I cannot see the way to do it from python. Maybe adding builtin like exception_from_errno(errno, filename=None) make some value? Function returns exception instance, concrete class depends of errno value For example if I've got EPOLLERR from poller call I can get error code via s.getsockopt(SOL_SOCKET, SO_ERROR) but I cannot raise concrete exception from given errno code. -- Thanks, Andrew Svetlov
2012/12/25 Andrew Svetlov
Currently we have exception tree of classes inherited from OSError When we use C API we can call PyErr_SetFromErrno and PyErr_SetFromErrnoWithFilename[Object] functions. This ones raise concrete exception class (FileNotFoundError for example) looking on implicit errno value. I cannot see the way to do it from python.
Maybe adding builtin like exception_from_errno(errno, filename=None) make some value? Function returns exception instance, concrete class depends of errno value
I think a static method on OSError like .from_errno would be good. -- Regards, Benjamin
static method is better than new builtin function, agree.
On Wed, Dec 26, 2012 at 12:03 AM, Benjamin Peterson
2012/12/25 Andrew Svetlov
: Currently we have exception tree of classes inherited from OSError When we use C API we can call PyErr_SetFromErrno and PyErr_SetFromErrnoWithFilename[Object] functions. This ones raise concrete exception class (FileNotFoundError for example) looking on implicit errno value. I cannot see the way to do it from python.
Maybe adding builtin like exception_from_errno(errno, filename=None) make some value? Function returns exception instance, concrete class depends of errno value
I think a static method on OSError like .from_errno would be good.
-- Regards, Benjamin
-- Thanks, Andrew Svetlov
On 25.12.12 23:55, Andrew Svetlov wrote:
Currently we have exception tree of classes inherited from OSError When we use C API we can call PyErr_SetFromErrno and PyErr_SetFromErrnoWithFilename[Object] functions. This ones raise concrete exception class (FileNotFoundError for example) looking on implicit errno value. I cannot see the way to do it from python.
raise OSError(errno.ENOENT, 'No such file or directory', 'qwerty') Traceback (most recent call last): File "<stdin>", line 1, in <module> FileNotFoundError: [Errno 2] No such file or directory: 'qwerty'
On Wed, Dec 26, 2012 at 6:50 PM, Serhiy Storchaka
On 25.12.12 23:55, Andrew Svetlov wrote:
Currently we have exception tree of classes inherited from OSError When we use C API we can call PyErr_SetFromErrno and PyErr_SetFromErrnoWithFilename[Object] functions. This ones raise concrete exception class (FileNotFoundError for example) looking on implicit errno value. I cannot see the way to do it from python.
raise OSError(errno.ENOENT, 'No such file or directory', 'qwerty') Traceback (most recent call last): File "<stdin>", line 1, in <module> FileNotFoundError: [Errno 2] No such file or directory: 'qwerty'
As Serhiy's example shows, this mapping of error numbers to subclasses is implemented directly in OSError.__new__. We did this so that code could catch the new exceptions, even when dealing with old code that raises the legacy exception types. http://docs.python.org/3/library/exceptions#OSError could probably do with an example like the one quoted in order to make this clearer Cheers, Nick. -- Nick Coghlan | ncoghlan@gmail.com | Brisbane, Australia
On Wed, Dec 26, 2012 at 12:16 PM, Nick Coghlan
On Wed, Dec 26, 2012 at 6:50 PM, Serhiy Storchaka
wrote: On 25.12.12 23:55, Andrew Svetlov wrote:
Currently we have exception tree of classes inherited from OSError When we use C API we can call PyErr_SetFromErrno and PyErr_SetFromErrnoWithFilename[Object] functions. This ones raise concrete exception class (FileNotFoundError for example) looking on implicit errno value. I cannot see the way to do it from python.
raise OSError(errno.ENOENT, 'No such file or directory', 'qwerty') Traceback (most recent call last): File "<stdin>", line 1, in <module> FileNotFoundError: [Errno 2] No such file or directory: 'qwerty'
As Serhiy's example shows, this mapping of error numbers to subclasses is implemented directly in OSError.__new__. We did this so that code could catch the new exceptions, even when dealing with old code that raises the legacy exception types.
Sorry. Looks like OSError.__new__ requires at least two arguments for executing subclass search mechanism:
OSError(errno.ENOENT) OSError(2,) OSError(errno.ENOENT, 'error msg') FileNotFoundError(2, 'error msg')
I had tried first one and got confuse.
http://docs.python.org/3/library/exceptions#OSError could probably do with an example like the one quoted in order to make this clearer
Added http://bugs.python.org/issue16785 for this.
Cheers, Nick.
-- Nick Coghlan | ncoghlan@gmail.com | Brisbane, Australia _______________________________________________ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/andrew.svetlov%40gmail.com
-- Thanks, Andrew Svetlov
On Wed, 26 Dec 2012 13:37:13 +0200
Andrew Svetlov
As Serhiy's example shows, this mapping of error numbers to subclasses is implemented directly in OSError.__new__. We did this so that code could catch the new exceptions, even when dealing with old code that raises the legacy exception types.
Sorry. Looks like OSError.__new__ requires at least two arguments for executing subclass search mechanism:
OSError(errno.ENOENT) OSError(2,) OSError(errno.ENOENT, 'error msg') FileNotFoundError(2, 'error msg')
Indeed, it does. I did this for consistency, because calling OSError with only one argument doesn't set the "errno" attribute at all:
e = OSError(5) e.errno
Regards Antoine.
Thanks for the elaboration!
On Wed, Dec 26, 2012 at 6:42 PM, Antoine Pitrou
On Wed, 26 Dec 2012 13:37:13 +0200 Andrew Svetlov
wrote: As Serhiy's example shows, this mapping of error numbers to subclasses is implemented directly in OSError.__new__. We did this so that code could catch the new exceptions, even when dealing with old code that raises the legacy exception types.
Sorry. Looks like OSError.__new__ requires at least two arguments for executing subclass search mechanism:
OSError(errno.ENOENT) OSError(2,) OSError(errno.ENOENT, 'error msg') FileNotFoundError(2, 'error msg')
Indeed, it does. I did this for consistency, because calling OSError with only one argument doesn't set the "errno" attribute at all:
e = OSError(5) e.errno
Regards
Antoine.
_______________________________________________ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/andrew.svetlov%40gmail.com
-- Thanks, Andrew Svetlov
It sould be in Python-Ideas: add keyword argument support for OSError and subclasses with suitable default values. I.e.
OSError(errno=errno.ENOENT) FileNotFoundError(2, 'No such file or directory') FileNotFoundError(filename='qwerty') FileNotFoundError(2, 'No such file or directory') FileNotFoundError(strerr='Bad file') FileNotFoundError(2, 'Bad file')
participants (5)
-
Andrew Svetlov
-
Antoine Pitrou
-
Benjamin Peterson
-
Nick Coghlan
-
Serhiy Storchaka