Gary Oberbrunner avatar Gary Oberbrunner committed 9d7239b Merge

Merge mortoray/SCons fix for #2821, copy Builders when env is cloned.

Comments (0)

Files changed (4)

 
   From Mortoray:
     - Make -s (silent mode) be silent about entering subdirs (#2976).
+    - Fix cloning of builders when cloning environment (#2821).
 
   From Gary Oberbrunner:
     - Show valid Visual Studio architectures in error message

src/engine/SCons/Environment.py

 CalculatorArgs = {}
 
 semi_deepcopy = SCons.Util.semi_deepcopy
+semi_deepcopy_dict = SCons.Util.semi_deepcopy_dict
 
 # Pull UserError into the global name space for the benefit of
 # Environment().SourceSignatures(), which has some import statements
         UserDict.__init__(self, dict)
 
     def __semi_deepcopy__(self):
-        return self.__class__(self.data, self.env)
+        # These cannot be copied since they would both modify the same builder object, and indeed
+        # just copying would modify the original builder
+        raise TypeError( 'cannot semi_deepcopy a BuilderDict' )
 
     def __setitem__(self, item, val):
         try:
         (like a function).  There are no references to any mutable
         objects in the original Environment.
         """
-        clone = copy.copy(self)
-        clone._dict = semi_deepcopy(self._dict)
-
         try:
-            cbd = clone._dict['BUILDERS']
+            builders = self._dict['BUILDERS']
         except KeyError:
             pass
-        else:
-            clone._dict['BUILDERS'] = BuilderDict(cbd, clone)
+            
+        clone = copy.copy(self)
+        # BUILDERS is not safe to do a simple copy
+        clone._dict = semi_deepcopy_dict(self._dict, ['BUILDERS'])
+        clone._dict['BUILDERS'] = BuilderDict(builders, clone)
 
         # Check the methods added via AddMethod() and re-bind them to
         # the cloned environment.  Only do this if the attribute hasn't
         except KeyError:
             pass
         else:
-            kwbd = semi_deepcopy(kwbd)
+            kwbd = BuilderDict(kwbd,self)
             del kw['BUILDERS']
             self.__setitem__('BUILDERS', kwbd)
         kw = copy_non_reserved_keywords(kw)

src/engine/SCons/Util.py

 # references to anything else it finds.
 #
 # A special case is any object that has a __semi_deepcopy__() method,
-# which we invoke to create the copy, which is used by the BuilderDict
-# class because of its extra initialization argument.
+# which we invoke to create the copy. Currently only used by
+# BuilderDict to actually prevent the copy operation (as invalid on that object)
 #
 # The dispatch table approach used here is a direct rip-off from the
 # normal Python copy module.
 
 _semi_deepcopy_dispatch = d = {}
 
-def _semi_deepcopy_dict(x):
+def semi_deepcopy_dict(x, exclude = [] ):
     copy = {}
     for key, val in x.items():
         # The regular Python copy.deepcopy() also deepcopies the key,
         #    copy[semi_deepcopy(key)] = semi_deepcopy(val)
         #
         # Doesn't seem like we need to, but we'll comment it just in case.
-        copy[key] = semi_deepcopy(val)
+        if key not in exclude:
+            copy[key] = semi_deepcopy(val)
     return copy
-d[dict] = _semi_deepcopy_dict
+d[dict] = semi_deepcopy_dict
 
 def _semi_deepcopy_list(x):
     return list(map(semi_deepcopy, x))
         if hasattr(x, '__semi_deepcopy__') and callable(x.__semi_deepcopy__):
             return x.__semi_deepcopy__()
         elif isinstance(x, UserDict):
-            return x.__class__(_semi_deepcopy_dict(x))
+            return x.__class__(semi_deepcopy_dict(x))
         elif isinstance(x, UserList):
             return x.__class__(_semi_deepcopy_list(x))
         
         return x
 
 
-
 class Proxy(object):
     """A simple generic Proxy class, forwarding all calls to
     subject.  So, for the benefit of the python newbie, what does

test/issue2821.py

+#!/usr/bin/env python
+#
+# __COPYRIGHT__
+#
+# Permission is hereby granted, free of charge, to any person obtaining
+# a copy of this software and associated documentation files (the
+# "Software"), to deal in the Software without restriction, including
+# without limitation the rights to use, copy, modify, merge, publish,
+# distribute, sublicense, and/or sell copies of the Software, and to
+# permit persons to whom the Software is furnished to do so, subject to
+# the following conditions:
+#
+# The above copyright notice and this permission notice shall be included
+# in all copies or substantial portions of the Software.
+#
+# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
+# KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
+# WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+#
+
+__revision__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__"
+
+import TestSCons
+
+test = TestSCons.TestSCons()
+
+test.write('SConstruct', """
+test = Environment()
+def blub():
+    pass
+
+test.Library.__call__ = blub
+test.Clone()
+assert test.Library.__call__ == blub
+""")
+
+test.run()
+
+test.pass_test()
+ 
+
+# Local Variables:
+# tab-width:4
+# indent-tabs-mode:nil
+# End:
+# vim: set expandtab tabstop=4 shiftwidth=4:
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.