Skip to content
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

Use Windows IFileOperation to implement copying/moving/deleting in pathlib #131262

Open
barneygale opened this issue Mar 14, 2025 · 1 comment
Open
Labels
OS-windows performance Performance or resource usage topic-pathlib type-feature A feature request or enhancement

Comments

@barneygale
Copy link
Contributor

barneygale commented Mar 14, 2025

Feature or enhancement

Proposal:

The Windows-only IFileOperation API allows us to copy, move and delete files and directories using the same code as explorer.exe. We could use this to provide pathlib.Path.copy(), move() and _delete() - it should be much faster, and in the case of _delete() it would work around #84324

Has this already been discussed elsewhere?

This is a minor feature, which does not need previous discussion elsewhere

Links to previous discussion of this feature:

No response

@barneygale barneygale added OS-windows performance Performance or resource usage topic-pathlib type-feature A feature request or enhancement labels Mar 14, 2025
@zooba
Copy link
Member

zooba commented Mar 17, 2025

This is true, but it also requires enabling COM (CoInitializeEx), which we historically try to avoid doing on the main thread in case the application wants to enable it differently (e.g. single-threaded vs. free-threaded). The WMI query in _wmi runs on its own thread to avoid touching the main thread's state.

I suspect the overhead of threading would probably take away the benefits here. If we were to go down this route, we'd probably have to figure out how to safely transition to either all CPython or just python.exe (i.e. embedders can choose) running with COM initialised. I dislike this kind of process-wide state being unpredictable (i.e. based on whether anything has done a copy/move/delete).

That said, I believe that Store packaged Python is already doing this, I just haven't tested exactly what the conditions are (specifically whether it's somehow active but the mode can be changed). I'm not aware of anyone hitting issues due to this, but it's possible they just worked around it with a different install. (Under PEP 773, we go back to never having COM initialised by default.)

Apart from that massive caveat, we'd also be formally introducing C++ code (which I've already kind of smuggled in anyway, but it's entirely restricted to the Windows side with only Python-level references, so it's very "clean"). I'm not opposed, but it's a consideration. We're not using the macro nonsense to try and emulate OOP from C 😉

Besides all that, I'd love to see it done. It's just not quite trivial in its impact, unfortunately.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
OS-windows performance Performance or resource usage topic-pathlib type-feature A feature request or enhancement
Projects
None yet
Development

No branches or pull requests

2 participants