[Python.NET] Getting an open Python file handle into CLR

Dan Lenski <[email protected]>
Newsgroups gmane.comp.python.dotnet
Message-ID <[email protected]>
Hi,
I posted this on StackOverflow but thought it might be a better question 
for this mailing list.

http://stackoverflow.com/questions/31530059/how-can-i-open-a-net-
filestream-object-from-a-python-file-handle

I'm trying to figure out how to transform an open Python file handle 
into a System.IO.FileStream object so that I can pass it to C# code.

Here's what I've got so far. This produces the good ol' win32 (HANDLE*) 
object, I believe:

import clr, msvcrt
from Microsoft.Win32.SafeHandles import SafeFileHandle
from System.IO import FileStream, FileAccess
from System import IntPtr, Int32, Int64

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

Unfortunately, I can't figure out how to convince the clr module to cast 
this handle into something that I can use as an IntPtr, as required by 
the constructors of FileStream or SafeFileHandle.

I've tried various versions of the following, but they all give me 
TypeError ("value cannot be converted to System.IntPtr"):

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

Thanks,
Dan Lenski

_________________________________________________
Python.NET mailing list - [email protected]
https://mail.python.org/mailman/listinfo/pythondotnet
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.