Author: Armin Rigo <[email protected]>
Branch: gc-del
Changeset: r63548:31de31576e68
Date: 2013-04-22 17:19 +0200
http://bitbucket.org/pypy/pypy/changeset/31de31576e68/

Log:    Transplant from "default" the fixes done to dotviewer.

diff --git a/dotviewer/drawgraph.py b/dotviewer/drawgraph.py
--- a/dotviewer/drawgraph.py
+++ b/dotviewer/drawgraph.py
@@ -7,9 +7,9 @@
 import re, os, math
 import pygame
 from pygame.locals import *
+from strunicode import forceunicode
 
 
-RAW_ENCODING = "utf-8"
 this_dir = os.path.dirname(os.path.abspath(__file__))
 FONT = os.path.join(this_dir, 'font', 'DroidSans.ttf')
 FIXEDFONT = os.path.join(this_dir, 'font', 'DroidSansMono.ttf')
@@ -52,12 +52,6 @@
     else:
         return default
 
-def forceunicode(name):
-    return name if isinstance(name, unicode) else name.decode(RAW_ENCODING)
-
-def forcestr(name):
-    return name if isinstance(name, str) else name.encode(RAW_ENCODING)
-
 
 class GraphLayout:
     fixedfont = False
diff --git a/dotviewer/graphclient.py b/dotviewer/graphclient.py
--- a/dotviewer/graphclient.py
+++ b/dotviewer/graphclient.py
@@ -1,6 +1,7 @@
 import os, sys, re
 import subprocess
 import msgstruct
+from strunicode import forcestr
 
 this_dir = os.path.dirname(os.path.abspath(__file__))
 GRAPHSERVER = os.path.join(this_dir, 'graphserver.py')
@@ -33,7 +34,6 @@
     def reload(graph_id):
         page = getpage(graph_id)
         if save_tmp_file:
-            from drawgraph import forcestr
             f = open(save_tmp_file, 'w')
             f.write(forcestr(page.source))
             f.close()
@@ -76,7 +76,6 @@
 
 def page_messages(page, graph_id):
     import graphparse
-    from drawgraph import forcestr
     return graphparse.parse_dot(graph_id, forcestr(page.source), page.links,
                                 getattr(page, 'fixedfont', False))
 
@@ -129,7 +128,14 @@
 
 def spawn_local_handler():
     if hasattr(sys, 'pypy_objspaceclass'):
-        python = '/usr/bin/python'
+        # if 'python' is actually PyPy, e.g. in a virtualenv, then
+        # try hard to find a real CPython
+        try:
+            python = subprocess.check_output(
+                'env -i $SHELL -l -c "which python"', shell=True).strip()
+        except subprocess.CalledProcessError:
+            # did not work, fall back to 'python'
+            python = 'python'
     else:
         python = sys.executable
     args = [python, '-u', GRAPHSERVER, '--stdio']
diff --git a/dotviewer/graphdisplay.py b/dotviewer/graphdisplay.py
--- a/dotviewer/graphdisplay.py
+++ b/dotviewer/graphdisplay.py
@@ -4,7 +4,8 @@
 from pygame.locals import *
 from drawgraph import GraphRenderer, FIXEDFONT
 from drawgraph import Node, Edge
-from drawgraph import EventQueue, wait_for_events, forceunicode, forcestr
+from drawgraph import EventQueue, wait_for_events
+from strunicode import forceunicode, forcestr
 
 
 METAKEYS = dict([
diff --git a/dotviewer/graphpage.py b/dotviewer/graphpage.py
--- a/dotviewer/graphpage.py
+++ b/dotviewer/graphpage.py
@@ -45,7 +45,7 @@
 class DotFileGraphPage(GraphPage):
     def compute(self, dotfile):
         import codecs
-        from drawgraph import RAW_ENCODING
+        from strunicode import RAW_ENCODING
         f = codecs.open(dotfile, 'r', RAW_ENCODING)
         self.source = f.read()
         f.close()
diff --git a/dotviewer/msgstruct.py b/dotviewer/msgstruct.py
--- a/dotviewer/msgstruct.py
+++ b/dotviewer/msgstruct.py
@@ -1,5 +1,6 @@
 import sys, os
 from struct import pack, unpack, calcsize
+from strunicode import tryencode
 
 MAGIC = -0x3b83728b
 
@@ -27,6 +28,7 @@
 def message(tp, *values):
     #print >> sys.stderr, tp, values
     typecodes = ['']
+    values = map(tryencode, values)
     for v in values:
         if type(v) is str:
             typecodes.append('%ds' % len(v))
diff --git a/dotviewer/test/test_interactive_unicode.py 
b/dotviewer/test/test_interactive_unicode.py
--- a/dotviewer/test/test_interactive_unicode.py
+++ b/dotviewer/test/test_interactive_unicode.py
@@ -4,7 +4,7 @@
 import py
 import sys, os, signal, thread, time, codecs
 from dotviewer.conftest import option
-from dotviewer.drawgraph import RAW_ENCODING
+from dotviewer.strunicode import RAW_ENCODING
 
 SOURCE1 = u"""digraph G{
 &#955; -> b
diff --git a/dotviewer/test/test_unicode_util.py 
b/dotviewer/test/test_unicode_util.py
--- a/dotviewer/test/test_unicode_util.py
+++ b/dotviewer/test/test_unicode_util.py
@@ -3,7 +3,7 @@
 #
 import py
 import codecs
-from dotviewer.drawgraph import RAW_ENCODING, forcestr, forceunicode
+from dotviewer.strunicode import RAW_ENCODING, forcestr, forceunicode, 
tryencode
 
 SOURCE1 = u"""digraph G{
 &#955; -> b
@@ -18,7 +18,7 @@
     def test_idempotent(self):
         x = u"a"
         assert forceunicode(forcestr(x)) == x
-        
+
         x = u"&#955;"
         assert forceunicode(forcestr(x)) == x
 
@@ -40,7 +40,7 @@
         x_u = forceunicode(x_e)
         assert forceunicode(x_u) == x_u
 
-    def test_file(self):       
+    def test_file(self):
         udir = py.path.local.make_numbered_dir(prefix='usession-dot-', keep=3)
         full_filename = str(udir.join(FILENAME))
         f = codecs.open(full_filename, 'wb', RAW_ENCODING)
@@ -55,3 +55,19 @@
         f3.close()
         result = (c == SOURCE1)
         assert result
+
+    def test_only_unicode_encode(self):
+        sut =      [1,   u"a", "miau", u"&#955;"]
+        expected = [int, str,  str,    str ]
+
+        results = map(tryencode, sut)
+        for result, expected_type in zip(results, expected):
+            assert isinstance(result, expected_type)
+
+    def test_forceunicode_should_not_fail(self):
+        garbage = "\xef\xff\xbb\xbf\xce\xbb\xff\xff"   # garbage with a lambda
+        result = forceunicode(garbage)                 # should not raise
+
+    def test_forcestr_should_not_fail(self):
+        garbage = u"\xef\xff\xbb\xbf\xce\xbb\xff\xff"  # garbage
+        result = forcestr(garbage)                     # should not raise
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to