Skip to content

python: fix BSUnit ctypes binding - #178

Open
jeroendiederen wants to merge 1 commit into
storaged-project:mainfrom
jeroendiederen:fix-python-ctypes-unit
Open

python: fix BSUnit ctypes binding#178
jeroendiederen wants to merge 1 commit into
storaged-project:mainfrom
jeroendiederen:fix-python-ctypes-unit

Conversation

@jeroendiederen

@jeroendiederen jeroendiederen commented Aug 13, 2026

Copy link
Copy Markdown

Pass BSUnit as a ctypes union to bs_size_convert_to and add a conversion regression test. Also make the translation canary shell test POSIX-compatible.

Fix the Python ctypes binding for bs_size_convert_to().

BSUnit is a C union containing the binary and decimal unit enums, but
the Python binding previously passed the unit as a plain ctypes.c_int.
Define a matching ctypes.Union and use it for the function argument.

Also fix the conversion unit test to verify that 1 MiB converts to
1024 KiB, and make the translation canary shell test POSIX-compatible.

Tests:

  • make check
  • git diff --check

All tests pass.

Summary by CodeRabbit

  • Bug Fixes

    • Improved conversion between binary and decimal size units.
    • Corrected conversion handling so values produce accurate results across unit types.
  • Tests

    • Added verification that converting 1 MiB to KiB returns 1024.
    • Improved portability and reliability of distribution detection checks.

Pass BSUnit as a ctypes union to bs_size_convert_to and add a
conversion regression test. Also make the translation canary shell
test POSIX-compatible.
@coderabbitai

coderabbitai Bot commented Aug 13, 2026

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

The Python binding now uses a public SizeUnit ctypes union for native conversions. Conversion tests verify 1 MiB to KiB. The canary script uses POSIX-compatible distro checks.

Changes

Unit conversion

Layer / File(s) Summary
ctypes unit binding
src/python/bytesize.py, tests/libbytesize_unittest.py
The binding defines SizeUnit, passes binary units through the union, updates the native signature, and verifies that 1 MiB converts to "1024" KiB.
Test script portability
tests/canary_tests.sh.in
The distro detection command uses multiline formatting and quoted POSIX `

Estimated code review effort: 3 (Moderate) | ~20 minutes

Mergeability Score: ⚪ Minimal · up to 97faf

The change corrects Python unit conversion binding behavior and updates related tests and shell compatibility; no actionable merge-blocking risk remains after normal checks and review.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely identifies the main change: fixing the Python ctypes binding for the size unit union.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🧹 Nitpick comments (2)
tests/libbytesize_unittest.py (1)

368-369: 🗄️ Data Integrity & Integration | 🔵 Trivial | ⚡ Quick win

Add coverage for decimal units.

This test covers only MiB to KiB. Add a 1 MB to KB assertion to verify the decimal-unit path through SizeUnit.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@tests/libbytesize_unittest.py` around lines 368 - 369, Add a decimal-unit
assertion alongside the existing SizeStruct.new_from_str test, converting “1 MB”
to KB and expecting “1000”. Keep the current binary-unit MiB-to-KiB assertion
unchanged to cover both SizeUnit paths.
src/python/bytesize.py (1)

84-88: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Address the RUF012 warning on SizeUnit._fields_.

Ruff reports this list as a mutable class attribute. If Ruff is enforced, annotate _fields_ as a ClassVar or use an immutable sequence accepted by the supported ctypes versions. Keep any suppression scoped to this declaration.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/python/bytesize.py` around lines 84 - 88, Update the SizeUnit._fields_
declaration to address RUF012 by annotating it as a ClassVar or replacing the
list with an immutable sequence supported by the project’s ctypes versions. Keep
any lint suppression narrowly scoped to this declaration.

Source: Linters/SAST tools

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@src/python/bytesize.py`:
- Around line 84-88: Update the SizeUnit._fields_ declaration to address RUF012
by annotating it as a ClassVar or replacing the list with an immutable sequence
supported by the project’s ctypes versions. Keep any lint suppression narrowly
scoped to this declaration.

In `@tests/libbytesize_unittest.py`:
- Around line 368-369: Add a decimal-unit assertion alongside the existing
SizeStruct.new_from_str test, converting “1 MB” to KB and expecting “1000”. Keep
the current binary-unit MiB-to-KiB assertion unchanged to cover both SizeUnit
paths.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: 9a7dcd7f-8547-4720-8cab-f3c355cd7a25

📥 Commits

Reviewing files that changed from the base of the PR and between 229561b and 97faf40.

📒 Files selected for processing (3)
  • src/python/bytesize.py
  • tests/canary_tests.sh.in
  • tests/libbytesize_unittest.py

@vojtechtrefny vojtechtrefny left a comment

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.

Thank you, looks good to me in general, just few small changes.

Comment thread src/python/bytesize.py
err = POINTER(SizeErrorStruct)()
ret = c_bytesize.bs_size_convert_to(self, unit, byref(err))
u = SizeUnit()
u.bunit = unit

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.

This works, but I think it would be better to set the correct member based on the unit. So dunit for MB/21 or larger and bunit else.

x = SizeStruct.new_from_str("1 KiB")
x.convert_to(KiB)
x = SizeStruct.new_from_str("1 MiB")
self.assertEqual(x.convert_to(KiB), "1024")

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.

Please also add a check with a decimal unit.

Comment thread tests/canary_tests.sh.in
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