Skip to content

Commit 9d3f66e

Browse files
committed
Only support one single string for _winapi.ReposrtEvent
1 parent 3d06503 commit 9d3f66e

File tree

7 files changed

+29
-142
lines changed

7 files changed

+29
-142
lines changed

Include/internal/pycore_global_objects_fini_generated.h

Lines changed: 0 additions & 3 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: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,6 @@ struct _Py_global_strings {
435435
STRUCT_FOR_ID(env)
436436
STRUCT_FOR_ID(errors)
437437
STRUCT_FOR_ID(event)
438-
STRUCT_FOR_ID(event_id)
439438
STRUCT_FOR_ID(eventmask)
440439
STRUCT_FOR_ID(exc)
441440
STRUCT_FOR_ID(exc_type)
@@ -692,7 +691,6 @@ struct _Py_global_strings {
692691
STRUCT_FOR_ID(query)
693692
STRUCT_FOR_ID(quotetabs)
694693
STRUCT_FOR_ID(raw)
695-
STRUCT_FOR_ID(raw_data)
696694
STRUCT_FOR_ID(read)
697695
STRUCT_FOR_ID(read1)
698696
STRUCT_FOR_ID(readable)
@@ -773,7 +771,6 @@ struct _Py_global_strings {
773771
STRUCT_FOR_ID(strict)
774772
STRUCT_FOR_ID(strict_mode)
775773
STRUCT_FOR_ID(string)
776-
STRUCT_FOR_ID(strings)
777774
STRUCT_FOR_ID(sub_key)
778775
STRUCT_FOR_ID(subcalls)
779776
STRUCT_FOR_ID(symmetric_difference_update)

Include/internal/pycore_runtime_init_generated.h

Lines changed: 0 additions & 3 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: 0 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Lib/logging/handlers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1223,7 +1223,7 @@ def emit(self, record):
12231223
handle = self._winapi.RegisterEventSource(None, self.appname)
12241224
if handle != self._winapi.INVALID_HANDLE_VALUE:
12251225
try:
1226-
self._winapi.ReportEvent(handle, type, cat, id, [msg])
1226+
self._winapi.ReportEvent(handle, type, cat, id, msg)
12271227
finally:
12281228
self._winapi.DeregisterEventSource(handle)
12291229
except Exception:

Modules/_winapi.c

Lines changed: 17 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -3046,99 +3046,44 @@ _winapi.ReportEvent
30463046
The event category.
30473047
event_id: unsigned_int(bitwise=False)
30483048
The event identifier.
3049-
strings: object(subclass_of='&PyList_Type')
3050-
A list of strings to be inserted into the event message.
3051-
raw_data: Py_buffer(accept={str, buffer, NoneType}) = None
3052-
The raw data for the event.
3049+
string: object
3050+
A string to be inserted into the event message.
3051+
/
30533052
30543053
Writes an entry at the end of the specified event log.
30553054
[clinic start generated code]*/
30563055

30573056
static PyObject *
30583057
_winapi_ReportEvent_impl(PyObject *module, HANDLE handle,
30593058
unsigned short type, unsigned short category,
3060-
unsigned int event_id, PyObject *strings,
3061-
Py_buffer *raw_data)
3062-
/*[clinic end generated code: output=fc3bbbde78cffd6c input=abcc01d4fc284975]*/
3059+
unsigned int event_id, PyObject *string)
3060+
/*[clinic end generated code: output=8eb5e919369c9c6d input=6db2c51252f95b93]*/
30633061
{
30643062
BOOL success;
3065-
LPCWSTR *string_array = NULL;
3066-
WORD num_strings = 0;
3067-
LPVOID data = NULL;
3068-
DWORD data_size = 0;
3069-
3070-
// Handle strings list
3071-
Py_ssize_t size = PyList_Size(strings);
3072-
if (size > USHRT_MAX) {
3073-
PyErr_SetString(PyExc_ValueError, "strings is too long");
3074-
return NULL;
3075-
}
3076-
num_strings = (WORD)size;
3063+
LPCWSTR wide_string = NULL;
30773064

3078-
// Handle raw data
3079-
if (raw_data->len > PY_DWORD_MAX) {
3080-
PyErr_SetString(PyExc_ValueError, "raw_data is too large");
3065+
if (!PyUnicode_Check(string)) {
3066+
PyErr_SetString(PyExc_TypeError, "string must be a str");
30813067
return NULL;
30823068
}
3083-
if (raw_data->obj != NULL) {
3084-
data = raw_data->buf;
3085-
data_size = (DWORD)raw_data->len;
3086-
}
3087-
3088-
if (num_strings > 0) {
3089-
string_array = (LPCWSTR *)PyMem_New(LPCWSTR, num_strings);
3090-
if (string_array == NULL) {
3091-
return PyErr_NoMemory();
3092-
}
30933069

3094-
for (WORD i = 0; i < num_strings; i++) {
3095-
PyObject *item = PyList_GetItemRef(strings, i);
3096-
if (item == NULL) {
3097-
// Clean up already allocated strings
3098-
for (WORD j = 0; j < i; j++) {
3099-
PyMem_Free((void *)string_array[j]);
3100-
}
3101-
PyMem_Free(string_array);
3102-
return NULL;
3103-
}
3104-
if (!PyUnicode_Check(item)) {
3105-
for (WORD j = 0; j < i; j++) {
3106-
PyMem_Free((void *)string_array[j]);
3107-
}
3108-
PyMem_Free(string_array);
3109-
PyErr_Format(PyExc_TypeError,
3110-
"expected a list of strings, not %T", item);
3111-
return NULL;
3112-
}
3113-
string_array[i] = PyUnicode_AsWideCharString(item, NULL);
3114-
Py_DECREF(item);
3115-
if (!string_array[i]) {
3116-
for (WORD j = 0; j < i; j++) {
3117-
PyMem_Free((void *)string_array[j]);
3118-
}
3119-
PyMem_Free(string_array);
3120-
return NULL;
3121-
}
3122-
}
3070+
wide_string = PyUnicode_AsWideCharString(string, NULL);
3071+
if (!wide_string) {
3072+
return NULL;
31233073
}
31243074

31253075
Py_BEGIN_ALLOW_THREADS
3126-
success = ReportEventW(handle, type, category, event_id,
3127-
NULL, num_strings, data_size,
3128-
string_array, data);
3076+
success = ReportEventW(handle, type, category, event_id, NULL, 1, 0,
3077+
&wide_string, NULL);
31293078
Py_END_ALLOW_THREADS
31303079

31313080
int ret = GetLastError();
3132-
// Clean up allocated strings
3133-
if (string_array) {
3134-
for (WORD i = 0; i < num_strings; i++) {
3135-
PyMem_Free((void *)string_array[i]);
3136-
}
3137-
PyMem_Free(string_array);
3138-
}
31393081

3140-
if (!success)
3082+
PyMem_Free((void *)wide_string);
3083+
3084+
if (!success) {
31413085
return PyErr_SetFromWindowsErr(ret);
3086+
}
31423087

31433088
Py_RETURN_NONE;
31443089
}

Modules/clinic/_winapi.c.h

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

0 commit comments

Comments
 (0)