Skip to content

Commit

Permalink
Monkey patch collections module to support Python 3.10.
Browse files Browse the repository at this point in the history
  • Loading branch information
rtibbles committed May 2, 2022
1 parent afd998a commit 17eb42f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
19 changes: 19 additions & 0 deletions kolibri/utils/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,22 @@ def parse_version(v):
parsed = _parse_version(v)

return VersionCompat(parsed)


def monkey_patch_collections():
"""
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.
"""
if sys.version_info < (3, 10):
return
import collections
from collections import abc

for name in dir(abc):
if not hasattr(collections, name):
setattr(collections, name, getattr(abc, name))
3 changes: 3 additions & 0 deletions kolibri/utils/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
ColoredFormatter = None

from .logger import LOG_COLORS
from kolibri.utils.compat import monkey_patch_collections


logging.basicConfig(format="%(levelname)s: %(message)s", level=logging.INFO)
Expand Down Expand Up @@ -124,6 +125,8 @@ def set_env():

check_python_versions()

monkey_patch_collections()

sys.path = [os.path.realpath(os.path.dirname(kolibri_dist.__file__))] + sys.path

# Add path for c extensions to sys.path
Expand Down

0 comments on commit 17eb42f

Please sign in to comment.