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 #1586

Closed
wants to merge 1 commit 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
3 changes: 3 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ CHANGES

- Log warning instead of RuntimeError is websocket connection is closed.

- 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 @@ -121,6 +121,7 @@ Raúl Cumplido
"Required Field" <requiredfield256@gmail.com>
Robert Lu
Samuel Colvin
Sean Hunt <seandhunt_7@yahoo.com>
Sebastian Hanula
Sebastian Hüther
SeongSoo Cho
Expand Down
62 changes: 40 additions & 22 deletions aiohttp/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,29 +57,47 @@ def __init__(self, *, connector=None, loop=None, cookies=None,
version=aiohttp.HttpVersion11,
cookie_jar=None, read_timeout=None, time_service=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
self._no_event_loop_possible = 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, AssertionError):
self._set_explicit_loop = False
self._no_event_loop_possible = True

if self._set_explicit_loop and not self._no_event_loop_possible:
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)
elif self._no_event_loop_possible:
raise RuntimeError(
"Could not create a client session when"
"running in the Main Thread. Please try"
" to get an event loop and then try to"
" create a client session with that.")

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
try:
sess = ClientSession()
except RuntimeError:
pass
try:
sess.close()
except AttributeError:
pass