From 7029c1a1c5b864056aa00298b1d0e0269f073f99 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Wed, 18 Oct 2023 00:32:53 +0200 Subject: gh-85283: Build _scproxy extension with limited C API (#111008) * Replace Py_SETREF(v, NULL) with Py_CLEAR(v). * Reformat the code. --- Doc/whatsnew/3.13.rst | 4 ++-- .../2023-10-17-01-56-11.gh-issue-85283.V156T2.rst | 6 +++--- Modules/_scproxy.c | 24 ++++++++++------------ 3 files changed, 16 insertions(+), 18 deletions(-) diff --git a/Doc/whatsnew/3.13.rst b/Doc/whatsnew/3.13.rst index 24a53b7..54f007f 100644 --- a/Doc/whatsnew/3.13.rst +++ b/Doc/whatsnew/3.13.rst @@ -933,8 +933,8 @@ Build Changes library, GCC built-in atomic functions, or MSVC interlocked intrinsics. * The ``errno``, ``md5``, ``resource``, ``winsound``, ``_ctypes_test``, - ``_stat`` and ``_testimportmultiple`` C extensions are now built with the - :ref:`limited C API `. + ``_scproxy``, ``_stat`` and ``_testimportmultiple`` C extensions are now + built with the :ref:`limited C API `. (Contributed by Victor Stinner in :gh:`85283`.) diff --git a/Misc/NEWS.d/next/Build/2023-10-17-01-56-11.gh-issue-85283.V156T2.rst b/Misc/NEWS.d/next/Build/2023-10-17-01-56-11.gh-issue-85283.V156T2.rst index 3e85d85..cd26f7b 100644 --- a/Misc/NEWS.d/next/Build/2023-10-17-01-56-11.gh-issue-85283.V156T2.rst +++ b/Misc/NEWS.d/next/Build/2023-10-17-01-56-11.gh-issue-85283.V156T2.rst @@ -1,4 +1,4 @@ -The ``errno``, ``md5``, ``resource``, ``winsound``, ``_ctypes_test``, ``_stat`` -and ``_testimportmultiple`` C extensions are now built with the :ref:`limited C -API `. +The ``errno``, ``md5``, ``resource``, ``winsound``, ``_ctypes_test``, +``_scproxy``, ``_stat`` and ``_testimportmultiple`` C extensions are now built +with the :ref:`limited C API `. Patch by Victor Stinner. diff --git a/Modules/_scproxy.c b/Modules/_scproxy.c index 6cc0908..2f1b861 100644 --- a/Modules/_scproxy.c +++ b/Modules/_scproxy.c @@ -2,6 +2,10 @@ * Helper method for urllib to fetch the proxy configuration settings * using the SystemConfiguration framework. */ + +// Need limited C API version 3.13 for Py_MOD_PER_INTERPRETER_GIL_SUPPORTED +#define Py_LIMITED_API 0x030d0000 + #include #include @@ -21,8 +25,7 @@ cfstring_to_pystring(CFStringRef ref) s = CFStringGetCStringPtr(ref, kCFStringEncodingUTF8); if (s) { - return PyUnicode_DecodeUTF8( - s, strlen(s), NULL); + return PyUnicode_DecodeUTF8(s, strlen(s), NULL); } else { CFIndex len = CFStringGetLength(ref); @@ -43,8 +46,7 @@ cfstring_to_pystring(CFStringRef ref) PyMem_Free(buf); return NULL; } else { - result = PyUnicode_DecodeUTF8( - buf, strlen(buf), NULL); + result = PyUnicode_DecodeUTF8(buf, strlen(buf), NULL); PyMem_Free(buf); } return result; @@ -84,7 +86,7 @@ get_proxy_settings(PyObject* Py_UNUSED(mod), PyObject *Py_UNUSED(ignored)) if (v == NULL) goto error; r = PyDict_SetItemString(result, "exclude_simple", v); - Py_SETREF(v, NULL); + Py_CLEAR(v); if (r == -1) goto error; anArray = CFDictionaryGetValue(proxyDict, @@ -104,13 +106,11 @@ get_proxy_settings(PyObject* Py_UNUSED(mod), PyObject *Py_UNUSED(ignored)) aString = CFArrayGetValueAtIndex(anArray, i); if (aString == NULL) { - PyTuple_SetItem(v, i, Py_None); - Py_INCREF(Py_None); + PyTuple_SetItem(v, i, Py_NewRef(Py_None)); } else { PyObject* t = cfstring_to_pystring(aString); if (!t) { - PyTuple_SetItem(v, i, Py_None); - Py_INCREF(Py_None); + PyTuple_SetItem(v, i, Py_NewRef(Py_None)); } else { PyTuple_SetItem(v, i, t); } @@ -148,15 +148,13 @@ set_proxy(PyObject* proxies, const char* proto, CFDictionaryRef proxyDict, if (h) { if (aNum) { int32_t port = cfnum_to_int32(aNum); - v = PyUnicode_FromFormat("http://%U:%ld", - h, (long)port); + v = PyUnicode_FromFormat("http://%U:%ld", h, (long)port); } else { v = PyUnicode_FromFormat("http://%U", h); } Py_DECREF(h); if (!v) return -1; - r = PyDict_SetItemString(proxies, proto, - v); + r = PyDict_SetItemString(proxies, proto, v); Py_DECREF(v); return r; } -- cgit v0.12