(the_type, fields, show_unset=False, use_unicode=False)
| 85 | |
| 86 | |
| 87 | def decode(the_type, fields, show_unset=False, use_unicode=False): |
| 88 | try: |
| 89 | s = next(fields) |
| 90 | except StopIteration: |
| 91 | raise BadMessage("no more fields") |
| 92 | |
| 93 | logger.debug("decode %s %s", the_type, s) |
| 94 | |
| 95 | if the_type is Decimal: |
| 96 | if ( |
| 97 | s is None |
| 98 | or len(s) == 0 |
| 99 | or s.decode() == "2147483647" |
| 100 | or s.decode() == "9223372036854775807" |
| 101 | or s.decode() == "1.7976931348623157E308" |
| 102 | or s.decode() == "-9223372036854775808" |
| 103 | ): |
| 104 | return UNSET_DECIMAL |
| 105 | return the_type(s.decode()) |
| 106 | |
| 107 | if the_type is str: |
| 108 | if type(s) is str: |
| 109 | return s |
| 110 | if type(s) is bytes: |
| 111 | return s.decode( |
| 112 | "unicode-escape" if use_unicode else "UTF-8", errors="backslashreplace" |
| 113 | ) |
| 114 | else: |
| 115 | raise TypeError( |
| 116 | "unsupported incoming type " + type(s) + " for desired type 'str" |
| 117 | ) |
| 118 | |
| 119 | orig_type = the_type |
| 120 | if the_type is bool: |
| 121 | the_type = int |
| 122 | |
| 123 | if the_type is float: |
| 124 | if s.decode() == INFINITY_STR: |
| 125 | return DOUBLE_INFINITY |
| 126 | |
| 127 | if show_unset: |
| 128 | if s is None or len(s) == 0: |
| 129 | if the_type is float: |
| 130 | n = UNSET_DOUBLE |
| 131 | elif the_type is int: |
| 132 | n = UNSET_INTEGER |
| 133 | else: |
| 134 | raise TypeError("unsupported desired type for empty value" + the_type) |
| 135 | else: |
| 136 | n = the_type(s) |
| 137 | else: |
| 138 | n = the_type(s or 0) |
| 139 | |
| 140 | if orig_type is bool: |
| 141 | n = n != 0 |
| 142 | |
| 143 | return n |
| 144 |
no test coverage detected