It took me a long while to pinpoint some specific cause of the bug. I am writing a simple XML RPC server that allows you for directory listing and possibly other read-only operations. I already made a simple method to list all folders and files and represent them as dictionary:
def list_dir(self, dirname):
"""Returns list of files and directories as a dictionary, where key is name and values is either 'file' or 'dir'"""
dirname = os.path.abspath(os.path.join(self.server.cwd,dirname))
#Check that the path doesn't lead above
if dirname.find(self.server.cwd)==-1:
raise SecurityError("There was an attempt to browse in %s wthich is above the root working directory %s."%(dirname, self.server.cwd))
check_for_valid_directory(dirname)
#Looping through directory
files = [i for i in os.listdir(dirname)]
#Create associative array
result = {}
#Iterate through files
for f in files:
fullpath = os.path.join(dirname, f)
#Appending directories
if os.path.isdir(fullpath):
result[f] = "dir"
else:
result[f] = "file"
print "Sending data", result
return result
Now when directory contains file (or rather folder) named Nová složka
the client receives error instead of desired list. When I removed the problematic filename I received data with no errors. I don't think Python library has this right - either the argument conversion should be complete, including any unicode stuff, or not present at all.
But anyway, how should I encode the data Python library can't handle?