On Mon, Feb 20, 2023 at 6:35 AM Florian Weimer <
fwe...@redhat.com> wrote:
>
> * D. Woods:
>
> > So I don't think we want to substantially change our recommendation.
>
> But it's going to stop working in practice in many cases if the guarded
> code uses functions that are not available in all Python versions. You
> will have to use conditional compilation for that. Even today, this
> approach of compiling both conditionals only works if compiling with
> optimization or with lazy binding (the latter is increasingly not the
> default anymore). So I think the current advice is unnecessarily
> confusing to programmers.
I don't think we want to step away from our desire to generate
Python-version agnostic C files. I'd probably go with the
macro-defined common API as well. Even better would be to avoid a
dependence on the C API at all, e.g.
if PY_MAJOR_VERSION >= 3:
return line[:i]
else:
return line[:i].encode('ASCII', 'strict')
(True, this creates an unneeded intermediate in Python 2, but IMHO
that's not the end of the world these days.)
> > Ideally we'd quite like to get rid of `DEF` and `IF` but we're not
> > really there yet with a suitable replacement for everyone's needs.
>
> Couldn't Cython translate “if PY_MAJOR_VERSION >= 3:” to #if
> automatically?
That's an interesting idea, but I think it'd be far from trivial to
actually implement. (E.g. what if the if statement contains multiple
clauses, some of which are available at compile time and some of which
are not?)