Skip to content

Replace removed cgi module in main.py (Python 3.13) - #916

Open
kasparovabi wants to merge 1 commit into
ronreiter:masterfrom
kasparovabi:fix/py313-cgi-import
Open

kasparovabi wants to merge 1 commit into
ronreiter:masterfrom
kasparovabi:fix/py313-cgi-import

Conversation

@kasparovabi

Copy link
Copy Markdown

main.py imports cgi at the top. Python 3.13 removed that module (PEP 594, What's New in 3.13), and the Dockerfile base image python:3-slim-bullseye currently resolves to Python 3.13.6. So make run (docker-compose, python main.py) and gunicorn main:app stop before Flask starts:

ModuleNotFoundError: No module named 'cgi'

The only use is cgi.escape(tutorial_id) in get_tutorial(). cgi.escape itself was removed back in 3.8, so that line could not run on any supported version either. html.escape(s, quote=False) escapes the same three characters (&, <, >) and leaves quotes alone, which is what cgi.escape(s) did by default. On python:3.7-slim the two returned identical strings for four inputs, including <script>, quotes and an existing &amp;.

I loaded the app with Flask's test client in Docker with no network, requesting /, /en/Hello,_World!, /en/Variables_and_Types and /about with Host: www.learnpython.org.

Python 3.12 (bookworm) Python 3.13.6 (python:3-slim-bullseye)
before 4 x 200 ModuleNotFoundError: No module named 'cgi'
after 4 x 200 4 x 200

The four response bodies after the change on 3.13 have the same SHA-256 as the unchanged code on 3.12. I did not run tests/ and did not try the ideone code runner, which needs credentials.

Happy to adjust this or close it if it does not fit the project.

cgi was removed from the standard library in Python 3.13 (PEP 594), so
main.py stopped at import with ModuleNotFoundError. The Dockerfile base
python:3-slim-bullseye resolves to 3.13, which makes 'make run' fail.

The only use was cgi.escape(), which was already gone since 3.8.
html.escape(..., quote=False) escapes the same three characters.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant