Sublime Forum

[SOLVED] Problems when non-ascii characters in path

#1

A user reported an issue with DetectSyntax and it’s related to him having a Russian username. He’s on Windows 7 64 bit and the path to the plugin directory is

C:\Users\Андрей\AppData\Roaming\Sublime Text 2\Packages\DetectSyntax

and this code blows up

	def function_matches(self, rule):
		function = rule.get("function")
		path_to_file = function.get("source")
		function_name = function.get("name")

		if not path_to_file:
			path_to_file = function_name + '.py'

		# is path_to_file absolute?
		if not os.path.isabs(path_to_file):
			# it's not, so look in Packages/User
			if os.path.exists(self.user_dir + os.path.sep + path_to_file):
				path_to_file = self.user_dir + os.path.sep + path_to_file
			else:
				# now look in the plugin's directory
				path_to_file = self.plugin_dir + os.path.sep + path_to_file

		# bubble exceptions up only if the user wants them
		try:
			with open(path_to_file, 'r') as the_file:
				function_source = the_file.read()
		except:
			if self.reraise_exceptions:
				raise
			else:
				return False

		try:
			exec(function_source)
		except:
			if self.reraise_exceptions:
				raise
			else:
				return False

		try:
			return eval(function_name + '(\'' + self.file_name + '\')')
		except:
			if self.reraise_exceptions:
				raise
			else:
				return False

In the console, he gets

File ".\DetectSyntax.py", line 140, in function_matches
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc0 in position 9: ordinal not in range(128)

Line 140 is

path_to_file = self.plugin_dir + os.path.sep + path_to_file

The gist of what’s happening is DetectSyntax is looking for a file that has a function defined in it. It first checks in the User folder and if it doesn’t find it, it looks in the plugin’s folder. I’ve been experimenting with different things and I can reproduce it on a Mac by switching to a Russian keyboard and putting Russian characters in a directory name and trying to load a file from it. What I can’t seem to figure out, though, is how to resolve it.

Anyone have any insight?

0 Likes

#2

The solution appears to be

os.getcwdu()

instead of

os.getcwd()

I need to know the directory the plugin is in so I have

import sublime, sublime_plugin
import os, string, re

plugin_directory = os.getcwdu()

class DetectSyntaxCommand(sublime_plugin.EventListener):

to grab it during initialization. Simple mistake. Lesson learned.

For other plugin developers, you might want to check these sorts of things in your own plugins. The user I was working with told me there are a handful of plugins he tried that didn’t work because of this issue. And because they are plugins providing functionality he really needs, he can’t currently use ST2.

0 Likes