-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #233 from rtibbles/python3.13
Add support and testing for Python 3.13
- Loading branch information
Showing
5 changed files
with
61 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters