René's URL Explorer Experiment


Title: Explanation makes unclear reference to "first" and "second" imports · Issue #1804 · gitpython-developers/GitPython · GitHub

Open Graph Title: Explanation makes unclear reference to "first" and "second" imports · Issue #1804 · gitpython-developers/GitPython

X Title: Explanation makes unclear reference to "first" and "second" imports · Issue #1804 · gitpython-developers/GitPython

Description: The git.cmd.Git.refresh class method includes this code: GitPython/git/cmd.py Lines 478 to 481 in d28c20b # We get here if this was the init refresh and the refresh mode was not # error. Go ahead and set the GIT_PYTHON_GIT_EXECUTABLE suc...

Open Graph Description: The git.cmd.Git.refresh class method includes this code: GitPython/git/cmd.py Lines 478 to 481 in d28c20b # We get here if this was the init refresh and the refresh mode was not # error. Go ahead a...

X Description: The git.cmd.Git.refresh class method includes this code: GitPython/git/cmd.py Lines 478 to 481 in d28c20b # We get here if this was the init refresh and the refresh mode was not # error. Go ahead a...

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

X: @github

direct link

Domain: github.com


Hey, it has json ld scripts:
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Explanation makes unclear reference to \"first\" and \"second\" imports","articleBody":"The `git.cmd.Git.refresh` class method includes this code:\r\n\r\nhttps://github.com/gitpython-developers/GitPython/blob/d28c20b1dbf18dc60a2b1859e818801bd8d742f2/git/cmd.py#L478-L481\r\n\r\nIt is unclear what \"the difference between a first import and a second import\" is referring to. (I should have noticed this issue when working on #1725, in which I actually edited that comment to improve its formatting, capitalization, and punctuation, but I apparently did not notice it at that time.)\r\n\r\nThat code can run multiple times, accessing the same class-wide state each time, because `Git.refresh` can be called multiple times (and, being public, may reasonably be called by code outside of GitPython). **But this is not related to how many imports, or import attempts, occur.** The comment would make sense if Python's import system worked differently, but seems at odds with the actual design. As I understand it:\r\n\r\n- **If an import succeeds**, the resulting module object is cached in `sys.modules`. Subsequent imports use the same module object and do not rerun the module's code.\r\n\r\n  For example, suppose `hello.py` contains:\r\n\r\n  ```python\r\n  print(\"Hello, world!\")\r\n  ```\r\n\r\n  Then running `import hello` from a REPL prints the greeting the first time it is run, but running it subsequent times produces no repeated side effect.\r\n\r\n- **If an import fails**, the new module object, which may itself be incompletely populated, is removed from `sys.modules`, before control returns via the raised exception propagating up the call stack to the code that attempted the import. Even if that module object turns out still to exist, it will not be reused in subsequent attempts to import the code. A subsequent import attempt will rerun the module's code from scratch, creating a new module object which serves as a new namespace. The state in the old module object is not used; global variables of the module, including classes, and thus also including their attributes, are all new.\r\n\r\n  For example, suppose `throw.py` contains:\r\n\r\n  ```python\r\n  import sys\r\n  \r\n  me = sys.modules[__name__]\r\n  try:\r\n      me.x += 1\r\n  except AttributeError:\r\n      me.x = 1\r\n  \r\n  try:\r\n      a = sys.a\r\n  except AttributeError:\r\n      a = sys.a = []\r\n  a.append(me)\r\n  \r\n  raise ValueError(x)\r\n  ```\r\n\r\n  Then running `import throw` from a REPL raises `ValueError` with a value of `1` each time it is run, and `sys.a` is populated with the abandoned module object from each attempt, all of which are separate objects. (Importing `sys` in the REPL allows it to be inspected, since `import sys` succeeds the first time it occurs, whether that is in the REPL or in `throw.py`, and thus all `import sys` get the same `sys` module object with the same `a` attribute.)\r\n\r\n- **If the contents of `sys.modules` are explicitly modified** (rather than implicitly, due to the effect of `import` and `from` statements), one of the above two situations still applies, depending on the nature of the mutation. The exception to this would be if a reference to the old module object is obtained and patched back into `sys.modules`. That would be a very unusual thing to do, except perhaps for purposes of debugging or experimentation, and I'm reasonably sure the intent of that comment--and the code it documents--is not to cover such cases. Furthermore, that would still not rerun top-level code of the module.\r\n\r\n- **Python's import system is extensible** and can be made to work differently from the usual rules. Of course, this cannot generally be anticipated by library code, and I likewise doubt this is a factor in the design or documentation.\r\n\r\nI suspect that the code is correct and the comment incorrect, but since I'm not sure what was originally intended, I am not entirely sure how to fix it. It could be changed to say something like \"discern the difference between the initial refresh at import time and subsequent calls,\" but this is very general and I worry it would paper over whatever really ought to be said. On the other hand, perhaps this really is all that was meant.\r\n\r\nI haven't looked into the history of the Python import system but I don't think it operated in a fundamentally different way at the time this explanation was written. This code, and the original accompanying comment, was added in #640 (in 2b3e769). The reasoning given in that pull request, for the actual code change, continues to makes sense, and does not seem to have relied on any incorrect--nor any now-outdated--assumptions about the import system. As far as I can tell, it is only the comment that should be improved.","author":{"url":"https://github.com/EliahKagan","@type":"Person","name":"EliahKagan"},"datePublished":"2024-01-17T06:17:35.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":2},"url":"https://github.com/1804/GitPython/issues/1804"}

