@@ -46,6 +46,7 @@ def f(x: int) -> int:
46
46
from mypy .visitor import ExpressionVisitor , StatementVisitor
47
47
from mypy .checkexpr import map_actuals_to_formals
48
48
from mypy .state import strict_optional_set
49
+ from mypy .util import split_target
49
50
50
51
from mypyc .common import (
51
52
ENV_ATTR_NAME , NEXT_LABEL_ATTR_NAME , TEMP_ATTR_NAME , LAMBDA_NAME ,
@@ -64,7 +65,8 @@ def f(x: int) -> int:
64
65
exc_rtuple ,
65
66
PrimitiveOp , ControlOp , OpDescription , RegisterOp ,
66
67
is_object_rprimitive , LiteralsMap , FuncSignature , VTableAttr , VTableMethod , VTableEntries ,
67
- NAMESPACE_TYPE , RaiseStandardError , LoadErrorValue , NO_TRACEBACK_LINE_NO , FuncDecl ,
68
+ NAMESPACE_TYPE , NAMESPACE_MODULE ,
69
+ RaiseStandardError , LoadErrorValue , NO_TRACEBACK_LINE_NO , FuncDecl ,
68
70
FUNC_NORMAL , FUNC_STATICMETHOD , FUNC_CLASSMETHOD ,
69
71
RUnion , is_optional_type , optional_value_type , all_concrete_classes
70
72
)
@@ -1365,7 +1367,7 @@ def cache_class_attrs(self, attrs_to_cache: List[Lvalue], cdef: ClassDef) -> Non
1365
1367
for lval in attrs_to_cache :
1366
1368
assert isinstance (lval , NameExpr )
1367
1369
rval = self .py_get_attr (typ , lval .name , cdef .line )
1368
- self .init_final_static (lval , rval , cdef .fullname )
1370
+ self .init_final_static (lval , rval , cdef .name )
1369
1371
1370
1372
def visit_class_def (self , cdef : ClassDef ) -> None :
1371
1373
ir = self .mapper .type_to_ir [cdef .info ]
@@ -1429,7 +1431,7 @@ def visit_class_def(self, cdef: ClassDef) -> None:
1429
1431
self .primitive_op (
1430
1432
py_setattr_op , [typ , self .load_static_unicode (lvalue .name ), value ], stmt .line )
1431
1433
if self .non_function_scope () and stmt .is_final_def :
1432
- self .init_final_static (lvalue , value , cdef .fullname )
1434
+ self .init_final_static (lvalue , value , cdef .name )
1433
1435
elif isinstance (stmt , ExpressionStmt ) and isinstance (stmt .expr , StrExpr ):
1434
1436
# Docstring. Ignore
1435
1437
pass
@@ -1503,13 +1505,13 @@ def gen_import(self, id: str, line: int) -> None:
1503
1505
self .imports [id ] = None
1504
1506
1505
1507
needs_import , out = BasicBlock (), BasicBlock ()
1506
- first_load = self .add ( LoadStatic ( object_rprimitive , 'module' , id ) )
1508
+ first_load = self .load_module ( id )
1507
1509
comparison = self .binary_op (first_load , self .none_object (), 'is not' , line )
1508
1510
self .add_bool_branch (comparison , out , needs_import )
1509
1511
1510
1512
self .activate_block (needs_import )
1511
1513
value = self .primitive_op (import_op , [self .load_static_unicode (id )], line )
1512
- self .add (InitStatic (value , 'module' , id ))
1514
+ self .add (InitStatic (value , id , namespace = NAMESPACE_MODULE ))
1513
1515
self .goto_and_activate (out )
1514
1516
1515
1517
def visit_import (self , node : Import ) -> None :
@@ -1555,7 +1557,7 @@ def visit_import_from(self, node: ImportFrom) -> None:
1555
1557
id = importlib .util .resolve_name ('.' * node .relative + node .id , module_package )
1556
1558
1557
1559
self .gen_import (id , node .line )
1558
- module = self .add ( LoadStatic ( object_rprimitive , 'module' , id ) )
1560
+ module = self .load_module ( id )
1559
1561
1560
1562
# Copy everything into our module's dict.
1561
1563
# Note that we miscompile import from inside of functions here,
@@ -1708,7 +1710,7 @@ def calculate_arg_defaults(self,
1708
1710
env .lookup (arg .variable ).type , arg .line )
1709
1711
if not fn_info .is_nested :
1710
1712
name = fitem .fullname () + '.' + arg .variable .name ()
1711
- self .add (InitStatic (value , name , 'final' ))
1713
+ self .add (InitStatic (value , name , self . module_name ))
1712
1714
else :
1713
1715
assert func_reg is not None
1714
1716
self .add (SetAttr (func_reg , arg .variable .name (), value , arg .line ))
@@ -1736,7 +1738,7 @@ def get_default() -> Value:
1736
1738
elif not self .fn_info .is_nested :
1737
1739
name = fitem .fullname () + '.' + arg .variable .name ()
1738
1740
self .final_names .append ((name , target .type ))
1739
- return self .add (LoadStatic (target .type , name , 'final' ))
1741
+ return self .add (LoadStatic (target .type , name , self . module_name ))
1740
1742
else :
1741
1743
name = arg .variable .name ()
1742
1744
self .fn_info .callable_class .ir .attributes [name ] = target .type
@@ -2020,19 +2022,21 @@ def init_final_static(self, lvalue: Lvalue, rvalue_reg: Value,
2020
2022
assert isinstance (lvalue .node , Var )
2021
2023
if lvalue .node .final_value is None :
2022
2024
if class_name is None :
2023
- name = lvalue .fullname
2025
+ name = lvalue .name
2024
2026
else :
2025
2027
name = '{}.{}' .format (class_name , lvalue .name )
2026
2028
assert name is not None , "Full name not set for variable"
2027
2029
self .final_names .append ((name , rvalue_reg .type ))
2028
- self .add (InitStatic (rvalue_reg , name , 'final' ))
2030
+ self .add (InitStatic (rvalue_reg , name , self . module_name ))
2029
2031
2030
2032
def load_final_static (self , fullname : str , typ : RType , line : int ,
2031
2033
error_name : Optional [str ] = None ) -> Value :
2032
2034
if error_name is None :
2033
2035
error_name = fullname
2034
2036
ok_block , error_block = BasicBlock (), BasicBlock ()
2035
- value = self .add (LoadStatic (typ , fullname , 'final' , line = line ))
2037
+ split_name = split_target (self .graph , fullname )
2038
+ assert split_name is not None
2039
+ value = self .add (LoadStatic (typ , split_name [1 ], split_name [0 ], line = line ))
2036
2040
self .add (Branch (value , error_block , ok_block , Branch .IS_ERROR , rare = True ))
2037
2041
self .activate_block (error_block )
2038
2042
self .add (RaiseStandardError (RaiseStandardError .VALUE_ERROR ,
@@ -5215,7 +5219,7 @@ def load_static_unicode(self, value: str) -> Value:
5215
5219
return self .add (LoadStatic (str_rprimitive , static_symbol , ann = value ))
5216
5220
5217
5221
def load_module (self , name : str ) -> Value :
5218
- return self .add (LoadStatic (object_rprimitive , 'module' , name ))
5222
+ return self .add (LoadStatic (object_rprimitive , name , namespace = NAMESPACE_MODULE ))
5219
5223
5220
5224
def load_module_attr_by_fullname (self , fullname : str , line : int ) -> Value :
5221
5225
module , _ , name = fullname .rpartition ('.' )
0 commit comments