Skip to content

Commit

Permalink
Cleanup no longer needed monkey patches.
Browse files Browse the repository at this point in the history
Forward port cgi module for use in Python 3.13.
  • Loading branch information
rtibbles committed Oct 19, 2024
1 parent da20643 commit 1d30659
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 67 deletions.
75 changes: 8 additions & 67 deletions tests/testapp/testapp/__init__.py
Original file line number Diff line number Diff line change
@@ -1,76 +1,17 @@
import sys


def monkey_patch_collections():
def forward_port_cgi_module():
"""
Monkey-patching for the collections module is required for Python 3.10
and above.
Prior to 3.10, the collections module still contained all the entities defined in
collections.abc from Python 3.3 onwards. Here we patch those back into main
collections module.
This can be removed when we upgrade to a version of Django that is Python 3.10 compatible.
Copied from:
https://github.com/learningequality/kolibri/blob/589dd15aa79e8694aff8754bb34f12384315dbb6/kolibri/utils/compat.py#L90
Forward ports the required parts of the removed cgi module.
This can be removed when we upgrade to a version of Django that is Python 3.13 compatible.
"""
if sys.version_info < (3, 10):
if sys.version_info < (3, 13):
return
import collections
from collections import abc
from importlib import import_module

for name in dir(abc):
if not hasattr(collections, name):
setattr(collections, name, getattr(abc, name))
module = import_module("testapp.cgi")
sys.modules["cgi"] = module


monkey_patch_collections()


def monkey_patch_translation():
"""
Monkey-patching for the gettext module is required for Python 3.11
and above.
Prior to 3.11, the gettext module classes still had the deprecated set_output_charset
This can be removed when we upgrade to a version of Django that no longer relies
on this deprecated Python 2.7 only call.
Copied from:
https://github.com/learningequality/kolibri/blob/589dd15aa79e8694aff8754bb34f12384315dbb6/kolibri/utils/compat.py#L109
"""
if sys.version_info < (3, 11):
return

import gettext

def set_output_charset(*args, **kwargs):
pass

gettext.NullTranslations.set_output_charset = set_output_charset

original_translation = gettext.translation

def translation(
domain,
localedir=None,
languages=None,
class_=None,
fallback=False,
codeset=None,
):
return original_translation(
domain,
localedir=localedir,
languages=languages,
class_=class_,
fallback=fallback,
)

gettext.translation = translation

original_install = gettext.install

def install(domain, localedir=None, codeset=None, names=None):
return original_install(domain, localedir=localedir, names=names)

gettext.install = install


monkey_patch_translation()
forward_port_cgi_module()
47 changes: 47 additions & 0 deletions tests/testapp/testapp/cgi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
"""
A minimal port of the removed cgi module for use in Python 3.13.
Only imports the specific parts of the module that are used by Django.
Informed by the PR that removed its use in Django:
https://github.com/django/django/pull/15679
"""
from django.utils.regex_helper import _lazy_re_compile


def _parseparam(s):
while s[:1] == ";":
s = s[1:]
end = s.find(";")
while end > 0 and (s.count('"', 0, end) - s.count('\\"', 0, end)) % 2:
end = s.find(";", end + 1)
if end < 0:
end = len(s)
f = s[:end]
yield f.strip()
s = s[end:]


def parse_header(line):
"""
Parse a Content-type like header.
Return the main content-type and a dictionary of options.
"""
parts = _parseparam(";" + line)
key = parts.__next__()
pdict = {}
for p in parts:
i = p.find("=")
if i >= 0:
name = p[:i].strip().lower()
value = p[i + 1 :].strip()
if len(value) >= 2 and value[0] == value[-1] == '"':
value = value[1:-1]
value = value.replace("\\\\", "\\").replace('\\"', '"')
pdict[name] = value
return key, pdict


boundary_re = _lazy_re_compile(rb"[ -~]{0,200}[!-~]")


def valid_boundary(boundary):
return boundary_re.fullmatch(boundary) is not None

0 comments on commit 1d30659

Please sign in to comment.