Python sys.getrefcount() Examples
The following are 30
code examples of sys.getrefcount().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
sys
, or try the search function
.
Example #1
Source File: test_memoryview.py From ironpython2 with Apache License 2.0 | 6 votes |
def test_attributes_writable(self): if not self.rw_type: self.skipTest("no writable type to test") m = self.check_attributes_with_type(self.rw_type) self.assertEqual(m.readonly, False) # Disabled: unicode uses the old buffer API in 2.x #def test_getbuffer(self): ## Test PyObject_GetBuffer() on a memoryview object. #for tp in self._types: #b = tp(self._source) #oldrefcount = sys.getrefcount(b) #m = self._view(b) #oldviewrefcount = sys.getrefcount(m) #s = unicode(m, "utf-8") #self._check_contents(tp, b, s.encode("utf-8")) #self.assertEqual(sys.getrefcount(m), oldviewrefcount) #m = None #self.assertEqual(sys.getrefcount(b), oldrefcount)
Example #2
Source File: test_dtype.py From recruit with Apache License 2.0 | 6 votes |
def test_structured_object_indexing(self, shape, index, items_changed, dt, pat, count, singleton): """Structured object reference counting for advanced indexing.""" zero = 0 one = 1 arr = np.zeros(shape, dt) gc.collect() before_zero = sys.getrefcount(zero) before_one = sys.getrefcount(one) # Test item getting: part = arr[index] after_zero = sys.getrefcount(zero) assert after_zero - before_zero == count * items_changed del part # Test item setting: arr[index] = one gc.collect() after_zero = sys.getrefcount(zero) after_one = sys.getrefcount(one) assert before_zero - after_zero == count * items_changed assert after_one - before_one == count * items_changed
Example #3
Source File: test_indexing.py From vnpy_crypto with MIT License | 6 votes |
def _check_single_index(self, arr, index): """Check a single index item getting and simple setting. Parameters ---------- arr : ndarray Array to be indexed, must be an arange. index : indexing object Index being tested. Must be a single index and not a tuple of indexing objects (see also `_check_multi_index`). """ try: mimic_get, no_copy = self._get_multi_index(arr, (index,)) except Exception as e: if HAS_REFCOUNT: prev_refcount = sys.getrefcount(arr) assert_raises(Exception, arr.__getitem__, index) assert_raises(Exception, arr.__setitem__, index, 0) if HAS_REFCOUNT: assert_equal(prev_refcount, sys.getrefcount(arr)) return self._compare_index_result(arr, index, mimic_get, no_copy)
Example #4
Source File: test_indexing.py From vnpy_crypto with MIT License | 6 votes |
def _check_multi_index(self, arr, index): """Check a multi index item getting and simple setting. Parameters ---------- arr : ndarray Array to be indexed, must be a reshaped arange. index : tuple of indexing objects Index being tested. """ # Test item getting try: mimic_get, no_copy = self._get_multi_index(arr, index) except Exception as e: if HAS_REFCOUNT: prev_refcount = sys.getrefcount(arr) assert_raises(Exception, arr.__getitem__, index) assert_raises(Exception, arr.__setitem__, index, 0) if HAS_REFCOUNT: assert_equal(prev_refcount, sys.getrefcount(arr)) return self._compare_index_result(arr, index, mimic_get, no_copy)
Example #5
Source File: test_dtype.py From recruit with Apache License 2.0 | 6 votes |
def test_structured_object_take_and_repeat(self, dt, pat, count, singleton): """Structured object reference counting for specialized functions. The older functions such as take and repeat use different code paths then item setting (when writing this). """ indices = [0, 1] arr = np.array([pat] * 3, dt) gc.collect() before = sys.getrefcount(singleton) res = arr.take(indices) after = sys.getrefcount(singleton) assert after - before == count * 2 new = res.repeat(10) gc.collect() after_repeat = sys.getrefcount(singleton) assert after_repeat - after == count * 2 * 10
Example #6
Source File: utils.py From recruit with Apache License 2.0 | 6 votes |
def _assert_valid_refcount(op): """ Check that ufuncs don't mishandle refcount of object `1`. Used in a few regression tests. """ if not HAS_REFCOUNT: return True import numpy as np, gc b = np.arange(100*100).reshape(100, 100) c = b i = 1 gc.disable() try: rc = sys.getrefcount(i) for j in range(15): d = op(b, c) assert_(sys.getrefcount(i) >= rc) finally: gc.enable() del d # for pyflakes
Example #7
Source File: test_regression.py From recruit with Apache License 2.0 | 6 votes |
def test_leak_in_structured_dtype_comparison(self): # gh-6250 recordtype = np.dtype([('a', np.float64), ('b', np.int32), ('d', (str, 5))]) # Simple case a = np.zeros(2, dtype=recordtype) for i in range(100): a == a assert_(sys.getrefcount(a) < 10) # The case in the bug report. before = sys.getrefcount(a) u, v = a[0], a[1] u == v del u, v gc.collect() after = sys.getrefcount(a) assert_equal(before, after)
Example #8
Source File: test_regression.py From auto-alt-text-lambda-api with MIT License | 6 votes |
def test_leak_in_structured_dtype_comparison(self): # gh-6250 recordtype = np.dtype([('a', np.float64), ('b', np.int32), ('d', (np.str, 5))]) # Simple case a = np.zeros(2, dtype=recordtype) for i in range(100): a == a assert_(sys.getrefcount(a) < 10) # The case in the bug report. before = sys.getrefcount(a) u, v = a[0], a[1] u == v del u, v gc.collect() after = sys.getrefcount(a) assert_equal(before, after)
Example #9
Source File: test_unpack.py From recruit with Apache License 2.0 | 6 votes |
def test_unpacker_hook_refcnt(self): if not hasattr(sys, 'getrefcount'): pytest.skip('no sys.getrefcount()') result = [] def hook(x): result.append(x) return x basecnt = sys.getrefcount(hook) up = Unpacker(object_hook=hook, list_hook=hook) assert sys.getrefcount(hook) >= basecnt + 2 up.feed(packb([{}])) up.feed(packb([{}])) assert up.unpack() == [{}] assert up.unpack() == [{}] assert result == [{}, [{}], {}, [{}]] del up assert sys.getrefcount(hook) == basecnt
Example #10
Source File: utils.py From lambda-packs with MIT License | 6 votes |
def _assert_valid_refcount(op): """ Check that ufuncs don't mishandle refcount of object `1`. Used in a few regression tests. """ if not HAS_REFCOUNT: return True import numpy as np, gc b = np.arange(100*100).reshape(100, 100) c = b i = 1 gc.disable() try: rc = sys.getrefcount(i) for j in range(15): d = op(b, c) assert_(sys.getrefcount(i) >= rc) finally: gc.enable() del d # for pyflakes
Example #11
Source File: test_refcounts.py From ironpython2 with Apache License 2.0 | 6 votes |
def test_1(self): from sys import getrefcount as grc f = dll._testfunc_callback_i_if f.restype = ctypes.c_int f.argtypes = [ctypes.c_int, MyCallback] def callback(value): #print "called back with", value return value self.assertEqual(grc(callback), 2) cb = MyCallback(callback) self.assertGreater(grc(callback), 2) result = f(-10, cb) self.assertEqual(result, -18) cb = None gc.collect() self.assertEqual(grc(callback), 2)
Example #12
Source File: test_indexing.py From auto-alt-text-lambda-api with MIT License | 6 votes |
def _check_single_index(self, arr, index): """Check a single index item getting and simple setting. Parameters ---------- arr : ndarray Array to be indexed, must be an arange. index : indexing object Index being tested. Must be a single index and not a tuple of indexing objects (see also `_check_multi_index`). """ try: mimic_get, no_copy = self._get_multi_index(arr, (index,)) except Exception: prev_refcount = sys.getrefcount(arr) assert_raises(Exception, arr.__getitem__, index) assert_raises(Exception, arr.__setitem__, index, 0) assert_equal(prev_refcount, sys.getrefcount(arr)) return self._compare_index_result(arr, index, mimic_get, no_copy)
Example #13
Source File: test_indexing.py From auto-alt-text-lambda-api with MIT License | 6 votes |
def _check_multi_index(self, arr, index): """Check a multi index item getting and simple setting. Parameters ---------- arr : ndarray Array to be indexed, must be a reshaped arange. index : tuple of indexing objects Index being tested. """ # Test item getting try: mimic_get, no_copy = self._get_multi_index(arr, index) except Exception: prev_refcount = sys.getrefcount(arr) assert_raises(Exception, arr.__getitem__, index) assert_raises(Exception, arr.__setitem__, index, 0) assert_equal(prev_refcount, sys.getrefcount(arr)) return self._compare_index_result(arr, index, mimic_get, no_copy)
Example #14
Source File: test_stringptr.py From ironpython2 with Apache License 2.0 | 6 votes |
def test__POINTER_c_char(self): class X(Structure): _fields_ = [("str", POINTER(c_char))] x = X() # NULL pointer access self.assertRaises(ValueError, getattr, x.str, "contents") b = c_buffer("Hello, World") from sys import getrefcount as grc self.assertEqual(grc(b), 2) x.str = b self.assertEqual(grc(b), 3) # POINTER(c_char) and Python string is NOT compatible # POINTER(c_char) and c_buffer() is compatible for i in range(len(b)): self.assertEqual(b[i], x.str[i]) self.assertRaises(TypeError, setattr, x, "str", "Hello, World")
Example #15
Source File: utils.py From auto-alt-text-lambda-api with MIT License | 6 votes |
def _assert_valid_refcount(op): """ Check that ufuncs don't mishandle refcount of object `1`. Used in a few regression tests. """ import numpy as np b = np.arange(100*100).reshape(100, 100) c = b i = 1 rc = sys.getrefcount(i) for j in range(15): d = op(b, c) assert_(sys.getrefcount(i) >= rc) del d # for pyflakes
Example #16
Source File: test_indexing.py From auto-alt-text-lambda-api with MIT License | 5 votes |
def _compare_index_result(self, arr, index, mimic_get, no_copy): """Compare mimicked result to indexing result. """ arr = arr.copy() indexed_arr = arr[index] assert_array_equal(indexed_arr, mimic_get) # Check if we got a view, unless its a 0-sized or 0-d array. # (then its not a view, and that does not matter) if indexed_arr.size != 0 and indexed_arr.ndim != 0: assert_(np.may_share_memory(indexed_arr, arr) == no_copy) # Check reference count of the original array if no_copy: # refcount increases by one: assert_equal(sys.getrefcount(arr), 3) else: assert_equal(sys.getrefcount(arr), 2) # Test non-broadcast setitem: b = arr.copy() b[index] = mimic_get + 1000 if b.size == 0: return # nothing to compare here... if no_copy and indexed_arr.ndim != 0: # change indexed_arr in-place to manipulate original: indexed_arr += 1000 assert_array_equal(arr, b) return # Use the fact that the array is originally an arange: arr.flat[indexed_arr.ravel()] += 1000 assert_array_equal(arr, b)
Example #17
Source File: test_nditer.py From auto-alt-text-lambda-api with MIT License | 5 votes |
def test_iter_object_arrays_basic(): # Check that object arrays work obj = {'a':3,'b':'d'} a = np.array([[1, 2, 3], None, obj, None], dtype='O') rc = sys.getrefcount(obj) # Need to allow references for object arrays assert_raises(TypeError, nditer, a) assert_equal(sys.getrefcount(obj), rc) i = nditer(a, ['refs_ok'], ['readonly']) vals = [x_[()] for x_ in i] assert_equal(np.array(vals, dtype='O'), a) vals, i, x = [None]*3 assert_equal(sys.getrefcount(obj), rc) i = nditer(a.reshape(2, 2).T, ['refs_ok', 'buffered'], ['readonly'], order='C') assert_(i.iterationneedsapi) vals = [x_[()] for x_ in i] assert_equal(np.array(vals, dtype='O'), a.reshape(2, 2).ravel(order='F')) vals, i, x = [None]*3 assert_equal(sys.getrefcount(obj), rc) i = nditer(a.reshape(2, 2).T, ['refs_ok', 'buffered'], ['readwrite'], order='C') for x in i: x[...] = None vals, i, x = [None]*3 assert_equal(sys.getrefcount(obj), rc-1) assert_equal(a, np.array([None]*4, dtype='O'))
Example #18
Source File: test_item_selection.py From vnpy_crypto with MIT License | 5 votes |
def test_refcounting(self): objects = [object() for i in range(10)] for mode in ('raise', 'clip', 'wrap'): a = np.array(objects) b = np.array([2, 2, 4, 5, 3, 5]) a.take(b, out=a[:6], mode=mode) del a if HAS_REFCOUNT: assert_(all(sys.getrefcount(o) == 3 for o in objects)) # not contiguous, example: a = np.array(objects * 2)[::2] a.take(b, out=a[:6], mode=mode) del a if HAS_REFCOUNT: assert_(all(sys.getrefcount(o) == 3 for o in objects))
Example #19
Source File: test_indexing.py From auto-alt-text-lambda-api with MIT License | 5 votes |
def test_small_regressions(self): # Reference count of intp for index checks a = np.array([0]) refcount = sys.getrefcount(np.dtype(np.intp)) # item setting always checks indices in separate function: a[np.array([0], dtype=np.intp)] = 1 a[np.array([0], dtype=np.uint8)] = 1 assert_raises(IndexError, a.__setitem__, np.array([1], dtype=np.intp), 1) assert_raises(IndexError, a.__setitem__, np.array([1], dtype=np.uint8), 1) assert_equal(sys.getrefcount(np.dtype(np.intp)), refcount)
Example #20
Source File: test_function_base.py From recruit with Apache License 2.0 | 5 votes |
def test_dtype_reference_leaks(self): # gh-6805 intp_refcount = sys.getrefcount(np.dtype(np.intp)) double_refcount = sys.getrefcount(np.dtype(np.double)) for j in range(10): np.bincount([1, 2, 3]) assert_equal(sys.getrefcount(np.dtype(np.intp)), intp_refcount) assert_equal(sys.getrefcount(np.dtype(np.double)), double_refcount) for j in range(10): np.bincount([1, 2, 3], [4, 5, 6]) assert_equal(sys.getrefcount(np.dtype(np.intp)), intp_refcount) assert_equal(sys.getrefcount(np.dtype(np.double)), double_refcount)
Example #21
Source File: gnumpy.py From imageqa-public with MIT License | 5 votes |
def __del__(self): if not hasattr(self, '_is_alias_of'): if _isTijmen: print 'gnumpy cleaning up an unfinished garray. mem counting may be off now.' return # this object was never finished, because an exception (error or interrupt) occurred in the constructor. This check avoids error messages. if self._is_alias_of is None: # this is not true in one case: if a reference to self._base is stored somewhere explicitly (somewhere outside self but not in another garray). This happens internally sometimes. I saw it happening on the last line of setitem: a transpose is created (transposes own their mem, are not aliases), and then it's dropped but _base (obtained by _base_as_row) is still in use for a cm assign call. assert _sys.getrefcount(self._base)==2, _sys.getrefcount(self._base) _cmsForReuse[self.size].append(self._base) if track_memory_usage: _memoryUsers[self.allocating_line] = (_memoryUsers[self.allocating_line][0]-1, _memoryUsers[self.allocating_line][1]-self.size*4) else: assert type(self._is_alias_of).__name__ == 'garray', '_is_alias_of is of unexpected type, of which the str() is: "%s"' % str(type(self._is_alias_of)) # del self._base # this is only to make the refcount assert not fail
Example #22
Source File: test_xml_etree.py From ironpython2 with Apache License 2.0 | 5 votes |
def test_bug_xmltoolkit63(self): # Check reference leak. def xmltoolkit63(): tree = ET.TreeBuilder() tree.start("tag", {}) tree.data("text") tree.end("tag") xmltoolkit63() count = sys.getrefcount(None) for i in range(1000): xmltoolkit63() self.assertEqual(sys.getrefcount(None), count)
Example #23
Source File: test_curses.py From ironpython2 with Apache License 2.0 | 5 votes |
def test_userptr_memory_leak(self): w = curses.newwin(10, 10) p = curses.panel.new_panel(w) obj = object() nrefs = sys.getrefcount(obj) for i in range(100): p.set_userptr(obj) p.set_userptr(None) self.assertEqual(sys.getrefcount(obj), nrefs, "set_userptr leaked references")
Example #24
Source File: test_optparse.py From ironpython2 with Apache License 2.0 | 5 votes |
def test_refleak(self): # If an OptionParser is carrying around a reference to a large # object, various cycles can prevent it from being GC'd in # a timely fashion. destroy() breaks the cycles to ensure stuff # can be cleaned up. big_thing = [42] refcount = sys.getrefcount(big_thing) parser = OptionParser() parser.add_option("-a", "--aaarggh") parser.big_thing = big_thing parser.destroy() #self.assertEqual(refcount, sys.getrefcount(big_thing)) del parser self.assertEqual(refcount, sys.getrefcount(big_thing))
Example #25
Source File: test_array.py From ironpython2 with Apache License 2.0 | 5 votes |
def test_bug_782369(self): for i in range(10): b = array.array('B', range(64)) rc = sys.getrefcount(10) for i in range(10): b = array.array('B', range(64)) self.assertEqual(rc, sys.getrefcount(10))
Example #26
Source File: test_os.py From ironpython2 with Apache License 2.0 | 5 votes |
def test_rename(self): path = unicode(test_support.TESTFN) old = sys.getrefcount(path) self.assertRaises(TypeError, os.rename, path, 0) new = sys.getrefcount(path) self.assertEqual(old, new)
Example #27
Source File: test_enumerate.py From ironpython2 with Apache License 2.0 | 5 votes |
def test_bug1229429(self): # this bug was never in reversed, it was in # PyObject_CallMethod, and reversed_new calls that sometimes. def f(): pass r = f.__reversed__ = object() rc = sys.getrefcount(r) for i in range(10): try: reversed(f) except TypeError: pass else: self.fail("non-callable __reversed__ didn't raise!") self.assertEqual(rc, sys.getrefcount(r))
Example #28
Source File: test_memoryview.py From ironpython2 with Apache License 2.0 | 5 votes |
def test_refs(self): for tp in self._types: m = memoryview(tp(self._source)) oldrefcount = sys.getrefcount(m) m[1:2] self.assertEqual(sys.getrefcount(m), oldrefcount)
Example #29
Source File: test_numeric.py From vnpy_crypto with MIT License | 5 votes |
def test_for_reference_leak(self): # Make sure we have an object for reference dim = 1 beg = sys.getrefcount(dim) np.zeros([dim]*10) assert_(sys.getrefcount(dim) == beg) np.ones([dim]*10) assert_(sys.getrefcount(dim) == beg) np.empty([dim]*10) assert_(sys.getrefcount(dim) == beg) np.full([dim]*10, 0) assert_(sys.getrefcount(dim) == beg)
Example #30
Source File: test_numeric.py From auto-alt-text-lambda-api with MIT License | 5 votes |
def test_for_reference_leak(self): # Make sure we have an object for reference dim = 1 beg = sys.getrefcount(dim) np.zeros([dim]*10) assert_(sys.getrefcount(dim) == beg) np.ones([dim]*10) assert_(sys.getrefcount(dim) == beg) np.empty([dim]*10) assert_(sys.getrefcount(dim) == beg) np.full([dim]*10, 0) assert_(sys.getrefcount(dim) == beg)