Bug#1063782: python-oauth2client: FTBFS with flask 3

2024-02-16 Thread Olivier Gayot
Package: python-oauth2client
Followup-For: Bug #1063782
User: ubuntu-de...@lists.ubuntu.com
Usertags: origin-ubuntu noble ubuntu-patch
Control: tags -1 patch

Dear Maintainer,

Flask 3.0 dropped support for _app_ctx_stack and _request_ctx_stack.
Those attributes were deprecated since Flask 2.2.

In Ubuntu, the attached patch was applied to achieve the following:

  * Replace use of flask._app_ctx_stack (dropped from flask 3) by flask.g to
fix FTBFS. (LP: #2052771)
 - debian/patches/flask3-use-g-instead-of-app_ctx_stack.patch


Thanks for considering the patch.


-- System Information:
Debian Release: trixie/sid
  APT prefers mantic-updates
  APT policy: (500, 'mantic-updates'), (500, 'mantic-security'), (500, 
'mantic'), (100, 'mantic-backports')
Architecture: amd64 (x86_64)
Foreign Architectures: i386

Kernel: Linux 6.1.0-16-generic (SMP w/8 CPU threads; PREEMPT)
Kernel taint flags: TAINT_PROPRIETARY_MODULE, TAINT_OOT_MODULE
Locale: LANG=en_US.UTF-8, LC_CTYPE=en_US.UTF-8 (charmap=UTF-8), LANGUAGE not set
Shell: /bin/sh linked to /usr/bin/dash
Init: systemd (via /run/systemd/system)
LSM: AppArmor: enabled
diff -Nru 
python-oauth2client-4.1.3/debian/patches/flask3-use-g-instead-of-app_ctx_stack.patch
 
python-oauth2client-4.1.3/debian/patches/flask3-use-g-instead-of-app_ctx_stack.patch
--- 
python-oauth2client-4.1.3/debian/patches/flask3-use-g-instead-of-app_ctx_stack.patch
1970-01-01 01:00:00.0 +0100
+++ 
python-oauth2client-4.1.3/debian/patches/flask3-use-g-instead-of-app_ctx_stack.patch
2024-02-16 10:51:37.0 +0100
@@ -0,0 +1,43 @@
+Description: Use flask.g instead of flask._app_ctx_stack
+ In flask 2.2, _app_ctx_stack and _request_ctx_stack got deprecated in favor of
+ using flask.g. The compatibility layer was dropped from flask 3, which now 
raises:
+   E ImportError: cannot import name '_app_ctx_stack' from 'flask'
+ Replaced by the use of flask.g as mentioned in the documentation.
+ .
+ The patch is not meant for upstream inclusion since the project upstream is
+ deprecated (in favor of python-google-auth) and the repository is read only.
+Author: Olivier Gayot 
+Bug-Ubuntu: https://launchpad.net/bugs/2052771
+Bug-Debian: https://bugs.debian.org/1063782
+Forwarded: not-needed
+Last-Update: 2024-02-16
+---
+This patch header follows DEP-3: http://dep.debian.net/deps/dep3/
+--- a/oauth2client/contrib/flask_util.py
 b/oauth2client/contrib/flask_util.py
+@@ -170,8 +170,8 @@
+ 
+ try:
+ from flask import Blueprint
+-from flask import _app_ctx_stack
+ from flask import current_app
++from flask import g as flask_g
+ from flask import redirect
+ from flask import request
+ from flask import session
+@@ -434,12 +434,10 @@
+ @property
+ def credentials(self):
+ """The credentials for the current user or None if unavailable."""
+-ctx = _app_ctx_stack.top
++if not hasattr(flask_g, _CREDENTIALS_KEY):
++flask_g.google_oauth2_credentials = self.storage.get()
+ 
+-if not hasattr(ctx, _CREDENTIALS_KEY):
+-ctx.google_oauth2_credentials = self.storage.get()
+-
+-return ctx.google_oauth2_credentials
++return flask_g.google_oauth2_credentials
+ 
+ def has_credentials(self):
+ """Returns True if there are valid credentials for the current 
user."""
diff -Nru python-oauth2client-4.1.3/debian/patches/series 
python-oauth2client-4.1.3/debian/patches/series
--- python-oauth2client-4.1.3/debian/patches/series 2023-08-17 
18:22:35.0 +0200
+++ python-oauth2client-4.1.3/debian/patches/series 2024-02-16 
10:51:37.0 +0100
@@ -1,3 +1,5 @@
+flask3-use-g-instead-of-app_ctx_stack.patch
 skip-network-doing-unit-test.patch
 remove-broken-tests.patch
 fix-hmac.new-call-in-py3.8.patch


Bug#1063782: python-oauth2client: FTBFS with flask 3

2024-02-12 Thread Olivier Gayot
Source: python-oauth2client
Severity: important

Dear Maintainer,

Building (or running autopkgtest) for python-oauth2client against flask
3.0 leads to an error when running the test-suite:

  Traceback:
  oauth2client/contrib/flask_util.py:173: in 
  from flask import _app_ctx_stack
  E   ImportError: cannot import name '_app_ctx_stack' from 'flask' 
(/usr/lib/python3/dist-packages/flask/__init__.py)

With previous versions of flask, the same code would produce a warning
instead:

  oauth2client/contrib/flask_util.py:173: DeprecationWarning: '_app_ctx_stack' 
is deprecated and will be removed in Flask 2.3.

Flask decided to remove the deprecated _app_ctx_stack [1] in 3.0.0. The
recommended approach is to "use 'g' to store data instead."

Quote from Version 2.2.0 changelog [2]:

> The app and request contexts are managed using Python context vars directly 
> rather than Werkzeug’s LocalStack. This should result in better performance 
> and memory use. #4682
> 
> Extension maintainers, be aware that _app_ctx_stack.top and 
> _request_ctx_stack.top are deprecated. Store data on g instead using a unique 
> prefix, like g._extension_name_attr.
> 

Nowadays oauth2client is marked deprecated [3] and the upstream repository [4]
has been read only for four years.

NOTE: When building with python3.12, there are other errors but they have
already been reported as part of bug 1058392 [5]

Thank you

[1] https://github.com/pallets/flask/pull/5223
[2] https://flask.palletsprojects.com/en/2.3.x/changes/#version-2-2-0
[3] https://google-auth.readthedocs.io/en/latest/oauth2client-deprecation.html
[4] https://github.com/googleapis/oauth2client
[5] https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=1058392

-- System Information:
Debian Release: trixie/sid
  APT prefers mantic-updates
  APT policy: (500, 'mantic-updates'), (500, 'mantic-security'), (500, 
'mantic'), (100, 'mantic-backports')
Architecture: amd64 (x86_64)
Foreign Architectures: i386

Kernel: Linux 6.1.0-16-generic (SMP w/8 CPU threads; PREEMPT)
Kernel taint flags: TAINT_PROPRIETARY_MODULE, TAINT_OOT_MODULE
Locale: LANG=en_US.UTF-8, LC_CTYPE=en_US.UTF-8 (charmap=UTF-8), LANGUAGE not set
Shell: /bin/sh linked to /usr/bin/dash
Init: systemd (via /run/systemd/system)
LSM: AppArmor: enabled