================
@@ -1000,6 +1004,63 @@ bool PythonFile::Check(PyObject *py_obj) {
return !!r;
}
+#if defined(_WIN32) && !defined(_DLL)
+// When LLVM is built with a different CRT allocator, it's built against the
+// static C runtime. The official Python builds link to the dynamic C runtime.
+// Since the file descriptors are managed per CRT instance, liblldb and Python
+// have different fd mappings. This translates between the two using the msvcrt
+// module.
+int PythonFile::TranslateFdToPython(int our_fd) {
+ intptr_t handle = _get_osfhandle(our_fd);
+ if (handle == 0 || (HANDLE)handle == INVALID_HANDLE_VALUE)
+ return -1;
+
+ PyObject *msvcrt = PyImport_ImportModule("msvcrt");
+ if (!msvcrt)
+ return -1;
+ PyObject *open_osf = PyObject_GetAttrString(msvcrt, "open_osfhandle");
+ Py_XDECREF(msvcrt);
+ if (!open_osf)
+ return -1;
+ PyObject *fd_obj =
+ PyObject_CallFunction(open_osf, "Li", (long long)handle, 0);
+ Py_XDECREF(open_osf);
+ if (!fd_obj)
+ return -1;
+ if (!PyLong_Check(fd_obj)) {
+ Py_XDECREF(fd_obj);
+ return -1;
+ }
+ long theirs = PyLong_AsLong(fd_obj);
+ Py_XDECREF(fd_obj);
+ return (int)theirs;
+}
+
+int PythonFile::TranslateFdFromPython(int their_fd) {
+ PyObject *msvcrt = PyImport_ImportModule("msvcrt");
+ if (!msvcrt)
+ return -1;
+ PyObject *get_handle = PyObject_GetAttrString(msvcrt, "get_osfhandle");
+ Py_XDECREF(msvcrt);
+ if (!get_handle)
+ return -1;
+ PyObject *handle_obj = PyObject_CallFunction(get_handle, "i", their_fd);
+ Py_XDECREF(get_handle);
+ if (!handle_obj)
+ return -1;
+ if (!PyLong_Check(handle_obj)) {
----------------
Nerixyz wrote:
I don't know why I thought this was part of it, I'll change that to check for
an exception instead.
https://github.com/llvm/llvm-project/pull/217126
_______________________________________________
lldb-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits