This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: mkdtemp fails on Windows if username has non-ASCII character
Type: behavior Stage: resolved
Components: Library (Lib), Windows Versions: Python 2.6
process
Status: closed Resolution: out of date
Dependencies: Superseder:
Assigned To: loewis Nosy List: BreamoreBoy, Ubik, ezio.melotti, georg.brandl, loewis, niemisto, tim.golden, vstinner
Priority: normal Keywords:

Created on 2007-03-16 10:03 by niemisto, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (10)
msg31537 - (view) Author: Markus Niemistö (niemisto) Date: 2007-03-16 10:03
mkdtemp fails miserably on Windows if Windows user name has any non-ASCII characters, like ä or ö, in it. mkdtemp throws an encoding error.

This seems to be because the default temp dir in Windows is "c:\documents and settings\<user name>\local settings\temp". Now if the user name has non-ASCII characters ASCII decoder cannot handle it and creating temp directories won't work.

As a work around I have used the following code:

tempdir = unicode(tempfile.gettempdir(), 'mbcs')
mkdtemp(suffix='foo', dir=tempdir)

This applies for both Python 2.4 and Python 2.5.
msg31538 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2007-03-21 11:42
Could you indicate where exactly what error is raised?
msg31539 - (view) Author: Markus Niemistö (niemisto) Date: 2007-04-10 06:48
Here is traceback. Sorry it took so long.

Traceback (most recent call last):
  File "c:\util\home\xxx\xxx.py", line 350, in OnOpen
    dir = tempfile.mkdtemp(prefix=u'test')
  File "C:\python24\lib\tempfile.py", line 326, in mkdtemp
    file = _os.path.join(dir, prefix + name + suffix)
  File "c:\python24\lib\ntpath.py", line 102, in join
    path += "\\" + b
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe4 in position 12: ordinal not in range(128)
msg84878 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2009-03-31 19:34
Why are you using a unicode string as your temp directory prefix? Does
it raise something different if you don't?
msg84985 - (view) Author: Markus Niemistö (niemisto) Date: 2009-04-01 06:45
Well, it's not me. As I stated in the problem description, Windows (2000
at least) uses path c:\documents and settings\<user name>\local
settings\temp as default temp dir, where also python tries to make temp
dirs. Now for example if user name is "niemistö", then the temp dir will
be ...\niemistö\local settings\temp, which won't work.

I'll try do test the latest versions of python soon.
msg116632 - (view) Author: Mark Lawrence (BreamoreBoy) * Date: 2010-09-16 22:51
I can't reproduce this problem on Windows Vista with any of the current maintainance branches of Python using the interactive prompt from the command line.
msg180739 - (view) Author: Ezio Melotti (ezio.melotti) * (Python committer) Date: 2013-01-27 03:41
On Win 7 with a non-ASCII username, this seems to work fine on both 2.7 and 3.3.  2.7 returns str or unicode depending on the type of the "prefix=", whereas 3.3 only accepts str (I think this is expected).

I'm going to close this as out of date, feel free to reopen if you can reproduce it on some older but still supported system.
msg234557 - (view) Author: (Ubik) Date: 2015-01-23 15:07
As detailed in this SO question:

http://stackoverflow.com/questions/28101187/deal-with-unicode-usernames-in-python-mkdtemp

I still see the issue in 2.7.8.

I use a unicode prefix and changing this is not an option (editing legacy code which expects unicode everywhere)

Is there some full proof workaround ? Is the one suggested in the OP good enough ?
msg234558 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2015-01-23 15:11
The best fix is to use Python 3. In 2015, it's maybe time to use Python 3 which has a very good Unicode support.
msg234563 - (view) Author: Martin v. Löwis (loewis) * (Python committer) Date: 2015-01-23 17:17
Ubik: this issue is closed, as we believe that it does not exist anymore. If you still think there is a bug surrounding mkdtemp, please make a new full bug report. Structure your report as follows:

1. this is what you did
2. this is what happened
3. this is what you expected to happen instead

Be as precise as possible. For example, reporting the exact user name of the user might be helpful. If you can, debug the problem, e.g. by arranging to display the value of 'path' and 'b' in line 102 of ntpath.py (assuming the error still occurs on the same line as it did for Markus).
History
Date User Action Args
2022-04-11 14:56:23adminsetgithub: 44729
2015-01-23 17:17:16loewissetmessages: + msg234563
2015-01-23 15:11:58vstinnersetnosy: + vstinner
messages: + msg234558
2015-01-23 15:07:12Ubiksetnosy: + Ubik
messages: + msg234557
2013-01-27 03:41:40ezio.melottisetstatus: open -> closed

nosy: + ezio.melotti
messages: + msg180739

resolution: out of date
stage: test needed -> resolved
2010-09-16 22:51:43BreamoreBoysetnosy: + BreamoreBoy
messages: + msg116632
2010-08-06 15:11:56tim.goldensetnosy: + tim.golden
2009-04-01 06:45:30niemistosetmessages: + msg84985
2009-03-31 19:34:04georg.brandlsetassignee: loewis

messages: + msg84878
nosy: + loewis
2009-03-30 23:13:56ajaksu2setstage: test needed
type: behavior
components: + Windows
versions: + Python 2.6, - Python 2.5
2007-03-16 10:03:49niemistocreate