flying-circus / pyfilesystem

Automatically exported from code.google.com/p/pyfilesystem
BSD 3-Clause "New" or "Revised" License
0 stars 0 forks source link

Line 668 in filelike.py kills flushing Parameter #127

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1.Use Filewrapper for your Virtual File
2.Build a write function which returns everything and let the flushing 
parameter be printed
3.Write to the file and Flush

What is the expected output? What do you see instead?
-While Flushing there should be a write command with flushing = True
-But the parameter is killed in filelike.py line 668.

-- So should the Line 668 look like:
-- "return self.wrapped_file.write(string,flushing)" 
-- instead of:
-- "return self.wrapped_file.write(string)" 

What version of the product are you using? On what operating system?
Latest SVN, 0.4.0

Please provide any additional information below.

Original issue reported on code.google.com by mkhom...@googlemail.com on 24 Aug 2012 at 10:11

GoogleCodeExporter commented 9 years ago
Looks like the file.write method doesn't accept a flushing parameter 
http://docs.python.org/library/stdtypes.html#file.write so I'm wondering 
(educated guess!) if the fix should actually be something more like:
ret = self.wrapped_file.write(string)
if flushing:
    self.flush()
return ret

Original comment by gc...@loowis.durge.org on 24 Aug 2012 at 10:41

GoogleCodeExporter commented 9 years ago
Yes, this could be a better way!

So the new function should look like:

    def _write(self,string,flushing=False):
        ret = self.wrapped_file.write(string)
        if flushing:
            self.flush()
        return ret
right?

I'll test it :-), THX.

Original comment by mkhom...@googlemail.com on 24 Aug 2012 at 12:04

GoogleCodeExporter commented 9 years ago
Doesn't help, I Attaced an exable script where you can see my Problem.

Is there a Bug in my implementation?????

##Code##

from fs.base import *
from fs.errors import *
from fs.filelike import *

class TestFS(FS):
    def open(self ,path ,mode='r'):
        myfileobj = TestFile()
        file_obj = FileWrapper(myfileobj,mode)
        return file_obj

#That is a testfile
#I Use the buffer of this file like described in filelike base:

    #~ def _write(self,string,flushing=False):
        #~ """Write the given string to the file-like object.
        #~ 
        #~ This method must be implemented by subclasses wishing to be writable.
        #~ It must attempt to write as much of the given data as possible to the
        #~ file, but need not guarantee that it is all written.  It may return
        #~ None to indicate that all data was written, or return as a string any
        #~ data that could not be written.
        #~ 
        #~ If the keyword argument 'flushing' is true, it indicates that the
        #~ internal write buffers are being flushed, and *all* the given data
        #~ is expected to be written to the file. If unwritten data is returned
        #~ when 'flushing' is true, an IOError will be raised.
        #~ """

class TestFile(file):
    def __init__(self):
        self.offset = 0

    def read(self, size):
        return None

    def seek(self, seek, whence=0):
        if whence == 0:
            if seek != self.offset:
                self.flush()
        if whence == 0:
            self.offset = seek
        if whence == 1:
            self.offset = self.offset + seek
        if whence == 2:
            self.offset = self.size - seek

    def tell(self):
        return self.offset

    def write(self, data, flushing=False):
        print 'write',len(data),flushing

        #I'm working with 1MB Blocks!!!
        if len(data) < 1024*1024 and flushing == False:
            return data
        else:
            print 'write: take 1 MB and write it, if no flushing: return the rest else write all'

    def flush(self):
        #Flush doesnt help, because the Data is not in my buffer
        print 'flush'

fso = TestFS()
file = fso.open(file,'w+')
file.write(100*'x')
file.flush()
file.close()

Original comment by mkhom...@googlemail.com on 25 Aug 2012 at 2:28

Attachments:

GoogleCodeExporter commented 9 years ago
I think you're muddling up the write() method on file objects (which doesn't 
have a flushing parameter) and the _write() method on FileWrapper objects 
(which does have a flushing parameter).
I can't quite tell what it is you're trying to do, but I think you might want 
to add a subclass of FileWrapper that does the write-buffering for you?

Original comment by gc...@loowis.durge.org on 25 Aug 2012 at 4:14

GoogleCodeExporter commented 9 years ago
Not sure what the resolution is on this one, if it's a bug at all. Closing it 
for now. If there is still an issue, feel free to re-open with more details...

Original comment by willmcgugan on 10 Sep 2013 at 9:14