Extend file type

Discussion in 'Python' started by abcd, Sep 26, 2006.

  1. abcd

    abcd Guest

    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
    abcd, Sep 26, 2006
    #1
    1. Advertising

  2. 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>
    Fredrik Lundh, Sep 26, 2006
    #2
    1. Advertising

  3. 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
    --
    Sybren Stüvel
    Stüvel IT - http://www.stuvel.eu/
    Sybren Stuvel, Sep 26, 2006
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Charles Fineman
    Replies:
    2
    Views:
    310
    Charles Fineman
    Feb 25, 2004
  2. Paul Rubin

    Can't extend function type

    Paul Rubin, Oct 7, 2005, in forum: Python
    Replies:
    5
    Views:
    463
    Paul Rubin
    Oct 7, 2005
  3. Stef Mientki
    Replies:
    3
    Views:
    437
    Bruno Desthuilliers
    Nov 27, 2008
  4. Chris Rebert
    Replies:
    0
    Views:
    596
    Chris Rebert
    Nov 26, 2008
  5. Terry Reedy
    Replies:
    3
    Views:
    271
    Steven D'Aprano
    Nov 27, 2008
Loading...

Share This Page