This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author dlesco
Recipients dlesco
Date 2009-03-08.19:31:14
SpamBayes Score 1.996627e-08
Marked as misclassified No
Message-id <1236540676.96.0.036576866383.issue5445@psf.upfronthosting.co.za>
In-reply-to
Content
This is the implementation of codecs.Streamwriter.writelines for all 
Python versions that I've checked:

    def writelines(self, list):

        """ Writes the concatenated list of strings to the stream
            using .write().
        """
        self.write(''.join(list))

This may be a problem if the 'list' parameter is a generator. The 
generator may be returning millions of values, which the join will 
concatenate in memory. It can surprise the programmer with large 
memory use. I think join should only be used when you know the size of 
your input, and this method does not know this. I think the safe 
implementation of this method would be:

    def writelines(self, list):

        """ Writes the concatenated list of strings to the stream
            using .write().
        """
        write = self.write
        for value in list:
            write(value)

If a caller knows that it's input list would use a reasonable amount 
of memory, it can get the same functionality as before by doing 
stream.write(''.join(list)).
History
Date User Action Args
2009-03-08 19:31:17dlescosetrecipients: + dlesco
2009-03-08 19:31:16dlescosetmessageid: <1236540676.96.0.036576866383.issue5445@psf.upfronthosting.co.za>
2009-03-08 19:31:15dlescolinkissue5445 messages
2009-03-08 19:31:15dlescocreate