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

Fixed ClientSession initialization warning #1518

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ CHANGES

- Allow users to specify what should happen to decoding errors when calling a responses `text()` method #1542

- Fix ClientSession is not aware of TestClient #1499

- Fixed warnings showing when giving ClientSession an event loop when called from a normal function. #[1468](https://github.com/KeepSafe/aiohttp/pull/1468#issuecomment-269112177)

-


1.2.0 (2016-12-17)
------------------
Expand Down
1 change: 1 addition & 0 deletions CONTRIBUTORS.txt
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ Raúl Cumplido
"Required Field" <[email protected]>
Robert Lu
Samuel Colvin
Sean Hunt <[email protected]>
Sebastian Hanula
Sebastian Hüther
SeongSoo Cho
Expand Down
57 changes: 35 additions & 22 deletions aiohttp/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,29 +41,42 @@ def __init__(self, *, connector=None, loop=None, cookies=None,
version=aiohttp.HttpVersion11,
cookie_jar=None):

if connector is None:
connector = aiohttp.TCPConnector(loop=loop)
loop = connector._loop # never None
else:
if loop is None:
self._set_explicit_loop = False
try:
if connector is None:
connector = aiohttp.TCPConnector(loop=loop)
loop = connector._loop # never None
elif connector._loop is not loop:
raise ValueError("loop argument must agree with connector")

self._loop = loop
if loop.get_debug():
self._source_traceback = traceback.extract_stack(sys._getframe(1))

if not loop.is_running():
warnings.warn("Creating a client session outside of coroutine is "
"a very dangerous idea", ResourceWarning,
stacklevel=2)
context = {'client_session': self,
'message': 'Creating a client session outside '
'of coroutine'}
if self._source_traceback is not None:
context['source_traceback'] = self._source_traceback
loop.call_exception_handler(context)
self._set_explicit_loop = True
else:
if loop is None:
loop = connector._loop # never None
self._set_explicit_loop = True
elif connector._loop is not loop:
raise ValueError(
"loop argument must agree with connector")

self._loop = loop
if loop.get_debug():
self._source_traceback = traceback.extract_stack(
sys._getframe(1))
except RuntimeError:
# We know for sure now that there was no event loop specified.
# TODO: throw a warning now here to but the warning must not use
# the exception handler from event loops as there is None.
self._set_explicit_loop = False # for now RIP.

if self._set_explicit_loop:
if not loop.is_running():
warnings.warn(
"Creating a client session outside of coroutine is "
"a very dangerous idea", ResourceWarning,
stacklevel=2)
context = {'client_session': self,
'message': 'Creating a client session outside '
'of coroutine'}
if self._source_traceback is not None:
context['source_traceback'] = self._source_traceback
loop.call_exception_handler(context)

if cookie_jar is None:
cookie_jar = CookieJar(loop=loop)
Expand Down
12 changes: 9 additions & 3 deletions tests/test_client_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,12 @@ def test_proxy_str(session, params):


def test_create_session_outside_of_coroutine(loop):
with pytest.warns(ResourceWarning):
sess = ClientSession(loop=loop)
sess.close()
sess = None # *sigh* to avoid UnboundLocalError for now.
try:
sess = ClientSession()
except RuntimeError:
pass
try:
sess.close()
except AttributeError:
pass