Skip to content

improve error message for saving ints to file #62059

Closed

Description

@techtonik
mannequin
BPO 17859
Nosy @terryjreedy, @ezio-melotti, @merwok, @bitdancer
Superseder
  • bpo-16518: add "buffer protocol" to glossary
  • 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 = None
    closed_at = <Date 2016-04-16.16:14:57.876>
    created_at = <Date 2013-04-28.07:37:56.173>
    labels = ['type-feature']
    title = 'improve error message for saving ints to file'
    updated_at = <Date 2016-04-16.16:14:57.875>
    user = 'https://bugs.python.org/techtonik'

    bugs.python.org fields:

    activity = <Date 2016-04-16.16:14:57.875>
    actor = 'serhiy.storchaka'
    assignee = 'none'
    closed = True
    closed_date = <Date 2016-04-16.16:14:57.876>
    closer = 'serhiy.storchaka'
    components = []
    creation = <Date 2013-04-28.07:37:56.173>
    creator = 'techtonik'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 17859
    keywords = []
    message_count = 8.0
    messages = ['187968', '187970', '187994', '188027', '188304', '188332', '188370', '263566']
    nosy_count = 6.0
    nosy_names = ['terry.reedy', 'techtonik', 'ezio.melotti', 'eric.araujo', 'r.david.murray', 'random832']
    pr_nums = []
    priority = 'normal'
    resolution = 'duplicate'
    stage = 'resolved'
    status = 'closed'
    superseder = '16518'
    type = 'enhancement'
    url = 'https://bugs.python.org/issue17859'
    versions = ['Python 3.3', 'Python 3.4']

    Activity

    1. techtonik commented on Apr 28, 2013

      techtonikmannequin
      MannequinAuthor

      I needed to write some bytes to file and got this message.

      >>> hex = open('hex', 'wb')
      >>> for x in range(0, 0xff, 0x11):
      ...   hex.write(x)
      ...
      Traceback (most recent call last):
        File "<stdin>", line 2, in <module>
      TypeError: 'int' does not support the buffer interface

      The cause of the error is not that 'int' doesn't support some interface (which is strange already, because the function analyzes and writes int, not the int writes itself), but because it is impossible to know how many binary bytes the int type should take when written.

      In Python 2 the solution is:
      ...
      hex.write(chr(x))

      But in Python 3 this is again:
      TypeError: 'str' does not support the buffer interface

      In Python 3 the solution is:
      ...
      hex.write(x.to_bytes(1, 'little'))

    2. ezio-melotti commented on Apr 28, 2013

      @ezio-melotti
      Member

      Maybe the error could be replaced with something like:
      TypeError: write() requires an object that supports the buffer interface, not '<type>'

      But that's a bit long and also not entirely accurate, because the type accepted by write depends on the type of the file (binary or text). The first problem could be solved by using "requires a bytes-like object"[0], the second problem could be fixed by omitting the name of the function:
      TypeError: a bytes-like object is required, not '<type>'

      [0]: bpo-16518 has a discussion about the best term to describe "objects that support the buffer protocol"

    3. merwok commented on Apr 28, 2013

      @merwok
      Member

      I don’t understand that the first message says.

      If one wants to call the write method of a file object opened in binary mode, one needs to pass bytes, as the doc surely explains. The same error that is seen here with ints would be seen with any other objects. A more practical way is to use print (with its file, sep and end parameters) which will do the conversion for you.

    4. bitdancer commented on Apr 28, 2013

      @bitdancer
      Member

      Éric, print doesn't help if one is writing binary data.

      What do you mean by not understanding the first message? If you are agreeing that the first error message in Anatoly's initial post isn't clear, does Ezio's proposed change make it clearer?

    5. merwok commented on May 3, 2013

      @merwok
      Member

      Ah, right, I missed the part about the file being opened in binary mode.

    6. terryjreedy commented on May 3, 2013

      @terryjreedy
      Member

      I think the point is that the error message does not make much sense unless one knows a) that the file is open in binary mode (and that could have happened in a different file) and b) that 'does not support the buffer interface' means 'is not a bytes-like object'. A possible replacement that addresses both problems:

      binary-mode write requires bytes-like object, not 'int'

    7. ezio-melotti commented on May 4, 2013

      @ezio-melotti
      Member

      binary-mode write requires bytes-like object, not 'int'

      Looks like the function that raises the error (Objects/abstract.c:352) doesn't have enough information to produce a message like that.
      I attached a patch to bpo-16518 that improves this and other error messages (this particular error is replaced by "a bytes-like object is required, not 'int'").

    8. random832 commented on Apr 16, 2016

      random832mannequin
      Mannequin

      This bug should be closed since bpo-16518 was accepted and the error is now "TypeError: a bytes-like object is required, not 'int'"

    9. 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

    Assignees

    No one assigned

      Labels

      type-featureA feature request or enhancement

      Projects

      No projects

        Milestone

        No milestone

        Relationships

        None yet

        Development

        No branches or pull requests

        Issue actions