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

2012-12-26 Thread Andrew Svetlov
Thanks for the elaboration!

On Wed, Dec 26, 2012 at 6:42 PM, Antoine Pitrou  wrote:
> 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
___
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/archive%40mail-archive.com


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

2012-12-26 Thread 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/archive%40mail-archive.com


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

2012-12-26 Thread Serhiy Storchaka
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')


___
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/archive%40mail-archive.com


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

2012-12-26 Thread Andrew Svetlov
On Wed, Dec 26, 2012 at 12:16 PM, Nick Coghlan  wrote:
> 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 "", line 1, in 
>> 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   |   ncogh...@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
___
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/archive%40mail-archive.com


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

2012-12-26 Thread 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 "", line 1, in 
> 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   |   ncogh...@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/archive%40mail-archive.com


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

2012-12-26 Thread 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 "", line 1, in 
FileNotFoundError: [Errno 2] No such file or directory: 'qwerty'


___
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/archive%40mail-archive.com


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

2012-12-25 Thread Andrew Svetlov
static method is better than new builtin function, agree.

On Wed, Dec 26, 2012 at 12:03 AM, Benjamin Peterson  wrote:
> 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
___
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/archive%40mail-archive.com


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

2012-12-25 Thread 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
___
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/archive%40mail-archive.com


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

2012-12-25 Thread 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

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
___
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/archive%40mail-archive.com