route-pattern/_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format)
route-controllervoltron_issues_fragments
route-actionissue_layout
fetch-noncev2:0aac9b7a-5e21-5bdf-1616-c92de857af20
current-catalog-service-hash81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114
request-idBE8A:165174:8F7CEE:BF91F7:696833B0
html-safe-nonce9897bb39bd2585da5d6d1edb8b512a291e2ebc7ac158656e31a8d61277c67585
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCRThBOjE2NTE3NDo4RjdDRUU6QkY5MUY3OjY5NjgzM0IwIiwidmlzaXRvcl9pZCI6IjcwOTA0MzExNDEyOTc1MzM4NzIiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ==
visitor-hmac6ebf9da8a2abc78d88dccef8903d689a3163d54b0783ab89726308e3585a2ac1
hovercard-subject-tagissue:2085468684
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/1804/issue_layout
twitter:imagehttps://opengraph.githubassets.com/2e22e1f89aaf52d0376414041c92527afac76d15c12dfbaab1db6283b4061183/gitpython-developers/GitPython/issues/1804
twitter:cardsummary_large_image
og:imagehttps://opengraph.githubassets.com/2e22e1f89aaf52d0376414041c92527afac76d15c12dfbaab1db6283b4061183/gitpython-developers/GitPython/issues/1804
og:image:altThe git.cmd.Git.refresh class method includes this code: GitPython/git/cmd.py Lines 478 to 481 in d28c20b # We get here if this was the init refresh and the refresh mode was not # error. Go ahead a...
og:image:width1200
og:image:height600
og:site_nameGitHub
og:typeobject
og:author:usernameEliahKagan
hostnamegithub.com
expected-hostnamegithub.com
Nonee25f416bb6d8a5f8624aad6cebc375ab2c50ac58f2175f32a7093325e66e5515
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
release32212b8b3bddd6432b3b35d27c050b1c22bd8cca
ui-targetcanary-2
theme-color#1e2327
color-schemelight dark

Links:

Skip to contenthttps://github.com/gitpython-developers/GitPython/issues/1804#start-of-content
https://github.com/
Sign in https://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fgitpython-developers%2FGitPython%2Fissues%2F1804
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%2F1804
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/1804
Reloadhttps://github.com/gitpython-developers/GitPython/issues/1804
Reloadhttps://github.com/gitpython-developers/GitPython/issues/1804
gitpython-developers https://github.com/gitpython-developers
GitPythonhttps://github.com/gitpython-developers/GitPython
Please reload this pagehttps://github.com/gitpython-developers/GitPython/issues/1804
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/1804
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/1804
New issuehttps://github.com/login?return_to=https://github.com/gitpython-developers/GitPython/issues/1804
#1810https://github.com/gitpython-developers/GitPython/pull/1810
Explanation makes unclear reference to "first" and "second" importshttps://github.com/gitpython-developers/GitPython/issues/1804#top
#1810https://github.com/gitpython-developers/GitPython/pull/1810
acknowledgedhttps://github.com/gitpython-developers/GitPython/issues?q=state%3Aopen%20label%3A%22acknowledged%22
help wantedhttps://github.com/gitpython-developers/GitPython/issues?q=state%3Aopen%20label%3A%22help%20wanted%22
https://github.com/EliahKagan
https://github.com/EliahKagan
EliahKaganhttps://github.com/EliahKagan
on Jan 17, 2024https://github.com/gitpython-developers/GitPython/issues/1804#issue-2085468684
GitPython/git/cmd.pyhttps://github.com/gitpython-developers/GitPython/blob/d28c20b1dbf18dc60a2b1859e818801bd8d742f2/git/cmd.py#L478-L481
d28c20bhttps://github.com/gitpython-developers/GitPython/commit/d28c20b1dbf18dc60a2b1859e818801bd8d742f2
#1725https://github.com/gitpython-developers/GitPython/pull/1725
#640https://github.com/gitpython-developers/GitPython/pull/640
2b3e769https://github.com/gitpython-developers/GitPython/commit/2b3e769989c4928cf49e335f9e7e6f9465a6bf99
acknowledgedhttps://github.com/gitpython-developers/GitPython/issues?q=state%3Aopen%20label%3A%22acknowledged%22
help wantedhttps://github.com/gitpython-developers/GitPython/issues?q=state%3Aopen%20label%3A%22help%20wanted%22
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.