Access to istream objects at the Python layer

Hi all,

I am attempting to find a way to pass a string (or strings) from the Python layer down to the C++ layer of Panda, and have it appear as an istream.

Specifically we are working with the DCFile class, and trying to pass it .dc files that are generated programatically, rather than physically stored on disk. But I can envision this being useful in other scenarios as well. There is already an implementation of the read() method of DCFile that takes an istream, but I’m stumped trying to figure out a way to implement an istream at the Python layer and make it do what I want.

I note that Panda has a LineStream class, which appears to do exactly the opposite of what I want. It implements an ostream object, allows the C++ layer to drop data into it, then allows the Python layer to read it back out as strings. I want an istream object that I can drop data into, and have the C++ layer read back.

Does any such functionality exist in Panda? I’ve looked through the documentation and couldn’t find anything, but I could be missing something obvious. We’re already mucking around in the source code, so I can implement it if needed, but I don’t want to duplicate work that’s already been done.

Also, I note that the Panda documentation references istream and ostream classes that should be available to me, but when I try to import them (e.g., from pandac.PandaModules import istream), Python simply tells me that it can’t. I thought that perhaps these were thin layers over the standard C++ istream and ostream classes that I could bend to do what I want. Does anyone know the nature of these classes, or why I am unable to pull them in?

Thanks,
-lem

Hmm, that’s something we never thought of adding. You’re right: the LineStream class goes the other way, and we use it pretty often (for instance, for defining str() and repr() for the wrapped C++ objects). But we never implemented an istream for Python. It would be pretty easy to do, though, and it does sound useful; so I’ll see about putting something in for that.

The istream and ostream you see referenced in the Panda documentation are primarily of benefit to C++ programmers and are generally vestigial to Python users, since you have to be able to create an instance of these objects to use these methods. We don’t wrap the entire C++ ostream and istream implementations. (But note that we do define a global Python object called “ostream” which can be passed to any method that takes an ostream, and which writes to the console. We don’t have a global istream object, though.)

David

Thanks David. The factor driving this need (programatic .dc file creation) is pretty low priority for me right now, but I’ll keep an eye on the source tree and see if something pops up :slight_smile: I appreciate it!

  • lem

I should point out that Panda3D 1.4.0 now provides a StringStream class, which can be used either as an istream or ostream, solving this problem. setData() and getData() are the two primary Python interfaces.

In fact, most low-level Panda I/O calls now accept either an istream or a filename, making it possible to load just about any file from an in-memory StringStream instead of from a file on disk. You can even create a Multifile from a StringStream, and then mount it, thus making an entire file system from an in-memory image.

David