René's URL Explorer Experiment


Title: test_git_work_tree_env renders os.environ inert in unpatching attempt · Issue #1671 · gitpython-developers/GitPython · GitHub

Open Graph Title: test_git_work_tree_env renders os.environ inert in unpatching attempt · Issue #1671 · gitpython-developers/GitPython

X Title: test_git_work_tree_env renders os.environ inert in unpatching attempt · Issue #1671 · gitpython-developers/GitPython

Description: TestRepo.test_git_work_tree_env contains this code, which is intended to patch and unpatch two environment variables: GitPython/test/test_repo.py Lines 1341 to 1350 in a5a6464 oldenv = os.environ.copy() os.environ["GIT_DIR"] = new_git_di...

Open Graph Description: TestRepo.test_git_work_tree_env contains this code, which is intended to patch and unpatch two environment variables: GitPython/test/test_repo.py Lines 1341 to 1350 in a5a6464 oldenv = os.environ.c...

X Description: TestRepo.test_git_work_tree_env contains this code, which is intended to patch and unpatch two environment variables: GitPython/test/test_repo.py Lines 1341 to 1350 in a5a6464 oldenv = os.environ.c...

Opengraph URL: https://github.com/gitpython-developers/GitPython/issues/1671

X: @github

direct link

Domain: github.com


Hey, it has json ld scripts:
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"test_git_work_tree_env renders os.environ inert in unpatching attempt","articleBody":"`TestRepo.test_git_work_tree_env` contains this code, which is intended to patch and unpatch two environment variables:\r\n\r\nhttps://github.com/gitpython-developers/GitPython/blob/a5a646494393478c65f26cd3a921f3505219d3e1/test/test_repo.py#L1341-L1350\r\n\r\nHowever, that does not unpatch the variables, and more importantly, it prevents writes through `os.environ` from ever actually setting environment variables (to be inherited automatically by subprocesses) for the rest of the process's lifetime.\r\n\r\n```text\r\n\u003e\u003e\u003e import os\r\n\u003e\u003e\u003e type(os.environ)\r\n\u003cclass 'os._Environ'\u003e\r\n\u003e\u003e\u003e oldenv = os.environ.copy()\r\n\u003e\u003e\u003e type(oldenv)\r\n\u003cclass 'dict'\u003e\r\n\u003e\u003e\u003e os.system(\"printenv FOO\")\r\n256\r\n\u003e\u003e\u003e os.environ[\"FOO\"] = \"bar\"\r\n\u003e\u003e\u003e os.system(\"printenv FOO\")\r\nbar\r\n0\r\n\u003e\u003e\u003e os.environ = oldenv\r\n\u003e\u003e\u003e os.system(\"printenv FOO\")\r\nbar\r\n0\r\n\u003e\u003e\u003e os.environ[\"BAZ\"] = \"quux\"\r\n\u003e\u003e\u003e os.system(\"printenv BAZ\")\r\n256\r\n```\r\n\r\nThis happens because, all types in Python being reference types, the `environ` attribute of `os` merely *refers to* the object of a special mutable mapping type that updates the process's environment variables when mutated. Calling its `copy` method constructs a `dict` object with the same items. Assigning that `dict` back to `os.environ` does not modify the original `os.environ` object in any way, but instead causes the `environ` attribute of `os` to point to the `dict` object from that point forward. From then on, writes to `os.environ` have no effect on the running process's environment variables.\r\n\r\nYet, this does not cause any other tests to fail, nor do any currently passing tests fail as a result of fixing it. I believe this is for three reasons, which I list in descending order of what I *guess* to be their significance:\r\n\r\n1. When GitPython runs `git`, it doesn't rely on automatically passing its own environment variables to the `git` subprocess. Instead, it builds a modified environment based on its own and passes that explicitly. The way it finds out about its own environment is by consulting `os.environ`, which doesn't actually have to work for setting environment variables, because `Popen` is doing it:\r\n\r\n   https://github.com/gitpython-developers/GitPython/blob/a5a646494393478c65f26cd3a921f3505219d3e1/git/cmd.py#L948-L955\r\n\r\n   https://github.com/gitpython-developers/GitPython/blob/a5a646494393478c65f26cd3a921f3505219d3e1/git/cmd.py#L987-L989\r\n\r\n2. When GitPython, or any library it uses if that library is written in Python, or the testing framework, accesses and changes environment variables for use within the process, that is also nearly always via `os.environ`, so again, it's okay if the real environment is not used.\r\n\r\n3. The test is in `test_submodules.py`, and test modules are usually exercised in alphabetical order, with only `test_tree.py` and `test_util.py` coming after it.\r\n\r\nThis only affects the tests, and it can be solved in any way appropriate for tests. Specifically, it can be solved--and also that test code significantly simplified--by patching `os.environ` with `unittest.mock.patch.dict`. We cannot use this in the code of the `git` module, because it upcases environment variable names (#1646). But it is not a problem to use it in tests (besides the test in #1650 that #1646 is fixed), and it is already being used in a few places in the test suite.\r\n\r\n---\r\n\r\nThe way I found out about this was that `flake8` reported it once I removed `test/` from its excluded directories:\r\n\r\n```text\r\ntest/test_repo.py:1350:13: B003 Assigning to `os.environ` doesn't clear the environment. Subprocesses are going to see outdated variables, in disagreement with the current process. Use `os.environ.clear()` or the `env=` argument to Popen.\r\n            os.environ = oldenv\r\n            ^\r\n```\r\n\r\nThe specific suggestions aren't quite applicable here (we do need to mutate `os.environ`, and to do so less drastically than with `clear`), but that message nonetheless accurately identifies the problem, which seems previously to have gone undetected. I think it is a good idea to enable `flake8` for `test/` as well as `git/` (though perhaps sometime soon we might manage to replace `flake8` with [`ruff`](https://docs.astral.sh/ruff/)). I plan to include a fix for this, and also for less serious, mostly merely stylistic issues found while examining `test/` with the help of `flake8`, in the same PR that fixes #1670.","author":{"url":"https://github.com/EliahKagan","@type":"Person","name":"EliahKagan"},"datePublished":"2023-09-21T09:46:18.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":0},"url":"https://github.com/1671/GitPython/issues/1671"}

