Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Lib/test/test_embed.py
Original file line number Diff line number Diff line change
Expand Up @@ -1349,7 +1349,7 @@ def test_audit_run_file(self):
returncode=1)

def test_audit_run_interactivehook(self):
startup = os.path.join(self.oldcwd, support.TESTFN) + ".py"
startup = os.path.join(self.oldcwd, support.TESTFN) + (support.TESTFN or '') + ".py"
with open(startup, "w", encoding="utf-8") as f:
print("import sys", file=f)
print("sys.__interactivehook__ = lambda: None", file=f)
Expand All @@ -1362,7 +1362,7 @@ def test_audit_run_interactivehook(self):
os.unlink(startup)

def test_audit_run_startup(self):
startup = os.path.join(self.oldcwd, support.TESTFN) + ".py"
startup = os.path.join(self.oldcwd, support.TESTFN) + (support.TESTFN or '') + ".py"
with open(startup, "w", encoding="utf-8") as f:
print("pass", file=f)
try:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix decoding errors with audit when open files with non-ASCII names on non-UTF-8
locale.
7 changes: 2 additions & 5 deletions Modules/_ctypes/callproc.c
Original file line number Diff line number Diff line change
Expand Up @@ -1415,15 +1415,12 @@ static PyObject *py_dl_open(PyObject *self, PyObject *args)
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);
Expand Down
9 changes: 8 additions & 1 deletion Modules/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -379,13 +379,20 @@ pymain_run_startup(PyConfig *config, PyCompilerFlags *cf, int *exitcode)
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;
Expand Down
23 changes: 19 additions & 4 deletions Python/fileutils.c
Original file line number Diff line number Diff line change
Expand Up @@ -1274,7 +1274,12 @@ _Py_open_impl(const char *pathname, int flags, int gil_held)
#endif

if (gil_held) {
if (PySys_Audit("open", "sOi", pathname, Py_None, flags) < 0) {
PyObject *pathname_obj = PyUnicode_DecodeFSDefault(pathname);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be possible to call should_audit() here to avoid decoding the string if audit is disabled?

Something like:

if (should_audit()) {
  pathname_obj = PyUnicode_DecodeFSDefault(pathname);
  PySys_Audit(pathname_obj)
  Py_DECREF(pathname_obj);
}

Same remark for _Py_fopen().

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not performance critical, and should_audit() is not available in Python/fileutils.c, so this needs additional work. I prefer to merge simple fix. You can add the call should_audit() in separate PR.

if (pathname_obj == NULL) {
return -1;
}
if (PySys_Audit("open", "OOi", pathname_obj, Py_None, flags) < 0) {
Py_DECREF(pathname_obj);
return -1;
}

Expand All @@ -1284,12 +1289,16 @@ _Py_open_impl(const char *pathname, int flags, int gil_held)
Py_END_ALLOW_THREADS
} while (fd < 0
&& errno == EINTR && !(async_err = PyErr_CheckSignals()));
if (async_err)
if (async_err) {
Py_DECREF(pathname_obj);
return -1;
}
if (fd < 0) {
PyErr_SetFromErrnoWithFilename(PyExc_OSError, pathname);
PyErr_SetFromErrnoWithFilenameObjects(PyExc_OSError, pathname_obj, NULL);
Py_DECREF(pathname_obj);
return -1;
}
Py_DECREF(pathname_obj);
}
else {
fd = open(pathname, flags);
Expand Down Expand Up @@ -1385,9 +1394,15 @@ _Py_wfopen(const wchar_t *path, const wchar_t *mode)
FILE*
_Py_fopen(const char *pathname, const char *mode)
{
if (PySys_Audit("open", "ssi", pathname, mode, 0) < 0) {
PyObject *pathname_obj = PyUnicode_DecodeFSDefault(pathname);
if (pathname_obj == NULL) {
return NULL;
}
if (PySys_Audit("open", "Osi", pathname_obj, mode, 0) < 0) {
Py_DECREF(pathname_obj);
return NULL;
}
Py_DECREF(pathname_obj);

FILE *f = fopen(pathname, mode);
if (f == NULL)
Expand Down