Skip to content

Commit

Permalink
Stop using imp python module. (#2011)
Browse files Browse the repository at this point in the history
imp module has been removed from Python 3.12

b/312559511
  • Loading branch information
isarkis authored Nov 23, 2023
1 parent ee2f99f commit c5a4b3c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
20 changes: 16 additions & 4 deletions starboard/build/platform_configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
# limitations under the License.
"""Base platform build configuration."""

import imp # pylint: disable=deprecated-module
import importlib.machinery
import importlib.util
import inspect
import logging
import os
Expand All @@ -32,8 +33,14 @@ def _GetApplicationConfigurationClass(application_path):
if not os.path.isfile(application_configuration_path):
return None

application_configuration = imp.load_source('application_configuration',
application_configuration_path)
loader = importlib.machinery.SourceFileLoader('application_configuration',
application_configuration_path)
spec = importlib.util.spec_from_file_location(
'application_configuration',
application_configuration_path,
loader=loader)
application_configuration = importlib.util.module_from_spec(spec)
loader.exec_module(application_configuration)
for name, cls in inspect.getmembers(application_configuration):
if not inspect.isclass(cls):
continue
Expand Down Expand Up @@ -137,7 +144,12 @@ def GetLauncher(self):
module_path = os.path.abspath(
os.path.join(self.GetLauncherPath(), 'launcher.py'))
try:
return imp.load_source('launcher', module_path)
loader = importlib.machinery.SourceFileLoader('launcher', module_path)
spec = importlib.util.spec_from_file_location(
'launcher', module_path, loader=loader)
launcher = importlib.util.module_from_spec(spec)
loader.exec_module(launcher)
return launcher
except (IOError, ImportError, RuntimeError) as error:
logging.error('Unable to load launcher module from %s.', module_path)
logging.error(error)
Expand Down
10 changes: 8 additions & 2 deletions starboard/tools/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@
#
"""Build related constants and helper functions."""

import imp # pylint: disable=deprecated-module
import importlib
import importlib.machinery
import importlib.util
import logging
import os
import sys
Expand Down Expand Up @@ -94,7 +95,12 @@ def _LoadPlatformModule(platform_name, file_name, function_name):
PLATFORMS[platform_name])
module_path = os.path.join(platform_path, file_name)
if not _ModuleLoaded('platform_module', module_path):
platform_module = imp.load_source('platform_module', module_path)
loader = importlib.machinery.SourceFileLoader('platform_module',
module_path)
spec = importlib.util.spec_from_file_location(
'platform_module', module_path, loader=loader)
platform_module = importlib.util.module_from_spec(spec)
loader.exec_module(platform_module)
else:
platform_module = sys.modules['platform_module']
else:
Expand Down

0 comments on commit c5a4b3c

Please sign in to comment.