13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
17
"""Test the lazy_import functionality."""
153
def test_setattr_replaces(self):
154
"""ScopeReplacer can create an instance in local scope.
156
An object should appear in globals() by constructing a ScopeReplacer,
157
and it will be replaced with the real object upon the first request.
160
TestClass.use_actions(actions)
161
def factory(replacer, scope, name):
166
# test_obj6 shouldn't exist yet
169
self.fail('test_obj6 was not supposed to exist yet')
171
orig_globals = set(globals().keys())
173
lazy_import.ScopeReplacer(scope=globals(), name='test_obj6',
176
new_globals = set(globals().keys())
178
# We can't use isinstance() because that uses test_obj6.__class__
179
# and that goes through __getattribute__ which would activate
181
self.assertEqual(lazy_import.ScopeReplacer,
182
object.__getattribute__(test_obj6, '__class__'))
183
test_obj6.bar = 'test'
184
self.assertNotEqual(lazy_import.ScopeReplacer,
185
object.__getattribute__(test_obj6, '__class__'))
186
self.assertEqual('test', test_obj6.bar)
153
188
def test_replace_side_effects(self):
154
189
"""Creating a new object should only create one entry in globals.
316
351
object.__getattribute__(test_obj2, '__class__'))
317
352
self.assertEqual(InstrumentedReplacer,
318
353
object.__getattribute__(test_obj3, '__class__'))
320
355
# The first use of the alternate variable causes test_obj2 to
322
357
self.assertEqual('foo', test_obj3.foo(1))
334
369
# because only now are we able to detect the problem.
335
370
self.assertRaises(errors.IllegalUseOfScopeReplacer,
336
371
getattr, test_obj3, 'foo')
338
373
self.assertEqual([('__getattribute__', 'foo'),