On Mon, Aug 7, 2017 at 12:45 PM, Jeroen Demeyer <[email protected]> wrote:
> On 2017-08-07 12:00, François Bissey wrote:
>>
>> Looking at this more closely. Why isn’t the “bytes” conversion
>> done earlier - before checking that the file exists.
>
>
> +1
>
> I know that Erik Bray will disagree with this, but I would advocate to use
> "bytes" as much as possible when dealing with filenames.

Yep. Like it or not that is exactly the wrong way to approach things
in Python 3, in *most* cases.  Even more wrong since the addition of
pathlib [1], augmented in Python 3.6 with PEP 519 [2] (as neither str
or bytes are necessarily the ideal types for representing filesystem
paths; this has nothing directly to do with encoding though).

-    handle = dlopen(lib, RTLD_GLOBAL|RTLD_LAZY)
+    lib = bytes(lib, encoding='utf-8')
+    handle = dlopen(lib, RTLD_GLOBAL|RTLD_LAZY)

Using `bytes(..., encoding=...)` is possible, of course, but not very
idiomatic for cases like this.  One reason being, as you mentioned, on
Python 2 `bytes = str` so this construction is not good for
cross-compatible code.  Much more idiomatic is to simply use
`lib.encode(...)`.

It's also bad to hard-code utf-8.  For encoding filenames it's safer
to use `sys.getfilesystemencoding()`. In certain pathological cases
this won't be correct, but in those cases neither usually will utf-8
be correct (though that scenario isn't impossible either).

I'll also add that this wouldn't even be an issue except for the fact
that dlopen is being called through Cython.  Most Python stdlib
interfaces are str/bytes agnostic for filenames and will do this
conversion automatically.  But in general the conversion should only
be done before interfacing with system calls, and generally no sooner
unless it's going to be used frequently for some reason.

Best,
Erik


[1] https://www.python.org/dev/peps/pep-0428
[2] https://www.python.org/dev/peps/pep-0519
[3] https://docs.python.org/3/library/sys.html#sys.getfilesystemencoding

-- 
You received this message because you are subscribed to the Google Groups 
"sage-devel" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/sage-devel.
For more options, visit https://groups.google.com/d/optout.

Reply via email to