summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/CHANGES.txt6
-rw-r--r--src/engine/SCons/Script/Main.py16
-rw-r--r--test/site_scons/site_init.py54
3 files changed, 70 insertions, 6 deletions
diff --git a/src/CHANGES.txt b/src/CHANGES.txt
index c63f206..a9d4372 100644
--- a/src/CHANGES.txt
+++ b/src/CHANGES.txt
@@ -7,6 +7,12 @@
RELEASE 2.1.0.alpha.yyyymmdd - NEW DATE WILL BE INSERTED HERE
+ From Arve Knudsen:
+
+ - Set module metadata when loading site_scons/site_init.py
+ so it is treated as a proper module; __doc__, __file__ and
+ __name__ now refer to the site_init.py file.
+
From Russel Winder:
- Users Guide updates explaining that Tools can be packages as
diff --git a/src/engine/SCons/Script/Main.py b/src/engine/SCons/Script/Main.py
index eed782e..b4ebb09 100644
--- a/src/engine/SCons/Script/Main.py
+++ b/src/engine/SCons/Script/Main.py
@@ -688,7 +688,7 @@ def _load_site_scons_dir(topdir, site_dir_name=None):
site_init_file = os.path.join(site_dir, site_init_filename)
site_tools_dir = os.path.join(site_dir, site_tools_dirname)
if os.path.exists(site_init_file):
- import imp
+ import imp, re
# TODO(2.4): turn this into try:-except:-finally:
try:
try:
@@ -707,14 +707,26 @@ def _load_site_scons_dir(topdir, site_dir_name=None):
fmt = 'cannot import site_init.py: missing SCons.Script module %s'
raise SCons.Errors.InternalError(fmt % repr(e))
try:
+ sfx = description[0]
+ modname = os.path.basename(pathname)[:-len(sfx)]
+ site_m = {"__file__": pathname, "__name__": modname, "__doc__": None}
+ re_special = re.compile("__[^_]+__")
+ for k in m.__dict__.keys():
+ if not re_special.match(k):
+ site_m[k] = m.__dict__[k]
+
# This is the magic.
- exec fp in m.__dict__
+ exec fp in site_m
except KeyboardInterrupt:
raise
except Exception, e:
fmt = '*** Error loading site_init file %s:\n'
sys.stderr.write(fmt % repr(site_init_file))
raise
+ else:
+ for k in site_m:
+ if not re_special.match(k):
+ m.__dict__[k] = site_m[k]
except KeyboardInterrupt:
raise
except ImportError, e:
diff --git a/test/site_scons/site_init.py b/test/site_scons/site_init.py
index 83c9a7f..9f2e411 100644
--- a/test/site_scons/site_init.py
+++ b/test/site_scons/site_init.py
@@ -24,18 +24,64 @@
__revision__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__"
+"""
+Verify various aspects of the handling of site_init.py.
+
+And more ..
+"""
+
import TestSCons
import sys
+import os.path
+
+test = TestSCons.TestSCons()
+
+test.subdir('site_scons')
+
+
+def _test_metadata():
+ """Test site_init's module metadata.
+
+ The following special variables should be predefined: __doc__,
+ __file__ and __name__. No special variables should be transferred from
+ SCons.Script.
+ """
+ test.write(['site_scons', 'site_init.py'], """\
+import os.path
+import re
+
+special = []
+for x in globals().keys():
+ if re.match("__[^_]+__", x):
+ if x in ("__builtins__", "__package__",):
+ # Ignore certain keywords, as they are known to be added by Python
+ continue
+ special.append(x)
+
+print sorted(special)
+print __doc__
+print os.path.realpath(__file__)
+print __name__
+""")
+
+ test.write('SConstruct', "\n")
+ test.run(arguments = '-Q .',
+ stdout = """\
+['__doc__', '__file__', '__name__']
+None
+%s
+site_init
+scons: `.' is up to date.
+""" % (os.path.realpath(os.path.join('site_scons', 'site_init.py')),))
+
+_test_metadata()
+
"""
Verify site_scons/site_init.py file can define a tool, and it shows up
automatically in the SCons.Script namespace.
"""
-test = TestSCons.TestSCons()
-
-test.subdir('site_scons')
-
if sys.platform == 'win32':
cat_cmd='type'
else: