================
@@ -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);
----------------
Nerixyz wrote:

[PEP 384](https://peps.python.org/pep-0384/#excluded-functions) mentions that 
all functions listed in `PC/python3.def` are part of the limited and thus 
stable ABI - `PyObject_CallFunction` is there:  
https://github.com/python/cpython/blob/39b2f82717a69dde7212bc39b673b0f55c99e6a3/PC/python3.def#L453

https://github.com/llvm/llvm-project/pull/217126
_______________________________________________
lldb-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits

Reply via email to