rl_accel: fixed GetExcV
authorrgbecker
Tue, 16 May 2006 17:58:25 +0000
changeset 2610 eed2609cb7a4
parent 2609 396e7b5907c9
child 2611 edcc26c81216
rl_accel: fixed GetExcV
rl_addons/rl_accel/_rl_accel.c
rl_addons/rl_accel/tests/getrc.py
rl_addons/rl_accel/tests/t0.py
rl_addons/rl_accel/tests/t1.py
--- a/rl_addons/rl_accel/_rl_accel.c	Tue May 16 14:58:47 2006 +0000
+++ b/rl_addons/rl_accel/_rl_accel.c	Tue May 16 17:58:25 2006 +0000
@@ -735,9 +735,7 @@
 
 static PyObject *_GetExcValue(void)
 {
-	PyObject *type = NULL, *value = NULL, *tb = NULL;
-	PyObject *result = NULL;
-	PyThreadState *tstate = PyThreadState_Get();
+	PyObject *type = NULL, *value = NULL, *tb = NULL, *result=NULL;
 	PyErr_Fetch(&type, &value, &tb);
 	PyErr_NormalizeException(&type, &value, &tb);
 	if(PyErr_Occurred()) goto L_BAD;
@@ -745,17 +743,8 @@
 		value = Py_None;
 		Py_INCREF(value);
 		}
-	Py_XDECREF(tstate->exc_type);
-	Py_XDECREF(tstate->exc_value);
-	Py_XDECREF(tstate->exc_traceback);
-	tstate->exc_type = type;
-	tstate->exc_value = value;
-	tstate->exc_traceback = tb;
+	Py_XINCREF(value);
 	result = value;
-	Py_XINCREF(result);
-	type = 0;
-	value = 0;
-	tb = 0;
 L_BAD:
 	Py_XDECREF(type);
 	Py_XDECREF(value);
@@ -892,16 +881,19 @@
 
 			if(!PyErr_ExceptionMatches(PyExc_UnicodeEncodeError)) ERROR_EXIT();
 			_o1 = _GetExcValue(); if(!_o1) ERROR_EXIT();
+			PyErr_Clear();
 			_o2 = _GetAttrString(_o1, "args"); if(!_o2) ERROR_EXIT();
 			Py_DECREF(_o1);
 			_o1 = PySequence_GetSlice(_o2, 2, 4); if(!_o1) ERROR_EXIT();
 			Py_DECREF(_o2);
-			_o2 = PySequence_GetItem(_o1, 0); if(!_o2) ERROR_EXIT();
-			i = PyInt_AsLong(_o2); if(PyErr_Occurred()) ERROR_EXIT();
-			Py_DECREF(_o2);
-			_o2 = PySequence_GetItem(_o1, 1); if(!_o1) ERROR_EXIT();
-			j = PyInt_AsLong(_o2); if(PyErr_Occurred()) ERROR_EXIT();
-			Py_DECREF(_o2);
+				_o2 = PySequence_GetItem(_o1, 0); if(!_o2) ERROR_EXIT();
+				i = PyInt_AsLong(_o2); if(PyErr_Occurred()) ERROR_EXIT();
+				Py_DECREF(_o2);
+
+				_o2 = PySequence_GetItem(_o1, 1); if(!_o1) ERROR_EXIT();
+				j = PyInt_AsLong(_o2); if(PyErr_Occurred()) ERROR_EXIT();
+				Py_DECREF(_o2);
+
 			Py_DECREF(_o1); _o2 = _o1 = 0;
 
 			if(i){
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/rl_addons/rl_accel/tests/getrc.py	Tue May 16 17:58:25 2006 +0000
@@ -0,0 +1,24 @@
+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])
--- a/rl_addons/rl_accel/tests/t0.py	Tue May 16 14:58:47 2006 +0000
+++ b/rl_addons/rl_accel/tests/t0.py	Tue May 16 17:58:25 2006 +0000
@@ -1,10 +1,12 @@
-import os, time, shutil, gc
+import os, time, sys
 from reportlab.pdfbase.pdfmetrics import _py_getFont, _py_unicode2T1
 from _rl_accel import unicode2T1
