-
Notifications
You must be signed in to change notification settings - Fork 130
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed the performance regression in LazyFile from #45
The performance regression was caused by implementing the next() or __next__() methods which were being called instead of the built in C implementation from the object file. The fix is to only implement __iter__() to return the file since it is its own iterator. Additionally this is wrapped with a keyword with block and returns lines with yield to make sure that after the iteration the file is closed. This also makes it so that calls __iter__ are completely independent where before they were related. Lastly, LazyFile was renamed to ReusableFile since the name more clearly communicates its purpose.
- Loading branch information
Showing
4 changed files
with
26 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,13 @@ | ||
import unittest | ||
from ..util import LazyFile | ||
from ..util import ReusableFile | ||
|
||
|
||
class TestUtil(unittest.TestCase): | ||
def test_lazy_file(self): | ||
license_file = LazyFile('LICENSE.txt') | ||
self.assertTrue(license_file.file is None) | ||
iter(license_file) | ||
handle_0 = license_file.file | ||
iter(license_file) | ||
handle_1 = license_file.file | ||
self.assertTrue(handle_0.closed) | ||
self.assertFalse(handle_1.closed) | ||
def test_reusable_file(self): | ||
license_file_lf = ReusableFile('LICENSE.txt') | ||
with open('LICENSE.txt') as license_file: | ||
self.assertEqual(list(license_file), list(license_file_lf)) | ||
self.assertTrue(license_file_lf.file is None) | ||
iter_1 = iter(license_file_lf) | ||
iter_2 = iter(license_file_lf) | ||
self.assertEqual(list(iter_1), list(iter_2)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters