Skip to content

add an optional "default" argument to tokenize.detect_encoding #53980

Closed as not planned

Description

@florentx
mannequin
BPO 9771
Nosy @vstinner, @benjaminp, @merwok, @florentx, @berkerpeksag, @serhiy-storchaka
Files
  • detect_encoding_default.diff: Patch, apply to 3.x
  • 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 = None
    created_at = <Date 2010-09-04.11:32:08.849>
    labels = ['type-feature', 'library']
    title = 'add an optional "default" argument to tokenize.detect_encoding'
    updated_at = <Date 2014-11-02.12:11:24.713>
    user = 'https://github.com/florentx'

    bugs.python.org fields:

    activity = <Date 2014-11-02.12:11:24.713>
    actor = 'berker.peksag'
    assignee = 'none'
    closed = False
    closed_date = None
    closer = None
    components = ['Library (Lib)']
    creation = <Date 2010-09-04.11:32:08.849>
    creator = 'flox'
    dependencies = []
    files = ['18745']
    hgrepos = []
    issue_num = 9771
    keywords = ['patch']
    message_count = 3.0
    messages = ['115567', '122106', '173002']
    nosy_count = 6.0
    nosy_names = ['vstinner', 'benjamin.peterson', 'eric.araujo', 'flox', 'berker.peksag', 'serhiy.storchaka']
    pr_nums = []
    priority = 'normal'
    resolution = None
    stage = 'patch review'
    status = 'open'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue9771'
    versions = ['Python 3.5']

    Activity

    1. florentx commented on Sep 4, 2010

      florentxmannequin
      MannequinAuthor

      The function tokenize.detect_encoding() detects the encoding either in the coding cookie or in the BOM. If no encoding is found, it returns 'utf-8':

      When result is 'utf-8', there's no (easy) way to know if the encoding was really detected in the file, or if it falls back to the default value.

      Cases (with utf-8):

      • UTF-8 BOM found, returns ('utf-8-sig', [])
      • cookie on 1st line, returns ('utf-8', [line1])
      • cookie on 2nd line, returns ('utf-8', [line1, line2])
      • no cookie found, returns ('utf-8', [line1, line2])

      The proposal is to allow to call the function with a different default value (None or ''), in order to know if the encoding is really detected.

      For example, this function could be used by the Tools/scripts/findnocoding.py script.

      Patch attached.

    2. added
      stdlibStandard Library Python modules in the Lib/ directory
      type-featureA feature request or enhancement
      on Sep 4, 2010
    3. vstinner commented on Nov 22, 2010

      @vstinner
      Member

      no cookie found, returns ('utf-8', [line1, line2])

      I never understood the usage of the second item. IMO it should be None if no cookie found.

    4. serhiy-storchaka commented on Oct 15, 2012

      @serhiy-storchaka
      Member

      I never understood the usage of the second item. IMO it should be None if no cookie found.

      UTF-8 is the default source encoding for Python 3.

    5. transferred this issue fromon Apr 10, 2022
    6. serhiy-storchaka commented on May 22, 2022

      @serhiy-storchaka
      Member

      Is there any use case besides the Tools/scripts/findnocoding.py script? It is a special tool and it needs to look under the hood.

      There are many ways of working with Python sources:

      1. Open a file in binary mode, read the content as bytes, and pass it to compile(). You do not even know what encoding was used.
      2. Open a text file with tokenize.open().
      3. Use tokenize.detect_encoding() and manually decode the rest of the file. It is the least convenient way.
    7. vstinner commented on May 23, 2022

      @vstinner
      Member

      I suggest to close this issue, it seems like there is no real need for this feature.

    8. AA-Turner commented on May 28, 2022

      @AA-Turner
      Member

      Closing per Victor, it's been several days with no comments to the contrary.

      A

    9. removed
      pendingThe issue will be closed if no feedback is provided
      on May 28, 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

      stdlibStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancement

      Projects

      No projects

        Milestone

        No milestone

        Relationships

        None yet

        Development

        No branches or pull requests

        Issue actions