Skip to content

Implement codecs.ignore_errors#711

Merged
slozier merged 1 commit intoIronLanguages:masterfrom
BCSharp:ignore_errors
Dec 18, 2019
Merged

Implement codecs.ignore_errors#711
slozier merged 1 commit intoIronLanguages:masterfrom
BCSharp:ignore_errors

Conversation

@BCSharp
Copy link
Copy Markdown
Member

@BCSharp BCSharp commented Dec 18, 2019

It works, but I feel uneasy about using a type which name starts with an underscore, although the type itself is public. Is it the right way of doing? If not, here is an alternative implementation of ignore_errors, which I can push in the next commit:

private static object IgnoreErrors(CodeContext context, object unicodeError) {
    switch (unicodeError) {
        case PythonExceptions.BaseException _:
            var ptype = DynamicHelpers.GetPythonType(unicodeError);
            if (ptype.Name == "UnicodeDecodeError" || ptype.Name == "UnicodeEncodeError") {
                if (ptype.TryGetBoundAttr(context, unicodeError, "end", out object ret) && ret is int end) {
                    return PythonTuple.MakeTuple(string.Empty, end);
                }
            }
            goto default;
        case DecoderFallbackException dfe: 
            return PythonTuple.MakeTuple(string.Empty, dfe.Index + dfe.BytesUnknown.Length);
        case EncoderFallbackException efe: 
            return PythonTuple.MakeTuple(string.Empty, efe.Index + (efe.CharUnknownHigh != '\0' ? 2 : 1));
        default: throw PythonOps.TypeError("codec must pass exception instance");
    }
}

@BCSharp BCSharp marked this pull request as ready for review December 18, 2019 21:22
Copy link
Copy Markdown
Contributor

@slozier slozier left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Underscore types seem to mostly be used within the exception classes, however I don't have any better suggestions so this works for me. Have you looked at the tests for these in test_codecscallback?

@slozier slozier merged commit 3df9890 into IronLanguages:master Dec 18, 2019
@BCSharp
Copy link
Copy Markdown
Member Author

BCSharp commented Dec 19, 2019

I haven't noticed test_codeccallbacks.py until now. This is great. The tests I am writing are just enough to guide the implementation; they are by no means exhaustive. test_codeccallbacks.py is invaluable. Right now, almost all tests from that file fail, but looking at the failures I see they are expected. So there is still some way to go.

@BCSharp BCSharp deleted the ignore_errors branch December 19, 2019 04:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants