Skip to content

Commit

Permalink
Make PyPISimple a context manager
Browse files Browse the repository at this point in the history
  • Loading branch information
jwodder committed Dec 13, 2020
1 parent 541d7aa commit a88729a
Show file tree
Hide file tree
Showing 6 changed files with 206 additions and 187 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
v0.8.0 (in development)
-----------------------
- Support Python 3.9
- `PyPISimple` is now usable as a context manager that will close the session
on exit

v0.7.0 (2020-10-15)
-------------------
Expand Down
4 changes: 2 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ Example
=======

>>> from pypi_simple import PyPISimple
>>> client = PyPISimple()
>>> requests_page = client.get_project_page('requests')
>>> with PyPISimple() as client:
... requests_page = client.get_project_page('requests')
>>> pkg = requests_page.packages[0]
>>> pkg
DistributionPackage(filename='requests-0.2.0.tar.gz', url='https://files.pythonhosted.org/packages/ba/bb/dfa0141a32d773c47e4dede1a617c59a23b74dd302e449cf85413fc96bc4/requests-0.2.0.tar.gz#sha256=813202ace4d9301a3c00740c700e012fb9f3f8c73ddcfe02ab558a8df6f175fd', project='requests', version='0.2.0', package_type='sdist', requires_python=None, has_sig=None, yanked=None)
Expand Down
2 changes: 2 additions & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ Changelog
v0.8.0 (in development)
-----------------------
- Support Python 3.9
- `PyPISimple` is now usable as a context manager that will close the session
on exit


v0.7.0 (2020-10-15)
Expand Down
4 changes: 2 additions & 2 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ Example
========

>>> from pypi_simple import PyPISimple
>>> client = PyPISimple()
>>> requests_page = client.get_project_page('requests')
>>> with PyPISimple() as client:
... requests_page = client.get_project_page('requests')
>>> pkg = requests_page.packages[0]
>>> pkg
DistributionPackage(filename='requests-0.2.0.tar.gz', url='https://files.pythonhosted.org/packages/ba/bb/dfa0141a32d773c47e4dede1a617c59a23b74dd302e449cf85413fc96bc4/requests-0.2.0.tar.gz#sha256=813202ace4d9301a3c00740c700e012fb9f3f8c73ddcfe02ab558a8df6f175fd', project='requests', version='0.2.0', package_type='sdist', requires_python=None, has_sig=None, yanked=None)
Expand Down
13 changes: 13 additions & 0 deletions src/pypi_simple/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ class PyPISimple:
caching), the user must create & configure a `requests.Session` object
appropriately and pass it to the constructor as the ``session`` parameter.
A `PyPISimple` instance can be used as a context manager that will
automatically close its session on exit, regardless of where the session
object came from.
.. versionchanged:: 0.8.0
Now usable as a context manager
.. versionchanged:: 0.5.0
``session`` argument added
Expand Down Expand Up @@ -64,6 +71,12 @@ def __init__(self, endpoint: str = PYPI_SIMPLE_ENDPOINT, auth: Any = None,
if auth is not None:
self.s.auth = auth

def __enter__(self) -> "PyPISimple":
return self

def __exit__(self, exc_type: Any, exc_value: Any, exc_tb: Any) -> None:
self.s.close()

def get_index_page(
self,
timeout: Union[float, Tuple[float,float], None] = None,
Expand Down

0 comments on commit a88729a

Please sign in to comment.