[Python-ideas] Introduce some obvious way to encode and decode filenames from Python code

Antoine Pitrou solipsis at pitrou.net
Tue Jul 17 01:41:24 CEST 2012


On Tue, 17 Jul 2012 00:00:32 +0100
And Clover <and-dev at doxdesk.com> wrote:

> On 16/07/12 18:23, Victor Stinner wrote:
>  > I wrote these functions when I worked in this topic for Python 3. Yes,
>  > it would be great if you write a patch to mention these functions in
>  > the doc.
> 
> Sure.
> 
> But should we be encouraging their use on Windows? I would have thought 
> it the best thing to stick with the Unicode string for paths on NT, so 
> that the native Win32 Unicode APIs are used instead of the 
> ANSI-code-page-bound C stdio. Encoding down to the fsencoding for 
> Windows just means that any path including a character that isn't in the 
> ANSI CP will fail.

Well even under Unix, these functions are only useful for very
specialized cases. For normal usage, PEP 383 guarantees that all
filenames, including theoretically undecodable ones, pass through
properly. When piping filenames between Python processes, you can use
whatever encoding you want (or you can also use json or pickle).

The only remaining use case is sending some filenames to an external
(non-Python) program over a bytes stream, or reading some filenames
emitted by such a program. Here, you need bytes under Windows as well.

Regards

Antoine.


-- 
Software development and contracting: http://pro.pitrou.net





More information about the Python-ideas mailing list