Fix isinstance to support Generic type object
#2952
Conversation
isinstance to support Generic type object
|
I found |
|
@youknowone I think failed tests in newly added |
|
@Snowapril yeah, you've done the right thing to mark them as |
|
totally positive. you don't need to make everything working to add a test file. |
This commit fix issue RustPython#2936. In original codes, there are problem in both `builtins::isinstance` and `vm.isinstance`. `builtins::isinstance` use `single_or_tuple_any` function for checking given object is `<class 'type'>` or if it is tuple, iterating over them recursively. But it did not check given object have `__instancecheck__` if it is not `<class 'type'>`. `vm.isinstance` get second argument as `PyTypeRef`. Because of it, `call_special_method` use `instancecheck` of `TypeProtocol`'s one and never reach to user defined `__instancecheck__` Therefore I referenced cpython implementation and fix it. Signed-off-by: snowapril <sinjihng@gmail.com>
Signed-off-by: snowapril <sinjihng@gmail.com>
Signed-off-by: snowapril <sinjihng@gmail.com>
Signed-off-by: snowapril <sinjihng@gmail.com>
| }, | ||
| vm, | ||
| ) | ||
| vm.isinstance(&obj, &typ) |
mm, i don't remember very well, but maybe this function was written like this because vm.isinstance was used when we don't want tuple comparision but builtins::isinstance is used when python isinstance is required.
@coolreader18 do you have any idea about this?
Ok, I found vm.isinstance practically doesn't have any difference, and even not used that much.
|
The approach looks good. But let me check about the |
| }, | ||
| vm, | ||
| ) | ||
| vm.isinstance(&obj, &typ) |
Ok, I found vm.isinstance practically doesn't have any difference, and even not used that much.
This revision fix issue #2936.
In original codes, there are problem in both
builtins::isinstanceandvm.isinstance.builtins::isinstanceusesingle_or_tuple_anyfunction for checkinggiven object is
<class 'type'>or if it is tuple, iterating over themrecursively.
But it did not check given object have
__instancecheck__if it isn't<class 'type'>.vm.isinstanceget second argument asPyTypeRef.Because of it,
call_special_methoduseinstancecheckofTypeProtocol's one andnever reach to user defined
__instancecheck__Therefore I fixed it with referenced cpython implementation.
And also add extra test about it.
Before
After
issue #2943 is almost same with this PR, but I want to know If I'm going right way. Thanks.
The text was updated successfully, but these errors were encountered: