Skip to content

Implement codecs.strict_errors#696

Merged
slozier merged 2 commits intoIronLanguages:masterfrom
BCSharp:strict_errors
Dec 10, 2019
Merged

Implement codecs.strict_errors#696
slozier merged 2 commits intoIronLanguages:masterfrom
BCSharp:strict_errors

Conversation

@BCSharp
Copy link
Copy Markdown
Member

@BCSharp BCSharp commented Dec 8, 2019

Current codecs module lacks a number of standalone error handling functions (or, to be exact, the exported symbols are not callable):

>>> import codecs
>>> [ f for f in dir(codecs) if f.endswith("errors")]
['backslashreplace_errors', 'ignore_errors', 'replace_errors', 'strict_errors', 'xmlcharrefreplace_errors']

This PR implements strict_errors as a pilot; if the approach is agreed upon, I will implement the remaining ones in a similar fashion.

I am not quite sure whether using a delegate to put into the error handlers dictionary is the right way; perhaps there is a better, more ironpythonic way of doing it. As it is now, there are some subtle differences between IronPython and CPython regarding the type:

CPython:

>>> codecs.strict_errors
<built-in function strict_errors>
>>> type(codecs.strict_errors)
<class 'builtin_function_or_method'>
>>> codecs.strict_errors()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: strict_errors() takes exactly one argument (0 given)

IronPython:

>>> codecs.strict_errors
<IronPython.Runtime.Operations.StringOps+CodecsInfo+ErrorHandler object at 0x000000000000008E [IronPython.Runtime.Operations.StringOps+CodecsInfo+ErrorHandler]>
>>> type(codecs.strict_errors)
<class 'ErrorHandler'>
>>> codecs.strict_errors()    
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: Invoke() takes exactly 1 argument (0 given)

Functionality-wise, they behave the same.

Comment thread Src/IronPython/Runtime/PythonContext.cs Outdated
Comment thread Src/IronPython/Runtime/Operations/StringOps.cs Outdated
private static Dictionary<string, object> MakeErrorHandlersDict() {
var d = new Dictionary<string, object>();

d["strict"] = new ErrorHandler(StrictErrors);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

You can wrap the method in BuiltinFunction instead of creating delegates. Something like the following:

d["strict"] = BuiltinFunction.MakeFunction("strict_errors", ReflectionUtils.GetMethodInfos(typeof(StringOps).GetMember(nameof(StrictErrors), BindingFlags.Static | BindingFlags.NonPublic)), typeof(StringOps));

Note that I'm super familiar with this part of the codebase (BuiltinFunction) so I may be misusing it (e.g. what is the proper declaringType).

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.

Thanks! Will merge once CI is done.

@slozier slozier merged commit 0911fb1 into IronLanguages:master Dec 10, 2019
@BCSharp BCSharp deleted the strict_errors branch December 10, 2019 02:38
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