Skip to content

Commit

Permalink
Add base.handlers.get_current_request()
Browse files Browse the repository at this point in the history
This function allows to retrieve the current web request in async
contexts.

Retrieving the current context can be useful for example when
customizing Enterprise Gateway RemoteProcessProxy, to retrieve
authentication headers.

The implementation makes uses of Python 3.7's new contextvars module.
There are backports efforts for 3.6:

- https://pypi.org/project/aiocontextvars/ - does not have automatic
  asyncio support

- https://pypi.org/project/contextvars/ - which doesn't have it either.
  The tracking issue on this matter has been stale for 3 years now, and
  the conclusion of the discussion is "upgrade to a newer version of
  Python": MagicStack/contextvars#2

Since notebook has no control on the event loop it is running into, I
preferred to drop Python 3.6 support.
  • Loading branch information
flupke committed Apr 22, 2021
1 parent a5ace13 commit c57a166
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 8 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
fail-fast: false
matrix:
os: [ubuntu]
python-version: [ '3.6' ]
python-version: [ '3.7' ]
steps:
- name: Checkout
uses: actions/checkout@v1
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/python-nbconvert.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
strategy:
fail-fast: false
matrix:
python-version: [ '3.6' , '3.7', '3.8', '3.9' ]
python-version: [ '3.7', '3.8', '3.9' ]
steps:
- name: Checkout
uses: actions/checkout@v1
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/python.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
fail-fast: false
matrix:
os: [ubuntu, macos, windows]
python-version: [ '3.6' , '3.7', '3.8', '3.9' ] # Windows 3.9 fails due to the pywinpty dependency not working (Issue #5967)
python-version: [ '3.7', '3.8', '3.9' ] # Windows 3.9 fails due to the pywinpty dependency not working (Issue #5967)
steps:
- name: Checkout
uses: actions/checkout@v1
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/selenium.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
fail-fast: false
matrix:
os: [ubuntu, macos]
python-version: [ '3.6', '3.7', '3.8', '3.9' ]
python-version: [ '3.7', '3.8', '3.9' ]
steps:
- name: Checkout
uses: actions/checkout@v2
Expand Down
15 changes: 15 additions & 0 deletions notebook/base/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import traceback
import types
import warnings
import contextvars
from http.client import responses
from http.cookies import Morsel

Expand All @@ -35,6 +36,10 @@
from notebook.utils import is_hidden, url_path_join, url_is_absolute, url_escape, urldecode_unix_socket_path
from notebook.services.security import csp_report_uri


_current_request_var: contextvars.ContextVar = contextvars.ContextVar("current_request")


#-----------------------------------------------------------------------------
# Top-level handlers
#-----------------------------------------------------------------------------
Expand All @@ -56,6 +61,9 @@ def log():
class AuthenticatedHandler(web.RequestHandler):
"""A RequestHandler with an authenticated user."""

def prepare(self):
_current_request_var.set(self.request)

@property
def content_security_policy(self):
"""The default Content-Security-Policy header
Expand Down Expand Up @@ -932,6 +940,13 @@ def get(self):
self.write(prometheus_client.generate_latest(prometheus_client.REGISTRY))


def get_current_request():
"""
Get :class:`tornado.httputil.HTTPServerRequest` that is currently being processed.
"""
return _current_request_var.get(None)


#-----------------------------------------------------------------------------
# URL pattern fragments for re-use
#-----------------------------------------------------------------------------
Expand Down
8 changes: 4 additions & 4 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

name = "notebook"

if sys.version_info < (3, 6):
if sys.version_info < (3, 7):
pip_message = 'This may be due to an out of date pip. Make sure you have pip >= 9.0.1.'
try:
import pip
Expand All @@ -31,7 +31,8 @@


error = """
Notebook 6.3+ supports Python 3.6 and above.
Notebook 6.4+ supports Python 3.7 and above.
When using Python 3.6, please install Notebook < 6.4.
When using Python 3.5, please install Notebook <= 6.2.
When using Python 3.4 or earlier (including 2.7), please install Notebook 5.x.
Expand Down Expand Up @@ -99,7 +100,6 @@
'License :: OSI Approved :: BSD License',
'Programming Language :: Python',
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.6',
'Programming Language :: Python :: 3.7',
'Programming Language :: Python :: 3.8',
'Programming Language :: Python :: 3.9'
Expand Down Expand Up @@ -131,7 +131,7 @@
'test:sys_platform != "win32"': ['requests-unixsocket'],
'json-logging': ['json-logging']
},
python_requires = '>=3.6',
python_requires = '>=3.7',
entry_points = {
'console_scripts': [
'jupyter-notebook = notebook.notebookapp:main',
Expand Down

0 comments on commit c57a166

Please sign in to comment.