Skip to content

Encoding invalid characters #2

@adrianschlatter

Description

@adrianschlatter

Trying to encode a character which is not supported by a codec will raise an exception (=> good). However, the error message is a bit misleading in some cases.

Example:

'ä'.encode('datamatrix.EDIFACT')

UnicodeEncodeError: 'ascii' codec can't encode character '\xe4' in position 0: ordinal not in range(128)
---------------------------------------------------------------------------
UnicodeEncodeError                        Traceback (most recent call last)
/InteractiveInput-1 in <module>
----> 1 'ä'.encode('datamatrix.EDIFACT')

~/Projects/ppf.datamatrix/src/ppf/datamatrix/codec_edifact.py in encode_to_edifact(msg)
     46     elif n_rest == 1:
     47         if len(msg) < 2:
---> 48             enc = (b'\xF0' + pack(msg.encode('ascii'), b'\x1F\x00\x00'))
     49         else:
     50             enc = (b'\xF0' +

UnicodeEncodeError: 'ascii' codec can't encode character '\xe4' in position 0: ordinal not in range(128)

Note that we are trying to encode in 'datamatrix.EDIFACT'. But the error message says there is a failure when encoding to 'ascii'. We should catch this exception and re-raise an exception that say 'ä' is an invalid datamatrix.EDIFACT character.

Similar things happen in other codecs.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions