pread/pwrite (was Re: files: direct access - NOT sequential)

Jeff Epler jepler at unpythonic.net
Tue Apr 15 14:21:42 EDT 2003


On Tue, Apr 15, 2003 at 07:25:57PM +0200, Alex Martelli wrote:
> I think the positioning argument[s] should be the same as for .seek --
> ONE argument (unless it's a tuple specifying origin and offset) would
> not suffice.  Consider a frequent case: "I want to write these bytes
> at the END of the file" - no matter how long it's now and where it's
> currently positioned; or, ditto but "at the START of the file"; I don't
> think the Pythonic convention of -1 to meand end would work here,
> i.e. to write _after the end_ -- I think that, like for seek, offset and
> indicator of origin (start, end, current) would in general be needed.

Well, pread()/pwrite() don't have 'whence' arguments.  I think the
reasoning is that only SEEK_SET makes sense.  SEEK_CUR doesn't, because
the idea is to be independent of a file position set in a different
thread.  SEEK_END doesn't, because again you'd have to have some sort of
mutex on the file to know just where the end *is* when you make your
pread() call.  (if you *always* want to append to the file, there's a way
to do that now with write(), isn't there? opening with os.open(,O_APPEND)
/ file(,"a").  Hm, what does pwrite() do on a file opened with
O_APPEND?)

Some prototypes:
       off_t lseek(int fildes, off_t offset, int whence);
       ssize_t pread(int fd, void *buf, size_t count, off_t offset);
       ssize_t pwrite(int fd, const void *buf, size_t count, off_t offset);

[snippage]
> I do think that the key issue is defining the syntax, semantics and
> use cases, as opposed to implementing via a patch

I was really thing in "wrap C routines to Python" mode, not "what would
be a useful way to expose this kind of functionality to Python" mode...

Frankly, I'm less and less sure this is actually useful in Python.
(Yeah, I know, I'm the fellow who brought it up)  If you're going to use
an existing library, you'll wrap it (and not pread/pwrite directly).
If you're a real masochist, and want to interoperate with something using
that library in another thread you'd want this.  Blech.  But if you're
writing a Python program you'd probably never use something as primitive
as a single file object shared in multiple threads.

Jeff





More information about the Python-list mailing list