Skip to content

Remove Python 2 specific code#50746

Merged
tgamblin merged 6 commits intospack:developfrom
haampie:python3-filesystem-refactor
Jun 2, 2025
Merged

Remove Python 2 specific code#50746
tgamblin merged 6 commits intospack:developfrom
haampie:python3-filesystem-refactor

Conversation

@haampie
Copy link
Copy Markdown
Member

@haampie haampie commented Jun 2, 2025

Changes by jules.google

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.
@spackbot-app spackbot-app bot added core PR affects Spack core functionality utilities labels Jun 2, 2025
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.
@spackbot-app spackbot-app bot added the tests General test capability(ies) label Jun 2, 2025
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.
Comment thread lib/spack/spack/test/llnl/util/lock.py Outdated
@haampie
Copy link
Copy Markdown
Member Author

haampie commented Jun 2, 2025

@spackbot fix style

@spackbot-app
Copy link
Copy Markdown

spackbot-app bot commented Jun 2, 2025

Let me see if I can fix that for you!

@spackbot-app
Copy link
Copy Markdown

spackbot-app bot commented Jun 2, 2025

I was able to run spack style --fix for you!

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
Keep in mind that I cannot fix your flake8 or mypy errors, so if you have any you'll need to fix them and update the pull request. If I was able to push to your branch, if you make further changes you will need to pull from your updated branch before pushing again.

I've updated the branch with style fixes.

@haampie haampie requested a review from Copilot June 2, 2025 15:38
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

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 from for cleaner recursive traversal in traverse_breadth_first_tree_edges
  • Removes the custom Barrier in llnl/util/multiproc.py in favor of multiprocessing.Barrier and updates related tests
  • Updates OSError checks in filesystem.py to use errno constants

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 Queue is not used in this test module, consider removing it from the import to eliminate unnecessary dependencies.
from multiprocessing import Barrier, Process, Queue

Comment thread lib/spack/llnl/util/filesystem.py
@haampie haampie changed the title Refactor OSError handling in filesystem.py for Python 3 Remove Python 2 specific code Jun 2, 2025
@tgamblin tgamblin merged commit 7431f03 into spack:develop Jun 2, 2025
35 of 36 checks passed
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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

core PR affects Spack core functionality tests General test capability(ies) utilities

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants