[Bug]: Cannot save options.ini when a path contains non-ASCII characters (UnicodeEncodeError)
🙂 Looking for an issue? Welcome! This issue is open for contribution. If this is the first time you’re requesting an issue, please:
- Read Contributing guidelines carefully. Pay extra attention to Using generative AI. Pull requests and comments that don’t follow the guidelines won’t be answered.
- Confirm that you’ve read the guidelines in your comment.
Description
Describe the bug
Kolibri can read options.ini values that contain non-ASCII characters (for example é or ó), but it cannot save the file afterwards. Saving raises:
UnicodeEncodeError: 'ascii' codec can't encode character ...
This affects any save that rewrites options.ini, including Device > Settings and kolibri manage content movedirectory.
The cause is in kolibri/utils/options.py: ConfigObj(ini_path, configspec=get_configspec()) is created without an encoding. configobj then reads the file as UTF-8 but writes it as ASCII. Because Kolibri rewrites the whole file on every save, one non-ASCII value in the file breaks every later save, even for an unrelated setting like HTTP_PORT.
To Reproduce
- Start Kolibri with a fresh
KOLIBRI_HOME. - Try to save a storage location whose path contains a non-ASCII character, e.g.
/media/usb/Vidéos. This is the request Device > Settings sends. - Observe HTTP 500 /
UnicodeEncodeError; nothing is saved.
For movedirectory:
- Run
kolibri configure setup. - Put a file in
$KOLIBRI_HOME/content/databases. - Run
kolibri manage content movedirectory "/media/usb/Vidéos". - The command copies the content and empties the old folder, then crashes with
UnicodeEncodeError.CONTENT_DIRis not updated, so Kolibri keeps looking in the old, now empty folder.
Expected behavior
Saving should succeed and the value should read back unchanged. movedirectory should update CONTENT_DIR to the new folder.
Errors and logs
From my Docker run (Python 3.10.22, configobj 5.0.8):
UnicodeEncodeError: 'ascii' codec can't encode character '\xe9' in position 78: ordinal not in range(128)
movedirectory before the fix:
UnicodeEncodeError: 'ascii' codec can't encode character '\xe9' in position 4132: ordinal not in range(128)
Kolibri will look for content in: /tmp/home/.kolibri/content
movedirectory after the fix:
Current content directory is /media/usb/Vidéos-2
Kolibri will look for content in: /media/usb/Vidéos-2
Environment
- Kolibri:
developatfe298f54(tested with0.20.0a1setup) - OS: Linux in Docker (
python:3.10-slim), Python 3.10.22 - Dependencies: configobj 5.0.8, Django 3.2.25, pytest 6.2.5
- Installation: source checkout in Docker
- Reproducible: always
Additional context
- One-line fix in
kolibri/utils/options.py: addencoding="utf-8"toConfigObj(...)inread_options_file(). - New test:
test_option_writing_non_ascii_valueinkolibri/utils/tests/test_options.py. It fails ondevelopand passes with the fix. - With the fix,
kolibri/utils/tests/test_options.pygives14 passed; related suites (kolibri/utils/tests,test_movedirectory.py,kolibri/core/device/test) give796 passed, 2 skipped. On the original code the new test fails withUnicodeEncodeError. - If PR #15646 lands first, its new
ConfigObj(conf.filename)line will also needencoding="utf-8"; the new test catches that. - I did not test on Windows, macOS, a real browser, Postgres, or the full test suite. GitHub CI will cover those.
- I could not find an existing report for this.
AI usage
I used an AI assistant to help structure this report and set up the Docker reproduction. I ran the reproduction command myself, verified the output and logs above, and edited the final text. The claims here are from my own run.
Could a maintainer assign this issue to me if it is valid? I would like to work on the fix.
🔍 Thanks for opening this issue! @rtibblesbot will check it against the code and comment with its findings.
What happens next:
- The
community-issue-reviewlabel stays on until the review comment is posted. - The comment identifies the files, lines, or commits examined so you can follow the reasoning and respond if you disagree.
- If the check confirms the issue, @rtibblesbot may also correct the title and description. GitHub preserves the edit history, so the original text remains available.
- @rtibblesbot's comments are LLM-generated, so please evaluate them critically.
A maintainer decides what happens next. This check does not close the issue.
Verdict: claim holds
Evidence: kolibri/utils/options.py:1018 on develop at fe298f5449 builds ConfigObj(ini_path, configspec=get_configspec()) with no encoding; configobj 5.0.8 write() encodes with self.encoding or self.default_encoding or 'ascii'.
- configobj 5.0.8 decodes a BOM-less file as UTF-8 when
encodingis unset, so a non-ASCII value reads back fine. update_options_file()(kolibri/utils/options.py:1124) rewrites the whole file viaconf.write(), which raisesUnicodeEncodeErroronce any value is non-ASCII, whichever key is being set.- Reproduced directly against configobj 5.0.8: an
options.iniholdingCONTENT_DIR = /media/usb/Vidéosreads back unchanged, thenwrite()fails with'ascii' codec can't encode character '\xe9'; withencoding="utf-8"the write succeeds and keeps the UTF-8 bytes. - Callers that hit it:
- Device settings:
primary_storage_location/secondary_storage_locationssetters (kolibri/core/device/models.py:238,:246), fed byDeviceSettingsSerializer(kolibri/core/device/viewsets/device_settings.py:38-39). content movedirectory:kolibri/core/content/management/commands/content.py:90-98copies the databases and storage folders, deletes them from the source, then callsupdate_options_file(:135). When that call fails, the content has already moved butCONTENT_DIRstill points to the old, empty folder, as the report states.
- Device settings:
- No existing issue reports this; the closest search hits (#6298, #3383) are about other encoding errors.
Written by rtibblesbot, an LLM-based coding agent.