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

Stop patches in the reverse order they were started #370

Merged

Conversation

kurtmckee
Copy link
Contributor

Because some functions are patched twice (like os.rename()), stopping the patches in the same order they were started can result in restoration of one of the patches.

For example:

fn = os.rename

# Patch nesting order: 1 then 2
os.rename = patch_1(fn)
os.rename = patch_2(patch_1(fn))

# Unpatch 1 then 2: A still-patched function is restored
os.rename = fn
os.rename = patch_1(fn)

# Unpatch 2 then 1: The original function is restored
os.rename = patch_1(fn)
os.rename = fn

Fixes #365

Because some functions are patched twice (like `os.rename()`),
stopping the patches in the same order they were started
can result in restoration of one of the patches.

For example:

```
fn = os.rename

# Patch nesting order: 1 then 2
os.rename = patch_1(fn)
os.rename = patch_2(patch_1(fn))

# Unpatch 1 then 2: A still-patched function is restored
os.rename = fn
os.rename = patch_1(fn)

# Unpatch 2 then 1: The original function is restored
os.rename = patch_1(fn)
os.rename = fn
```

Fixes anishathalye#365
Copy link

codecov bot commented Dec 18, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 89.48%. Comparing base (7e6ec66) to head (33f5390).
Report is 4 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master     #370   +/-   ##
=======================================
  Coverage   89.48%   89.48%           
=======================================
  Files          36       36           
  Lines        1674     1674           
  Branches      205      205           
=======================================
  Hits         1498     1498           
  Misses        143      143           
  Partials       33       33           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@anishathalye anishathalye merged commit 82834e6 into anishathalye:master Dec 19, 2024
20 checks passed
@anishathalye
Copy link
Owner

Thank you!

@kurtmckee kurtmckee deleted the fix-patch-stop-order-issue-365 branch December 19, 2024 13:28
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.

Tests fail because the mocking strategy conflicts with pytest on Nixos
2 participants