test_rl_accel.py: fix ref count checks
authorrgbecker
Wed, 17 May 2006 09:39:29 +0000
changeset 2612 e92bcf68420f
parent 2611 edcc26c81216
child 2613 a14cad0267e5
test_rl_accel.py: fix ref count checks
reportlab/test/test_rl_accel.py
--- a/reportlab/test/test_rl_accel.py	Wed May 17 09:36:56 2006 +0000
+++ b/reportlab/test/test_rl_accel.py	Wed May 17 09:39:29 2006 +0000
@@ -4,6 +4,31 @@
 from reportlab.test import unittest
 from reportlab.test.utils import makeSuiteForClasses, printLocation
 
+def getrc(defns,depth=1):
+    from sys import getrefcount, _getframe
+    f = _getframe(depth)
+    G0 = f.f_globals
+    L = f.f_locals
+    if L is not G0:
+        LL = [L]
+        while 1:
+            f = f.f_back
+            G = f.f_globals
+            L = f.f_locals
+            if G is not G0 or G is L: break
+            LL.append(L)
+        L = {}
+        for l in reversed(LL):
+            L.update(l)
+    else:
+        L = L.copy()
+    G0 = G0.copy()
+    return [getrefcount(eval(x,L,G0))-1 for x in defns.split()]
+
+def checkrc(defns,rcv0):
+    rcv1 = getrc(defns,2)
+    return ' '.join(["%s %d-->%d" % (x,v,w) for x,v,w in zip(defns.split(),rcv0,rcv1) if v!=w])
+
 class RlAccelTestCase(unittest.TestCase):
 
     def testFpStr(self):
@@ -39,21 +64,22 @@
 
     def testStringWidth(self):
         from _rl_accel import stringWidthU
-        from reportlab.pdfbase.pdfmetrics import _py_stringWidth, _py_getFont, registerFont
+        from reportlab.pdfbase.pdfmetrics import _py_stringWidth, getFont, registerFont, _fonts 
         from reportlab.pdfbase.ttfonts import TTFont
-        from sys import getrefcount
         ttfn = 'Luxi-Serif'
         t1fn = 'Times-Roman'
         registerFont(TTFont(ttfn, "luxiserif.ttf"))
-        ttf = _py_getFont(ttfn)
-        t1f = _py_getFont(t1fn)
+        ttf = getFont(ttfn)
+        t1f = getFont(t1fn)
         testCp1252 = 'copyright %s trademark %s registered %s ReportLab! Ol%s!' % (chr(169), chr(153),chr(174), chr(0xe9))
         enc='cp1252'
         senc = 'utf8'
         intern(senc)
         ts = 'ABCDEF\xce\x91\xce\xb2G'
         utext = 'ABCDEF\xce\x91\xce\xb2G'.decode('utf8')
-        fontSize = 12.
+        fontSize = 12
+        defns="ttfn t1fn ttf t1f testCp1252 enc senc ts utext fontSize ttf.face ttf.face.charWidths ttf.face.defaultWidth t1f.widths t1f.encName t1f.substitutionFonts _fonts"
+        rcv = getrc(defns)
         def tfunc(ts,fn,fontSize,enc):
             w1 = stringWidthU(ts,fn,fontSize,enc)
             w2 = _py_stringWidth(ts,fn,fontSize,enc)
@@ -64,21 +90,25 @@
         tfunc(ts,ttfn,fontSize,senc)
         tfunc(testCp1252,ttfn,fontSize,enc)
         tfunc(utext,ttfn,fontSize,senc)
+        rcc = checkrc(defns,rcv)
+        assert not rcc, "rc diffs (%s)" % rcc
 
     def test_instanceStringWidth(self):
-        from reportlab.pdfbase.pdfmetrics import registerFont, _py_getFont
+        from reportlab.pdfbase.pdfmetrics import registerFont, getFont, _fonts, unicode2T1
         from reportlab.pdfbase.ttfonts import TTFont
         ttfn = 'Luxi-Serif'
         t1fn = 'Times-Roman'
         registerFont(TTFont(ttfn, "luxiserif.ttf"))
-        ttf = _py_getFont(ttfn)
-        t1f = _py_getFont(t1fn)
+        ttf = getFont(ttfn)
+        t1f = getFont(t1fn)
         testCp1252 = 'copyright %s trademark %s registered %s ReportLab! Ol%s!' % (chr(169), chr(153),chr(174), chr(0xe9))
         enc='cp1252'
         senc = 'utf8'
         ts = 'ABCDEF\xce\x91\xce\xb2G'
         utext = 'ABCDEF\xce\x91\xce\xb2G'.decode(senc)
-        fontSize = 12.
+        fontSize = 12
+        defns="ttfn t1fn ttf t1f testCp1252 enc senc ts utext fontSize ttf.face ttf.face.charWidths ttf.face.defaultWidth t1f.widths t1f.encName t1f.substitutionFonts _fonts"
+        rcv = getrc(defns)
         def tfunc(f,ts,fontSize,enc):
             w1 = f.stringWidth(ts,fontSize,enc)
             w2 = f._py_stringWidth(ts,fontSize,enc)
@@ -89,12 +119,14 @@
         tfunc(ttf,ts,fontSize,senc)
         tfunc(ttf,testCp1252,fontSize,enc)
         tfunc(ttf,utext,fontSize,senc)
+        rcc = checkrc(defns,rcv)
+        assert not rcc, "rc diffs (%s)" % rcc
 
     def test_unicode2T1(self):
-        from reportlab.pdfbase.pdfmetrics import _py_unicode2T1, _py_getFont
+        from reportlab.pdfbase.pdfmetrics import _py_unicode2T1, getFont, _fonts
         from _rl_accel import unicode2T1
         t1fn = 'Times-Roman'
-        t1f = _py_getFont(t1fn)
+        t1f = getFont(t1fn)
         enc = 'cp1252'
         senc = 'utf8'
         testCp1252 = ('copyright %s trademark %s registered %s ReportLab! Ol%s!' % (chr(169), chr(153),chr(174), chr(0xe9))).decode(enc)
@@ -103,12 +135,17 @@
             w1 = unicode2T1(ts,[f]+f.substitutionFonts)
             w2 = _py_unicode2T1(ts,[f]+f.substitutionFonts)
             assert w1==w2,"%r != %r" % (w1,w2)
+        defns="t1fn t1f testCp1252 enc senc utext t1f.widths t1f.encName t1f.substitutionFonts _fonts"
+        rcv = getrc(defns)
         tfunc(t1f,testCp1252)
         tfunc(t1f,utext)
+        rcc = checkrc(defns,rcv)
+        assert not rcc, "rc diffs (%s)" % rcc
 
     def test_getFont(self):
-        from reportlab.pdfbase.pdfmetrics import _py_getFont
+        from reportlab.pdfbase.pdfmetrics import _py_getFont, getFont
         from _rl_accel import getFontU
+        assert getFontU is getFont
         t1fn = 'Times-Roman'
         assert _py_getFont(t1fn) is getFontU(t1fn)