+from getrc import getrc, checkrc
+utext = 'ABCDEF \xce\x91BCDEF\xce\x91'.decode('utf8')
 utext = 'This is the end of the \xce\x91\xce\xb2 world. This is the end of the \xce\x91\xce\xb2 world jap=\xe3\x83\x9b\xe3\x83\x86. This is the end of the \xce\x91\xce\xb2 world. This is the end of the \xce\x91\xce\xb2 world jap=\xe3\x83\x9b\xe3\x83\x86'.decode('utf8')
 fontName = 'Times-Roman'
 fontSize=12
-N = 10000
+N = 30000
 def tim(msg,func,*args):
     t0 = time.time()
     for i in xrange(N):
@@ -16,12 +18,11 @@
 #print tim('stringWidth2', stringWidth2, utext, fontName, fontSize)
 
 font = _py_getFont(fontName)
-print unicode2T1(utext,[font]+font.substitutionFonts)==_py_unicode2T1(utext,[font]+font.substitutionFonts)
-print unicode2T1(u'ABCDEF',[font]+font.substitutionFonts)
-print _py_unicode2T1(u'ABCDEF',[font]+font.substitutionFonts)
-for i in (0,1,2):
-    print gc.collect(),len(gc.get_objects())
-    print tim('unicode2T1',unicode2T1,utext,[font]+font.substitutionFonts)
-    print gc.collect(),len(gc.get_objects())
-    print tim('_py_unicode2T1',_py_unicode2T1,utext,[font]+font.substitutionFonts)
-    print gc.collect(),len(gc.get_objects())
+assert unicode2T1(utext,[font]+font.substitutionFonts)==_py_unicode2T1(utext,[font]+font.substitutionFonts)
+#print unicode2T1(u'ABCDEF',[font]+font.substitutionFonts)
+#print _py_unicode2T1(u'ABCDEF',[font]+font.substitutionFonts)
+defns = "font font.widths font.substitutionFonts font.encName fontName utext"
+rcv = getrc(defns)
+print tim('unicode2T1',unicode2T1,utext,[font]+font.substitutionFonts)
+print tim('_py_unicode2T1',_py_unicode2T1,utext,[font]+font.substitutionFonts)
+print "rc diffs=(%s)" % checkrc(defns,rcv)
--- a/rl_addons/rl_accel/tests/t1.py	Tue May 16 14:58:47 2006 +0000
+++ b/rl_addons/rl_accel/tests/t1.py	Tue May 16 17:58:25 2006 +0000
@@ -1,6 +1,7 @@
 import time
-from reportlab.pdfbase.pdfmetrics import _fonts, findFontAndRegister, getFont as _py_getFont
-from sys import getrefcount as rc
+from reportlab.pdfbase.pdfmetrics import _fonts, findFontAndRegister, _py_getFont
+from _rl_accel import getFontU
+from getrc import getrc, checkrc
 import sys
 #fn0 = 'Times-Bold'
 #fn1 = 'Times-Roman'
@@ -11,10 +12,17 @@
         x = func(*args)
     t1 = time.time()
     return "%s N=%d t=%.3f\n%r" % (msg,N,t1-t0,x)
+fn0='Courier'
+fn1='Helvetica'
+font0=_py_getFont(fn0)
+font1=_py_getFont(fn1)
+getFontU(fn0)
+
+defns = "font0 font1 fn0 fn1 _fonts"
+rcv = getrc(defns)
 for i in (0,1,2):
-    for fn in 'Courier','Helvetica':
-        print hex(id(_fonts)), hex(id(findFontAndRegister)), hex(id(fn)), rc(fn)
-        from _rl_accel import getFontU
+    for fn in fn0,fn1:
         print tim(N,'getFontU',getFontU,fn)
         print tim(N,'_py_getFont',_py_getFont,fn)
-        print hex(id(_fonts)), hex(id(findFontAndRegister)), hex(id(fn)), rc(fn)
+del fn
+print "rc diffs=(%s)" % checkrc(defns,rcv)