route-pattern/_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format)
route-controllervoltron_issues_fragments
route-actionissue_layout
fetch-noncev2:6998b9fc-83bc-dee2-5375-d3c6528f9fdb
current-catalog-service-hash81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114
request-idD7F8:24D534:4B8D78:6953E5:696897F0
html-safe-nonce5f079cee5d5815c5e67fe95854295462b65454eb8bea579393b47d520ae402e7
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJEN0Y4OjI0RDUzNDo0QjhENzg6Njk1M0U1OjY5Njg5N0YwIiwidmlzaXRvcl9pZCI6Ijg4MTc0NjM1NDQyOTA3NzcwNzIiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ==
visitor-hmacb3ae7bdc7e437e8d2fafe70b743a53629dc188473de2d697f0ad2ec07ed267a4
hovercard-subject-tagissue:1906563191
github-keyboard-shortcutsrepository,issues,copilot
google-site-verificationApib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I
octolytics-urlhttps://collector.github.com/github/collect
analytics-location///voltron/issues_fragments/issue_layout
fb:app_id1401488693436528
apple-itunes-appapp-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/gitpython-developers/GitPython/1671/issue_layout
twitter:imagehttps://opengraph.githubassets.com/38ff1f116c1111f31c14c23750e9f31fcc7a6740fa0b7a95d5c0780611ca3b98/gitpython-developers/GitPython/issues/1671
twitter:cardsummary_large_image
og:imagehttps://opengraph.githubassets.com/38ff1f116c1111f31c14c23750e9f31fcc7a6740fa0b7a95d5c0780611ca3b98/gitpython-developers/GitPython/issues/1671
og:image:altTestRepo.test_git_work_tree_env contains this code, which is intended to patch and unpatch two environment variables: GitPython/test/test_repo.py Lines 1341 to 1350 in a5a6464 oldenv = os.environ.c...
og:image:width1200
og:image:height600
og:site_nameGitHub
og:typeobject
og:author:usernameEliahKagan
hostnamegithub.com
expected-hostnamegithub.com
None50f46dc2d6192249fd8ebf20e76c800f4f2596d4a5f3ab63dd63a754df154f54
turbo-cache-controlno-preview
go-importgithub.com/gitpython-developers/GitPython git https://github.com/gitpython-developers/GitPython.git
octolytics-dimension-user_id503709
octolytics-dimension-user_logingitpython-developers
octolytics-dimension-repository_id1126087
octolytics-dimension-repository_nwogitpython-developers/GitPython
octolytics-dimension-repository_publictrue
octolytics-dimension-repository_is_forkfalse
octolytics-dimension-repository_network_root_id1126087
octolytics-dimension-repository_network_root_nwogitpython-developers/GitPython
turbo-body-classeslogged-out env-production page-responsive
disable-turbofalse
browser-stats-urlhttps://api.github.com/_private/browser/stats
browser-errors-urlhttps://api.github.com/_private/browser/errors
releasefef287f17234b4529a4b112a3d47fe8551e32ddd
ui-targetfull
theme-color#1e2327
color-schemelight dark

