Skip to content

Reorder struct lconv members to match locale.h #6073

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

Merged

Conversation

jackoconnordev
Copy link
Contributor

@jackoconnordev jackoconnordev commented Aug 5, 2025

Resolves #6072

struct lconv in locale.h
https://codebrowser.dev/glibc/glibc/locale/locale.h.html#lconv::int_p_cs_precedes.

Order of relevant section in glibc locale.h

char int_p_cs_precedes;
char int_p_sep_by_space;
char int_n_cs_precedes;
char int_n_sep_by_space;

Summary by CodeRabbit

  • Refactor
    • Adjusted the internal order of fields in a locale-related data structure for Windows systems. No visible changes to app behavior or functionality.

`struct lconv` in locale.h
https://codebrowser.dev/glibc/glibc/locale/locale.h.html#lconv::int_p_cs_precedes.

Order of relevant section in glibc locale.h
```C
char int_p_cs_precedes;
char int_p_sep_by_space;
char int_n_cs_precedes;
char int_n_sep_by_space;
```
Copy link
Contributor

coderabbitai bot commented Aug 5, 2025

Walkthrough

The change reorders two adjacent fields, int_n_cs_precedes and int_p_sep_by_space, within the private lconv struct for Windows targets in stdlib/src/locale.rs. No other logic or structure was modified, and no exported/public APIs were affected.

Changes

Cohort / File(s) Change Summary
lconv struct field order update
stdlib/src/locale.rs
Reordered two fields (int_n_cs_precedes and int_p_sep_by_space) in the lconv struct for Windows targets to match the glibc definition.

Sequence Diagram(s)

Not applicable: The change only affects struct field order and does not alter control flow or introduce new features.

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~2 minutes

Assessment against linked issues

Objective Addressed Explanation
Align struct lconv field order with glibc, specifically placing int_n_cs_precedes after int_p_sep_by_space instead of before (#6072)

Assessment against linked issues: Out-of-scope changes

None found.

Poem

In the garden of structs, two fields took a hop,
Swapping their places with a delicate plop.
Now glibc and RustPython are perfectly in line,
The locale fields march, all orderly and fine.
🥕 A bunny’s delight—such neatness divine!

Note

⚡️ Unit Test Generation is now available in beta!

Learn more here, or try it out under "Finishing Touches" below.

✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai generate unit tests to generate unit tests for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (1)
stdlib/src/locale.rs (1)

6-32: Add a compile-time layout assertion to guard against future regressions
Because this struct is hand-rolled and must stay byte-for-byte identical to the C version, consider adding a static_assertions::assert_eq_size! (or similar) check against the header-generated layout on Windows. This will fail CI immediately if someone accidentally reorders or changes a field in the future.

+#[cfg(windows)]
+const _: () = {
+    use static_assertions::assert_eq_size;
+    // Ensure our Rust representation matches the C layout size wise.
+    assert_eq_size!(lconv, libc::lconv);
+};

(The libc crate exposes lconv on Windows nowadays; if that ever changes, bindgen-generated bindings could be used instead.)

📜 Review details

Configuration used: .coderabbit.yml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 91979a3 and cab51aa.

📒 Files selected for processing (1)
  • stdlib/src/locale.rs (1 hunks)
🧰 Additional context used
📓 Path-based instructions (1)
**/*.rs

📄 CodeRabbit Inference Engine (.github/copilot-instructions.md)

**/*.rs: Follow the default rustfmt code style (cargo fmt to format)
Always run clippy to lint code (cargo clippy) before completing tasks. Fix any warnings or lints that are introduced by your changes
Follow Rust best practices for error handling and memory management
Use the macro system (pyclass, pymodule, pyfunction, etc.) when implementing Python functionality in Rust

Files:

  • stdlib/src/locale.rs
🔇 Additional comments (1)
stdlib/src/locale.rs (1)

26-29: struct lconv field order now matches C definition – good catch
The re-ordering of int_p_sep_by_space / int_n_cs_precedes fixes the offset mismatch reported in #6072 and brings the layout in line with the C standard / glibc definition. This should eliminate the incorrect values that were observed when reading the lconv returned by localeconv().

Copy link
Member

@youknowone youknowone left a comment

Choose a reason for hiding this comment

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

Thank you!

Just in curious. Does this difference made any incompatibiltiy issue?

@youknowone youknowone merged commit b480d23 into RustPython:main Aug 6, 2025
12 checks passed
@jackoconnordev
Copy link
Contributor Author

It doesn't affect tests but I think since C structs members are ordered in memory it technically could lead to some unusual errors.

I came across this while investigating a separate issue I had where the French language locale on my laptop (Fedora OS) used a non-UTF-8 string as the thousands separator and RustPython was panicking in test_float_with_comma.

On MacOS my french thousands separator is 'thousands_sep': '\u202f' where \u202f is strictly speaking the UTF-16 encoding of narrow nobreak space, but this can be parsed as utf-8 without RustPython panicking (expects UTF-8 strings).

On Fedora if I remember correctly (I can double check if needed) thousands_sep was set to \xA0, which is only the second half of the UTF-16 encoding \u00A0 of non-breaking space, which is not valid UTF-8 and causes RustPython to panic.

The panic would happen at

set_string_field!(lc, thousands_sep);
which is a macro so hard to debug. This .expect would fail if the string value in struct lconv was not utf-8 parsable. The CPython equivalent to this function accepts a wider variety of encodings without panicking. I believe CPython uses PyUnicode_DecodeUTF8Stateful to parse the string in its equivalent macro PyDict_SetItemString.


Method to check the values of struct lconv using CPython/RustPython.

>> import locale
>> locale.setlocale(locale.LC_ALL, 'fr_FR')
>> locale.localeconv()
{'int_curr_symbol': 'EUR ', 'currency_symbol': '€', 'mon_decimal_point': ',', 'mon_thousands_sep': '\u202f', 'mon_grouping': [3, 0], 'positive_sign': '', 'negative_sign': '-', 'int_frac_digits': 2, 'frac_digits': 2, 'p_cs_precedes': 0, 'p_sep_by_space': 1, 'n_cs_precedes': 0, 'n_sep_by_space': 1, 'p_sign_posn': 1, 'n_sign_posn': 1, 'decimal_point': ',', 'thousands_sep': '\u202f', 'grouping': [3, 0]}

A0 is not valid UTF-8

image

@youknowone
Copy link
Member

Thank you for explanation!

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.

struct lconv field mismatch RustPython and glibc
2 participants