New submission from Kyle Stanley <[email protected]>:
In Python 3.1, three constants were added to the IO module to be used for the
whence argument of seek():
SEEK_SET or 0 – start of the stream
SEEK_CUR or 1 – current stream position
SEEK_END or 2 – end of the stream
However, there are at least 102 occurrences across CPython where the integer
values are used instead. This only includes instances when a value for the
whence arg is explicitly specified. All of the occurrences can be easily found
with the usage of git grep:
git grep -E "seek\(-?[0-9]+, [0-2]\)"
This doesn't affect functionality in any way, but it would result in
significant readability improvement if these were all replaced with constants.
The only requirement would be importing IO or manually specifying the value of
constants.
For simplicity, I would probably start with anything that directly involves IO.
The largest number of occurrences are in Lib/test/test_io, so that probably
would be the best place to begin. Also, this module already imports io anyways,
so it would be a straightforward find and replace.
It would also be quite useful to make this change in the documentation, in
Doc/tutorial/inputoutput there are 4 occurrences. If anything, anyone less
familiar with the IO module in general would be the most likely to benefit from
this change so modifying the tutorial would likely be the most useful change
for the majority of users.
As a single example of what I'm talking about, here's line 334 of
Lib/test/test_io:
self.assertEqual(f.seek(-1, 1), 5)
I would propose changing it to:
self.assertEqual(f.seek(-1, io.SEEK_CUR), 5)
----------
components: IO
messages: 348207
nosy: aeros167, benjamin.peterson, stutzbach
priority: normal
severity: normal
status: open
title: Using constant for whence arg in seek()
type: enhancement
versions: Python 3.9
_______________________________________
Python tracker <[email protected]>
<https://bugs.python.org/issue37635>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe:
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com