summary refs log tree commit diff stats
path: root/ranger
diff options
context:
space:
mode:
authorhut <hut@lavabit.com>2010-05-16 20:14:33 +0200
committerhut <hut@lavabit.com>2010-05-16 20:14:33 +0200
commitce421875249b2ce9c6eacbade9b750e46ae80a20 (patch)
tree1bdd32b7c75c6c26562572c97c35efbcb365c243 /ranger
parentef0157ff37404cf757f57711c62bb352a2971fa6 (diff)
downloadranger-ce421875249b2ce9c6eacbade9b750e46ae80a20.tar.gz
fix exceptions from invalid unicode characters
Diffstat (limited to 'ranger')
-rw-r--r--ranger/container/bookmarks.py5
-rw-r--r--ranger/gui/curses_shortcuts.py26
-rw-r--r--ranger/gui/ui.py6
-rw-r--r--ranger/gui/widgets/browsercolumn.py37
4 files changed, 51 insertions, 23 deletions
diff --git a/ranger/container/bookmarks.py b/ranger/container/bookmarks.py
index f06b7498..1e801638 100644
--- a/ranger/container/bookmarks.py
+++ b/ranger/container/bookmarks.py
@@ -156,7 +156,10 @@ class Bookmarks(object):
 			for key, value in self.dct.items():
 				if type(key) == str\
 						and key in ALLOWED_KEYS:
-					f.write("{0}:{1}\n".format(str(key), str(value)))
+					try:
+						f.write("{0}:{1}\n".format(str(key), str(value)))
+					except:
+						pass
 
 			f.close()
 		self._update_mtime()
diff --git a/ranger/gui/curses_shortcuts.py b/ranger/gui/curses_shortcuts.py
index 82d8c787..84df5930 100644
--- a/ranger/gui/curses_shortcuts.py
+++ b/ranger/gui/curses_shortcuts.py
@@ -18,6 +18,22 @@ import _curses
 from ranger.ext.iter_tools import flatten
 from ranger.shared import SettingsAware
 
+def ascii_only(string):
+	# Some python versions have problems with invalid unicode strings.
+	# I think this exception is rare enough that this naive hack is enough.
+	# It simply removes all non-ascii chars from a string.
+	def validate_char(char):
+		try:
+			if ord(char) > 127:
+				return '?'
+		except:
+			return '?'
+		return char
+	if isinstance(string, str):
+		return ''.join(validate_char(c) for c in string)
+	return string
+
+
 class CursesShortcuts(SettingsAware):
 	"""
 	This class defines shortcuts to faciliate operations with curses.
@@ -33,12 +49,22 @@ class CursesShortcuts(SettingsAware):
 			self.win.addstr(*args)
 		except (_curses.error, TypeError):
 			pass
+		except UnicodeEncodeError:
+			try:
+				self.win.addstr(*(ascii_only(obj) for obj in args))
+			except (_curses.error, TypeError):
+				pass
 
 	def addnstr(self, *args):
 		try:
 			self.win.addnstr(*args)
 		except (_curses.error, TypeError):
 			pass
+		except UnicodeEncodeError:
+			try:
+				self.win.addnstr(*(ascii_only(obj) for obj in args))
+			except (_curses.error, TypeError):
+				pass
 
 	def color(self, *keys):
 		"""Change the colors from now on."""
diff --git a/ranger/gui/ui.py b/ranger/gui/ui.py
index e9c20395..b0c1a352 100644
--- a/ranger/gui/ui.py
+++ b/ranger/gui/ui.py
@@ -19,6 +19,7 @@ import curses
 import _curses
 
 from .displayable import DisplayableContainer
+from ranger.gui.curses_shortcuts import ascii_only
 from ranger.container.keymap import CommandArgs
 from .mouse_event import MouseEvent
 
@@ -239,7 +240,10 @@ class UI(DisplayableContainer):
 				split = cwd.rsplit(os.sep, self.settings.shorten_title)
 				if os.sep in split[0]:
 					cwd = os.sep.join(split[1:])
-			sys.stdout.write("\033]2;ranger:" + cwd + "\007")
+			try:
+				sys.stdout.write("\033]2;ranger:" + cwd + "\007")
+			except UnicodeEncodeError:
+				sys.stdout.write("\033]2;ranger:" + ascii_only(cwd) + "\007")
 		self.win.refresh()
 
 	def finalize(self):
diff --git a/ranger/gui/widgets/browsercolumn.py b/ranger/gui/widgets/browsercolumn.py
index fbacbccd..1e24e2dd 100644
--- a/ranger/gui/widgets/browsercolumn.py
+++ b/ranger/gui/widgets/browsercolumn.py
@@ -295,27 +295,22 @@ class BrowserColumn(Pager):
 				this_color.append(drawn.exists and 'good' or 'bad')
 
 			string = drawn.basename
-			try:
-				if self.main_column:
-					if tagged:
-						self.win.addnstr(line, 0, text, self.wid - 2)
-					elif self.wid > 1:
-						self.win.addnstr(line, 1, text, self.wid - 2)
-				else:
-					self.win.addnstr(line, 0, text, self.wid)
-
-				if self.display_infostring and drawn.infostring \
-						and self.settings.display_size_in_main_column:
-					info = drawn.infostring
-					x = self.wid - 1 - len(info)
-					if info is BAD_INFO:
-						bad_info_color = (x, len(str(info)))
-					if x > 0:
-						self.win.addstr(line, x, str(info) + ' ')
-			except:
-				# the drawing of the last string will cause an error
-				# because ncurses tries to move out of the bounds
-				pass
+			if self.main_column:
+				if tagged:
+					self.addnstr(line, 0, text, self.wid - 2)
+				elif self.wid > 1:
+					self.addnstr(line, 1, text, self.wid - 2)
+			else:
+				self.addnstr(line, 0, text, self.wid)
+
+			if self.display_infostring and drawn.infostring \
+					and self.settings.display_size_in_main_column:
+				info = drawn.infostring
+				x = self.wid - 1 - len(info)
+				if info is BAD_INFO:
+					bad_info_color = (x, len(str(info)))
+				if x > 0:
+					self.addstr(line, x, str(info) + ' ')
 
 			self.color_at(line, 0, self.wid, this_color)
 			if bad_info_color: