aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2020-07-10 23:26:06 +0300
committerGitHub <noreply@github.com>2020-07-10 23:26:06 +0300
commit4c8f09d7cef8c7aa07d5b5232b5b64f63819a743 (patch)
tree2d90e13b6dc939f019a6cafc45ea604cbb90f584 /Modules/overlapped.c
parentbpo-20179: Convert the _overlapped module to the Argument Clinic (GH-14275) (diff)
downloadcpython-4c8f09d7cef8c7aa07d5b5232b5b64f63819a743.tar.gz
cpython-4c8f09d7cef8c7aa07d5b5232b5b64f63819a743.tar.bz2
cpython-4c8f09d7cef8c7aa07d5b5232b5b64f63819a743.zip
bpo-36346: Make using the legacy Unicode C API optional (GH-21437)
Add compile time option USE_UNICODE_WCHAR_CACHE. Setting it to 0 makes the interpreter not using the wchar_t cache and the legacy Unicode C API.
Diffstat (limited to 'Modules/overlapped.c')
-rw-r--r--Modules/overlapped.c64
1 files changed, 49 insertions, 15 deletions
diff --git a/Modules/overlapped.c b/Modules/overlapped.c
index 9c4e2da9dfb..4f0ba85d798 100644
--- a/Modules/overlapped.c
+++ b/Modules/overlapped.c
@@ -1291,6 +1291,7 @@ _overlapped_Overlapped_AcceptEx_impl(OverlappedObject *self,
static int
parse_address(PyObject *obj, SOCKADDR *Address, int Length)
{
+ PyObject *Host_obj;
Py_UNICODE *Host;
unsigned short Port;
unsigned long FlowInfo;
@@ -1298,33 +1299,66 @@ parse_address(PyObject *obj, SOCKADDR *Address, int Length)
memset(Address, 0, Length);
- if (PyArg_ParseTuple(obj, "uH", &Host, &Port))
- {
+ switch (PyTuple_GET_SIZE(obj)) {
+ case 2: {
+ if (!PyArg_ParseTuple(obj, "UH", &Host_obj, &Port)) {
+ return -1;
+ }
+#if USE_UNICODE_WCHAR_CACHE
+ Host = (wchar_t *)_PyUnicode_AsUnicode(Host_obj);
+#else /* USE_UNICODE_WCHAR_CACHE */
+ Host = PyUnicode_AsWideCharString(Host_obj, NULL);
+#endif /* USE_UNICODE_WCHAR_CACHE */
+ if (Host == NULL) {
+ return -1;
+ }
Address->sa_family = AF_INET;
if (WSAStringToAddressW(Host, AF_INET, NULL, Address, &Length) < 0) {
SetFromWindowsErr(WSAGetLastError());
- return -1;
+ Length = -1;
+ }
+ else {
+ ((SOCKADDR_IN*)Address)->sin_port = htons(Port);
}
- ((SOCKADDR_IN*)Address)->sin_port = htons(Port);
+#if !USE_UNICODE_WCHAR_CACHE
+ PyMem_Free(Host);
+#endif /* USE_UNICODE_WCHAR_CACHE */
return Length;
}
- else if (PyArg_ParseTuple(obj,
- "uHkk;ConnectEx(): illegal address_as_bytes "
- "argument", &Host, &Port, &FlowInfo, &ScopeId))
- {
- PyErr_Clear();
+ case 4: {
+ if (!PyArg_ParseTuple(obj,
+ "UHkk;ConnectEx(): illegal address_as_bytes argument",
+ &Host_obj, &Port, &FlowInfo, &ScopeId))
+ {
+ return -1;
+ }
+#if USE_UNICODE_WCHAR_CACHE
+ Host = (wchar_t *)_PyUnicode_AsUnicode(Host_obj);
+#else /* USE_UNICODE_WCHAR_CACHE */
+ Host = PyUnicode_AsWideCharString(Host_obj, NULL);
+#endif /* USE_UNICODE_WCHAR_CACHE */
+ if (Host == NULL) {
+ return -1;
+ }
Address->sa_family = AF_INET6;
if (WSAStringToAddressW(Host, AF_INET6, NULL, Address, &Length) < 0) {
SetFromWindowsErr(WSAGetLastError());
- return -1;
+ Length = -1;
+ }
+ else {
+ ((SOCKADDR_IN6*)Address)->sin6_port = htons(Port);
+ ((SOCKADDR_IN6*)Address)->sin6_flowinfo = FlowInfo;
+ ((SOCKADDR_IN6*)Address)->sin6_scope_id = ScopeId;
}
- ((SOCKADDR_IN6*)Address)->sin6_port = htons(Port);
- ((SOCKADDR_IN6*)Address)->sin6_flowinfo = FlowInfo;
- ((SOCKADDR_IN6*)Address)->sin6_scope_id = ScopeId;
+#if !USE_UNICODE_WCHAR_CACHE
+ PyMem_Free(Host);
+#endif /* USE_UNICODE_WCHAR_CACHE */
return Length;
}
-
- return -1;
+ default:
+ PyErr_SetString(PyExc_ValueError, "illegal address_as_bytes argument");
+ return -1;
+ }
}
/*[clinic input]