Skip to content

Load encoding files - #1882

Merged
mmatera merged 7 commits into
masterfrom
load_encoding_files
Jul 30, 2026
Merged

Load encoding files#1882
mmatera merged 7 commits into
masterfrom
load_encoding_files

Conversation

@mmatera

@mmatera mmatera commented Jul 30, 2026

Copy link
Copy Markdown
Contributor

This PR start allowing to load character encodings from the WL encoding files. Also, generate Python encodings that can be used to read/write file operations.

@rocky

rocky commented Jul 30, 2026

Copy link
Copy Markdown
Member

LGTM. I'm kind of glad to see this happening.

Algunos comentarios están en español, pero probablemente sea bueno para practicar.

Comment thread mathics/eval/encoding.py Outdated
Comment thread mathics/eval/encoding.py
# see https://docs.python.org/2/library/codecs.html#standard-encodings
"ASCII": "ascii",
"CP949": "cp949",
"CP950": "cp950",

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Home-grown table removed - Yay!.

Comment thread mathics/builtin/atomic/strings.py Outdated
Comment on lines +291 to +295
try:
load_encoding_table(value.value, evaluation)
evaluation.definitions.set_ownvalue("System`$CharacterEncoding", value)
except EncodingNameError:
pass

@rocky rocky Jul 30, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It would be nice to start moving code like this into mathics.eval.atomic.strings

(Whether the parameter checking gets moved there is kind of squishy; I don't really know. It would depend on how it might be used other places.)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I guess it is fine to do parameter checks inside the eval_ methods, when it helps to decide if the parameters are acceptable for some of the implemented eval_ functions. An eval_ function should always receive suitable parameters. For this particular case, I do not know either: This just sets the encoding, so adding an extra call may be excessive.

@rocky rocky Jul 30, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

An eval_ function should always receive suitable parameters.

What I've come to see from use is that in some cases it can be rather involved to check validity. This parameter is only valid when the data is of a certain form, which is easier to check at the Python level, as opposed to at the S-Expression level.

Checking for compatibility of matrices in matrix multiplication would be an example of this.

For this particular case, I do not know either: This just sets the encoding, so adding an extra call may be excessive.

You have to consider also whether somewhere else in the code, it will be desirable to do the equivalent of the WL construct as part of its evaluation.

Here, that would be:

$CharacterEncoding := ...

While this is probably unlikely, I can imagine it might happen that some other built-in, as a side effect sets the $CharacterEncoding.

But rather than spend mental effort on stuff like this, I think it is better just to follow this pattern and not have to worry about such stuff. It also makes it easier for someone else to follow the code and pattern and do things that way.

# "noopen" matches WMA. This is nonsensical.
evaluation.message("Get", "noopen", encoding)
py_encoding = py_current_encoding
try:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Notice that this code is pretty similar to the code mentioned above that was suggested to be moved into mathics.eval. So maybe there should be a function for the try block.

Not sure how to handle the small differences, though.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

What I tried to do here was avoid the extra call for "native" encodings. But it probably should be written differently, using an exception.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I am trying to rewrite this code in order to use the stdlib custom encodings that now produce wl_charmap_codec.py. It is included in the following PR.

Comment thread mathics/eval/strings.py Outdated
return String(boxes.to_text(evaluation=evaluation, encoding="Unicode"))

if encoding not in ("utf8", "utf-8", "unicode"):
try:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Another try block wrapping load_encoding_table; this should definitely be isolated in a function in eval.

el interprete real para Klingon.wl / ISO8859-1.m / ISO8859-8.m.

Correr con: python3 test_wl_charmap_codec.py
Tests for wl_charmap_codec.py as pure toolkit: does not read .wl files

@rocky rocky Jul 30, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I looked at all of the translations in commit aaf000c. They are pretty good!

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks :)

@mmatera

mmatera commented Jul 30, 2026

Copy link
Copy Markdown
Contributor Author

LGTM. I'm kind of glad to see this happening.

Algunos comentarios están en español, pero probablemente sea bueno para practicar.

Ja, sorry. I just forgot to translate them to English, or just remove them. I was doing experiments with Claude IA to build a part of this code, and as my interactions were in Spanish, it left comments in Spanish. Also it seems it left some "ideosincratic" lines, like add "futures" import :)

Removed unused future import for annotations.
@rocky

rocky commented Jul 30, 2026

Copy link
Copy Markdown
Member

LGTM. I'm kind of glad to see this happening.
Algunos comentarios están en español, pero probablemente sea bueno para practicar.

Ja, sorry. I just forgot to translate them to English, or just remove them.

It's better to translate the comments or leave them in than remove them. Especially if AI is assisting, because it can make mistakes and the comments help verify whether the intent was correct or incorrect.

@mmatera

mmatera commented Jul 30, 2026

Copy link
Copy Markdown
Contributor Author

.

Sure! But some of the comments were related to the prompt I used to generate the code. In any case, I supervised most of the process: I just wanted to see how Claude produces code compatible with stdlib codec functions.

Just a small spelling typo noticed while going over encoding.

Co-authored-by: Juan Mauricio Matera <matera@fisica.unlp.edu.ar>
Comment thread mathics/eval/strings.py
pass

return String(boxes.to_text(evaluation=evaluation, encoding="Unicode"))
# If the encoding does not already exists, try to load it and do it again.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I think this way makes more sense...

@mmatera
mmatera merged commit 4556f89 into master Jul 30, 2026
18 checks passed
@mmatera
mmatera deleted the load_encoding_files branch July 30, 2026 14:01
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