c-std-porting.lists.linux.dev archive mirror
 help / color / mirror / Atom feed
* Cython and conditional compilation
@ 2023-02-09 10:38 Florian Weimer
  0 siblings, 0 replies; only message in thread
From: Florian Weimer @ 2023-02-09 10:38 UTC (permalink / raw)
  To: cython-users; +Cc: c-std-porting

Cython/Includes/cpython/version.pxd contains this recommendation:

# Python version constants
# It's better to evaluate these at runtime (i.e. C compile time) using
#      if PY_MAJOR_VERSION >= 3:
#           do_stuff_in_Py3_0_and_later()
#      if PY_VERSION_HEX >= 0x02070000:
#           do_stuff_in_Py2_7_and_later()
# than using the IF/DEF statements, which are evaluated at Cython
# compile time.  This will keep your C code portable.

I have seen one case, in an older version of breezy, where this only
works because the compiler accepts calls to undeclared functions
(implicit function declarations) and optimizes out the branches not

cdef object _split_first_line_unicode(Py_UNICODE *line, int len,
                                      Py_UNICODE *value, Py_ssize_t *value_len):
    cdef int i
    for i from 0 <= i < len:
        if line[i] == c':':
            if line[i+1] != c' ':
                raise ValueError("invalid tag in line %r" %
                                 PyUnicode_FromUnicode(line, len))
            memcpy(value, &line[i+2], (len-i-2) * sizeof(Py_UNICODE))
            value_len[0] = len-i-2
            if PY_MAJOR_VERSION >= 3:
                return PyUnicode_FromUnicode(line, i)
            return PyUnicode_EncodeASCII(line, i, "strict")
    raise ValueError("tag/value separator not found in line %r" %
                     PyUnicode_FromUnicode(line, len))

The PyUnicode_EncodeASCII still ends up in the generated C code, but
it's no longer part of the Python 3 API, hence the implicit function

It's since been fixed in breezy, but maybe it makes sense to revise the
recommendation in version.pxd?


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2023-02-09 10:38 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-02-09 10:38 Cython and conditional compilation Florian Weimer

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).