|
21 | 21 | from typed_python.compiler.type_wrappers.typed_list_masquerading_as_list_wrapper import TypedListMasqueradingAsList |
22 | 22 | import typed_python.compiler.type_wrappers.runtime_functions as runtime_functions |
23 | 23 | from typed_python.compiler.type_wrappers.bound_method_wrapper import BoundMethodWrapper |
| 24 | +from typed_python.compiler.conversion_level import ConversionLevel |
24 | 25 |
|
25 | 26 | import typed_python.compiler.native_ast as native_ast |
26 | 27 | import typed_python.compiler |
@@ -1010,7 +1011,31 @@ def _can_convert_to_type(self, targetType, conversionLevel): |
1010 | 1011 | Float32, Int8, Int16, Int32, UInt8, UInt16, UInt32, UInt64, float, int, bool, str |
1011 | 1012 | ) |
1012 | 1013 |
|
| 1014 | + def convert_to_self_with_target(self, context, targetVal, sourceVal, level: ConversionLevel, mayThrowOnFailure=False): |
| 1015 | + if sourceVal.expr_type.typeRepresentation is str: |
| 1016 | + targetVal.convert_copy_initialize(targetVal) |
| 1017 | + return context.constant(True) |
| 1018 | + |
| 1019 | + if level.isNewOrHigher(): |
| 1020 | + if not sourceVal.isReference: |
| 1021 | + sourceVal = context.pushMove(sourceVal) |
| 1022 | + |
| 1023 | + return context.pushPod( |
| 1024 | + bool, |
| 1025 | + runtime_functions.np_try_pyobj_to_str.call( |
| 1026 | + sourceVal.expr.cast(VoidPtr), |
| 1027 | + targetVal.expr.cast(VoidPtr), |
| 1028 | + context.getTypePointer(sourceVal.expr_type.typeRepresentation) |
| 1029 | + ) |
| 1030 | + ) |
| 1031 | + |
| 1032 | + return super().convert_to_self_with_target(context, targetVal, sourceVal, level, mayThrowOnFailure) |
| 1033 | + |
1013 | 1034 | def convert_to_type_with_target(self, context, instance, targetVal, conversionLevel, mayThrowOnFailure=False): |
| 1035 | + if targetVal.expr_type.typeRepresentation is str: |
| 1036 | + targetVal.convert_copy_initialize(instance) |
| 1037 | + return context.constant(True) |
| 1038 | + |
1014 | 1039 | if not conversionLevel.isNewOrHigher(): |
1015 | 1040 | return super().convert_to_type_with_target(context, instance, targetVal, conversionLevel, mayThrowOnFailure) |
1016 | 1041 |
|
|
0 commit comments