Skip to content
Open
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
2 changes: 1 addition & 1 deletion Include/internal/pycore_moduleobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ static inline PyObject* _PyModule_GetDict(PyObject *mod) {
}

extern PyObject * _PyModule_GetFilenameObject(PyObject *);
extern Py_ssize_t _PyModule_GetFilenameUTF8(
extern Py_ssize_t _PyModule_GetFilename(
PyObject *module,
char *buffer,
Py_ssize_t maxlen);
Expand Down
14 changes: 14 additions & 0 deletions Lib/test/test_interpreters/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -1873,6 +1873,20 @@ def test_call_in_thread(self):
t.join()
self.assertIsNotNone(ctx.caught)

def test_call_with_surrogate_in_main_filename(self):
# https://github.com/python/cpython/issues/156122
script = dedent(r"""
import __main__
from concurrent import interpreters

__main__.__file__ = "bad\ud800.py"
interp = interpreters.create()
interp.call(lambda x: x, [1])
""")
with os_helper.temp_dir() as tempdir:
filename = script_helper.make_script(tempdir, 'my-script', script)
res = script_helper.assert_python_ok(filename)
self.assertEqual(res.out, b'')

class TestIsShareable(TestBase):

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix a crash in :meth:`concurrent.interpreters.Interpreter.call` when
``__main__.__file__`` contains lone surrogates.
17 changes: 13 additions & 4 deletions Objects/moduleobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -975,7 +975,7 @@ PyModule_GetFilename(PyObject *m)
}

Py_ssize_t
_PyModule_GetFilenameUTF8(PyObject *mod, char *buffer, Py_ssize_t maxlen)
_PyModule_GetFilename(PyObject *mod, char *buffer, Py_ssize_t maxlen)
{
// We "return" an empty string for an invalid module
// and for a missing, empty, or invalid filename.
Expand All @@ -991,16 +991,25 @@ _PyModule_GetFilenameUTF8(PyObject *mod, char *buffer, Py_ssize_t maxlen)
size = 0;
}
else {
const char *filename = PyUnicode_AsUTF8AndSize(filenameobj, &size);
assert(size >= 0);
if (size > maxlen) {
char *filename;
PyObject *bytes = PyUnicode_EncodeFSDefault(filenameobj);
if (bytes == NULL) {
goto exit;
}
if (PyBytes_AsStringAndSize(bytes, &filename, &size) < 0) {
Py_DECREF(bytes);
goto exit;
}
if (size >= maxlen) {
size = -1;
PyErr_SetString(PyExc_ValueError, "__file__ too long");
}
else {
(void)strcpy(buffer, filename);
}
Py_DECREF(bytes);
}
exit:
Py_DECREF(filenameobj);
return size;
}
Expand Down
2 changes: 1 addition & 1 deletion Python/crossinterp.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ _Py_GetMainfile(char *buffer, size_t maxlen)
Py_XDECREF(module);
return -1;
}
Py_ssize_t size = _PyModule_GetFilenameUTF8(module, buffer, maxlen);
Py_ssize_t size = _PyModule_GetFilename(module, buffer, maxlen);
Py_DECREF(module);
return size;
}
Expand Down
Loading