| 34 | |
| 35 | |
| 36 | def cast_to_value(from_value, to_value): |
| 37 | try: |
| 38 | target_type = type(to_value).__name__ |
| 39 | if target_type == "int": |
| 40 | # Casting str -> float means that notation like '1e3' is preserved |
| 41 | # We do not cast to int because 42.0 == 42 and 42.3 != 42 |
| 42 | return float(from_value) |
| 43 | elif target_type == "bool": |
| 44 | if from_value in ("true", "1"): |
| 45 | return True |
| 46 | elif from_value in ("false", "0"): |
| 47 | return False |
| 48 | return builtins.__dict__[target_type](from_value) |
| 49 | except ValueError: |
| 50 | pass |
| 51 | |
| 52 | |
| 53 | # See bug 4716. |