~james-w/ubuntu/lucid/psycopg2/precise-backport

« back to all changes in this revision

Viewing changes to scripts/refcounter.py

  • Committer: Bazaar Package Importer
  • Author(s): Fabio Tranchitella, Jakub Wilk, Fabio Tranchitella
  • Date: 2011-06-19 18:25:53 UTC
  • mfrom: (5.1.10 sid)
  • Revision ID: james.westby@ubuntu.com-20110619182553-uye7z0g5ewab98px
Tags: 2.4.2-1
[ Jakub Wilk ]
* Add Debian Python Modules Team to Uploaders.

[ Fabio Tranchitella ]
* New upstream release.
* debian/watch: updated, use pypi.
* debian/control, debian/rules: switched to dh_python2.
* debian/control: bumped Standard-Version to 3.9.2, no changes required.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
"""Detect reference leaks after several unit test runs.
 
3
 
 
4
The script runs the unit test and counts the objects alive after the run. If
 
5
the object count differs between the last two runs, a report is printed and the
 
6
script exits with error 1.
 
7
"""
 
8
 
 
9
# Copyright (C) 2011 Daniele Varrazzo <daniele.varrazzo@gmail.com>
 
10
#
 
11
# This program is free software; you can redistribute it and/or modify
 
12
# it under the terms of the GNU General Public License as published by
 
13
# the Free Software Foundation; either version 2 of the License, or
 
14
# (at your option) any later version.
 
15
#
 
16
# This program is distributed in the hope that it will be useful,
 
17
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
18
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
19
# GNU General Public License for more details.
 
20
#
 
21
# You should have received a copy of the GNU General Public License
 
22
# along with this program; if not, write to the Free Software
 
23
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 
24
 
 
25
import gc
 
26
import sys
 
27
import difflib
 
28
import unittest
 
29
from pprint import pprint
 
30
from collections import defaultdict
 
31
 
 
32
def main():
 
33
    opt = parse_args()
 
34
 
 
35
    import psycopg2.tests
 
36
    test = psycopg2.tests
 
37
    if opt.suite:
 
38
        test = getattr(test, opt.suite)
 
39
 
 
40
    sys.stdout.write("test suite %s\n" % test.__name__)
 
41
 
 
42
    for i in range(1, opt.nruns + 1):
 
43
        sys.stdout.write("test suite run %d of %d\n" % (i, opt.nruns))
 
44
        runner = unittest.TextTestRunner()
 
45
        runner.run(test.test_suite())
 
46
        dump(i, opt)
 
47
 
 
48
    f1 = open('debug-%02d.txt' % (opt.nruns - 1)).readlines()
 
49
    f2 = open('debug-%02d.txt' % (opt.nruns)).readlines()
 
50
    for line in difflib.unified_diff(f1, f2,
 
51
            "run %d" % (opt.nruns - 1), "run %d" % opt.nruns):
 
52
        sys.stdout.write(line)
 
53
 
 
54
    rv = f1 != f2 and 1 or 0
 
55
 
 
56
    if opt.objs:
 
57
        f1 = open('objs-%02d.txt' % (opt.nruns - 1)).readlines()
 
58
        f2 = open('objs-%02d.txt' % (opt.nruns)).readlines()
 
59
        for line in difflib.unified_diff(f1, f2,
 
60
                "run %d" % (opt.nruns - 1), "run %d" % opt.nruns):
 
61
            sys.stdout.write(line)
 
62
 
 
63
    return rv
 
64
 
 
65
def parse_args():
 
66
    import optparse
 
67
 
 
68
    parser = optparse.OptionParser(description=__doc__)
 
69
    parser.add_option('--nruns', type='int', metavar="N", default=3,
 
70
        help="number of test suite runs [default: %default]")
 
71
    parser.add_option('--suite', metavar="NAME",
 
72
        help="the test suite to run (e.g. 'test_cursor'). [default: all]")
 
73
    parser.add_option('--objs', metavar="TYPE",
 
74
        help="in case of leaks, print a report of object TYPE "
 
75
            "(support still incomplete)")
 
76
 
 
77
    opt, args = parser.parse_args()
 
78
    return opt
 
79
 
 
80
 
 
81
def dump(i, opt):
 
82
    gc.collect()
 
83
    objs = gc.get_objects()
 
84
 
 
85
    c = defaultdict(int)
 
86
    for o in objs:
 
87
        c[type(o)] += 1
 
88
 
 
89
    pprint(
 
90
        sorted(((v,str(k)) for k,v in c.items()), reverse=True),
 
91
        stream=open("debug-%02d.txt" % i, "w"))
 
92
 
 
93
    if opt.objs:
 
94
        co = []
 
95
        t = getattr(__builtins__, opt.objs)
 
96
        for o in objs:
 
97
            if type(o) is t:
 
98
                co.append(o)
 
99
 
 
100
        # TODO: very incomplete
 
101
        if t is dict:
 
102
            co.sort(key = lambda d: d.items())
 
103
        else:
 
104
            co.sort()
 
105
 
 
106
        pprint(co, stream=open("objs-%02d.txt" % i, "w"))
 
107
 
 
108
 
 
109
if __name__ == '__main__':
 
110
    sys.exit(main())
 
111