Repository navigation
fix: open metadata and benchmark task files as UTF-8 - #1252
Conversation
open() without an encoding uses the locale's encoding, which on Windows
is the ANSI code page (cp1252, cp932, gbk, ...). The CLI read
dataset-metadata.json and the other metadata files that way, so UTF-8
text was uploaded garbled ("×" became "×") or the command failed with
UnicodeDecodeError. kernels push already reads kernel-metadata.json and
the code file as UTF-8. Do the same for the dataset, model, model
instance and competition metadata files, kernels pull, the benchmarks
task file, and the files these commands write.
| owner_slug, dataset_slug, effective_path = self.dataset_metadata_prep(dataset, path) | ||
| meta_file = self.get_dataset_metadata_file(effective_path) | ||
| with open(meta_file, "r") as f: | ||
| with open(meta_file, "r", encoding="utf-8") as f: |
There was a problem hiding this comment.
Small Nit: files saved as UTF-8 with a BOM (older Notepad, or PowerShell 5 Set-Content -Encoding UTF8) still fail here with "Unexpected UTF-8 BOM". They failed before too, so it's not a regression, but utf-8-sig would read files both with and without a BOM. The same change would apply to the other metadata reads, while the writes can stay utf-8. Totally fine to skip if you'd rather keep it consistent with kernels push.
CC: @stevemessick
| with open(meta_file, "r", encoding="utf-8") as f: | |
| with open(meta_file, "r", encoding="utf-8-sig") as f: |
|
|
||
| # read json | ||
| with open(meta_file) as f: | ||
| with open(meta_file, encoding="utf-8") as f: |
There was a problem hiding this comment.
Another small one: a metadata file saved in cp1252 with non-ASCII text now ends in a raw UnicodeDecodeError traceback, which doesn't really tell the user what to do. Maybe catch it and raise something like ValueError(f"{meta_file} is not UTF-8 encoded, please re-save it as UTF-8")? Not a blocker, could also be a follow-up.
Metadata files are now read as utf-8-sig, so a file saved with a BOM (Notepad, or PowerShell 5's Set-Content -Encoding UTF8) no longer fails with "Unexpected UTF-8 BOM". kernels push reads kernel-metadata.json the same way. A metadata file saved in another encoding now raises "<file> is not UTF-8 encoded, please re-save it as UTF-8" instead of the codec error. Writes stay plain UTF-8. Co-authored-by: Sridip Basu <122687772+sridipbasu@users.noreply.github.com>
|
Thanks @sridipbasu, both are done in fbde4d9:
Each metadata test now runs with and without a BOM, and a new test covers the cp1252 error. The new cases fail on the previous commit. I also tried a file written by PowerShell 5's |
|
/gcbrun |
|
Thanks @tb91117 — great catch and very clean fix, especially the Two small suggestions if you'd like to fold them in before we merge:
Approving as-is and holding off on merging so you have a chance to push an update if you'd like! |
kaggle benchmarks tasks push now reads the task file the same way as the metadata files: a UTF-8 BOM is accepted (it used to reach ast.parse as U+FEFF and end in a misleading "No @task decorators found"), and a file in another encoding raises "<file> is not UTF-8 encoded, please re-save it as UTF-8" instead of a raw UnicodeDecodeError. The task-file test now runs with and without a BOM, and a cp1252 task file has its own test.
|
Thanks @stevemessick, both are in 982343a:
|
|
/gcbrun |
I hit this when a
×in one of my dataset descriptions showed up as×on the dataset page afterkaggle datasets metadata --updateon Windows.open()without an encoding uses the locale's encoding. On Windows that is the ANSI code page: cp1252 on most Western machines, cp932 on Japanese ones, gbk on Chinese ones. The CLI opensdataset-metadata.jsonand the other metadata files that way, so on Windows:datasets metadata --update,datasets createanddatasets versionupload UTF-8 text garbled, with no error.144,606 trees × 28 columnsbecomes144,606 trees × 28 columns.UnicodeDecodeError: 'charmap' codec can't decode byte 0x9d.The same happens with model, model instance and competition metadata, and with the task file in
benchmarks tasks push.kernels pushno longer has this problem (it was #186): it already openskernel-metadata.jsonand the code file withencoding="utf-8".Change
Sets the encoding on 22
open()calls inkaggle_api_extended.py, and changes it on one more (kernels push):utf-8-sig, which reads UTF-8 with or without a BOM, so files saved by Notepad or PowerShell 5'sSet-Content -Encoding UTF8work too: the metadata file incompetitions create,datasets metadata --update,datasets create,datasets version,models create,models update,models instances create,models instances update,kernels pull(2 places) andkernels push.benchmarks tasks pushis read asutf-8.utf-8: the metadata files written by theinitcommands,datasets metadata,kernels pull -m,models getandmodels instances get, and the example and reference files written bybenchmarks init.json.dumpescapes non-ASCII, so the metadata files it writes don't change. The reference file has an em dash, which was written in cp1252, and on Japanese Windows (cp932) it can't be written at all.The only
open()calls left without an encoding are for the config file, the download resume marker and downloaded files.One behavior change: a metadata file saved in a legacy code page with non-ASCII characters is no longer read in that code page. It now fails with
<file> is not UTF-8 encoded, please re-save it as UTF-8, since JSON exchanged between systems must be UTF-8 (RFC 8259).Tests
tests/unit/test_metadata_encoding.pywith 44 tests. An autouse fixture makesopen()inkaggle_api_extendeddefault to cp1252, as on a Western Windows machine, so the tests reproduce the bug on any OS. The readers are tested with text that cp1252 garbles (×) and text it can't decode (Japanese), each metadata file is written with and without a BOM, a cp1252 file is checked to get the new error, and the reference file is checked to be UTF-8.main, 42 of the 44 fail: the request gets the garbled text, the command raisesUnicodeDecodeErroror "Unexpected UTF-8 BOM", or the reference file isn't UTF-8. The 2 that pass arekernels pushwithout a BOM, which Encoding issues when pushing a kernel #186 already fixed.test_kernels_pull.pynow expect the new encodings in theopen()calls.pytest tests/unit: 1515 passed. The one failure,test_symlinked_state_dir_is_rejected, also fails onmainon my Windows machine and is unrelated.black --checkandmypy src/kaggle testspass.