Extend file type

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • abcd

    #1

    Extend file type

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

    class MyFile(file):
    def __init__(self, fname, mode='r'):
    file.__init__(s elf, 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.Pope n(cmd, stdout=f)

    .....and I can see that stuff goes into out.txt.....how ever, 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__(s elf, *args)


    but I never see that either.

    thanks

  • Fredrik Lundh

    #2
    Re: Extend file type

    abcd wrote:
    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>

    Comment

    • Sybren Stuvel

      #3
      Re: Extend file type

      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__(s elf, *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
      --
      Sybren Stüvel
      Stüvel IT - http://www.stuvel.eu/

      Comment

      Working...