Nikolaus Rath, 30.04.2013 02:28:
> I noticed that when compiling with -3 semantics,
>
> def foo(str s):
> pass
>
> requires a unicode argument when compiled into a Python 2.x extension,
> and a str argument when compiled into a Python 3.x extension.
>
> Is there a way to declare that I want a str argument in both cases, i.e.
> a bytes rather than unicode argument under 2.x?
Seriously, you don't want that. In Python 2, it's very difficult to write
code that never uses unicode strings anywhere, because whenever you mix
bytes and unicode for whatever reason, even by accident or because some
library hands it to you or whatnot, the result will be unicode, and you
wouldn't easily notice it. So explicitly excluding unicode input in Python
2 will not make your users happy.
IMHO, using str in a function signature is always a bad idea. Unless you're
completely sure that you want either exactly bytes input (e.g. a data chunk
from the network) or exactly unicode (i.e. text), don't type the input
argument and apply an appropriate conversion yourself.
Stefan