bpo-41094: Fix decoding errors with audit when open files. (GH-21095)
(cherry picked from commit 6c6810d98979add7a89391c3c38990d0859f7a29)
Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
diff --git a/Modules/_ctypes/callproc.c b/Modules/_ctypes/callproc.c
index 55fc226..a9b8675 100644
--- a/Modules/_ctypes/callproc.c
+++ b/Modules/_ctypes/callproc.c
@@ -1416,15 +1416,12 @@
if (name != Py_None) {
if (PyUnicode_FSConverter(name, &name2) == 0)
return NULL;
- if (PyBytes_Check(name2))
- name_str = PyBytes_AS_STRING(name2);
- else
- name_str = PyByteArray_AS_STRING(name2);
+ name_str = PyBytes_AS_STRING(name2);
} else {
name_str = NULL;
name2 = NULL;
}
- if (PySys_Audit("ctypes.dlopen", "s", name_str) < 0) {
+ if (PySys_Audit("ctypes.dlopen", "O", name) < 0) {
return NULL;
}
handle = ctypes_dlopen(name_str, mode);
diff --git a/Modules/main.c b/Modules/main.c
index 2a360b5..788bc11 100644
--- a/Modules/main.c
+++ b/Modules/main.c
@@ -391,13 +391,20 @@
if (startup == NULL) {
return 0;
}
- if (PySys_Audit("cpython.run_startup", "s", startup) < 0) {
+ PyObject *startup_obj = PyUnicode_DecodeFSDefault(startup);
+ if (startup_obj == NULL) {
return pymain_err_print(exitcode);
}
+ if (PySys_Audit("cpython.run_startup", "O", startup_obj) < 0) {
+ Py_DECREF(startup_obj);
+ return pymain_err_print(exitcode);
+ }
+ Py_DECREF(startup_obj);
FILE *fp = _Py_fopen(startup, "r");
if (fp == NULL) {
int save_errno = errno;
+ PyErr_Clear();
PySys_WriteStderr("Could not open PYTHONSTARTUP\n");
errno = save_errno;