Skip to content

Preserve empty permission fields in FilePerms.from_int - #484

Closed
oyeong011 wants to merge 1 commit into
mahmoud:masterfrom
oyeong011:fix/fileperms-preserve-leading-zero-fields
Closed

oyeong011 wants to merge 1 commit into
mahmoud:masterfrom
oyeong011:fix/fileperms-preserve-leading-zero-fields

Conversation

@oyeong011

Copy link
Copy Markdown

FilePerms.from_int() drops leading empty permission groups, shifting group/other permissions into a different class:

>>> perms = FilePerms.from_int(0o007)
>>> repr(perms)
"FilePerms(user='rwx', group='', other='')"
>>> oct(int(perms))
'0o700'

The same problem affects from_path(), which delegates to from_int(). Reading a file's mode and passing int(perms) back to os.chmod can therefore change its permission classes.

Read exactly three groups instead of stopping when the remaining integer becomes zero. The existing lower-nine-bit mask is unchanged. Add roundtrip coverage for all 512 standard modes plus special-bit/file-type/negative inputs to retain existing masking behavior.

Validation on macOS, CPython 3.12.13:

  • Before: 65 of 516 roundtrip cases fail.
  • After: 1,188 tests and doctests pass (python -m pytest --doctest-modules boltons tests -q).
  • Real-file from_path/chmod roundtrips preserve 0o000, 0o007, 0o070, 0o077, 0o644, and 0o777.
  • Toggling original/patched/original code on a real file reproduces 0o007 → 0o700, preserves 0o007, and reproduces the failure again.

Independent of #482 (setter updates) and #483 (rotation). AI assistance was used to prepare the patch and tests; runtime behavior was verified locally. Windows and the Python 3.7 runtime were not tested locally.

Read all three POSIX permission groups even when the remaining value is
zero, so leading empty groups do not shift permissions to another class.

Tested: 1188 tests/doctests; six real-file from_path/chmod roundtrips
Confidence: high
Scope-risk: narrow
mahmoud added a commit that referenced this pull request Oct 25, 2026
…micSaver cleanup, root-home shrinkuser, non-identifier names; include docs in sdist (#484, #518, #485, #539, #504, #573, #557)
@mahmoud

mahmoud commented Oct 25, 2026

Copy link
Copy Markdown
Owner

Folded into #609 (see the PR description for credit). Thanks for the report and the repro.

@mahmoud mahmoud closed this Oct 25, 2026
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