22 * Helper method for urllib to fetch the proxy configuration settings
33 * using the SystemConfiguration framework.
44 */
5+
6+ // Need limited C API version 3.13 for Py_MOD_PER_INTERPRETER_GIL_SUPPORTED
7+ #define Py_LIMITED_API 0x030d0000
8+
59#include <Python.h>
610#include <SystemConfiguration/SystemConfiguration.h>
711
@@ -21,8 +25,7 @@ cfstring_to_pystring(CFStringRef ref)
2125
2226 s = CFStringGetCStringPtr (ref , kCFStringEncodingUTF8 );
2327 if (s ) {
24- return PyUnicode_DecodeUTF8 (
25- s , strlen (s ), NULL );
28+ return PyUnicode_DecodeUTF8 (s , strlen (s ), NULL );
2629
2730 } else {
2831 CFIndex len = CFStringGetLength (ref );
@@ -43,8 +46,7 @@ cfstring_to_pystring(CFStringRef ref)
4346 PyMem_Free (buf );
4447 return NULL ;
4548 } else {
46- result = PyUnicode_DecodeUTF8 (
47- buf , strlen (buf ), NULL );
49+ result = PyUnicode_DecodeUTF8 (buf , strlen (buf ), NULL );
4850 PyMem_Free (buf );
4951 }
5052 return result ;
@@ -84,7 +86,7 @@ get_proxy_settings(PyObject* Py_UNUSED(mod), PyObject *Py_UNUSED(ignored))
8486 if (v == NULL ) goto error ;
8587
8688 r = PyDict_SetItemString (result , "exclude_simple" , v );
87- Py_SETREF ( v , NULL );
89+ Py_CLEAR ( v );
8890 if (r == -1 ) goto error ;
8991
9092 anArray = CFDictionaryGetValue (proxyDict ,
@@ -104,13 +106,11 @@ get_proxy_settings(PyObject* Py_UNUSED(mod), PyObject *Py_UNUSED(ignored))
104106
105107 aString = CFArrayGetValueAtIndex (anArray , i );
106108 if (aString == NULL ) {
107- PyTuple_SetItem (v , i , Py_None );
108- Py_INCREF (Py_None );
109+ PyTuple_SetItem (v , i , Py_NewRef (Py_None ));
109110 } else {
110111 PyObject * t = cfstring_to_pystring (aString );
111112 if (!t ) {
112- PyTuple_SetItem (v , i , Py_None );
113- Py_INCREF (Py_None );
113+ PyTuple_SetItem (v , i , Py_NewRef (Py_None ));
114114 } else {
115115 PyTuple_SetItem (v , i , t );
116116 }
@@ -148,15 +148,13 @@ set_proxy(PyObject* proxies, const char* proto, CFDictionaryRef proxyDict,
148148 if (h ) {
149149 if (aNum ) {
150150 int32_t port = cfnum_to_int32 (aNum );
151- v = PyUnicode_FromFormat ("http://%U:%ld" ,
152- h , (long )port );
151+ v = PyUnicode_FromFormat ("http://%U:%ld" , h , (long )port );
153152 } else {
154153 v = PyUnicode_FromFormat ("http://%U" , h );
155154 }
156155 Py_DECREF (h );
157156 if (!v ) return -1 ;
158- r = PyDict_SetItemString (proxies , proto ,
159- v );
157+ r = PyDict_SetItemString (proxies , proto , v );
160158 Py_DECREF (v );
161159 return r ;
162160 }
0 commit comments