bpo-36842: Implement PEP 578 (GH-12613)

Adds sys.audit, sys.addaudithook, io.open_code, and associated C APIs.
This commit is contained in:
Steve Dower 2019-05-23 08:45:22 -07:00 committed by GitHub
parent e788057a91
commit b82e17e626
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
70 changed files with 3565 additions and 1816 deletions

View file

@ -461,6 +461,12 @@ _winapi_CreateFile_impl(PyObject *module, LPCTSTR file_name,
{
HANDLE handle;
if (PySys_Audit("_winapi.CreateFile", "uIIII",
file_name, desired_access, share_mode,
creation_disposition, flags_and_attributes) < 0) {
return INVALID_HANDLE_VALUE;
}
Py_BEGIN_ALLOW_THREADS
handle = CreateFile(file_name, desired_access,
share_mode, security_attributes,
@ -542,6 +548,10 @@ _winapi_CreateJunction_impl(PyObject *module, LPWSTR src_path,
if (wcsncmp(src_path, L"\\??\\", prefix_len) == 0)
return PyErr_SetFromWindowsErr(ERROR_INVALID_PARAMETER);
if (PySys_Audit("_winapi.CreateJunction", "uu", src_path, dst_path) < 0) {
return NULL;
}
/* Adjust privileges to allow rewriting directory entry as a
junction point. */
if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &token))
@ -670,6 +680,11 @@ _winapi_CreateNamedPipe_impl(PyObject *module, LPCTSTR name, DWORD open_mode,
{
HANDLE handle;
if (PySys_Audit("_winapi.CreateNamedPipe", "uII",
name, open_mode, pipe_mode) < 0) {
return INVALID_HANDLE_VALUE;
}
Py_BEGIN_ALLOW_THREADS
handle = CreateNamedPipe(name, open_mode, pipe_mode,
max_instances, out_buffer_size,
@ -704,6 +719,10 @@ _winapi_CreatePipe_impl(PyObject *module, PyObject *pipe_attrs, DWORD size)
HANDLE write_pipe;
BOOL result;
if (PySys_Audit("_winapi.CreatePipe", NULL) < 0) {
return NULL;
}
Py_BEGIN_ALLOW_THREADS
result = CreatePipe(&read_pipe, &write_pipe, NULL, size);
Py_END_ALLOW_THREADS
@ -1055,6 +1074,11 @@ _winapi_CreateProcess_impl(PyObject *module,
wchar_t *command_line_copy = NULL;
AttributeList attribute_list = {0};
if (PySys_Audit("_winapi.CreateProcess", "uuu", application_name,
command_line, current_directory) < 0) {
return NULL;
}
ZeroMemory(&si, sizeof(si));
si.StartupInfo.cb = sizeof(si);
@ -1270,8 +1294,10 @@ _winapi_GetModuleFileName_impl(PyObject *module, HMODULE module_handle)
BOOL result;
WCHAR filename[MAX_PATH];
Py_BEGIN_ALLOW_THREADS
result = GetModuleFileNameW(module_handle, filename, MAX_PATH);
filename[MAX_PATH-1] = '\0';
Py_END_ALLOW_THREADS
if (! result)
return PyErr_SetFromWindowsErr(GetLastError());
@ -1402,9 +1428,16 @@ _winapi_OpenProcess_impl(PyObject *module, DWORD desired_access,
{
HANDLE handle;
if (PySys_Audit("_winapi.OpenProcess", "II",
process_id, desired_access) < 0) {
return INVALID_HANDLE_VALUE;
}
Py_BEGIN_ALLOW_THREADS
handle = OpenProcess(desired_access, inherit_handle, process_id);
Py_END_ALLOW_THREADS
if (handle == NULL) {
PyErr_SetFromWindowsErr(0);
PyErr_SetFromWindowsErr(GetLastError());
handle = INVALID_HANDLE_VALUE;
}
@ -1539,6 +1572,7 @@ _winapi_SetNamedPipeHandleState_impl(PyObject *module, HANDLE named_pipe,
PyObject *oArgs[3] = {mode, max_collection_count, collect_data_timeout};
DWORD dwArgs[3], *pArgs[3] = {NULL, NULL, NULL};
int i;
BOOL b;
for (i = 0 ; i < 3 ; i++) {
if (oArgs[i] != Py_None) {
@ -1549,7 +1583,11 @@ _winapi_SetNamedPipeHandleState_impl(PyObject *module, HANDLE named_pipe,
}
}
if (!SetNamedPipeHandleState(named_pipe, pArgs[0], pArgs[1], pArgs[2]))
Py_BEGIN_ALLOW_THREADS
b = SetNamedPipeHandleState(named_pipe, pArgs[0], pArgs[1], pArgs[2]);
Py_END_ALLOW_THREADS
if (!b)
return PyErr_SetFromWindowsErr(0);
Py_RETURN_NONE;
@ -1573,6 +1611,11 @@ _winapi_TerminateProcess_impl(PyObject *module, HANDLE handle,
{
BOOL result;
if (PySys_Audit("_winapi.TerminateProcess", "nI",
(Py_ssize_t)handle, exit_code) < 0) {
return NULL;
}
result = TerminateProcess(handle, exit_code);
if (! result)