From f27ff0a5693aa6d96937a25e048636e4a3bf6524 Mon Sep 17 00:00:00 2001 From: Maximillian Dornseif Date: Sun, 27 May 2012 22:05:57 +0200 Subject: [PATCH 1/2] Fix encoding issues with StringIO The current code took the (unicode) view and stuffed it into a `StringIO`. The resulting encoding beeing seen by PythonTidy was dependant on the configuration of the system the code was running on and on implementation details in StringIO. It than read the result (a bytestream) and wrote it into the (unicode) view. Again with indeterminable results based on the default encodings of the host system. For me opening the file http://filez.foxel.org/211a0I1L3t3n2I3P1L25 and pressing CTRL-ALT-CMD-T worked once but pressing it twice resulted in an `UnicodeException`. https://github.com/witsch/SublimePythonTidy/issues/1 shows the same issue and https://github.com/do3cc/SublimePythonTidy/commit/efc88be seems to be an attempt to fix it. This changeset ensures that a) views are treated as unicode b) StringIOs are treated as bytestream --- Tidy.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/Tidy.py b/Tidy.py index cf949ce..a6944bf 100644 --- a/Tidy.py +++ b/Tidy.py @@ -1,11 +1,13 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- + from sublime_plugin import TextCommand -from sublime import Region +from sublime import Region, error_message from subprocess import call from os.path import abspath, expanduser, exists, join from StringIO import StringIO from sys import path - # load the git submodule extra = abspath('PythonTidy') if not exists(join(extra, '.git')): @@ -32,7 +34,11 @@ def run(self, edit): setup() view = self.view region = Region(0L, view.size()) - source = StringIO(view.substr(region)) + sourcestr = view.substr(region) + encoding = view.encoding() + if encoding = 'undefined': + encoding = 'utf-8' + source = StringIO(sourcestr.encode(encoding)) output = StringIO() PythonTidy.tidy_up(source, output) - view.replace(edit, region, output.getvalue()) + view.replace(edit, region, output.getvalue().decode(encoding)) From d41086e7abea50db0599c9714005b1a7ced490b6 Mon Sep 17 00:00:00 2001 From: Maximillian Dornseif Date: Sun, 27 May 2012 22:30:35 +0200 Subject: [PATCH 2/2] f27ff0a checked in the wrong file --- Tidy.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Tidy.py b/Tidy.py index a6944bf..8631dfa 100644 --- a/Tidy.py +++ b/Tidy.py @@ -2,7 +2,7 @@ # -*- coding: utf-8 -*- from sublime_plugin import TextCommand -from sublime import Region, error_message +from sublime import Region from subprocess import call from os.path import abspath, expanduser, exists, join from StringIO import StringIO @@ -36,7 +36,7 @@ def run(self, edit): region = Region(0L, view.size()) sourcestr = view.substr(region) encoding = view.encoding() - if encoding = 'undefined': + if encoding == 'undefined': encoding = 'utf-8' source = StringIO(sourcestr.encode(encoding)) output = StringIO()