···1-From 4b55837e7c747e0f3bd2df1b5c8996ce86c6f60a Mon Sep 17 00:00:00 2001
2-From: "Miss Islington (bot)"
3- <31488909+miss-islington@users.noreply.github.com>
4-Date: Mon, 30 Aug 2021 02:08:16 -0700
5-Subject: [PATCH] bpo-44689: ctypes.util.find_library() now finds macOS 11+
6- system libraries when built on older macOS systems (GH-27251) (GH-28053)
7-8-Previously, when built on older macOS systems, `find_library` was not able to find macOS system libraries when running on Big Sur due to changes in how system libraries are stored.
9-(cherry picked from commit 71853a73024a98aa38a3c0444fe364dbd9709134)
10-11-Co-authored-by: Tobias Bergkvist <tobias@bergkv.ist>
12----
13- .../2021-07-20-22-27-01.bpo-44689.mmT_xH.rst | 5 ++++
14- Modules/_ctypes/callproc.c | 29 +++++++++++++++++--
15- 2 files changed, 31 insertions(+), 3 deletions(-)
16- create mode 100644 Misc/NEWS.d/next/macOS/2021-07-20-22-27-01.bpo-44689.mmT_xH.rst
17-18-diff --git a/Misc/NEWS.d/next/macOS/2021-07-20-22-27-01.bpo-44689.mmT_xH.rst b/Misc/NEWS.d/next/macOS/2021-07-20-22-27-01.bpo-44689.mmT_xH.rst
19-new file mode 100644
20-index 0000000000..b1e878d1ee
21---- /dev/null
22-+++ b/Misc/NEWS.d/next/macOS/2021-07-20-22-27-01.bpo-44689.mmT_xH.rst
23-@@ -0,0 +1,5 @@
24-+ :meth:`ctypes.util.find_library` now works correctly on macOS 11 Big Sur
25-+ even if Python is built on an older version of macOS. Previously, when
26-+ built on older macOS systems, ``find_library`` was not able to find
27-+ macOS system libraries when running on Big Sur due to changes in
28-+ how system libraries are stored.
29-diff --git a/Modules/_ctypes/callproc.c b/Modules/_ctypes/callproc.c
30-index 18984d15ab..b0f1e0bd04 100644
31---- a/Modules/_ctypes/callproc.c
32-+++ b/Modules/_ctypes/callproc.c
33-@@ -1449,14 +1449,37 @@ copy_com_pointer(PyObject *self, PyObject *args)
34- return r;
35- }
36- #else
37--
38-+#ifdef __APPLE__
39- #ifdef HAVE_DYLD_SHARED_CACHE_CONTAINS_PATH
40-+#define HAVE_DYLD_SHARED_CACHE_CONTAINS_PATH_RUNTIME \
41-+ __builtin_available(macOS 11.0, iOS 14.0, tvOS 14.0, watchOS 7.0, *)
42-+#else
43-+// Support the deprecated case of compiling on an older macOS version
44-+static void *libsystem_b_handle;
45-+static bool (*_dyld_shared_cache_contains_path)(const char *path);
46-+
47-+__attribute__((constructor)) void load_dyld_shared_cache_contains_path(void) {
48-+ libsystem_b_handle = dlopen("/usr/lib/libSystem.B.dylib", RTLD_LAZY);
49-+ if (libsystem_b_handle != NULL) {
50-+ _dyld_shared_cache_contains_path = dlsym(libsystem_b_handle, "_dyld_shared_cache_contains_path");
51-+ }
52-+}
53-+
54-+__attribute__((destructor)) void unload_dyld_shared_cache_contains_path(void) {
55-+ if (libsystem_b_handle != NULL) {
56-+ dlclose(libsystem_b_handle);
57-+ }
58-+}
59-+#define HAVE_DYLD_SHARED_CACHE_CONTAINS_PATH_RUNTIME \
60-+ _dyld_shared_cache_contains_path != NULL
61-+#endif
62-+
63- static PyObject *py_dyld_shared_cache_contains_path(PyObject *self, PyObject *args)
64- {
65- PyObject *name, *name2;
66- char *name_str;
67-68-- if (__builtin_available(macOS 11.0, iOS 14.0, tvOS 14.0, watchOS 7.0, *)) {
69-+ if (HAVE_DYLD_SHARED_CACHE_CONTAINS_PATH_RUNTIME) {
70- int r;
71-72- if (!PyArg_ParseTuple(args, "O", &name))
73-@@ -1999,7 +2022,7 @@ PyMethodDef _ctypes_module_methods[] = {
74- {"dlclose", py_dl_close, METH_VARARGS, "dlclose a library"},
75- {"dlsym", py_dl_sym, METH_VARARGS, "find symbol in shared library"},
76- #endif
77--#ifdef HAVE_DYLD_SHARED_CACHE_CONTAINS_PATH
78-+#ifdef __APPLE__
79- {"_dyld_shared_cache_contains_path", py_dyld_shared_cache_contains_path, METH_VARARGS, "check if path is in the shared cache"},
80- #endif
81- {"alignment", align_func, METH_O, alignment_doc},
82---
83-2.33.1
84-
···240 ] ++ optionals (pythonAtLeast "3.9" && stdenv.isDarwin) [
241 # Stop checking for TCL/TK in global macOS locations
242 ./3.9/darwin-tcl-tk.patch
243- # ctypes.util.find_library() now finds macOS 11+ system libraries when built on older macOS systems
244- # https://github.com/python/cpython/pull/28053
245- ./3.9/bpo-44689-ctypes.util.find_library-now-finds-macOS-1.patch
246 ] ++ optionals (isPy3k && hasDistutilsCxxPatch) [
247 # Fix for http://bugs.python.org/issue1222585
248 # Upstream distutils is calling C compiler to compile C++ code, which
···240 ] ++ optionals (pythonAtLeast "3.9" && stdenv.isDarwin) [
241 # Stop checking for TCL/TK in global macOS locations
242 ./3.9/darwin-tcl-tk.patch
000243 ] ++ optionals (isPy3k && hasDistutilsCxxPatch) [
244 # Fix for http://bugs.python.org/issue1222585
245 # Upstream distutils is calling C compiler to compile C++ code, which