c-std-porting.lists.linux.dev archive mirror
 help / color / mirror / Atom feed
From: Florian Weimer <fweimer@redhat.com>
To: cython-users@googlegroups.com
Cc: c-std-porting@lists.linux.dev
Subject: Cython and conditional compilation
Date: Thu, 09 Feb 2023 11:38:55 +0100	[thread overview]
Message-ID: <875ycbkts0.fsf@oldenburg.str.redhat.com> (raw)

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
taken:

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
declaration.

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

Thanks,
Florian


                 reply	other threads:[~2023-02-09 10:38 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=875ycbkts0.fsf@oldenburg.str.redhat.com \
    --to=fweimer@redhat.com \
    --cc=c-std-porting@lists.linux.dev \
    --cc=cython-users@googlegroups.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).