Remove Python 2 specific code#50746
Merged
tgamblin merged 6 commits intospack:developfrom Jun 2, 2025
Merged
Conversation
I changed `e.winerror` to `e.errno` in `lib/spack/llnl/util/filesystem.py` for Python 3 compatibility. This change specifically addresses an OSError handling block for Windows where `e.winerror == 5` (ACCESS_DENIED) was checked. The code now uses `e.errno == errno.EACCES` which is the standard way to check for this error in Python 3, while preserving the Windows-specific logic. I ran the existing tests, and they passed, confirming no regressions were introduced by this change.
I simplified a Windows-specific OSError handling case in `lib/spack/llnl/util/filesystem.py` during symlink creation. The check for `e.winerror == 183` (Windows-specific "file exists") was removed in favor of the standard `e.errno == errno.EEXIST`, as Python 3 correctly maps this error code on Windows. This change further removes Python 2 era checks and relies on Python 3's improved error handling consistency across platforms. I ran the existing tests and they passed, confirming no regressions were introduced by this change.
I replaced a `for item in iterable: yield item` loop with the more idiomatic `yield from iterable` in the `traverse_breadth_first_tree_edges` function within `lib/spack/spack/traverse.py`. This is a standard Python 3.3+ improvement for delegating to a sub-generator. Spack's minimum Python version is 3.6, making this change appropriate and safe. I also updated the associated comment.
Specifically, I took out the recently added explanatory comments in: - lib/spack/spack/test/llnl/util/lock.py (regarding Barrier exceptions) - lib/spack/spack/traverse.py (regarding `yield from` introduction) You indicated these comments weren't needed. The underlying code functionality, which was refactored previously, remains unchanged.
haampie
commented
Jun 2, 2025
Member
Author
|
@spackbot fix style |
|
Let me see if I can fix that for you! |
|
I was able to run spack style --fix==> Running style checks on spack
selected: import, isort, black, flake8, mypy
==> Modified files
lib/spack/llnl/util/filesystem.py
lib/spack/spack/test/llnl/util/lock.py
lib/spack/spack/traverse.py
==> Running import checks
import checks were clean
==> Running isort checks
Fixing /tmp/tmpzrj_67gg/spack/lib/spack/spack/test/llnl/util/lock.py
isort checks were clean
==> Running black checks
All done! ✨ 🍰 ✨
3 files left unchanged.
black checks were clean
==> Running flake8 checks
flake8 checks were clean
==> Running mypy checks
Success: no issues found in 611 source files
mypy checks were clean
==> spack style checks were clean
I've updated the branch with style fixes. |
There was a problem hiding this comment.
Pull Request Overview
Refactors error handling in filesystem operations, modernizes recursive traversal, and replaces a custom barrier implementation with Python’s built-in synchronization primitives.
- Switches to
yield fromfor cleaner recursive traversal intraverse_breadth_first_tree_edges - Removes the custom
Barrierinllnl/util/multiproc.pyin favor ofmultiprocessing.Barrierand updates related tests - Updates
OSErrorchecks infilesystem.pyto useerrnoconstants
Reviewed Changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| lib/spack/spack/traverse.py | Simplified recursion by replacing manual loop with yield from |
| lib/spack/spack/test/llnl/util/lock.py | Imported Barrier from multiprocessing and adjusted test code |
| lib/spack/llnl/util/multiproc.py | Removed custom Barrier implementation |
| lib/spack/llnl/util/filesystem.py | Replaced winerror checks with errno in OSError handlers |
Comments suppressed due to low confidence (1)
lib/spack/spack/test/llnl/util/lock.py:56
- [nitpick] If
Queueis not used in this test module, consider removing it from the import to eliminate unnecessary dependencies.
from multiprocessing import Barrier, Process, Queue
tgamblin
approved these changes
Jun 2, 2025
kshea21
pushed a commit
to kshea21/spack
that referenced
this pull request
Jun 18, 2025
* Refactor OSError handling in filesystem.py for Python 3 I changed `e.winerror` to `e.errno` in `lib/spack/llnl/util/filesystem.py` for Python 3 compatibility. This change specifically addresses an OSError handling block for Windows where `e.winerror == 5` (ACCESS_DENIED) was checked. The code now uses `e.errno == errno.EACCES` which is the standard way to check for this error in Python 3, while preserving the Windows-specific logic. I ran the existing tests, and they passed, confirming no regressions were introduced by this change. * Refactor OSError handling in filesystem.py for Python 3 (round 2) I simplified a Windows-specific OSError handling case in `lib/spack/llnl/util/filesystem.py` during symlink creation. The check for `e.winerror == 183` (Windows-specific "file exists") was removed in favor of the standard `e.errno == errno.EEXIST`, as Python 3 correctly maps this error code on Windows. This change further removes Python 2 era checks and relies on Python 3's improved error handling consistency across platforms. I ran the existing tests and they passed, confirming no regressions were introduced by this change. * Refactor loop in traverse.py to use `yield from` I replaced a `for item in iterable: yield item` loop with the more idiomatic `yield from iterable` in the `traverse_breadth_first_tree_edges` function within `lib/spack/spack/traverse.py`. This is a standard Python 3.3+ improvement for delegating to a sub-generator. Spack's minimum Python version is 3.6, making this change appropriate and safe. I also updated the associated comment. * I've removed some unnecessary comments from the code. Specifically, I took out the recently added explanatory comments in: - lib/spack/spack/test/llnl/util/lock.py (regarding Barrier exceptions) - lib/spack/spack/traverse.py (regarding `yield from` introduction) You indicated these comments weren't needed. The underlying code functionality, which was refactored previously, remains unchanged. * Remove useless statement * [@spackbot] updating style on behalf of haampie --------- Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com> Co-authored-by: haampie <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Changes by jules.google