···11-From 4b55837e7c747e0f3bd2df1b5c8996ce86c6f60a Mon Sep 17 00:00:00 2001
22-From: "Miss Islington (bot)"
33- <31488909+miss-islington@users.noreply.github.com>
44-Date: Mon, 30 Aug 2021 02:08:16 -0700
55-Subject: [PATCH] bpo-44689: ctypes.util.find_library() now finds macOS 11+
66- system libraries when built on older macOS systems (GH-27251) (GH-28053)
77-88-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.
99-(cherry picked from commit 71853a73024a98aa38a3c0444fe364dbd9709134)
1010-1111-Co-authored-by: Tobias Bergkvist <tobias@bergkv.ist>
1212----
1313- .../2021-07-20-22-27-01.bpo-44689.mmT_xH.rst | 5 ++++
1414- Modules/_ctypes/callproc.c | 29 +++++++++++++++++--
1515- 2 files changed, 31 insertions(+), 3 deletions(-)
1616- create mode 100644 Misc/NEWS.d/next/macOS/2021-07-20-22-27-01.bpo-44689.mmT_xH.rst
1717-1818-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
1919-new file mode 100644
2020-index 0000000000..b1e878d1ee
2121---- /dev/null
2222-+++ b/Misc/NEWS.d/next/macOS/2021-07-20-22-27-01.bpo-44689.mmT_xH.rst
2323-@@ -0,0 +1,5 @@
2424-+ :meth:`ctypes.util.find_library` now works correctly on macOS 11 Big Sur
2525-+ even if Python is built on an older version of macOS. Previously, when
2626-+ built on older macOS systems, ``find_library`` was not able to find
2727-+ macOS system libraries when running on Big Sur due to changes in
2828-+ how system libraries are stored.
2929-diff --git a/Modules/_ctypes/callproc.c b/Modules/_ctypes/callproc.c
3030-index 18984d15ab..b0f1e0bd04 100644
3131---- a/Modules/_ctypes/callproc.c
3232-+++ b/Modules/_ctypes/callproc.c
3333-@@ -1449,14 +1449,37 @@ copy_com_pointer(PyObject *self, PyObject *args)
3434- return r;
3535- }
3636- #else
3737--
3838-+#ifdef __APPLE__
3939- #ifdef HAVE_DYLD_SHARED_CACHE_CONTAINS_PATH
4040-+#define HAVE_DYLD_SHARED_CACHE_CONTAINS_PATH_RUNTIME \
4141-+ __builtin_available(macOS 11.0, iOS 14.0, tvOS 14.0, watchOS 7.0, *)
4242-+#else
4343-+// Support the deprecated case of compiling on an older macOS version
4444-+static void *libsystem_b_handle;
4545-+static bool (*_dyld_shared_cache_contains_path)(const char *path);
4646-+
4747-+__attribute__((constructor)) void load_dyld_shared_cache_contains_path(void) {
4848-+ libsystem_b_handle = dlopen("/usr/lib/libSystem.B.dylib", RTLD_LAZY);
4949-+ if (libsystem_b_handle != NULL) {
5050-+ _dyld_shared_cache_contains_path = dlsym(libsystem_b_handle, "_dyld_shared_cache_contains_path");
5151-+ }
5252-+}
5353-+
5454-+__attribute__((destructor)) void unload_dyld_shared_cache_contains_path(void) {
5555-+ if (libsystem_b_handle != NULL) {
5656-+ dlclose(libsystem_b_handle);
5757-+ }
5858-+}
5959-+#define HAVE_DYLD_SHARED_CACHE_CONTAINS_PATH_RUNTIME \
6060-+ _dyld_shared_cache_contains_path != NULL
6161-+#endif
6262-+
6363- static PyObject *py_dyld_shared_cache_contains_path(PyObject *self, PyObject *args)
6464- {
6565- PyObject *name, *name2;
6666- char *name_str;
6767-6868-- if (__builtin_available(macOS 11.0, iOS 14.0, tvOS 14.0, watchOS 7.0, *)) {
6969-+ if (HAVE_DYLD_SHARED_CACHE_CONTAINS_PATH_RUNTIME) {
7070- int r;
7171-7272- if (!PyArg_ParseTuple(args, "O", &name))
7373-@@ -1999,7 +2022,7 @@ PyMethodDef _ctypes_module_methods[] = {
7474- {"dlclose", py_dl_close, METH_VARARGS, "dlclose a library"},
7575- {"dlsym", py_dl_sym, METH_VARARGS, "find symbol in shared library"},
7676- #endif
7777--#ifdef HAVE_DYLD_SHARED_CACHE_CONTAINS_PATH
7878-+#ifdef __APPLE__
7979- {"_dyld_shared_cache_contains_path", py_dyld_shared_cache_contains_path, METH_VARARGS, "check if path is in the shared cache"},
8080- #endif
8181- {"alignment", align_func, METH_O, alignment_doc},
8282---
8383-2.33.1
8484-
···240240 ] ++ optionals (pythonAtLeast "3.9" && stdenv.isDarwin) [
241241 # Stop checking for TCL/TK in global macOS locations
242242 ./3.9/darwin-tcl-tk.patch
243243- # ctypes.util.find_library() now finds macOS 11+ system libraries when built on older macOS systems
244244- # https://github.com/python/cpython/pull/28053
245245- ./3.9/bpo-44689-ctypes.util.find_library-now-finds-macOS-1.patch
246243 ] ++ optionals (isPy3k && hasDistutilsCxxPatch) [
247244 # Fix for http://bugs.python.org/issue1222585
248245 # Upstream distutils is calling C compiler to compile C++ code, which