"except" and "subclasscheck" changed between CPython2 and 3

2018-03-06 Thread Youta TAKAOKA
Thank you, Steven.

> There is a feature-request to support that (as Python 2.7 does):
>
> https://bugs.python.org/issue12029
>
> but it is stalled.

I passed over the ticket.

Now, I know that this is a bug, but has not fixed yet.
There are (or ware ?) problems about performance and integrity for handling
exceptions.

It seems that a narrow range of use-cases cause the stalling.
Someday we have a type-focused VERY NICE library, (I hope that) it will be
resolved.

Thanks.

--
yout...@gmail.com
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: "except" and "subclasscheck" changed between CPython2 and 3

2018-03-05 Thread Steven D'Aprano
On Sat, 03 Mar 2018 04:28:24 +, 高岡陽太 wrote:

> Hello,
> 
> I found a difference of behavior about `except` statement between
> CPython 2.7 and 3.x .
> `except EXC_CLASS:` calls `__subclasscheck__` in 2.7, but does not in
> 3.x .


Python 3 does not accept virtual subclasses for exception handling. They 
have to be concrete subclasses of BaseException.

There is a feature-request to support that (as Python 2.7 does):

https://bugs.python.org/issue12029

but it is stalled.



-- 
Steve

-- 
https://mail.python.org/mailman/listinfo/python-list


"except" and "subclasscheck" changed between CPython2 and 3

2018-03-03 Thread 高岡陽太
Hello,

I found a difference of behavior about `except` statement between CPython
2.7 and 3.x .
`except EXC_CLASS:` calls `__subclasscheck__` in 2.7, but does not in 3.x .

Let me show you an example.
Now, define a class "ExceptionLike" (with metaclass "ExceptionLikeMeta")
below.


class ExceptionLikeMeta(type):
def __subclasscheck__(cls, other):
return other.__name__.endswith('Exception')

ExceptionLike = ExceptionLikeMeta('ExceptionLike', (Exception,), {})

try:
raise Exception()
except ExceptionLike:
print('catch it!')
except:
assert False, 'not caught!'


The exception is caught with `ExceptionLike` in 2.7, but not in 3.x .
(I tested it with CPython 3.6.2 and 2.7.13 on mac)


The difference seems to be introduced with this commit [
https://github.com/python/cpython/commit/ec569b794737be248671d0dfac11b664fc930eef#diff-73da65b698644ee286bc60b703916bbbL163]
and this ticket [https://bugs.python.org/issue2534].

The behavior of CPython 3 may have some advantages.

- `except EXC_TYPE:` does never raise an exception caused by
`__subclasscheck__`.
- (may be) fast, because it just scan mro.

So, the diff can be intended one. But the ticket itself seems not.

Should not I expect that `__subclasscheck__` is used for subclass checking
anywhere? It effects only `issubclass` anymore?
(or, should I go to other list?)

---
yout...@gmail.com
-- 
https://mail.python.org/mailman/listinfo/python-list