Home Download Buy Blog Forum Support

[SOLVED] Problems when non-ascii characters in path

[SOLVED] Problems when non-ascii characters in path

Postby phillip.koebbe on Fri Jun 29, 2012 8:31 pm

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

Code: Select all
   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

Code: Select all
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

Code: Select all
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?
Last edited by phillip.koebbe on Mon Jul 02, 2012 4:02 pm, edited 1 time in total.
-- Phillip

Code: Select all
while self.living? do
    mistake = Mistake.new
    self.learn_from!(mistake) unless mistake.fatal?
end
phillip.koebbe
 
Posts: 208
Joined: Tue Dec 20, 2011 4:58 pm

Re: Problems when non-ascii characters in path

Postby phillip.koebbe on Mon Jul 02, 2012 4:02 pm

The solution appears to be

os.getcwdu()

instead of

os.getcwd()

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

Code: Select all
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.
-- Phillip

Code: Select all
while self.living? do
    mistake = Mistake.new
    self.learn_from!(mistake) unless mistake.fatal?
end
phillip.koebbe
 
Posts: 208
Joined: Tue Dec 20, 2011 4:58 pm


Return to Plugin Development

Who is online

Users browsing this forum: No registered users and 9 guests