Wrap create mutex

This commit is contained in:
Kovid Goyal 2020-10-14 09:31:47 +05:30
parent 250fbf8c3f
commit e0d98c07e7
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
2 changed files with 18 additions and 0 deletions

View File

@ -247,6 +247,9 @@ class BuildTest(unittest.TestCase):
os.rmdir(dpath) os.rmdir(dpath)
del h del h
shutil.rmtree(tdir) shutil.rmtree(tdir)
m = winutil.create_mutex("test-mutex", False)
self.assertRaises(OSError, winutil.create_mutex, 'test-mutex', False)
m.close()
def test_sqlite(self): def test_sqlite(self):
import sqlite3 import sqlite3

View File

@ -786,6 +786,20 @@ load_library(PyObject *self, PyObject *args) {
return (PyObject*)Handle_create(h, ModuleHandle, PyTuple_GET_ITEM(args, 0)); return (PyObject*)Handle_create(h, ModuleHandle, PyTuple_GET_ITEM(args, 0));
} }
static PyObject*
create_mutex(PyObject *self, PyObject *args) {
int initial_owner = 0, allow_existing = 1;
wchar_raii name;
if (!PyArg_ParseTuple(args, "O&|pp", py_to_wchar, &name, &allow_existing, &initial_owner)) return NULL;
HANDLE h = CreateMutexW(NULL, initial_owner, name.ptr());
if (h == NULL) return PyErr_SetExcFromWindowsErrWithFilenameObject(PyExc_OSError, 0, PyTuple_GET_ITEM(args, 0));
if (!allow_existing && GetLastError() == ERROR_ALREADY_EXISTS) {
CloseHandle(h);
return PyErr_SetExcFromWindowsErrWithFilenameObject(PyExc_FileExistsError, ERROR_ALREADY_EXISTS, PyTuple_GET_ITEM(args, 0));
}
return (PyObject*)Handle_create(h);
}
// Icon loading {{{ // Icon loading {{{
#pragma pack( push ) #pragma pack( push )
#pragma pack( 2 ) #pragma pack( 2 )
@ -920,6 +934,7 @@ static const char winutil_doc[] = "Defines utility methods to interface with win
#define M(name, args) { #name, name, args, ""} #define M(name, args) { #name, name, args, ""}
static PyMethodDef winutil_methods[] = { static PyMethodDef winutil_methods[] = {
M(get_dll_directory, METH_NOARGS), M(get_dll_directory, METH_NOARGS),
M(create_mutex, METH_VARARGS),
M(get_async_key_state, METH_VARARGS), M(get_async_key_state, METH_VARARGS),
M(create_named_pipe, METH_VARARGS), M(create_named_pipe, METH_VARARGS),
M(set_handle_information, METH_VARARGS), M(set_handle_information, METH_VARARGS),