Skip to content

Commit

Permalink
Drop Python2 support from Main.py site_dir code
Browse files Browse the repository at this point in the history
Also simplify cleanup a bit - we don't need to leave the site file open,
can use a context manager to read the code and let it close there.

Signed-off-by: Mats Wichmann <mats@linux.com>
  • Loading branch information
mwichmann committed Feb 11, 2020
1 parent 4f5bb72 commit 82df077
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 37 deletions.
4 changes: 1 addition & 3 deletions src/CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ RELEASE VERSION/DATE TO BE FILLED IN LATER
- Accommodate VS 2017 Express - it's got a more liberal license then VS
Community, so some people prefer it (from 2019, no more Express)
- vswhere call should also now work even if programs aren't on the C: drive.
- Script/Main.py now uses importlib instead of imp module.


RELEASE 3.1.2 - Mon, 17 Dec 2019 02:06:27 +0000
Expand Down Expand Up @@ -150,9 +151,6 @@ RELEASE 3.1.1 - Mon, 07 Aug 2019 20:09:12 -0500
- JSON encoding errors for CacheDir config
- JSON decoding errors for CacheDir config

From Mats Wichmann:
- Script/Main.py now uses importlib in the PY3 case.


RELEASE 3.1.0 - Mon, 20 Jul 2019 16:59:23 -0700

Expand Down
52 changes: 18 additions & 34 deletions src/engine/SCons/Script/Main.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@

import SCons.compat

import importlib.util
import os
import re
import sys
import time
import traceback
Expand All @@ -64,7 +66,6 @@
import SCons.Script
import SCons.Taskmaster
import SCons.Util
from SCons.Util import PY3
import SCons.Warnings
import SCons.Script.Interactive

Expand Down Expand Up @@ -730,12 +731,6 @@ def _load_site_scons_dir(topdir, site_dir_name=None):
if not os.path.exists(site_init_file):
return

if PY3:
import importlib.util
else:
import imp
import re

# "import" the site_init.py file into the SCons.Script namespace.
# This is a variant on the basic Python import flow in that the globals
# dict for the compile step is prepopulated from the SCons.Script
Expand All @@ -749,52 +744,41 @@ def _load_site_scons_dir(topdir, site_dir_name=None):
fmt = 'cannot import {}: missing SCons.Script module'
raise SCons.Errors.InternalError(fmt.format(site_init_file))

if PY3:
spec = importlib.util.spec_from_file_location(site_init_modname,
site_init_file)
fp = open(spec.origin, 'r')
site_m = {"__file__": spec.origin,
"__name__": spec.name,
"__doc__": None}
else:
fp, pathname, description = imp.find_module(site_init_modname,
[site_dir])
sfx = description[0]
modname = os.path.basename(pathname)[:-len(sfx)]
site_m = {"__file__": pathname,
"__name__": modname,
"__doc__": None}

spec = importlib.util.spec_from_file_location(site_init_modname, site_init_file)
site_m = {
"__file__": spec.origin,
"__name__": spec.name,
"__doc__": None,
}
re_dunder = re.compile(r"__[^_]+__")
# update site dict with all but magic (dunder) methods
for k, v in m.__dict__.items():
if not re_dunder.match(k):
site_m[k] = v

with open(spec.origin, 'r') as f:
code = f.read()
try:
codeobj = compile(fp.read(), fp.name, 'exec')
codeobj = compile(code, spec.name, "exec")
exec(codeobj, site_m)
except KeyboardInterrupt:
raise
except Exception:
fmt = '*** Error loading site_init file %s:\n'
sys.stderr.write(fmt % repr(site_init_file))
fmt = "*** Error loading site_init file {}:\n"
sys.stderr.write(fmt.format(site_init_file))
raise
else:
# now refill globals with site_init's symbols
for k, v in site_m.items():
if not re_dunder.match(k):
m.__dict__[k] = v

except KeyboardInterrupt:
raise
except Exception:
fmt = '*** cannot import site init file %s:\n'
sys.stderr.write(fmt % repr(site_init_file))
fmt = "*** cannot import site init file {}:\n"
sys.stderr.write(fmt.format(site_init_file))
raise
finally:
try:
fp.close()
except Exception:
pass


def _load_all_site_scons_dirs(topdir, verbose=None):
"""Load all of the predefined site_scons dir.
Expand Down

0 comments on commit 82df077

Please sign in to comment.