如何从Python文件句柄打开.NET FileStream对象?

2024-06-16 06:38:24 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要用Python打开一个可写的文件句柄,然后将文件描述符交给.NET程序集中的函数(通过pythonnet's ^{} module访问)。在

从Python file对象到win32 HANDLE*相当简单,如this question所示:

import clr
from Microsoft.Win32.SafeHandles import SafeFileHandle
from System.IO import FileStream, FileAccess

pyf=open("c:/temp/testing123.txt","w")
fileno=pyf.fileno()
print fileno               # 6
handle = msvcrt.get_osfhandle(fileno)
print handle               # 1832L

According to MSDN,现在应该可以从一个直的IntPtr(句柄)或SafeFileHandle包装构造一个标准的FileStream对象。在

^{pr2}$

问题是。。。如何说服clr模块将handle转换为IntPtr?在

我试过以下各种版本,但都有错误:

FileStream(IntPtr(handle), True)
FileStream(IntPtr(Int64(handle), True)
FileStream(IntPtr(Int32(handle), True)
SafeFileHandle(IntPtr(handle), True)
...

=> TypeError ("value cannot be converted to System.IntPtr")

关于如何将这个该死的文件句柄放入C中有什么建议吗?在


Tags: 文件对象fromimporttrue句柄systemhandle
2条回答

感谢^{} mailing list上的好心人,我得到了答案。在

关键是使用Overloads构造函数将win32 HANDLE强制转换为IntPtr类型。在

下面是一个完整的工作示例:

import tempfile, msvcrt
import clr, msvcrt
from System.IO import FileStream, FileAccess
from System import IntPtr

with tempfile.NamedTemporaryFile(suffix='.txt', delete=False) as pyf:
    fileno=pyf.fileno()
    print "fileno", fileno
    handle = msvcrt.get_osfhandle(fileno)
    print "HANDLE", handle

    pyf.write("Python\n")
    pyf.flush()

    cs_handle = IntPtr.Overloads[long](handle)
    cs_fs = FileStream(cs_handle, FileAccess.Write)
    cs_fs.Write("CLR\n", 0, 4)
    cs_fs.Flush()

print "file should contain a line from Python and a line from CLR: ", pyf.name

对于这个答案可能有点晚了,但是像this这样的方法有用吗?在

from contextlib import contextmanager
import ctypes
import io
import os, sys
import tempfile

libc = ctypes.CDLL(None)
c_stdout = ctypes.c_void_p.in_dll(libc, 'stdout')

@contextmanager
def stdout_redirector(stream):
    # The original fd stdout points to. Usually 1 on POSIX systems.
    original_stdout_fd = sys.stdout.fileno()

    def _redirect_stdout(to_fd):
        """Redirect stdout to the given file descriptor."""
        # Flush the C-level buffer stdout
        libc.fflush(c_stdout)
        # Flush and close sys.stdout - also closes the file descriptor (fd)
        sys.stdout.close()
        # Make original_stdout_fd point to the same file as to_fd
        os.dup2(to_fd, original_stdout_fd)
        # Create a new sys.stdout that points to the redirected fd
        sys.stdout = io.TextIOWrapper(os.fdopen(original_stdout_fd, 'wb'))

    # Save a copy of the original stdout fd in saved_stdout_fd
    saved_stdout_fd = os.dup(original_stdout_fd)
    try:
        # Create a temporary file and redirect stdout to it
        tfile = tempfile.TemporaryFile(mode='w+b')
        _redirect_stdout(tfile.fileno())
        # Yield to caller, then redirect stdout back to the saved fd
        yield
        _redirect_stdout(saved_stdout_fd)
        # Copy contents of temporary file to the given stream
        tfile.flush()
        tfile.seek(0, io.SEEK_SET)
        stream.write(tfile.read())
    finally:
        tfile.close()
        os.close(saved_stdout_fd)

相关问题 更多 >