You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Under Python 3.2.3, this error message is returned:
File "c:\python32\lib\genericpath.py", line 49, in getsize
return os.stat(filename).st_size
TypeError: Can't convert 'list' object to str implicitly
Under Python 3.3.0rc2, this error message is returned:
File "c:\Python33\lib\genericpath.py", line 49, in getsize
return os.stat(filename).st_size
TypeError: an integer is required
I feel like the 3.2.3 behavior is more accurate and would like to propose that the 3.3 error message says something about a list instead of an integer.
>>> os.stat([])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
FileNotFoundError: [Errno 2] No such file or directory: ''
[60996 refs]
>>> os.stat([None])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: an integer is required
[60993 refs]
Functions of the os module uses PyUnicode_FSConverter() function (which uses PyBytes_Check() on bytes) in Python 3.2, whereas PyBytes_FromObject() is used in Python 3.3. Related change:
changeset: 77597:27f9c26fdd8b
user: Larry Hastings <[email protected]>
date: Fri Jun 22 16:30:09 2012 -0700
files: Doc/library/os.rstLib/os.pyLib/shutil.pyLib/test/support.pyLib/test/test_os.pyLib/test/test_posix.pyLib/test/test_shutil.pyMisc/NEWSModules/posixmodule.c
description:
Issue bpo-14626: Large refactoring of functions / parameters in the os module.
Many functions now support "dir_fd" and "follow_symlinks" parameters;
some also support accepting an open file descriptor in place of of a path
string. Added os.support_* collections as LBYL helpers. Removed many
functions only previously seen in 3.3 alpha releases (often starting with
"f" or "l", or ending with "at"). Originally suggested by Serhiy Storchaka;
implemented by Larry Hastings.
Patch looks like it'll work fine. But please add regression tests checking that the error message is what we want.
Are the new error messages okay with the OP? It looks like now it'll throw TypeError("argument must be string, bytes or integer, not list").
v
I'd personally prefer the Oxford Comma there ("string, bytes, or integer"). But I don't know if there is a style preference one way or the other in Python error messages.
But please add regression tests checking
that the error message is what we want.
Immediately as soon as I find a suitable place for this test. Should be
somewhere tests for bytes/unicode filenames.
I'd personally prefer the Oxford Comma there ("string, bytes, or integer").
But I don't know if there is a style preference one way or the other in
Python error messages.
"Invalid whence (%i, should be 0, 1 or 2)"
"expect bytes or str of length 1, or int, "
"argument should be bytes, buffer or ASCII string, "
"coercing to str: need bytes, bytearray or buffer-like object, %.80s found"
"character mapping must return integer, bytes or None, not %.400s"
Activity
jftuga commentedon Sep 19, 2012
Under Python 3.2.3, this error message is returned:
File "c:\python32\lib\genericpath.py", line 49, in getsize
return os.stat(filename).st_size
TypeError: Can't convert 'list' object to str implicitly
Under Python 3.3.0rc2, this error message is returned:
File "c:\Python33\lib\genericpath.py", line 49, in getsize
return os.stat(filename).st_size
TypeError: an integer is required
I feel like the 3.2.3 behavior is more accurate and would like to propose that the 3.3 error message says something about a list instead of an integer.
tiran commentedon Sep 19, 2012
Linux:
>>> os.stat([]) Traceback (most recent call last): File "<stdin>", line 1, in <module> FileNotFoundError: [Errno 2] No such file or directory: '' [60996 refs] >>> os.stat([None]) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: an integer is required [60993 refs]vstinner commentedon Sep 20, 2012
It looks like os.stat() and os.path.getsize() converts the list into a byte string. It does something like:
vstinner commentedon Sep 20, 2012
Functions of the os module uses PyUnicode_FSConverter() function (which uses PyBytes_Check() on bytes) in Python 3.2, whereas PyBytes_FromObject() is used in Python 3.3. Related change:
changeset: 77597:27f9c26fdd8b
user: Larry Hastings <[email protected]>
date: Fri Jun 22 16:30:09 2012 -0700
files: Doc/library/os.rst Lib/os.py Lib/shutil.py Lib/test/support.py Lib/test/test_os.py Lib/test/test_posix.py Lib/test/test_shutil.py Misc/NEWS Modules/posixmodule.c
description:
Issue bpo-14626: Large refactoring of functions / parameters in the os module.
Many functions now support "dir_fd" and "follow_symlinks" parameters;
some also support accepting an open file descriptor in place of of a path
string. Added os.support_* collections as LBYL helpers. Removed many
functions only previously seen in 3.3 alpha releases (often starting with
"f" or "l", or ending with "at"). Originally suggested by Serhiy Storchaka;
implemented by Larry Hastings.
vstinner commentedon Sep 20, 2012
Set the priority to release blocker until it is decided if this issue is a regression, or a new feature :-)
serhiy-storchaka commentedon Sep 20, 2012
Here is a patch.
Are there any tests for string and bytes arguments as filenames? I will add
float and list there.
larryhastings commentedon Sep 20, 2012
Patch looks like it'll work fine. But please add regression tests checking that the error message is what we want.
Are the new error messages okay with the OP? It looks like now it'll throw TypeError("argument must be string, bytes or integer, not list").
I'd personally prefer the Oxford Comma there ("string, bytes, or integer"). But I don't know if there is a style preference one way or the other in Python error messages.
serhiy-storchaka commentedon Sep 21, 2012
Immediately as soon as I find a suitable place for this test. Should be
somewhere tests for bytes/unicode filenames.
"Invalid whence (%i, should be 0, 1 or 2)"
"expect bytes or str of length 1, or int, "
"argument should be bytes, buffer or ASCII string, "
"coercing to str: need bytes, bytearray or buffer-like object, %.80s found"
"character mapping must return integer, bytes or None, not %.400s"
larryhastings commentedon Sep 21, 2012
Ah! It seems Python is anti-Oxford Comma. Carry on! ;-)
Wouldn't test_os be the natural place? I don't understand why you're having difficulty finding a suitable place.
serhiy-storchaka commentedon Sep 21, 2012
Patch updated. Added tests.
jftuga commentedon Sep 21, 2012
OP here. These error messages are much better. Thanks!
larryhastings commentedon Sep 21, 2012
Patch looks fine, except please fix 80 columns.
serhiy-storchaka commentedon Sep 21, 2012
Patch updated. Long lines in tests fixed.
larryhastings commentedon Sep 21, 2012
LGTM. Serhiy, you have the commit bit?
serhiy-storchaka commentedon Sep 21, 2012
It is zero.
larryhastings commentedon Sep 21, 2012
Georg: this okay to check in? It passes the regression test.
birkenfeld commentedon Sep 22, 2012
This certainly isn't a release blocker. Check it into default, and it will go into 3.3.1.
python-dev commentedon Jan 7, 2013
New changeset 1b68dc917321 by Serhiy Storchaka in branch '3.3':
Issue bpo-15972: Fix error messages when os functions expecting a file name or
http://hg.python.org/cpython/rev/1b68dc917321
New changeset 71fb426ee972 by Serhiy Storchaka in branch 'default':
Issue bpo-15972: Fix error messages when os functions expecting a file name or
http://hg.python.org/cpython/rev/71fb426ee972
serhiy-storchaka commentedon Jan 7, 2013
Fixed. Thank you for report, John.