Example #1
0
def measure_ref_leakage(f, numsamples=2**7, iterspersample=2**4, *args, **kwargs):
    """
    The idea is we are going to use sys.gettotalrefcount() to see how many
    references are extant, and keep track of that number with respect to how
    many times we've invoked f(), and return the slope of the best linear
    fit.

    @param numsamples: recommended: 2**7

    @param iterspersample: how many times f() should be invoked per sample;
                           Basically, choose iterspersample such that
                           iterspersample * numsamples *
                           how-long-it-takes-to-compute-f() is slightly less
                           than how long you are willing to wait for this
                           leak test.

    @return: the slope of the best linear fit, which can be interpreted as 'the
             approximate number of Python references created and not
             nullified per invocation of f()'
    """
    precondition(numsamples > 0, "numsamples is required to be positive.", numsamples)
    precondition(iterspersample > 0, "iterspersample is required to be positive.", iterspersample)

    try:
        sys.gettotalrefcount()
    except AttributeError, le:
        raise AttributeError(le, "Probably this is not a debug build of Python, so it doesn't have a sys.gettotalrefcount function.")
Example #2
0
def test_with_refcounts(runner, verbosity, testcase):
    """Run testcase several times, tracking reference counts."""
    import gc
    import ctypes
    ptc = ctypes._pointer_type_cache.copy()
    cfc = ctypes._c_functype_cache.copy()
    wfc = ctypes._win_functype_cache.copy()

    # when searching for refcount leaks, we have to manually reset any
    # caches that ctypes has.
    def cleanup():
        ctypes._pointer_type_cache = ptc.copy()
        ctypes._c_functype_cache = cfc.copy()
        ctypes._win_functype_cache = wfc.copy()
        gc.collect()

    test = unittest.makeSuite(testcase)
    for i in range(5):
        rc = sys.gettotalrefcount()
        runner.run(test)
        cleanup()
    COUNT = 5
    refcounts = [None] * COUNT
    for i in range(COUNT):
        rc = sys.gettotalrefcount()
        runner.run(test)
        cleanup()
        refcounts[i] = sys.gettotalrefcount() - rc
    if filter(None, refcounts):
        shout "%s leaks:\n\t" % testcase, refcounts
    elif verbosity:
        shout "%s: ok." % testcase
Example #3
0
File: test.py Project: 0day-ci/xen
def main(module_filter, test_filter, libdir):
    if not KEEP_STALE_BYTECODE:
        os.path.walk(os.curdir, remove_stale_bytecode, None)

    configure_logging()

    # Initialize the path and cwd
    global pathinit
    pathinit = PathInit(BUILD, BUILD_INPLACE, libdir)

    files = find_tests(module_filter)
    files.sort()

    if GUI:
        gui_runner(files, test_filter)
    elif LOOP:
        if REFCOUNT:
            rc = sys.gettotalrefcount()
            track = TrackRefs()
        while True:
            runner(files, test_filter, DEBUG)
            gc.collect()
            if gc.garbage:
                print "GARBAGE:", len(gc.garbage), gc.garbage
                return
            if REFCOUNT:
                prev = rc
                rc = sys.gettotalrefcount()
                print "totalrefcount=%-8d change=%-6d" % (rc, rc - prev)
                track.update()
    else:
        runner(files, test_filter, DEBUG)

    os.chdir(pathinit.org_cwd)
Example #4
0
def application(e, sr):

    sr('200 OK', [('Content-Type','text/html')])

    t = gevent.spawn(long_task)

    t.join()

    yield "sleeping for 3 seconds...<br/>"

    gevent.sleep(3)

    yield "done<br>"

    yield "getting some ips...<br/>"

    urls = ['www.google.com', 'www.example.com', 'www.python.org', 'projects.unbit.it']
    jobs = [gevent.spawn(gevent.socket.gethostbyname, url) for url in urls]
    gevent.joinall(jobs, timeout=2)

    for j in jobs:
        yield "ip = %s<br/>" % j.value

    if REFCNT:
        print sys.gettotalrefcount()
        yield "%d" % sys.gettotalrefcount()

    # this task will goes on after request end
    gevent.spawn(bg_task)
Example #5
0
 def test_dir_class(self):
     from java.util import ArrayList
     refcount1 = sys.gettotalrefcount()
     result = dir(ArrayList)
     del result
     refcount2 = sys.gettotalrefcount()
     self.assertEquals(refcount1, refcount2 - 1)
Example #6
0
def dump_refs(preheat=10, iter1=10, iter2=10, *testargs):

    rc1 = rc2 = None
    #testMethod()
    for i in xrange(preheat):
        testMethod(*testargs)
    gc.collect()
    rc1 = sys.gettotalrefcount()
    track = TrackRefs()
    for i in xrange(iter1):
        testMethod(*testargs)
    print "First output of TrackRefs:"
    gc.collect()
    rc2 = sys.gettotalrefcount()
    track.update()
    print >>sys.stderr, "Inc refs in function testMethod --> %5d" % (rc2-rc1)
    for i in xrange(iter2):
        testMethod(*testargs)
        track.update(verbose=1)
    print "Second output of TrackRefs:"
    gc.collect()
    rc3 = sys.gettotalrefcount()

    print >>sys.stderr, "Inc refs in function testMethod --> %5d" % (rc3-rc2)
    ok = 1
