KEMBAR78
[mypyc] Add a primitive for loading bool type by 97littleleaf11 · Pull Request #10257 · python/mypy · GitHub
Skip to content

Conversation

@97littleleaf11
Copy link
Collaborator

Description

Closes mypyc/mypyc#821

Test Plan

For this piece of python code:

def f(x: object) -> bool:
    return isinstance(x, bool)

Previous version would generate:

def f(x):                                      
    x, r0 :: object                            
    r1 :: str                                  
    r2 :: object                               
    r3 :: int32                                
    r4 :: bit                                  
    r5 :: bool                                 
L0:                                            
    r0 = builtins :: module                    
    r1 = 'bool'                                
    r2 = CPyObject_GetAttr(r0, r1)             
    r3 = PyObject_IsInstance(x, r2)            
    r4 = r3 >= 0 :: signed                     
    r5 = truncate r3: int32 to builtins.bool   
    return r5   

And now it would only generate this:

def f(x):                                      
    x, r0 :: object                            
    r1 :: int32                                
    r2 :: bit                                  
    r3 :: bool                                 
L0:                                            
    r0 = load_address PyBool_Type              
    r1 = PyObject_IsInstance(x, r0)            
    r2 = r1 >= 0 :: signed                     
    r3 = truncate r1: int32 to builtins.bool   
    return r3    

Copy link
Collaborator

@JukkaL JukkaL left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, looks good!

@JukkaL JukkaL merged commit 341bf19 into python:master Mar 29, 2021
@97littleleaf11 97littleleaf11 deleted the loading-bool-type branch May 6, 2021 08:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add primitive for loading bool type object

2 participants