Links:

Skip to contenthttps://github.com/gitpython-developers/GitPython/issues/1671#start-of-content
https://github.com/
Sign in https://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fgitpython-developers%2FGitPython%2Fissues%2F1671
GitHub CopilotWrite better code with AIhttps://github.com/features/copilot
GitHub SparkBuild and deploy intelligent appshttps://github.com/features/spark
GitHub ModelsManage and compare promptshttps://github.com/features/models
MCP RegistryNewIntegrate external toolshttps://github.com/mcp
ActionsAutomate any workflowhttps://github.com/features/actions
CodespacesInstant dev environmentshttps://github.com/features/codespaces
IssuesPlan and track workhttps://github.com/features/issues
Code ReviewManage code changeshttps://github.com/features/code-review
GitHub Advanced SecurityFind and fix vulnerabilitieshttps://github.com/security/advanced-security
Code securitySecure your code as you buildhttps://github.com/security/advanced-security/code-security
Secret protectionStop leaks before they starthttps://github.com/security/advanced-security/secret-protection
Why GitHubhttps://github.com/why-github
Documentationhttps://docs.github.com
Bloghttps://github.blog
Changeloghttps://github.blog/changelog
Marketplacehttps://github.com/marketplace
View all featureshttps://github.com/features
Enterpriseshttps://github.com/enterprise
Small and medium teamshttps://github.com/team
Startupshttps://github.com/enterprise/startups
Nonprofitshttps://github.com/solutions/industry/nonprofits
App Modernizationhttps://github.com/solutions/use-case/app-modernization
DevSecOpshttps://github.com/solutions/use-case/devsecops
DevOpshttps://github.com/solutions/use-case/devops
CI/CDhttps://github.com/solutions/use-case/ci-cd
View all use caseshttps://github.com/solutions/use-case
Healthcarehttps://github.com/solutions/industry/healthcare
Financial serviceshttps://github.com/solutions/industry/financial-services
Manufacturinghttps://github.com/solutions/industry/manufacturing
Governmenthttps://github.com/solutions/industry/government
View all industrieshttps://github.com/solutions/industry
View all solutionshttps://github.com/solutions
AIhttps://github.com/resources/articles?topic=ai
Software Developmenthttps://github.com/resources/articles?topic=software-development
DevOpshttps://github.com/resources/articles?topic=devops
Securityhttps://github.com/resources/articles?topic=security
View all topicshttps://github.com/resources/articles
Customer storieshttps://github.com/customer-stories
Events & webinarshttps://github.com/resources/events
Ebooks & reportshttps://github.com/resources/whitepapers
Business insightshttps://github.com/solutions/executive-insights
GitHub Skillshttps://skills.github.com
Documentationhttps://docs.github.com
Customer supporthttps://support.github.com
Community forumhttps://github.com/orgs/community/discussions
Trust centerhttps://github.com/trust-center
Partnershttps://github.com/partners
GitHub SponsorsFund open source developershttps://github.com/sponsors
Security Labhttps://securitylab.github.com
Maintainer Communityhttps://maintainers.github.com
Acceleratorhttps://github.com/accelerator
Archive Programhttps://archiveprogram.github.com
Topicshttps://github.com/topics
Trendinghttps://github.com/trending
Collectionshttps://github.com/collections
Enterprise platformAI-powered developer platformhttps://github.com/enterprise
GitHub Advanced SecurityEnterprise-grade security featureshttps://github.com/security/advanced-security
Copilot for BusinessEnterprise-grade AI featureshttps://github.com/features/copilot/copilot-business
Premium SupportEnterprise-grade 24/7 supporthttps://github.com/premium-support
Pricinghttps://github.com/pricing
Search syntax tipshttps://docs.github.com/search-github/github-code-search/understanding-github-code-search-syntax
documentationhttps://docs.github.com/search-github/github-code-search/understanding-github-code-search-syntax
Sign in https://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fgitpython-developers%2FGitPython%2Fissues%2F1671
Sign up https://github.com/signup?ref_cta=Sign+up&ref_loc=header+logged+out&ref_page=%2F%3Cuser-name%3E%2F%3Crepo-name%3E%2Fvoltron%2Fissues_fragments%2Fissue_layout&source=header-repo&source_repo=gitpython-developers%2FGitPython
Reloadhttps://github.com/gitpython-developers/GitPython/issues/1671
Reloadhttps://github.com/gitpython-developers/GitPython/issues/1671
Reloadhttps://github.com/gitpython-developers/GitPython/issues/1671
gitpython-developers https://github.com/gitpython-developers
GitPythonhttps://github.com/gitpython-developers/GitPython
Please reload this pagehttps://github.com/gitpython-developers/GitPython/issues/1671
Notifications https://github.com/login?return_to=%2Fgitpython-developers%2FGitPython
Fork 964 https://github.com/login?return_to=%2Fgitpython-developers%2FGitPython
Star 5k https://github.com/login?return_to=%2Fgitpython-developers%2FGitPython
Code https://github.com/gitpython-developers/GitPython
Issues 169 https://github.com/gitpython-developers/GitPython/issues
Pull requests 8 https://github.com/gitpython-developers/GitPython/pulls
Discussions https://github.com/gitpython-developers/GitPython/discussions
Actions https://github.com/gitpython-developers/GitPython/actions
Security Uh oh! There was an error while loading. Please reload this page. https://github.com/gitpython-developers/GitPython/security
Please reload this pagehttps://github.com/gitpython-developers/GitPython/issues/1671
Insights https://github.com/gitpython-developers/GitPython/pulse
Code https://github.com/gitpython-developers/GitPython
Issues https://github.com/gitpython-developers/GitPython/issues
Pull requests https://github.com/gitpython-developers/GitPython/pulls
Discussions https://github.com/gitpython-developers/GitPython/discussions
Actions https://github.com/gitpython-developers/GitPython/actions
Security https://github.com/gitpython-developers/GitPython/security
Insights https://github.com/gitpython-developers/GitPython/pulse
New issuehttps://github.com/login?return_to=https://github.com/gitpython-developers/GitPython/issues/1671
New issuehttps://github.com/login?return_to=https://github.com/gitpython-developers/GitPython/issues/1671
#1673https://github.com/gitpython-developers/GitPython/pull/1673
test_git_work_tree_env renders os.environ inert in unpatching attempthttps://github.com/gitpython-developers/GitPython/issues/1671#top
#1673https://github.com/gitpython-developers/GitPython/pull/1673
acknowledgedhttps://github.com/gitpython-developers/GitPython/issues?q=state%3Aopen%20label%3A%22acknowledged%22
v3.1.37 - Bugfixeshttps://github.com/gitpython-developers/GitPython/milestone/67
https://github.com/EliahKagan
https://github.com/EliahKagan
EliahKaganhttps://github.com/EliahKagan
on Sep 21, 2023https://github.com/gitpython-developers/GitPython/issues/1671#issue-1906563191
GitPython/test/test_repo.pyhttps://github.com/gitpython-developers/GitPython/blob/a5a646494393478c65f26cd3a921f3505219d3e1/test/test_repo.py#L1341-L1350
a5a6464https://github.com/gitpython-developers/GitPython/commit/a5a646494393478c65f26cd3a921f3505219d3e1
GitPython/git/cmd.pyhttps://github.com/gitpython-developers/GitPython/blob/a5a646494393478c65f26cd3a921f3505219d3e1/git/cmd.py#L948-L955
a5a6464https://github.com/gitpython-developers/GitPython/commit/a5a646494393478c65f26cd3a921f3505219d3e1
GitPython/git/cmd.pyhttps://github.com/gitpython-developers/GitPython/blob/a5a646494393478c65f26cd3a921f3505219d3e1/git/cmd.py#L987-L989
a5a6464https://github.com/gitpython-developers/GitPython/commit/a5a646494393478c65f26cd3a921f3505219d3e1
#1646https://github.com/gitpython-developers/GitPython/issues/1646
#1650https://github.com/gitpython-developers/GitPython/pull/1650
#1646https://github.com/gitpython-developers/GitPython/issues/1646
ruffhttps://docs.astral.sh/ruff/
#1670https://github.com/gitpython-developers/GitPython/issues/1670
acknowledgedhttps://github.com/gitpython-developers/GitPython/issues?q=state%3Aopen%20label%3A%22acknowledged%22
v3.1.37 - Bugfixeshttps://github.com/gitpython-developers/GitPython/milestone/67
https://github.com
Termshttps://docs.github.com/site-policy/github-terms/github-terms-of-service
Privacyhttps://docs.github.com/site-policy/privacy-policies/github-privacy-statement
Securityhttps://github.com/security
Statushttps://www.githubstatus.com/
Communityhttps://github.community/
Docshttps://docs.github.com/
Contacthttps://support.github.com?tags=dotcom-footer

Viewport: width=device-width


URLs of crawlers that visited me.