Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mkdtemp fails on Windows if username has non-ASCII character #44729

Closed
niemisto mannequin opened this issue Mar 16, 2007 · 10 comments
Closed

mkdtemp fails on Windows if username has non-ASCII character #44729

niemisto mannequin opened this issue Mar 16, 2007 · 10 comments
Assignees
Labels
OS-windows stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@niemisto
Copy link
Mannequin

niemisto mannequin commented Mar 16, 2007

BPO 1681974
Nosy @loewis, @birkenfeld, @vstinner, @tjguk, @ezio-melotti, @Utumno

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = 'https://github.com/loewis'
closed_at = <Date 2013-01-27.03:41:40.468>
created_at = <Date 2007-03-16.10:03:49.000>
labels = ['type-bug', 'library', 'OS-windows']
title = 'mkdtemp fails on Windows if username has non-ASCII character'
updated_at = <Date 2015-01-23.17:17:16.866>
user = 'https://bugs.python.org/niemisto'

bugs.python.org fields:

activity = <Date 2015-01-23.17:17:16.866>
actor = 'loewis'
assignee = 'loewis'
closed = True
closed_date = <Date 2013-01-27.03:41:40.468>
closer = 'ezio.melotti'
components = ['Library (Lib)', 'Windows']
creation = <Date 2007-03-16.10:03:49.000>
creator = 'niemisto'
dependencies = []
files = []
hgrepos = []
issue_num = 1681974
keywords = []
message_count = 10.0
messages = ['31537', '31538', '31539', '84878', '84985', '116632', '180739', '234557', '234558', '234563']
nosy_count = 8.0
nosy_names = ['loewis', 'georg.brandl', 'vstinner', 'niemisto', 'tim.golden', 'ezio.melotti', 'BreamoreBoy', 'Ubik']
pr_nums = []
priority = 'normal'
resolution = 'out of date'
stage = 'resolved'
status = 'closed'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue1681974'
versions = ['Python 2.6']

@niemisto
Copy link
Mannequin Author

niemisto mannequin commented Mar 16, 2007

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.

@niemisto niemisto mannequin added the stdlib Python modules in the Lib dir label Mar 16, 2007
@birkenfeld
Copy link
Member

Could you indicate where exactly what error is raised?

@niemisto
Copy link
Mannequin Author

niemisto mannequin commented Apr 10, 2007

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)

@devdanzin devdanzin mannequin added OS-windows type-bug An unexpected behavior, bug, or error labels Mar 30, 2009
@birkenfeld
Copy link
Member

Why are you using a unicode string as your temp directory prefix? Does
it raise something different if you don't?

@niemisto
Copy link
Mannequin Author

niemisto mannequin commented Apr 1, 2009

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.

@BreamoreBoy
Copy link
Mannequin

BreamoreBoy mannequin commented Sep 16, 2010

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.

@ezio-melotti
Copy link
Member

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.

@Utumno
Copy link
Mannequin

Utumno mannequin commented Jan 23, 2015

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 ?

@vstinner
Copy link
Member

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.

@loewis
Copy link
Mannequin

loewis mannequin commented Jan 23, 2015

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).

@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
OS-windows stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

3 participants