New submission from Graham Dumpleton <graham.dumple...@gmail.com>:

In:

http://bugs.repoze.org/issue169

Tres said:

"""
Thanks for the report!  I agree that your suggested solution is probably
the correct one, with the exception that I would have it apply 'list()'
only for results which were generators:  hmm, how do I detect that?
"""

Bad call unfortunately as you miss out on a whole class of iterables which 
aren't generators.

For example, will not work with custom iterable:

class _WSGIApplicationIterable(object):

    def __init__(self, transaction, generator):
        self.transaction = transaction
        self.generator = generator

    def __iter__(self):
        for item in self.generator:
            yield item

    def close(self):
        try:
            if hasattr(self.generator, 'close'):
                self.generator.close()
        except:
            self.transaction.__exit__(*sys.exc_info())
            raise
        else:
            self.transaction.__exit__(None, None, None)

You would need to check for __iter__() for this one. Even this isn't enough 
though as you can also have:

class FileWrapper:

    def __init__(self, filelike, blksize=8192):
        self.filelike = filelike
        self.blksize = blksize
        if hasattr(filelike, 'close'):
            self.close = filelike.close

    def __getitem__(self, key):
        data = self.filelike.read(self.blksize)
        if data:
            return data
        raise IndexError

Ie., __getitem__() also provides iterable abilities.

If you are concerned about the list([]) case, ie., of doing shallow copy of 
list, then change:

            self.profiler.runctx(
                'app_iter = self.app(environ, start_response); '
                'app_iter = (type(app_iter) is GeneratorType) '
                                 'and list(app_iter) or app_iter',
                globals(), _locals)

to:

            self.profiler.runctx(
                'app_iter = self.app(environ, start_response); '
                'app_iter = (type(app_iter) is not ListType) '
                                 'and list(app_iter) or app_iter',
                globals(), _locals)

Ie., if not a ListType assume is an iterable and convert to list().

----------
messages: 495
nosy: grahamd
priority: bug
status: unread
title: respoze.profile doesn't handle WSGI iterables properly.

__________________________________
Repoze Bugs <b...@bugs.repoze.org>
<http://bugs.repoze.org/issue179>
__________________________________
_______________________________________________
Repoze-dev mailing list
Repoze-dev@lists.repoze.org
http://lists.repoze.org/listinfo/repoze-dev

Reply via email to