~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_lazy_import.py

  • Committer: Aaron Bentley
  • Date: 2007-11-09 01:55:37 UTC
  • mto: This revision was merged to the branch mainline in revision 2976.
  • Revision ID: aaron.bentley@utoronto.ca-20071109015537-q83rlmzstfggjebb
Fix exception when ScopeReplacer is assigned to before retrieving any members

Show diffs side-by-side

added added

removed removed

Lines of Context:
150
150
                          ('foo', 2),
151
151
                         ], actions)
152
152
 
 
153
    def test_setattr_replaces(self):
 
154
        """ScopeReplacer can create an instance in local scope.
 
155
 
 
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.
 
158
        """
 
159
        def factory(replacer, scope, name):
 
160
            return TestClass()
 
161
        try:
 
162
            test_obj6
 
163
        except NameError:
 
164
            # test_obj6 shouldn't exist yet
 
165
            pass
 
166
        else:
 
167
            self.fail('test_obj6 was not supposed to exist yet')
 
168
 
 
169
        orig_globals = set(globals().keys())
 
170
 
 
171
        lazy_import.ScopeReplacer(scope=globals(), name='test_obj6',
 
172
                                  factory=factory)
 
173
 
 
174
        new_globals = set(globals().keys())
 
175
 
 
176
        # We can't use isinstance() because that uses test_obj6.__class__
 
177
        # and that goes through __getattribute__ which would activate
 
178
        # the replacement
 
179
        self.assertEqual(lazy_import.ScopeReplacer,
 
180
                         object.__getattribute__(test_obj6, '__class__'))
 
181
        test_obj6.bar = 'test'
 
182
        self.assertNotEqual(lazy_import.ScopeReplacer,
 
183
                            object.__getattribute__(test_obj6, '__class__'))
 
184
 
153
185
    def test_replace_side_effects(self):
154
186
        """Creating a new object should only create one entry in globals.
155
187