Skip to content

Commit

Permalink
Replace the usage of deprecated imp module in order to work with Py…
Browse files Browse the repository at this point in the history
…thon 3.12.

Fixes: google#24.
PiperOrigin-RevId: 641152140
Change-Id: Ide2d88b5fdbe804617495df129176c3692033d45
  • Loading branch information
marcenacp committed Jun 11, 2024
1 parent e5290de commit 76029a3
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions ml_collections/config_flags/config_flags.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,20 @@
import enum
import errno
import functools as ft
import imp
import importlib.machinery
import os
import re
import sys
import traceback
import types
from typing import Any, Callable, Dict, Generic, List, MutableMapping, Optional, Sequence, Tuple, Type, TypeVar

from absl import flags
from absl import logging
from ml_collections import config_dict
from ml_collections.config_flags import config_path
from ml_collections.config_flags import tuple_parser

FLAGS = flags.FLAGS

# Forward for backwards compatibility.
Expand All @@ -43,6 +45,20 @@
flags._helpers.disclaim_module_ids.add(id(sys.modules[__name__])) # pylint: disable=protected-access


def _load_source(module_name: str, module_path: str) -> types.ModuleType:
"""Loads a Python module from its source file.
Args:
module_name: name of the module in sys.modules.
module_path: path to the Python file containing the module.
Returns:
The loaded Python module.
"""
loader = importlib.machinery.SourceFileLoader(module_name, module_path)
return loader.load_module()


class _LiteralParser(flags.ArgumentParser):
"""Parse arbitrary built-in (`--cfg.val=1`, `--cfg.val="[1, 2, {}]"`,...)."""

Expand Down Expand Up @@ -560,7 +576,7 @@ def _LoadConfigModule(name: str, path: str):

# Works for relative paths.
with ignoring_errors.Attempt('Relative path', path):
config_module = imp.load_source(name, path)
config_module = _load_source(name, path)
return config_module

# Nothing worked. Log the paths that were attempted.
Expand Down

0 comments on commit 76029a3

Please sign in to comment.