Skip to content

Return Python enums from _pygit2 #1263

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

Merged
merged 3 commits into from
Jan 5, 2024
Merged

Return Python enums from _pygit2 #1263

merged 3 commits into from
Jan 5, 2024

Conversation

jorio
Copy link
Contributor

@jorio jorio commented Jan 3, 2024

Following up on #1251: C functions in _pygit2 now return actual Python enums instead of ints.

For example, Repository.status() would previously return simple ints. Now it returns FileStatus which transparently demotes to ints for legacy code:

>>> repo.status()
{'file1.txt': <FileStatus.CONFLICTED: 32768>,
 'file2.txt': <FileStatus.INDEX_MODIFIED|WT_MODIFIED: 258>}

Implementation details: References to the enums are cached via cache_modules so that lookups are faster. The references are decref'd in free_module (new module destructor function)

Affected functions:

  • Repository.status
  • Repository.status_file
  • Repository.merge_analysis
  • DiffFile.flags
  • DiffFile.mode
  • DiffDelta.flags
  • DiffDelta.status

Affected functions:
- Repository.status
- Repository.status_file
- Repository.merge_analysis
- DiffFile.flags
- DiffFile.mode
- DiffDelta.flags
- DiffDelta.status
Comment on lines 64 to 65
cache_enums()
del cache_enums # Don't expose this to user code
Copy link
Member

Choose a reason for hiding this comment

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

Maybe rename to __cache_enums() to emphasize that it's private?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

OK; in the latest commit, I had to import it explicitly from _pygit2 because from _pygit2 import * skips anything starting with an underscore.

Because the function now starts with an underscore, it won't be exposed to user code with from pygit2 import *, so I also removed the del cache_enums statement. (And if user code goes out of its way to call it anyway, the world won't stop spinning.)

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.

2 participants