Skip to content

wrong error message for os.path.getsize #60176

Closed

Description

@jftuga
mannequin
BPO 15972
Nosy @birkenfeld, @vstinner, @larryhastings, @tiran, @serhiy-storchaka, @jftuga
Files
  • posix_path_converter_3.patch
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = 'https://github.com/serhiy-storchaka'
    closed_at = <Date 2013-01-07.21:20:17.524>
    created_at = <Date 2012-09-19.16:12:01.804>
    labels = ['extension-modules', 'type-bug']
    title = 'wrong error message for os.path.getsize'
    updated_at = <Date 2013-01-07.21:20:17.521>
    user = 'https://github.com/jftuga'

    bugs.python.org fields:

    activity = <Date 2013-01-07.21:20:17.521>
    actor = 'serhiy.storchaka'
    assignee = 'serhiy.storchaka'
    closed = True
    closed_date = <Date 2013-01-07.21:20:17.524>
    closer = 'serhiy.storchaka'
    components = ['Extension Modules']
    creation = <Date 2012-09-19.16:12:01.804>
    creator = 'jftuga'
    dependencies = []
    files = ['27247']
    hgrepos = []
    issue_num = 15972
    keywords = ['patch', '3.3regression']
    message_count = 19.0
    messages = ['170726', '170727', '170804', '170805', '170806', '170825', '170855', '170864', '170865', '170867', '170890', '170902', '170905', '170906', '170908', '170934', '170954', '179283', '179284']
    nosy_count = 7.0
    nosy_names = ['georg.brandl', 'vstinner', 'larry', 'christian.heimes', 'python-dev', 'serhiy.storchaka', 'jftuga']
    pr_nums = []
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue15972'
    versions = ['Python 3.3', 'Python 3.4']

    Activity

    1. jftuga commented on Sep 19, 2012

      jftugamannequin
      MannequinAuthor
      import os.path
      a = [ r'c:\Windows\notepad.exe' ]
      print( os.path.getsize(a) )

      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.

    2. tiran commented on Sep 19, 2012

      @tiran
      Member

      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]
    3. vstinner commented on Sep 20, 2012

      @vstinner
      Member

      It looks like os.stat() and os.path.getsize() converts the list into a byte string. It does something like:

      >> x=[]; y=bytes(x); print(y.decode("ascii"))

      >>> x=[65, 66, 67]; y=bytes(x); print(y.decode("ascii"))
      ABC
      >>> x=[None]; y=bytes(x); print(y.decode("ascii"))
      Traceback (most recent call last):
        File "<stdin>", line 1, in <module>
      TypeError: 'NoneType' object cannot be interpreted as an integer
    4. vstinner commented on Sep 20, 2012

      @vstinner
      Member

      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.

    5. vstinner commented on Sep 20, 2012

      @vstinner
      Member

      Set the priority to release blocker until it is decided if this issue is a regression, or a new feature :-)

    6. serhiy-storchaka commented on Sep 20, 2012

      @serhiy-storchaka
      Member

      Here is a patch.

      Are there any tests for string and bytes arguments as filenames? I will add
      float and list there.

    7. larryhastings commented on Sep 20, 2012

      @larryhastings
      Contributor

      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.

    8. serhiy-storchaka commented on Sep 21, 2012

      @serhiy-storchaka
      Member

      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"

    9. larryhastings commented on Sep 21, 2012

      @larryhastings
      Contributor

      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.

    10. serhiy-storchaka commented on Sep 21, 2012

      @serhiy-storchaka
      Member

      Patch updated. Added tests.

    11. jftuga commented on Sep 21, 2012

      jftugamannequin
      MannequinAuthor

      OP here. These error messages are much better. Thanks!

    12. larryhastings commented on Sep 21, 2012

      @larryhastings
      Contributor

      Patch looks fine, except please fix 80 columns.

    13. serhiy-storchaka commented on Sep 21, 2012

      @serhiy-storchaka
      Member

      Patch looks fine, except please fix 80 columns.

      Patch updated. Long lines in tests fixed.

    14. larryhastings commented on Sep 21, 2012

      @larryhastings
      Contributor

      LGTM. Serhiy, you have the commit bit?

    15. serhiy-storchaka commented on Sep 21, 2012

      @serhiy-storchaka
      Member

      Serhiy, you have the commit bit?

      It is zero.

    16. larryhastings commented on Sep 21, 2012

      @larryhastings
      Contributor

      Georg: this okay to check in? It passes the regression test.

    17. birkenfeld commented on Sep 22, 2012

      @birkenfeld
      Member

      This certainly isn't a release blocker. Check it into default, and it will go into 3.3.1.

    18. python-dev commented on Jan 7, 2013

      python-devmannequin
      Mannequin

      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

    19. serhiy-storchaka commented on Jan 7, 2013

      @serhiy-storchaka
      Member

      Fixed. Thank you for report, John.

    20. transferred this issue fromon Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Labels

    extension-modulesC modules in the Modules dirtype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions