classification
Title: Remove old ctypes callback workaround: creating the first instance of a callback
Type: Stage: resolved
Components: Library (Lib) Versions: Python 3.8
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: vstinner
Priority: normal Keywords: patch

Created on 2018-12-18 12:04 by vstinner, last changed 2018-12-18 14:10 by vstinner. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 11211 merged vstinner, 2018-12-18 12:53
Messages (3)
msg332054 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2018-12-18 12:04
ctypes._reset_cache() contains the following code:

    # XXX for whatever reasons, creating the first instance of a callback
    # function is needed for the unittests on Win64 to succeed.  This MAY
    # be a compiler bug, since the problem occurs only when _ctypes is
    # compiled with the MS SDK compiler.  Or an uninitialized variable?
    CFUNCTYPE(c_int)(lambda: None)

This code has been added 11 years ago:

commit 674e9389e9fdadd622829f4833367ac3b38475b5
Author: Thomas Heller <theller@ctypes.org>
Date:   Fri Aug 31 13:06:44 2007 +0000

    Add a workaround for a strange bug on win64, when _ctypes is compiled
    with the SDK compiler.  This should fix the failing
    Lib\ctypes\test\test_as_parameter.py test.

diff --git a/Lib/ctypes/__init__.py b/Lib/ctypes/__init__.py
index cdf6d36e47..f55d194b8f 100644
--- a/Lib/ctypes/__init__.py
+++ b/Lib/ctypes/__init__.py
@@ -535,3 +535,9 @@ for kind in [c_ushort, c_uint, c_ulong, c_ulonglong]:
     elif sizeof(kind) == 4: c_uint32 = kind
     elif sizeof(kind) == 8: c_uint64 = kind
 del(kind)
+
+# XXX for whatever reasons, creating the first instance of a callback
+# function is needed for the unittests on Win64 to succeed.  This MAY
+# be a compiler bug, since the problem occurs only when _ctypes is
+# compiled with the MS SDK compiler.  Or an uninitialized variable?
+CFUNCTYPE(c_int)(lambda: None)

--

This call is removed from Fedora package by the following patch:

https://src.fedoraproject.org/rpms/python3/blob/master/f/00155-avoid-ctypes-thunks.patch

Extract of Fedora python3.spec:

# 00155 #
# Avoid allocating thunks in ctypes unless absolutely necessary, to avoid
# generating SELinux denials on "import ctypes" and "import uuid" when
# embedding Python within httpd
# See https://bugzilla.redhat.com/show_bug.cgi?id=814391
Patch155: 00155-avoid-ctypes-thunks.patch

The patch has been added 6 years ago in Fedora:

commit 8a28107df1670a03a12cf6a7787160f103d8d8c8
Author: David Malcolm <dmalcolm@redhat.com>
Date:   Fri Apr 20 15:28:39 2012 -0400

    3.2.3-4: avoid allocating thunks in ctypes unless absolutely necessary (patch 155; rhbz#814391)
    
    * Fri Apr 20 2012 David Malcolm <dmalcolm@redhat.com> - 3.2.3-4
    - avoid allocating thunks in ctypes unless absolutely necessary, to avoid
    generating SELinux denials on "import ctypes" and "import uuid" when embedding
    Python within httpd (patch 155; rhbz#814391)

https://src.fedoraproject.org/rpms/python3/c/8a28107df1670a03a12cf6a7787160f103d8d8c8?branch=master

--

I don't understand the purpose of the workaround and ctypes is working well on Fedora. I propose to also remove the workaround in the master branch.

In case of doubt, I prefer to keep the workaround in Python 3.7.

Attached PR removes the workaround.
msg332059 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2018-12-18 13:47
New changeset e6b247c8e524dbe5fc03b3492f628d0d5348bc49 by Victor Stinner in branch 'master':
bpo-35523: Remove ctypes callback workaround (GH-11211)
https://github.com/python/cpython/commit/e6b247c8e524dbe5fc03b3492f628d0d5348bc49
msg332061 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2018-12-18 14:10
I will keep an eye on buildbots next days.
History
Date User Action Args
2018-12-18 14:10:26vstinnersetstatus: open -> closed
resolution: fixed
messages: + msg332061

stage: patch review -> resolved
2018-12-18 13:47:29vstinnersetmessages: + msg332059
2018-12-18 12:57:10vstinnersettitle: Remove ctypes old workaround: creating the first instance of a callback -> Remove old ctypes callback workaround: creating the first instance of a callback
2018-12-18 12:53:11vstinnersetkeywords: + patch
stage: patch review
pull_requests: + pull_request10448
2018-12-18 12:04:01vstinnercreate