Generating a unique filename in the face of unicode filename

skip at pobox.com skip at pobox.com
Sat Jun 13 17:22:51 EDT 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()
    if threaded:
        name = threading.current_thread().get_name()
        tname = "%s-" % quote(name, safe="")
    else:
        tname = ""
    dirname = os.path.dirname(self.lock_file)
    self.unique_name = os.path.join(dirname,
                                    "%s.%s%s" % (self.hostname,
                                                 tname,
                                                 self.pid))

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
    FileLock
    File "taskcoachlib\thirdparty\lockfile\lockfile.pyo", line 296, in
    __init__
    File "taskcoachlib\thirdparty\lockfile\lockfile.pyo", line 175, in
    __init__
    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()))
    u'/tmp/\xef.MainThread.11004'

It works for Frank on his Windows box as well.  Any ideas how to properly
Unicode-proof this code?

Thanks,

-- 
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 mailing list