Generating a unique filename in the face of unicode filename
skip at pobox.com
skip at pobox.com
Sat Jun 13 23:22:51 CEST 2009
In my lockfile module I generate a unique name like so:
self.path = path
self.lock_file = os.path.abspath(path) + ".lock"
self.hostname = socket.gethostname()
self.pid = os.getpid()
name = threading.current_thread().get_name()
tname = "%s-" % quote(name, safe="")
tname = ""
dirname = os.path.dirname(self.lock_file)
self.unique_name = os.path.join(dirname,
"%s.%s%s" % (self.hostname,
where path is the file which is to be locked. Frank Niessink uses lockfile
in his Task Coach application and reported a problem to me one of his
Windows users encountered:
File "taskcoachlib\persistence\taskfile.pyo", line 266, in acquire_lock
File "taskcoachlib\thirdparty\lockfile\lockfile.pyo", line 537, in
File "taskcoachlib\thirdparty\lockfile\lockfile.pyo", line 296, in
File "taskcoachlib\thirdparty\lockfile\lockfile.pyo", line 175, in
File "ntpath.pyo", line 102, in join
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe9 in position 3: ordinal not in range(128)
where line 175 is the assignment to self.unique_name. After a little
back-and-forth with his user it turns out that her computer's hostname
contains non-ASCII data, so presumably self.hostname is a unicode object.
I tried to replicate this on my Mac but can't:
>>> dirname = "/tmp"
>>> h = u"\xef"
>>> tname = threading.currentThread().getName()
>>> os.path.join(dirname, "%s.%s.%s" % (h, tname, os.getpid()))
It works for Frank on his Windows box as well. Any ideas how to properly
Unicode-proof this code?
Skip Montanaro - skip at pobox.com - http://www.smontanaro.net/
when i wake up with a heart rate below 40, i head right for the espresso
machine. -- chaos @ forums.usms.org
More information about the Python-list