~jelmer/meliae/hurd

« back to all changes in this revision

Viewing changes to meliae/tests/test__scanner.py

  • Committer: John Arbash Meinel
  • Date: 2010-08-10 16:21:52 UTC
  • mfrom: (183.1.6 special_cases)
  • Revision ID: john@arbash-meinel.com-20100810162152-kwinrlv78flsdox5
We now support custom sizeof methods registered at runtime.

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
import sys
19
19
import tempfile
20
20
import types
 
21
import zlib
21
22
 
22
23
from meliae import (
23
24
    _scanner,
170
171
        # back to the original size
171
172
        self.assertSizeOf(4, CustomSize(-1), has_gc=True)
172
173
 
 
174
    def test_size_of_special(self):
 
175
        class CustomWithoutSizeof(object):
 
176
            pass
 
177
        log = []
 
178
        def _size_32(obj):
 
179
            log.append(obj)
 
180
            return 800
 
181
        def _size_64(obj):
 
182
            log.append(obj)
 
183
            return 1600
 
184
            
 
185
        obj = CustomWithoutSizeof()
 
186
        self.assertSizeOf(4, obj)
 
187
        _scanner.add_special_size('CustomWithoutSizeof', _size_32, _size_64)
 
188
        try:
 
189
            self.assertSizeOf(200, obj)
 
190
        finally:
 
191
            _scanner.add_special_size('CustomWithoutSizeof', None, None)
 
192
        self.assertEqual([obj], log)
 
193
        del log[:]
 
194
        self.assertSizeOf(4, obj)
 
195
        self.assertEqual([], log)
 
196
 
 
197
    def test_size_of_special_neg1(self):
 
198
        # Returning -1 falls back to the regular __sizeof__, etc interface
 
199
        class CustomWithoutSizeof(object):
 
200
            pass
 
201
        log = []
 
202
        def _size_neg1(obj):
 
203
            log.append(obj)
 
204
            return -1
 
205
        obj = CustomWithoutSizeof()
 
206
        self.assertSizeOf(4, obj)
 
207
        _scanner.add_special_size('CustomWithoutSizeof', _size_neg1, _size_neg1)
 
208
        try:
 
209
            self.assertSizeOf(4, obj)
 
210
        finally:
 
211
            _scanner.add_special_size('CustomWithoutSizeof', None, None)
 
212
        self.assertEqual([obj], log)
 
213
 
 
214
    def test_size_of_zlib_compress_obj(self):
 
215
        # zlib compress objects allocate a lot of extra buffers, we want to
 
216
        # track that. Note that we are approximating it, because we don't
 
217
        # actually inspect the C attributes. But it is a closer approximation
 
218
        # than not doing this.
 
219
        c = zlib.compressobj()
 
220
        self.assertTrue(_scanner.size_of(c) > 256000)
 
221
        self.assertEqual(0, _scanner.size_of(c) % _scanner._word_size)
 
222
 
 
223
    def test_size_of_zlib_decompress_obj(self):
 
224
        d = zlib.decompressobj()
 
225
        self.assertTrue(_scanner.size_of(d) > 30000)
 
226
        self.assertEqual(0, _scanner.size_of(d) % _scanner._word_size)
 
227
 
173
228
 
174
229
def _string_to_json(s):
175
230
    out = ['"']