hrrrmpff.<br><br>there really is a need for "enhanced descriptors", or something like<br>that. i'm having serious trouble implementing the position property, <br>as python is currently limited in this area.<br><br>
the rules were:<br>* when you assign a positive integer, it's an absolute position to<br>seek to<br>* when you assign a negative integer, it's relative to the end, as<br>is the case with slices<br>* when you assign None, it's the ultimate last position --
<br>seek(0, "end"), although you would use f.END instead of None <br>directly<br>* when you use the +=/-= operators, it's relative to the current<br>position (if optimized via __iadd__, can reduce one unnecessary
<br>system call)<br><br>but descriptors don't support augmented __set__()ing. one solution<br>would be to return an int-like object, where __iadd__ would seek <br>relative to the current position. <br><br>aside of being slower than expected, complicating __set__, and
<br>implementing position caching, this has a major drawback:<br>f.position += 4 # this assignment seeks<br>p = f.position<br>p += 4 # and this assignment seeks as well!<br><br>so that's out of the question, and we'll have to suffer two system
<br>calls, at least in the experimental branch. maybe the C-branch <br>could utilize under-the-hood tricks to avoid that. <br><br>the current code of the descriptor looks like this:<br><br>class PositionDesc(object):<br> def __get__(self, obj, cls):
<br> if obj is None:<br> return self<br> return obj.tell()<br> <br> def __set__(self, obj, value):<br> if value is None:<br> obj.seek(0, "end")<br> elif value < 0:
<br> obj.seek(value, "end")<br> else:<br> obj.seek(value, "start")<br><br>but now we come to another problem... files became cyclic!<br>or sorta... if f.position < x, then
f.position -= x would assign a <br>negative value to f.position. this, in turn, seeks relative to the <br>end of the file, thus making the file behave like a semi-cyclic <br>entity with a not-so-intuitive behavior... for example, assuming
<br>a file size of 100, and a current position of 70:<br><br>pos = 70<br>pos -= 71 ==> pos = -1 ==> pos = (100 - 1) ==> pos = 99<br><br>baaaaaah!<br><br>in the original design, i wanted to raise an exception if seeking
<br>
relative to the current position got negative... but due to the<br>aforementioned technical limitations, it's not possible. <br><br>the whole issue could be solved if the descriptor protocol <br>supported augmented assignment -- but it requires, of course,
<br>a drastic change to the language, something like the suggested<br>STORE_INPLACE_ATTR or the __iattr__ suggested by Greg. <br><br>will Guido pronounce on his choice (or dischard it altogether)?<br><br><br><br>-tomer