Skip to content

Commit b3dfe0c

Browse files
authored
Replace list comprehensions with generators (#11)
By using [generator expressions][0] in `_ProxyWriter.writerow` and `_ProxyWriter.writerows` instead of [list comprehensions][1], those methods can avoid loading the entire row or iterable of rows into memory at once, reducing peak memory consumption. No tests were written due to the difficulty in asserting the behavior we're looking for is actually happening. Fixes #10 [0]: https://docs.python.org/3/glossary.html#term-generator-expression [1]: https://docs.python.org/3/glossary.html#term-list-comprehension
1 parent 2d2d7fb commit b3dfe0c

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

defusedcsv/csv.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,10 @@ def writerow(self, row):
4848
except TypeError as err:
4949
msg = "iterable expected, not %s" % type(row).__name__
5050
raise Error(msg) from err
51-
return self.writer.writerow([_escape(field) for field in row])
51+
return self.writer.writerow(_escape(field) for field in row)
5252

5353
def writerows(self, rows):
54-
return self.writer.writerows([[_escape(field) for field in row] for row in rows])
54+
return self.writer.writerows((_escape(field) for field in row) for row in rows)
5555

5656
def __getattr__(self, item):
5757
return getattr(self.writer, item)

0 commit comments

Comments
 (0)