From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1FAF66D38 for ; Thu, 9 Feb 2023 10:38:59 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1675939139; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type; bh=QqFVa/+CadlXUS1/qYL8wCtA4BAuB4e7sVbF3mgBqW8=; b=gQ5ox79oqivfakVew2H6Y2BT76qv/bZ+X8142w3CK35+DOive+0bTFj3xlb2c536AWIKFR 87wSWkzfxzyogn/BYy7VxUYWeylmayLJZIiYi819S4qjA6//MoW0fJSai1xogmbshm7t0A j5+nXVM9LL0nIuzdA1EZhI4vY/GsiOY= Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-364-2bLL5WbLPoCBoGdtR70Qqw-1; Thu, 09 Feb 2023 05:38:57 -0500 X-MC-Unique: 2bLL5WbLPoCBoGdtR70Qqw-1 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.rdu2.redhat.com [10.11.54.1]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 21B843C0F66A; Thu, 9 Feb 2023 10:38:57 +0000 (UTC) Received: from oldenburg.str.redhat.com (unknown [10.2.16.7]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 9FF0F40B42D4; Thu, 9 Feb 2023 10:38:56 +0000 (UTC) From: Florian Weimer 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 Message-ID: <875ycbkts0.fsf@oldenburg.str.redhat.com> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/28.2 (gnu/linux) Precedence: bulk X-Mailing-List: c-std-porting@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.1 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain 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