[Python-Dev] Raising OSError concrete classes from errno code

Andrew Svetlov andrew.svetlov at gmail.com
Wed Dec 26 12:37:13 CET 2012

On Wed, Dec 26, 2012 at 12:16 PM, Nick Coghlan <ncoghlan at gmail.com> wrote:
> On Wed, Dec 26, 2012 at 6:50 PM, Serhiy Storchaka <storchaka at gmail.com> 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.
Looks like OSError.__new__ requires at least two arguments for
executing subclass search mechanism:

>>> OSError(errno.ENOENT)
>>> 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 at gmail.com   |   Brisbane, Australia
> _______________________________________________
> Python-Dev mailing list
> Python-Dev at python.org
> http://mail.python.org/mailman/listinfo/python-dev
> Unsubscribe: http://mail.python.org/mailman/options/python-dev/andrew.svetlov%40gmail.com

Andrew Svetlov

More information about the Python-Dev mailing list