(s)
| 251 | encoding_fallback_warning_issued = False |
| 252 | encoding_escape_warning_issued = False |
| 253 | def metadata_stream_to_writable_bytes(s): |
| 254 | encodingStrategy = gitConfig('git-p4.metadataDecodingStrategy') or defaultMetadataDecodingStrategy |
| 255 | fallbackEncoding = gitConfig('git-p4.metadataFallbackEncoding') or defaultFallbackMetadataEncoding |
| 256 | if not isinstance(s, bytes): |
| 257 | return s.encode('utf_8') |
| 258 | if encodingStrategy == 'passthrough': |
| 259 | return s |
| 260 | try: |
| 261 | s.decode('utf_8') |
| 262 | return s |
| 263 | except UnicodeDecodeError: |
| 264 | if encodingStrategy == 'fallback' and fallbackEncoding: |
| 265 | global encoding_fallback_warning_issued |
| 266 | global encoding_escape_warning_issued |
| 267 | try: |
| 268 | if not encoding_fallback_warning_issued: |
| 269 | print("\nCould not decode value as utf-8; using configured fallback encoding %s: %s" % (fallbackEncoding, s)) |
| 270 | print("\n(this warning is only displayed once during an import)") |
| 271 | encoding_fallback_warning_issued = True |
| 272 | return s.decode(fallbackEncoding).encode('utf_8') |
| 273 | except Exception as exc: |
| 274 | if not encoding_escape_warning_issued: |
| 275 | print("\nCould not decode value with configured fallback encoding %s; escaping bytes over 127: %s" % (fallbackEncoding, s)) |
| 276 | print("\n(this warning is only displayed once during an import)") |
| 277 | encoding_escape_warning_issued = True |
| 278 | escaped_bytes = b'' |
| 279 | # bytes and strings work very differently in python2 vs python3... |
| 280 | if str is bytes: |
| 281 | for byte in s: |
| 282 | byte_number = struct.unpack('>B', byte)[0] |
| 283 | if byte_number > 127: |
| 284 | escaped_bytes += b'%' |
| 285 | escaped_bytes += hex(byte_number)[2:].upper() |
| 286 | else: |
| 287 | escaped_bytes += byte |
| 288 | else: |
| 289 | for byte_number in s: |
| 290 | if byte_number > 127: |
| 291 | escaped_bytes += b'%' |
| 292 | escaped_bytes += hex(byte_number).upper().encode()[2:] |
| 293 | else: |
| 294 | escaped_bytes += bytes([byte_number]) |
| 295 | return escaped_bytes |
| 296 | |
| 297 | raise MetadataDecodingException(s) |
| 298 | |
| 299 | |
| 300 | def decode_path(path): |
no test coverage detected