Example #7
0
def TopTest(function):
    end_ref_count = 0
    start_ref_count = sys.gettotalrefcount()
    Reset()
    function()
    end_ref_count = sys.gettotalrefcount()
    print(function.__name__, end_ref_count - start_ref_count)
Example #8
0
def test_with_refcounts(runner, verbosity, testcase):
    """Run testcase several times, tracking reference counts."""
    import gc
    import ctypes
    ptc = ctypes._pointer_type_cache.copy()
    cfc = ctypes._c_functype_cache.copy()
    wfc = ctypes._win_functype_cache.copy()

    def cleanup():
        ctypes._pointer_type_cache = ptc.copy()
        ctypes._c_functype_cache = cfc.copy()
        ctypes._win_functype_cache = wfc.copy()
        gc.collect()

    test = unittest.makeSuite(testcase)
    for i in range(5):
        rc = sys.gettotalrefcount()
        runner.run(test)
        cleanup()

    COUNT = 5
    refcounts = [None] * COUNT
    for i in range(COUNT):
        rc = sys.gettotalrefcount()
        runner.run(test)
        cleanup()
        refcounts[i] = sys.gettotalrefcount() - rc

    if filter(None, refcounts):
        print '%s leaks:\n\t' % testcase, refcounts
    elif verbosity:
        print '%s: ok.' % testcase
    return
Example #9
0
def test_refleaks(options, args):
    from sys import gettotalrefcount
    from gc import collect
    testsuite = load_tests(options, args)
    testsuite._cleanup =  False
    for case in testsuite:
        case._cleanup = False
    class EmptyIO(object):
        def write(self, *args):
            pass
    runner = unittest.TextTestRunner(stream=EmptyIO(), verbosity=0)
    rank, name = getprocessorinfo()
    r1 = r2 = 0
    repeats = options.repeats
    while repeats:
        collect()
        r1 = gettotalrefcount()
        run_tests(options, testsuite, runner)
        collect()
        r2 = gettotalrefcount()
        leaks = r2-r1
        if leaks and repeats < options.repeats:
            writeln('[%d@%s] refleaks:  (%d - %d) --> %d'
                    % (rank, name, r2, r1, leaks))
        repeats -= 1
Example #10
0
 def test_construction(self):
     from java.lang import Object
     refcount1 = sys.gettotalrefcount()
     result = Object()
     del result
     refcount2 = sys.gettotalrefcount()
     self.assertEquals(refcount1, refcount2 - 1)
def checkReferenceCount( checked_function, warmup = False ):
   sys.exc_clear()
   print checked_function,

   ref_count1 = 17
   ref_count2 = 17

   gc.collect()
   ref_count1 = sys.gettotalrefcount()

   if warmup:
      checked_function()

   sys.exc_clear()
   gc.collect()

   ref_count2 = sys.gettotalrefcount()

   if ref_count1 == ref_count2 and warmup:
      print "WARMUP not needed",

   gc.collect()
   ref_count1 = sys.gettotalrefcount()

   checked_function()

   sys.exc_clear()
   gc.collect()

   ref_count2 = sys.gettotalrefcount()

   if ref_count1 != ref_count2:
      print "FAILED", ref_count1, ref_count2
   else:
      print "PASSED"
Example #12
0
 def test_array_assignment(self):
     from jep import jarray
     from java.lang import Object
     arr = jarray(1, Object)
     refcount1 = sys.gettotalrefcount()
     arr[0] = self.obj
     refcount2 = sys.gettotalrefcount()
     self.assertEquals(refcount1, refcount2 - 1)
Example #13
0
 def test_array_creation(self):
     from jep import jarray
     from java.lang import Object
     refcount1 = sys.gettotalrefcount()
     result = jarray(1, Object)
     del result
     refcount2 = sys.gettotalrefcount()
     self.assertEquals(refcount1, refcount2 - 1)
Example #14
0
 def test_method_call(self):
     # First call to hashCode will cache info about the hashCode method
     self.obj.hashCode()
     refcount1 = sys.gettotalrefcount()
     result = self.obj.hashCode()
     del result
     refcount2 = sys.gettotalrefcount()
     self.assertEquals(refcount1, refcount2 - 1)
