Alf P. Steinbach wrote: >* Gertjan Klein: >> I reproduced (with Python 2.5.2 on WinXP) the code the OP wrote after >> creating an empty (0-byte) test file; after the write() the read() >> returns random garbage. I can't imagine why anyone would want that >> behaviour. The file grew to be 4099 bytes after f.close(). I wrote >> 'hello' to it, so the length of garbage added was 4094 bytes, which I >> find a strange number also. > >Could you post (copy and paste) the code, and description of results?
The code is exactly the OP's code, with an f.close() after f.read(). The results are described above. >"Shall not" means UB. This applies to C "FILE*" handling. Yes. But Python is not C. I would have expected that Python shields me from such bizarre results. The fact that, apparently, the Python file object is a thin wrapper over a C library function is an explanation of the behaviour, but not a justification. >And for a language used by so many newbies (this is positive!) I agree that it >should ideally get rid of that UB (assuming that's what the problem is), or, >if >it doesn't already, mention that in the Python documentation. I'm not sure I can defend this statement, but I think Python should not have undefined behaviour at all. Gertjan. -- http://mail.python.org/mailman/listinfo/python-list