Extend file type

A

abcd

I have a class which extends 'file' ....

class MyFile(file):
def __init__(self, fname, mode='r'):
file.__init__(self, fname, mode)

def write(self, str):
print "writing a string"
file.write(self, str)

def writelines(self, lines):
print "writing lines"
file.writelines(self, lines)

I use this with subprocess....

f = MyFile('out.txt', 'w')
p = subprocess.Popen(cmd, stdout=f)

.....and I can see that stuff goes into out.txt.....however, I never see
"writing a string" or "writing lines" from the class MyFile.

Any ideas what methods the stdout (and I guess stderr) of Popen objects
from subprocess call?

Any suggestions on how to find out? I did try adding to MyFile....

def __call__(self, *args):
print "calling:", args
return file.__call__(self, *args)


but I never see that either.

thanks
 
F

Fredrik Lundh

abcd said:
Any ideas what methods the stdout (and I guess stderr) of Popen objects
from subprocess call?

the external process only sees OS-level file handles (the number you get
from the fileno() method on your file objects), not Python objects. no
matter how you override things in your process, you cannot make your OS
pretend that the other process is written in Python...

</F>
 
S

Sybren Stuvel

abcd enlightened us with:
Any suggestions on how to find out? I did try adding to MyFile....

def __call__(self, *args):
print "calling:", args
return file.__call__(self, *args)

but I never see that either.

I don't know the answer to your problem, but I can explain why this
doesn't work. __call__ is used when an instance if MyFile is called as
if it were a function:

mf = MyFile("blabla")
mf()

Sybren
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top