Example #15
0
def dash_R(the_module, test, indirect_test, huntrleaks):
    """Run a test multiple times, looking for reference leaks.

    Returns:
        False if the test didn't leak references; True if we detected refleaks.
    """
    # This code is hackish and inelegant, but it seems to do the job.
    import copy_reg, _abcoll, io

    if not hasattr(sys, 'gettotalrefcount'):
        raise Exception("Tracking reference leaks requires a debug build "
                        "of Python")

    # Save current values for dash_R_cleanup() to restore.
    fs = warnings.filters[:]
    ps = copy_reg.dispatch_table.copy()
    pic = sys.path_importer_cache.copy()
    abcs = {}
    modules = _abcoll, io
    for abc in [getattr(mod, a) for mod in modules for a in mod.__all__]:
        # XXX isinstance(abc, ABCMeta) leads to infinite recursion
        if not hasattr(abc, '_abc_registry'):
            continue
        for obj in abc.__subclasses__() + [abc]:
            abcs[obj] = obj._abc_registry.copy()

    if indirect_test:
        def run_the_test():
            indirect_test()
    else:
        def run_the_test():
            reload(the_module)

    deltas = []
    nwarmup, ntracked, fname = huntrleaks
    repcount = nwarmup + ntracked
    print >> sys.stderr, "beginning", repcount, "repetitions"
    print >> sys.stderr, ("1234567890"*(repcount//10 + 1))[:repcount]
    if _llvm:
        _llvm.set_jit_control("never")
    for i in range(repcount):
        dash_R_cleanup(fs, ps, pic, abcs)
        rc_before = sys.gettotalrefcount()
        run_the_test()
        sys.stderr.write('.')
        dash_R_cleanup(fs, ps, pic, abcs)
        rc_after = sys.gettotalrefcount()
        if i >= nwarmup:
            deltas.append(rc_after - rc_before)
    print >> sys.stderr
    if any(deltas):
        msg = '%s leaked %s references, sum=%s' % (test, deltas, sum(deltas))
        print >> sys.stderr, msg
        refrep = open(fname, "a")
        print >> refrep, msg
        refrep.close()
        return True
    return False
Example #16
0
def checkReferenceCount( checked_function, max_rounds = 10 ):
   assert sys.exc_info() == ( None, None, None ), sys.exc_info()

   print checked_function.func_name + ":",

   ref_count1 = 17
   ref_count2 = 17

   explain = False

   for count in range( max_rounds ):
      x1 = 0
      x2 = 0

      gc.collect()
      ref_count1 = sys.gettotalrefcount()

      if explain and count == max_rounds - 1:
         snapObjRefCntMap( True )

      checked_function()

      assert sys.exc_info() == ( None, None, None ), sys.exc_info()

      gc.collect()

      if explain and count == max_rounds - 1:
         snapObjRefCntMap( False )

      ref_count2 = sys.gettotalrefcount()

      if ref_count1 == ref_count2:
         print "PASSED"
         break

      # print count, ref_count1, ref_count2
   else:
      print "FAILED", ref_count1, ref_count2, "leaked", ref_count2 - ref_count1

      if explain:
         assert m1
         assert m2

         for key in m1.keys():
            if key not in m2:
               print "*" * 80
               print key
            elif m1[key] != m2[key]:
               print "*" * 80
               print key
            else:
               pass
               # print m1[key]

   assert sys.exc_info() == ( None, None, None ), sys.exc_info()

   gc.collect()
Example #17
0
 def test_number_compare(self):
     x = 5
     from java.lang import Integer
     y = Integer(9)
     refcount1 = sys.gettotalrefcount()
     result = x < y
     del result
     refcount2 = sys.gettotalrefcount()
     self.assertEquals(refcount1, refcount2 - 1)
Example #18
0
def check_refcount():
    gc.collect()
    before = sys.gettotalrefcount()
    yield
    gc.collect()
    diff = sys.gettotalrefcount() - before

    if diff != 0:
        raise AssertionError('leaked %d references' % diff)
Example #19
0
 def test_list_setslice(self):
     from java.util import ArrayList
     jlist = ArrayList()
     for i in range(5):
         jlist.add(i)
     refcount1 = sys.gettotalrefcount()
     jlist[2:4] = [7, 19]
     refcount2 = sys.gettotalrefcount()
     self.assertEquals(refcount1, refcount2 - 1)
Example #20
0
 def test_field_access(self):
     from java.lang import System
     # First time will cache info about the PrintStream class
     result = System.out
     del result
     refcount1 = sys.gettotalrefcount()
     result = System.out
     del result
     refcount2 = sys.gettotalrefcount()
     self.assertEquals(refcount1, refcount2 - 1)
Example #21
0
 def test_array_access(self):
     from jep import jarray
     from java.lang import Object
     arr = jarray(1, Object)
     arr[0] = self.obj
     refcount1 = sys.gettotalrefcount()
     result = arr[0]
     del result
     refcount2 = sys.gettotalrefcount()
     self.assertEquals(refcount1, refcount2 - 1)
Example #22
0
    def test_error_log_leak(self):
        if not hasattr(sys, 'gettotalrefcount'):
            return

        before = sys.gettotalrefcount()
        for x in xrange(50):
            parse_test_doc()
        after = sys.gettotalrefcount()

        self.assert_equal(before, after)
Example #23
0
def dash_R(the_module, test, indirect_test, huntrleaks):
    """Run a test multiple times, looking for reference leaks.

    Returns:
        False if the test didn't leak references; True if we detected refleaks.
    """
    # This code is hackish and inelegant, but it seems to do the job.
    import copyreg, _abcoll

    if not hasattr(sys, 'gettotalrefcount'):
        raise Exception("Tracking reference leaks requires a debug build "
                        "of Python")

    # Save current values for dash_R_cleanup() to restore.
    fs = warnings.filters[:]
    ps = copyreg.dispatch_table.copy()
    pic = sys.path_importer_cache.copy()
    abcs = {}
    for abc in [getattr(_abcoll, a) for a in _abcoll.__all__]:
        if not isabstract(abc):
            continue
        for obj in abc.__subclasses__() + [abc]:
            abcs[obj] = obj._abc_registry.copy()

    if indirect_test:
        def run_the_test():
            indirect_test()
    else:
        def run_the_test():
            del sys.modules[the_module.__name__]
            exec('import ' + the_module.__name__)

    deltas = []
    nwarmup, ntracked, fname = huntrleaks
    repcount = nwarmup + ntracked
    print("beginning", repcount, "repetitions", file=sys.stderr)
    print(("1234567890"*(repcount//10 + 1))[:repcount], file=sys.stderr)
    dash_R_cleanup(fs, ps, pic, abcs)
    for i in range(repcount):
        rc = sys.gettotalrefcount()
        run_the_test()
        sys.stderr.write('.')
        sys.stderr.flush()
        dash_R_cleanup(fs, ps, pic, abcs)
        if i >= nwarmup:
            deltas.append(sys.gettotalrefcount() - rc - 2)
    print(file=sys.stderr)
    if any(deltas):
        msg = '%s leaked %s references, sum=%s' % (test, deltas, sum(deltas))
        print(msg, file=sys.stderr)
        refrep = open(fname, "a")
        print(msg, file=refrep)
        refrep.close()
        return True
    return False
Example #24
0
def main(module_filter, test_filter, libdir):
    if not keepStaleBytecode:
        os.path.walk(os.curdir, remove_stale_bytecode, None)

    # Get the log.ini file from the current directory instead of possibly
    # buried in the build directory.  XXX This isn't perfect because if
    # log.ini specifies a log file, it'll be relative to the build directory.
    # Hmm...
    logini = os.path.abspath("log.ini")

    from setup import check_manifest
    check_manifest()

    # Initialize the path and cwd
    global pathinit
    pathinit = PathInit(build, build_inplace, libdir)

# No logging module in py 2.1
#    # Initialize the logging module.

#    import logging.config
#    logging.basicConfig()

#    level = os.getenv("LOGGING")
#    if level:
#        level = int(level)
#    else:
#        level = logging.CRITICAL
#    logging.root.setLevel(level)

#    if os.path.exists(logini):
#        logging.config.fileConfig(logini)

    files = find_tests(module_filter)
    files.sort()

    if GUI:
        gui_runner(files, test_filter)
    elif LOOP:
        if REFCOUNT:
            rc = sys.gettotalrefcount()
            track = TrackRefs()
        while 1:
            runner(files, test_filter, debug)
            gc.collect()
            if gc.garbage:
                print "GARBAGE:", len(gc.garbage), gc.garbage
                return
            if REFCOUNT:
                prev = rc
                rc = sys.gettotalrefcount()
                print "totalrefcount=%-8d change=%-6d" % (rc, rc - prev)
                track.update()
    else:
        runner(files, test_filter, debug)
Example #25
0
 def test_wrapper(*args, **kwargs):
     deltas = []
     _cleanup()
     for i in xrange(5):
         before = sys.gettotalrefcount()
         test(*args, **kwargs)
         _cleanup()
         after = sys.gettotalrefcount()
         if i > 2:
             deltas.append(after - before)
     if any(deltas):
         print("{0!r} leaks: {1}".format(test, deltas))
Example #26
0
def dash_R(the_module, test, indirect_test, huntrleaks):
    # This code is hackish and inelegant, but it seems to do the job.
    import copy_reg, _abcoll, io

    if not hasattr(sys, "gettotalrefcount"):
        raise Exception("Tracking reference leaks requires a debug build " "of Python")

    # Save current values for dash_R_cleanup() to restore.
    fs = warnings.filters[:]
    ps = copy_reg.dispatch_table.copy()
    pic = sys.path_importer_cache.copy()
    abcs = {}
    modules = _abcoll, io
    for abc in [getattr(mod, a) for mod in modules for a in mod.__all__]:
        # XXX isinstance(abc, ABCMeta) leads to infinite recursion
        if not hasattr(abc, "_abc_registry"):
            continue
        for obj in abc.__subclasses__() + [abc]:
            abcs[obj] = obj._abc_registry.copy()

    if indirect_test:

        def run_the_test():
            indirect_test()

    else:

        def run_the_test():
            reload(the_module)

    deltas = []
    nwarmup, ntracked, fname = huntrleaks
    repcount = nwarmup + ntracked
    print >> sys.stderr, "beginning", repcount, "repetitions"
    print >> sys.stderr, ("1234567890" * (repcount // 10 + 1))[:repcount]
    dash_R_cleanup(fs, ps, pic, abcs)
    for i in range(repcount):
        rc = sys.gettotalrefcount()
        run_the_test()
        sys.stderr.write(".")
        dash_R_cleanup(fs, ps, pic, abcs)
        if i >= nwarmup:
            deltas.append(sys.gettotalrefcount() - rc - 2)
    print >> sys.stderr
    if any(deltas):
        msg = "%s leaked %s references, sum=%s" % (test, deltas, sum(deltas))
        print >> sys.stderr, msg
        refrep = open(fname, "a")
        print >> refrep, msg
        refrep.close()
Example #27
0
def runtestsleak(repeats, *args, **kargs):
    import gc
    test = r1 = r2 = None
    while repeats:
        repeats -= 1
        gc.collect()
        r1 = sys.gettotalrefcount()
        for test in alltests:
            mpiunittest.main(test, *args, **kargs)
        gc.collect()
        r2 = sys.gettotalrefcount()
        sys.stderr.flush()
        sys.stderr.write('\nREF LEAKS -- before: %d, after: %d, diff: [%d]\n' % (r1, r2, r2-r1))
        sys.stderr.flush()
Example #28
0
 def test_create_read(self):
     delta = 0
     lastrc = sys.gettotalrefcount()
     for i in xrange(20):
         gc.collect()
         self.assertEqual(gc.garbage, [])
         rc = sys.gettotalrefcount()
         csv.reader(["a,b,c\r\n"])
         csv.reader(["a,b,c\r\n"])
         csv.reader(["a,b,c\r\n"])
         delta = rc-lastrc
         lastrc = rc
     # if csv.reader() leaks, last delta should be 3 or more
     self.assertEqual(delta < 3, True)
Example #29
0
 def test_read(self):
     delta = 0
     rows = ["a,b,c\r\n"]*5
     lastrc = sys.gettotalrefcount()
     for i in xrange(20):
         gc.collect()
         self.assertEqual(gc.garbage, [])
         rc = sys.gettotalrefcount()
         rdr = csv.reader(rows)
         for row in rdr:
             pass
         delta = rc-lastrc
         lastrc = rc
     # if reader leaks during read, delta should be 5 or more
     self.assertEqual(delta < 5, True)
Example #30
0
 def test_create_write(self):
     delta = 0
     lastrc = sys.gettotalrefcount()
     s = NUL()
     for i in xrange(20):
         gc.collect()
         self.assertEqual(gc.garbage, [])
         rc = sys.gettotalrefcount()
         csv.writer(s)
         csv.writer(s)
         csv.writer(s)
         delta = rc-lastrc
         lastrc = rc
     # if csv.writer() leaks, last delta should be 3 or more
     self.assertEqual(delta < 3, True)
Example #31
0
def test_refleaks(options, args):
    from sys import gettotalrefcount
    from gc import collect
    from copy import deepcopy
    testsuite = load_tests(options, args)
    class EmptyIO(object):
        def write(self, *args):
            pass
    runner = unittest.TextTestRunner(stream=EmptyIO(), verbosity=0)
    rank, name = getprocessorinfo()
    r1 = r2 = 0
    repeats = options.repeats
    while repeats:
        collect()
        r1 = gettotalrefcount()
        run_tests(options, deepcopy(testsuite), runner)
        collect()
        r2 = gettotalrefcount()
        leaks = r2-r1
        if leaks and repeats < options.repeats:
            writeln('[%d@%s] refleaks:  (%d - %d) --> %d'
                    % (rank, name, r2, r1, leaks))
        repeats -= 1
Example #32
0
def test_main(verbose=None):
    test_classes = [TestModules, TestHeapPython, TestHeapC,
                    TestErrorHandlingPython, TestErrorHandlingC]
    test_support.run_unittest(*test_classes)

    # verify reference counting
    if verbose and hasattr(sys, "gettotalrefcount"):
        import gc
        counts = [None] * 5
        for i in xrange(len(counts)):
            test_support.run_unittest(*test_classes)
            gc.collect()
            counts[i] = sys.gettotalrefcount()
        print counts
Example #33
0
def test_main(verbose=None):
    print('test_main')
    support.run_unittest(ListTest)

    # verify reference counting
    import sys
    if verbose and hasattr(sys, "gettotalrefcount"):
        import gc
        counts = [None] * 5
        for i in range(len(counts)):
            support.run_unittest(ListTest)
            gc.collect()
            counts[i] = sys.gettotalrefcount()
        print(counts)
 def test_refcount(self):
     # n here must be a global in order for this test to pass while
     # tracing with a python function.  Tracing calls PyFrame_FastToLocals
     # which will add a copy of any locals to the frame object, causing
     # the reference count to increase by 2 instead of 1.
     global n
     self.assertRaises(TypeError, sys.getrefcount)
     c = sys.getrefcount(None)
     n = None
     self.assertEqual(sys.getrefcount(None), c + 1)
     del n
     self.assertEqual(sys.getrefcount(None), c)
     if hasattr(sys, "gettotalrefcount"):
         self.assertIsInstance(sys.gettotalrefcount(), int)
Example #35
0
def test_main(verbose=None):
    test_classes = (BuiltinTest, TestSorted)

    run_unittest(*test_classes)

    # verify reference counting
    if verbose and hasattr(sys, "gettotalrefcount"):
        import gc
        counts = [None] * 5
        for i in range(len(counts)):
            run_unittest(*test_classes)
            gc.collect()
            counts[i] = sys.gettotalrefcount()
        print(counts)
Example #36
0
def test_main(verbose=None):
    import sys

    test_support.run_unittest(*tests)

    # verify reference counting
    if verbose and hasattr(sys, "gettotalrefcount"):
        import gc
        counts = [None] * 5
        for i in xrange(len(counts)):
            test_support.run_unittest(*tests)
            gc.collect()
            counts[i] = sys.gettotalrefcount()
        print counts
def test_main(verbose=None):
    from test import support
    from test import test_genexps
    support.run_doctest(test_genexps, verbose)

    # verify reference counting
    if verbose and hasattr(sys, "gettotalrefcount"):
        import gc
        counts = [None] * 5
        for i in range(len(counts)):
            support.run_doctest(test_genexps, verbose)
            gc.collect()
            counts[i] = sys.gettotalrefcount()
        print(counts)
Example #38
0
    def test(self):
        result = ''
        try:
            Timeout.start_new(0.01)
            gevent.sleep(1)
            raise AssertionError('must raise Timeout')
        except KeyboardInterrupt:
            raise
        except:
            pass

        result += '%s ' % sys.gettotalrefcount()

        try:
            Timeout.start_new(0.01)
            gevent.sleep(1)
            raise AssertionError('must raise Timeout')
        except KeyboardInterrupt:
            raise
        except:
            pass

        result += '%s ' % sys.gettotalrefcount()

        try:
            Timeout.start_new(0.01)
            gevent.sleep(1)
            raise AssertionError('must raise Timeout')
        except KeyboardInterrupt:
            raise
        except:
            pass

        result += '%s' % sys.gettotalrefcount()

        _, b, c = result.split()
        assert b == c, 'total refcount mismatch: %s' % result
Example #39
0
def test_main(verbose=None):
    import sys
    test_classes = (TestPartial, TestPartialSubclass, TestPythonPartial,
                    TestUpdateWrapper, TestWraps, TestReduce)
    support.run_unittest(*test_classes)

    # verify reference counting
    if verbose and hasattr(sys, "gettotalrefcount"):
        import gc
        counts = [None] * 5
        for i in range(len(counts)):
            support.run_unittest(*test_classes)
            gc.collect()
            counts[i] = sys.gettotalrefcount()
        print(counts)
Example #40
0
def measure_ref_leakage(f,
                        numsamples=2**7,
                        iterspersample=2**4,
                        *args,
                        **kwargs):
    """
    The idea is we are going to use sys.gettotalrefcount() to see how many
    references are extant, and keep track of that number with respect to how
    many times we've invoked f(), and return the slope of the best linear
    fit.

    @param numsamples: recommended: 2**7

    @param iterspersample: how many times f() should be invoked per sample;
                           Basically, choose iterspersample such that
                           iterspersample * numsamples *
                           how-long-it-takes-to-compute-f() is slightly less
                           than how long you are willing to wait for this
                           leak test.

    @return: the slope of the best linear fit, which can be interpreted as 'the
             approximate number of Python references created and not
             nullified per invocation of f()'
    """
    precondition(numsamples > 0, "numsamples is required to be positive.",
                 numsamples)
    precondition(iterspersample > 0,
                 "iterspersample is required to be positive.", iterspersample)

    try:
        sys.gettotalrefcount()
    except AttributeError, le:
        raise AttributeError(
            le,
            "Probably this is not a debug build of Python, so it doesn't have a sys.gettotalrefcount function."
        )
Example #41
0
def test_main(verbose=None):
    import sys
    from test import support
    test_classes = (TestTranforms, )
    support.run_unittest(*test_classes)

    # verify reference counting
    if verbose and hasattr(sys, "gettotalrefcount"):
        import gc
        counts = [None] * 5
        for i in range(len(counts)):
            support.run_unittest(*test_classes)
            gc.collect()
            counts[i] = sys.gettotalrefcount()
        print(counts)
def test_main(verbose=None):
    from types import BuiltinFunctionType

    test_classes = [TestHeapPython, TestHeapC, TestErrorHandling]
    support.run_unittest(*test_classes)

    # verify reference counting
    if verbose and hasattr(sys, "gettotalrefcount"):
        import gc
        counts = [None] * 5
        for i in range(len(counts)):
            support.run_unittest(*test_classes)
            gc.collect()
            counts[i] = sys.gettotalrefcount()
        print(counts)
	def test_exception(self):
		before = middle = after = 0

		def inner():
			try:
				raise heimdal.KRB5KDC_ERR_NONE()
			except heimdal.KRB5KDC_ERR_NONE as ex:
				middle = sys.gettotalrefcount()
				del ex

			if PY2:
				# Py2 keeps a reference for the last exception for re-raising
				# <https://cosmicpercolator.com/2016/01/13/exception-leaks-in-python-2-and-3/>
				sys.exc_clear()

			return middle

		before = sys.gettotalrefcount()
		middle = inner()
		after = sys.gettotalrefcount()

		self.assertGreater(middle, before)
		self.assertLess(after, middle)
		self.assertEqual(before, after)
Example #44
0
def test_main(verbose=None):
    import sys
    test_classes = (TestBasic, TestVariousIteratorArgs, TestSubclass,
        TestSubclassWithKwargs, TestSequence)
    support.run_unittest(*test_classes)
    if verbose and hasattr(sys, 'gettotalrefcount'):
        import gc
        counts = [None] * 5
        for i in range(len(counts)):
            support.run_unittest(*test_classes)
            gc.collect()
            counts[i] = sys.gettotalrefcount()
        print(counts)
    from test import test_deque
    support.run_doctest(test_deque, verbose)
Example #45
0
def test_main(verbose=None):
    import sys
    from test import test_sets
    test_classes = (
        TestSet,
        TestSetSubclass,
        TestFrozenSet,
        TestFrozenSetSubclass,
        TestSetOfSets,
        TestExceptionPropagation,
        TestBasicOpsEmpty,
        TestBasicOpsSingleton,
        TestBasicOpsTuple,
        TestBasicOpsTriple,
        TestBinaryOps,
        TestUpdateOps,
        TestMutate,
        TestSubsetEqualEmpty,
        TestSubsetEqualNonEmpty,
        TestSubsetEmptyNonEmpty,
        TestSubsetPartial,
        TestSubsetNonOverlap,
        TestOnlySetsNumeric,
        TestOnlySetsDict,
        TestOnlySetsOperator,
        TestOnlySetsTuple,
        TestOnlySetsString,
        TestOnlySetsGenerator,
        TestCopyingEmpty,
        TestCopyingSingleton,
        TestCopyingTriple,
        TestCopyingTuple,
        TestCopyingNested,
        TestIdentities,
        TestVariousIteratorArgs,
        )

    test_support.run_unittest(*test_classes)

    # verify reference counting
    if verbose and hasattr(sys, "gettotalrefcount"):
        import gc
        counts = [None] * 5
        for i in xrange(len(counts)):
            test_support.run_unittest(*test_classes)
            gc.collect()
            counts[i] = sys.gettotalrefcount()
        print counts
Example #46
0
def test_main(verbose=None):
    from types import BuiltinFunctionType

    test_classes = [TestHeap]
    if isinstance(heapify, BuiltinFunctionType):
        test_classes.append(TestErrorHandling)
    test_support.run_unittest(*test_classes)

    # verify reference counting
    if verbose and hasattr(sys, "gettotalrefcount"):
        import gc
        counts = [None] * 5
        for i in xrange(len(counts)):
            test_support.run_unittest(*test_classes)
            gc.collect()
            counts[i] = sys.gettotalrefcount()
        print counts
Example #47
0
def test_main(verbose=None):
    test_classes = (TestBasicOps, TestVariousIteratorArgs, TestGC,
                    RegressionTests, LengthTransparency)
    test_support.run_unittest(*test_classes)

    # verify reference counting
    if verbose and hasattr(sys, "gettotalrefcount"):
        import gc
        counts = [None] * 5
        for i in xrange(len(counts)):
            test_support.run_unittest(*test_classes)
            gc.collect()
            counts[i] = sys.gettotalrefcount()
        print counts

    # doctest the examples in the library reference
    test_support.run_doctest(sys.modules[__name__], verbose)
Example #48
0
def test_main(verbose=None):
    if support.is_jython:
        # XXX: CPython implementation details
        del EnumerateTestCase.test_tuple_reuse
        del TestReversed.test_len
    testclasses = (EnumerateTestCase, SubclassTestCase, TestEmpty, TestBig,
                   TestReversed)
    support.run_unittest(*testclasses)

    # verify reference counting
    import sys
    if verbose and hasattr(sys, "gettotalrefcount"):
        counts = [None] * 5
        for i in range(len(counts)):
            support.run_unittest(*testclasses)
            counts[i] = sys.gettotalrefcount()
        print(counts)
Example #49
0
def test_main(verbose=None):
    test_classes = (
        TestBase,
        TestDecorateSortUndecorate,
        TestBugs,
    )

    support.run_unittest(*test_classes)

    # verify reference counting
    if verbose and hasattr(sys, "gettotalrefcount"):
        import gc
        counts = [None] * 5
        for i in range(len(counts)):
            support.run_unittest(*test_classes)
            gc.collect()
            counts[i] = sys.gettotalrefcount()
        print(counts)
Example #50
0
 def test_getallocatedblocks(self):
     with_pymalloc = sysconfig.get_config_var('WITH_PYMALLOC')
     a = sys.getallocatedblocks()
     self.assertIs(type(a), int)
     if with_pymalloc:
         self.assertGreater(a, 0)
     else:
         self.assertGreaterEqual(a, 0)
     try:
         self.assertLess(a, sys.gettotalrefcount())
     except AttributeError:
         pass
     gc.collect()
     b = sys.getallocatedblocks()
     self.assertLessEqual(b, a)
     gc.collect()
     c = sys.getallocatedblocks()
     self.assertIn(c, range(b - 50, b + 50))
Example #51
0
 def test_refcount(self):
     if test.test_support.due_to_ironpython_bug(
             "http://tkbgitvstfat01:8080/WorkItemTracking/WorkItem.aspx?artifactMoniker=148342"
     ):
         return
     # n here must be a global in order for this test to pass while
     # tracing with a python function.  Tracing calls PyFrame_FastToLocals
     # which will add a copy of any locals to the frame object, causing
     # the reference count to increase by 2 instead of 1.
     global n
     self.assertRaises(TypeError, sys.getrefcount)
     c = sys.getrefcount(None)
     n = None
     self.assertEqual(sys.getrefcount(None), c + 1)
     del n
     self.assertEqual(sys.getrefcount(None), c)
     if hasattr(sys, "gettotalrefcount"):
         self.assertIsInstance(sys.gettotalrefcount(), int)
Example #52
0
def test_main(verbose=None):
    import sys
    from test import test_support
    test_classes = (TestTranforms,)

    with test_support.check_py3k_warnings(
            ("backquote not supported", SyntaxWarning)):
        test_support.run_unittest(*test_classes)

        # verify reference counting
        if verbose and hasattr(sys, "gettotalrefcount"):
            import gc
            counts = [None] * 5
            for i in xrange(len(counts)):
                test_support.run_unittest(*test_classes)
                gc.collect()
                counts[i] = sys.gettotalrefcount()
            print counts
Example #53
0
def test_main(verbose=None):
    import sys

    if test_support.is_jython:
        # CPython specific; returns a memory address
        del BaseTest.test_buffer_info

    test_support.run_unittest(*tests)

    # verify reference counting
    if verbose and hasattr(sys, "gettotalrefcount"):
        import gc
        counts = [None] * 5
        for i in xrange(len(counts)):
            test_support.run_unittest(*tests)
            gc.collect()
            counts[i] = sys.gettotalrefcount()
        print counts
Example #54
0
def test_main(verbose=None):
    from test import test_bisect

    test_classes = [TestBisectPython, TestBisectC,
                    TestInsortPython, TestInsortC,
                    TestErrorHandlingPython, TestErrorHandlingC]

    test_support.run_unittest(*test_classes)
    test_support.run_doctest(test_bisect, verbose)

    # verify reference counting
    if verbose and hasattr(sys, "gettotalrefcount"):
        import gc
        counts = [None] * 5
        for i in xrange(len(counts)):
            test_support.run_unittest(*test_classes)
            gc.collect()
            counts[i] = sys.gettotalrefcount()
        print counts
Example #55
0
def test_main(verbose=None):
    testclasses = [MersenneTwister_TestBasicOps, TestDistributions, TestModule]

    try:
        random.SystemRandom().random()
    except NotImplementedError:
        pass
    else:
        testclasses.append(SystemRandom_TestBasicOps)

    support.run_unittest(*testclasses)

    # verify reference counting
    import sys
    if verbose and hasattr(sys, "gettotalrefcount"):
        counts = [None] * 5
        for i in range(len(counts)):
            support.run_unittest(*testclasses)
            counts[i] = sys.gettotalrefcount()
        print(counts)
Example #56
0
def test_main(verbose=None):
    test_classes = (
        TestBase,
        TestDecorateSortUndecorate,
        TestBugs,
    )

    with test_support.check_py3k_warnings(
        ("the cmp argument is not supported", DeprecationWarning)):
        test_support.run_unittest(*test_classes)

        # verify reference counting
        if verbose and hasattr(sys, "gettotalrefcount"):
            import gc
            counts = [None] * 5
            for i in xrange(len(counts)):
                test_support.run_unittest(*test_classes)
                gc.collect()
                counts[i] = sys.gettotalrefcount()
            print counts
Example #57
0
def test_main(verbose=None):
    from test import test_bisect
    from types import BuiltinFunctionType
    import sys

    test_classes = [TestBisect, TestInsort]
    if isinstance(bisect_left, BuiltinFunctionType):
        test_classes.append(TestErrorHandling)

    test_support.run_unittest(*test_classes)
    test_support.run_doctest(test_bisect, verbose)

    # verify reference counting
    if verbose and hasattr(sys, "gettotalrefcount"):
        import gc
        counts = [None] * 5
        for i in xrange(len(counts)):
            test_support.run_unittest(*test_classes)
            gc.collect()
            counts[i] = sys.gettotalrefcount()
        print counts
Example #58
0
    def test_getallocatedblocks(self):
        try:
            import _testcapi
        except ImportError:
            with_pymalloc = support.with_pymalloc()
        else:
            try:
                alloc_name = _testcapi.pymem_getallocatorsname()
            except RuntimeError as exc:
                # "cannot get allocators name" (ex: tracemalloc is used)
                with_pymalloc = True
            else:
                with_pymalloc = (alloc_name in ('pymalloc', 'pymalloc_debug'))

        # Some sanity checks
        a = sys.getallocatedblocks()
        self.assertIs(type(a), int)
        if with_pymalloc:
            self.assertGreater(a, 0)
        else:
            # When WITH_PYMALLOC isn't available, we don't know anything
            # about the underlying implementation: the function might
            # return 0 or something greater.
            self.assertGreaterEqual(a, 0)
        try:
            # While we could imagine a Python session where the number of
            # multiple buffer objects would exceed the sharing of references,
            # it is unlikely to happen in a normal test run.
            self.assertLess(a, sys.gettotalrefcount())
        except AttributeError:
            # gettotalrefcount() not available
            pass
        gc.collect()
        b = sys.getallocatedblocks()
        self.assertLessEqual(b, a)
        gc.collect()
        c = sys.getallocatedblocks()
        self.assertIn(c, range(b - 50, b + 50))
Example #59
0
def test_main(verbose=None):
    import sys
    test_classes = (
        TestBasic,
        TestVariousIteratorArgs,
        TestSubclass,
    )

    test_support.run_unittest(*test_classes)

    # verify reference counting
    if verbose and hasattr(sys, "gettotalrefcount"):
        import gc
        counts = [None] * 5
        for i in xrange(len(counts)):
            test_support.run_unittest(*test_classes)
            gc.collect()
            counts[i] = sys.gettotalrefcount()
        print counts

    # doctests
    from test import test_deque
    test_support.run_doctest(test_deque, verbose)
def test_main(verbose=None):
    test_classes = (
        TestPartial,
        TestPartialSubclass,
        TestPythonPartial,
        TestUpdateWrapper,
        TestWraps,
        TestReduce,
    )
    if sys.flags.optimize >= 2:
        print >> sys.stderr, "test_functools --",
        print >> sys.stderr, "skipping some tests due to -O flag."
        sys.stderr.flush()
    test_support.run_unittest(*test_classes)

    # verify reference counting
    if verbose and hasattr(sys, "gettotalrefcount"):
        import gc
        counts = [None] * 5
        for i in xrange(len(counts)):
            test_support.run_unittest(*test_classes)
            gc.collect()
            counts[i] = sys.gettotalrefcount()
        print counts