Using the 'with' statement with cStringIO objects

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

    Using the 'with' statement with cStringIO objects

    I've been experimenting with the 'with' statement (in __future__), and
    so far I like it. However, I can't get it to work with a cStringIO
    object. Here's a minimum working example:

    ###
    from __future__ import with_statement
    import cStringIO

    teststring='thi s is a test'

    with cStringIO.Strin gIO(teststring) as testfile:
    pass
    ###

    I get the following error message:

    Traceback (most recent call last):
    File "testfile.p y", line 6, in <module>
    with cStringIO.Strin gIO(teststring) as testfile:
    AttributeError: 'cStringIO.Stri ngI' object has no attribute '__exit__'

    So, I'm guessing you can't use the 'with' statement with cStringIO
    objects? Is this a bug, or do I need to use the 'with' statement
    differently to get this to work?

    Thanks,
    peppergrower
  • Fredrik Lundh

    #2
    Re: Using the 'with' statement with cStringIO objects

    peppergrower wrote:
    teststring='thi s is a test'
    >
    with cStringIO.Strin gIO(teststring) as testfile:
    pass
    umm. what exactly do you expect that code to do?

    </F>

    Comment

    • George Boutsioukis

      #3
      Re: Using the 'with' statement with cStringIO objects

      So, I'm guessing you can't use the 'with' statement with cStringIO
      objects? Is this a bug, or do I need to use the 'with' statement
      differently to get this to work?
      >
      Thanks,
      peppergrower
      Neither, just not implemented. Only classes with __enter__ and __exit__
      methods(ie context manager types) can be used in with statements. And,
      correct me if I'm wrong, I think it's pointless for a StringIO object to
      have those.

      Comment

      • Hrvoje Niksic

        #4
        Re: Using the 'with' statement with cStringIO objects

        George Boutsioukis <gboutsioukis@g mail.comwrites:
        Neither, just not implemented. Only classes with __enter__ and
        __exit__ methods(ie context manager types) can be used in with
        statements. And, correct me if I'm wrong, I think it's pointless for
        a StringIO object to have those.
        It's definitely superfluous, but it should still be provided, for the
        same reason a "close" method is provided, to allow StringIO objects to
        be treated like other file-like objects. For example, code that does
        the following:

        with obj.open_file(. ..) as f:
        ...

        shouldn't have to care if obj.open_file returns a built-in file
        instance, or a StringIO instance. As the above code becomes more
        popular, __enter__ and __exit__ are beginning to be part of the file
        interface (in the duck-typing sense) and should be implemented.

        Until then, the the contextlib.clos ing context manager can be used
        instead:

        with contextlib.clos ing(obj.open_fi le(...)) as f:
        ...

        Comment

        • peppergrower

          #5
          Re: Using the 'with' statement with cStringIO objects

          Thanks for the help. I'm fairly new to programming (which you
          probably could have guessed...). When I realized that you could use a
          StringIO instance as if it were a file, I wanted to try some of the
          same techniques on it as I would with a file. In this case, I wanted
          to use a "for line in testfile" construction to iterate over the
          StringIO instance. (I did find a better way for my particular case,
          one that didn't involve StringIO at all.) When I got that particular
          error, I suspected that it had something to do with the relative
          newness of the 'with' statement.

          If this is something that should be considered for addition in the
          future, is there somewhere specific I should suggest that?

          Comment

          • Gabriel Genellina

            #6
            Re: Using the 'with' statement with cStringIO objects

            En Sat, 27 Sep 2008 19:28:49 -0300, peppergrower
            <spamcomefindme please@gmail.co mescribió:
            When I got that particular
            error, I suspected that it had something to do with the relative
            newness of the 'with' statement.
            >
            If this is something that should be considered for addition in the
            future, is there somewhere specific I should suggest that?
            Yes: http://bugs.python.org/ (setting type="feature request", I think)

            --
            Gabriel Genellina

            Comment

            Working...