Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

locale.strxfrm raising an error on characters incompatible with the locale. #778

Open
oussjarrousse opened this issue Oct 18, 2024 · 4 comments
Labels

Comments

@oussjarrousse
Copy link

after rednotebook reads all the day files, and when it is trying to sort categories, it raises an error.

After some investigation I found out that the error is related to locale.strxfrm in line 516 in journal.py:

    @property
    def categories(self):
        return sorted(
            set(itertools.chain.from_iterable(day.categories for day in self.days)),
            key=locale.strxfrm,
        )

What I think is happening is that locale.strxfrm does not accept characters that are incompatible with the locale.
For example, my locale is en.US.UTF-8. For example: locale.strxfrm("عربي") will raise an error

Traceback (most recent call last):
  File "/Users/ouss/Documents/workspace/rednotebook/rednotebook/journal.py", line 624, in <module>
    main()
  File "/Users/ouss/Documents/workspace/rednotebook/rednotebook/journal.py", line 606, in main
    journal = Journal()
  File "/Users/ouss/Documents/workspace/rednotebook/rednotebook/journal.py", line 238, in __init__
    self.open_journal(journal_path)
  File "/Users/ouss/Documents/workspace/rednotebook/rednotebook/journal.py", line 401, in open_journal
    self.frame.categories_tree_view.categories = self.categories
  File "/Users/ouss/Documents/workspace/rednotebook/rednotebook/journal.py", line 514, in categories
    return sorted(
OSError: [Errno 22] Invalid argument

What I wish to happen, is that categories are sorted regardless of the characters type or locale.

@jendrikseipp
Copy link
Owner

Do you know what needs to be changed to fix this?

@oussjarrousse
Copy link
Author

oussjarrousse commented Oct 23, 2024

We have several options. Can you confirm that the purpose of using locale.strxfrm here is to sort the categories based on the locale settings of the user?

If that was the case. We could use an alternative sorting mechanism that will respect the locale but not break on incompatible characters.

Is there a test for this function?

My plan is to:

  • create a test that reproduce it (regression test)
  • create a fix and make sure all tests pass.
  • create a pull request.

@jendrikseipp
Copy link
Owner

Yes, I can confirm this. Your approach sounds great!

@oussjarrousse
Copy link
Author

I've created a pull request that fixes the issue... at least for me...
Looking forward for your review...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants