Skip to content

Commit 427587f

Browse files
committed
Add ReportEvent to _winapi
1 parent 54016b1 commit 427587f

File tree

7 files changed

+219
-64
lines changed

7 files changed

+219
-64
lines changed

Include/internal/pycore_global_objects_fini_generated.h

Lines changed: 5 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Include/internal/pycore_global_strings.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,9 @@ struct _Py_global_strings {
428428
STRUCT_FOR_ID(env)
429429
STRUCT_FOR_ID(errors)
430430
STRUCT_FOR_ID(event)
431+
STRUCT_FOR_ID(event_category)
432+
STRUCT_FOR_ID(event_id)
433+
STRUCT_FOR_ID(event_type)
431434
STRUCT_FOR_ID(eventmask)
432435
STRUCT_FOR_ID(exc)
433436
STRUCT_FOR_ID(exc_type)
@@ -681,6 +684,7 @@ struct _Py_global_strings {
681684
STRUCT_FOR_ID(query)
682685
STRUCT_FOR_ID(quotetabs)
683686
STRUCT_FOR_ID(raw)
687+
STRUCT_FOR_ID(raw_data)
684688
STRUCT_FOR_ID(read)
685689
STRUCT_FOR_ID(read1)
686690
STRUCT_FOR_ID(readable)
@@ -740,7 +744,6 @@ struct _Py_global_strings {
740744
STRUCT_FOR_ID(sock)
741745
STRUCT_FOR_ID(sort)
742746
STRUCT_FOR_ID(source)
743-
STRUCT_FOR_ID(source_name)
744747
STRUCT_FOR_ID(source_traceback)
745748
STRUCT_FOR_ID(spam)
746749
STRUCT_FOR_ID(src)
@@ -760,6 +763,7 @@ struct _Py_global_strings {
760763
STRUCT_FOR_ID(strict)
761764
STRUCT_FOR_ID(strict_mode)
762765
STRUCT_FOR_ID(string)
766+
STRUCT_FOR_ID(strings)
763767
STRUCT_FOR_ID(sub_key)
764768
STRUCT_FOR_ID(subcalls)
765769
STRUCT_FOR_ID(symmetric_difference_update)
@@ -800,7 +804,6 @@ struct _Py_global_strings {
800804
STRUCT_FOR_ID(tzinfo)
801805
STRUCT_FOR_ID(tzname)
802806
STRUCT_FOR_ID(uid)
803-
STRUCT_FOR_ID(unc_server_name)
804807
STRUCT_FOR_ID(unlink)
805808
STRUCT_FOR_ID(unraisablehook)
806809
STRUCT_FOR_ID(updates)

Include/internal/pycore_runtime_init_generated.h

Lines changed: 5 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Include/internal/pycore_unicodeobject_generated.h

Lines changed: 20 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Lib/test/test_winapi.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,10 +163,32 @@ def test_event_source_registration(self):
163163
handle = _winapi.RegisterEventSource(None, source_name)
164164
self.assertNotEqual(handle, _winapi.INVALID_HANDLE_VALUE)
165165

166-
_winapi.DeregisterEventSource(handle)
166+
# Test ReportEvent with the registered event source
167+
try:
168+
# Test with strings and raw data
169+
test_strings = ["Test message 1", "Test message 2"]
170+
test_data = b"test raw data"
171+
_winapi.ReportEvent(handle, 4, 1, 1002, test_strings, test_data)
172+
173+
# Test with empty strings list
174+
# _winapi.ReportEvent(handle, 2, 0, 1003, [], None) # TODO
175+
finally:
176+
_winapi.DeregisterEventSource(handle)
167177

168178
with self.assertRaises(OSError):
169179
_winapi.RegisterEventSource(None, "")
170180

171181
with self.assertRaises(OSError):
172182
_winapi.DeregisterEventSource(_winapi.INVALID_HANDLE_VALUE)
183+
184+
# Test ReportEvent with invalid handle
185+
with self.assertRaises(OSError):
186+
_winapi.ReportEvent(_winapi.INVALID_HANDLE_VALUE, 1, 0, 1001, [], test_data)
187+
188+
# Test ReportEvent with invalid string types
189+
handle2 = _winapi.RegisterEventSource(None, "PythonTestEventSource2")
190+
try:
191+
with self.assertRaises(TypeError):
192+
_winapi.ReportEvent(handle2, 1, 0, 1001, ["string", 123], test_data)
193+
finally:
194+
_winapi.DeregisterEventSource(handle2)

Modules/_winapi.c

Lines changed: 92 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2985,17 +2985,18 @@ _winapi.RegisterEventSource -> HANDLE
29852985
If NULL, registers the event source on the local computer.
29862986
source_name: LPCWSTR
29872987
The name of the event source to register.
2988+
/
29882989
[clinic start generated code]*/
29892990

29902991
static HANDLE
29912992
_winapi_RegisterEventSource_impl(PyObject *module, LPCWSTR unc_server_name,
29922993
LPCWSTR source_name)
2993-
/*[clinic end generated code: output=e376c8950a89ae8f input=ee83ab132b89b99c]*/
2994+
/*[clinic end generated code: output=e376c8950a89ae8f input=16ae3c812a905cab]*/
29942995
{
29952996
HANDLE handle;
29962997

29972998
Py_BEGIN_ALLOW_THREADS
2998-
handle = RegisterEventSource(unc_server_name, source_name);
2999+
handle = RegisterEventSourceW(unc_server_name, source_name);
29993000
Py_END_ALLOW_THREADS
30003001

30013002
if (handle == NULL) {
@@ -3011,11 +3012,12 @@ _winapi.DeregisterEventSource
30113012
30123013
handle: HANDLE
30133014
The handle to the event log to be deregistered.
3015+
/
30143016
[clinic start generated code]*/
30153017

30163018
static PyObject *
30173019
_winapi_DeregisterEventSource_impl(PyObject *module, HANDLE handle)
3018-
/*[clinic end generated code: output=7387ff34c7358bce input=07d42083b03ba7b0]*/
3020+
/*[clinic end generated code: output=7387ff34c7358bce input=0973c68eddcfd5a7]*/
30193021
{
30203022
BOOL success;
30213023

@@ -3029,6 +3031,92 @@ _winapi_DeregisterEventSource_impl(PyObject *module, HANDLE handle)
30293031
Py_RETURN_NONE;
30303032
}
30313033

3034+
/*[clinic input]
3035+
_winapi.ReportEvent
3036+
3037+
handle: HANDLE
3038+
The handle to the event log.
3039+
event_type: int
3040+
The type of event being reported.
3041+
event_category: int
3042+
The event category.
3043+
event_id: int
3044+
The event identifier.
3045+
strings: object(subclass_of='&PyList_Type')
3046+
A list of strings to be inserted into the event message.
3047+
raw_data: Py_buffer
3048+
The raw data for the event.
3049+
[clinic start generated code]*/
3050+
3051+
static PyObject *
3052+
_winapi_ReportEvent_impl(PyObject *module, HANDLE handle, int event_type,
3053+
int event_category, int event_id, PyObject *strings,
3054+
Py_buffer *raw_data)
3055+
/*[clinic end generated code: output=9066f114cdfdf5f2 input=fc37cfe1816d02d7]*/
3056+
{
3057+
BOOL success;
3058+
LPCWSTR *string_array = NULL;
3059+
WORD num_strings = 0;
3060+
LPVOID data = NULL;
3061+
DWORD data_size = 0;
3062+
3063+
// Handle strings list
3064+
if (strings != Py_None && PyList_Check(strings)) {
3065+
Py_ssize_t size = PyList_Size(strings);
3066+
num_strings = (WORD)size;
3067+
3068+
if (num_strings > 0) {
3069+
string_array = (LPCWSTR *)PyMem_Malloc(num_strings * sizeof(LPCWSTR));
3070+
if (!string_array) {
3071+
return PyErr_NoMemory();
3072+
}
3073+
3074+
for (WORD i = 0; i < num_strings; i++) {
3075+
PyObject *item = PyList_GetItem(strings, i);
3076+
if (!PyUnicode_Check(item)) {
3077+
PyMem_Free(string_array);
3078+
PyErr_SetString(PyExc_TypeError, "All strings must be unicode");
3079+
return NULL;
3080+
}
3081+
string_array[i] = PyUnicode_AsWideCharString(item, NULL);
3082+
if (!string_array[i]) {
3083+
// Clean up already allocated strings
3084+
for (WORD j = 0; j < i; j++) {
3085+
PyMem_Free((void *)string_array[j]);
3086+
}
3087+
PyMem_Free(string_array);
3088+
return NULL;
3089+
}
3090+
}
3091+
}
3092+
}
3093+
3094+
// Handle raw data
3095+
if (raw_data->buf != NULL) {
3096+
data = raw_data->buf;
3097+
data_size = (DWORD) raw_data->len;
3098+
}
3099+
3100+
Py_BEGIN_ALLOW_THREADS
3101+
success = ReportEventW(handle, event_type, event_category, event_id,
3102+
NULL, num_strings, data_size,
3103+
string_array, data);
3104+
Py_END_ALLOW_THREADS
3105+
3106+
// Clean up allocated strings
3107+
if (string_array) {
3108+
for (WORD i = 0; i < num_strings; i++) {
3109+
PyMem_Free((void *)string_array[i]);
3110+
}
3111+
PyMem_Free(string_array);
3112+
}
3113+
3114+
if (!success)
3115+
return PyErr_SetFromWindowsErr(0);
3116+
3117+
Py_RETURN_NONE;
3118+
}
3119+
30323120

30333121
static PyMethodDef winapi_functions[] = {
30343122
_WINAPI_CLOSEHANDLE_METHODDEF
@@ -3059,6 +3147,7 @@ static PyMethodDef winapi_functions[] = {
30593147
_WINAPI_OPENPROCESS_METHODDEF
30603148
_WINAPI_PEEKNAMEDPIPE_METHODDEF
30613149
_WINAPI_REGISTEREVENTSOURCE_METHODDEF
3150+
_WINAPI_REPORTEVENT_METHODDEF
30623151
_WINAPI_LCMAPSTRINGEX_METHODDEF
30633152
_WINAPI_READFILE_METHODDEF
30643153
_WINAPI_RELEASEMUTEX_METHODDEF

0 commit comments

Comments
 (0)