You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Patch to add tempfile.SpooledTemporaryFile (for #415692) #44424
Attached please find a patch that adds a SpooledTemporaryFile class to tempfile, along with the corresponding documentation (optimistically labeling the feature as added in Python 2.5) and some test cases.
The problem is that the captured 'f.read' method is the one from the StringIO instance, even after the write() rolled the file over to disk. Given that capturing bound methods is a semi-official speed hack advertised in some respected places, we might have to be careful about it. About such matters I am biased towards first getting it right and then getting it fast...
Also, Python 2.5 is already out, so this will probably be a 2.6 addition.
djmitche commented on Jan 8, 2007
djmitchemannequin
MannequinAuthor
More actions
I agree it would break in such a situation, but I'm not clear on which direction your bias leads you (specifically, which do we get right -- don't use bound methods, or don't use the __getattr__ magic?).
I could fix this by defining "proxy" functions (and some properties) for the whole file interface, rather than just the methods that potentially trigger rollover. That would lose a little efficiency, but mostly only in reading (calling e.g., f.read() will always result in two function applications; in the current model, after the first call it runs at "native" speed). It would also lose forward compatibility if the file protocol changes, although I'm not sure how likely that is.
Would you like me to do that?
arigo commented on Jan 11, 2007
arigomannequin
Mannequin
More actions
Reimplementing the whole file interface as a proxy functions might be the safest route, yes.
I realized that the __getattr__() magic is also used to serve at least one special method, namely the __iter__() of the file objects. This only works with old-style classes. In the long-term future, when old-style classes disappear and these classes become new-style, this is likely to introduce a subtle bug.
djmitche commented on Feb 3, 2007
djmitchemannequin
MannequinAuthor
More actions
Sounds good -- here's a new version.
File Added: SpooledTemporaryFile.patch
collinwinter commented on Mar 9, 2007
collinwintermannequin
Mannequin
More actions
Did you mean to leave SpooledTemporaryFile out of tempfile.py's __all__? Also, would it be possible for you to add some tests for the pathological cases you and Armin discussed related to bound .read() and .write() methods?
djmitche commented on Mar 18, 2007
djmitchemannequin
MannequinAuthor
More actions
Slightly updated version to add STF to __all__ and add a test that bound methods work correctly.
File Added: SpooledTemporaryFile.patch
Activity
djmitche commentedon Jan 7, 2007
Attached please find a patch that adds a SpooledTemporaryFile class to tempfile, along with the corresponding documentation (optimistically labeling the feature as added in Python 2.5) and some test cases.
djmitche commentedon Jan 7, 2007
File Added: SpooledTemporaryFile.patch
arigo commentedon Jan 8, 2007
The __getattr__ magic makes the following kind of code fail with SpooledTemporaryFile:
The problem is that the captured 'f.read' method is the one from the StringIO instance, even after the write() rolled the file over to disk. Given that capturing bound methods is a semi-official speed hack advertised in some respected places, we might have to be careful about it. About such matters I am biased towards first getting it right and then getting it fast...
Also, Python 2.5 is already out, so this will probably be a 2.6 addition.
djmitche commentedon Jan 8, 2007
I agree it would break in such a situation, but I'm not clear on which direction your bias leads you (specifically, which do we get right -- don't use bound methods, or don't use the __getattr__ magic?).
I could fix this by defining "proxy" functions (and some properties) for the whole file interface, rather than just the methods that potentially trigger rollover. That would lose a little efficiency, but mostly only in reading (calling e.g., f.read() will always result in two function applications; in the current model, after the first call it runs at "native" speed). It would also lose forward compatibility if the file protocol changes, although I'm not sure how likely that is.
Would you like me to do that?
arigo commentedon Jan 11, 2007
Reimplementing the whole file interface as a proxy functions might be the safest route, yes.
I realized that the __getattr__() magic is also used to serve at least one special method, namely the __iter__() of the file objects. This only works with old-style classes. In the long-term future, when old-style classes disappear and these classes become new-style, this is likely to introduce a subtle bug.
djmitche commentedon Feb 3, 2007
Sounds good -- here's a new version.
File Added: SpooledTemporaryFile.patch
collinwinter commentedon Mar 9, 2007
Did you mean to leave SpooledTemporaryFile out of tempfile.py's __all__? Also, would it be possible for you to add some tests for the pathological cases you and Armin discussed related to bound .read() and .write() methods?
djmitche commentedon Mar 18, 2007
Slightly updated version to add STF to __all__ and add a test that bound methods work correctly.
File Added: SpooledTemporaryFile.patch
collinwinter commentedon Mar 19, 2007
Applied as r54439. Thanks for your patch!