Generating a unique filename in the face of unicode filename

S

skip

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:
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,
 
M

Martin v. Löwis

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.

Most likely, it is not. It's rather the hostname encoded in the ANSI
code page.
It works for Frank on his Windows box as well. Any ideas how to properly
Unicode-proof this code?

You should first decode the gethostname result, using the locale's
encoding; expect this decoding to fail possibly, so the "ignore" error
handler might be your best choice.

Regards,
Martin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top