GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — C API: Add PyUnicode_EqualToUTF8() function · Issue #110289 · python/cpython

C API: Add PyUnicode_EqualToUTF8() function #110289

New issue
Copy link
New issue
Copy link
Closed
Closed
C API: Add PyUnicode_EqualToUTF8() function#110289
Copy link
Labels
topic-C-APItopic-unicodetype-featureA feature request or enhancementA feature request or enhancement

Description

@serhiy-storchaka
serhiy-storchaka
opened on Oct 3, 2023
Issue body actions

Feature or enhancement

There is public PyUnicode_CompareWithASCIIString() function. Despite it name, it compares Python string object with ISO-8859-1 encoded C string. it returns -1, 0 or 1 and never sets an error.

There is private _PyUnicode_EqualToASCIIString() function. It only works with ASCII encoded C string and crashes in debug build it it is not ASCII. It returns 0 or 1 and never sets an error.

_PyUnicode_EqualToASCIIString() is more efficient than PyUnicode_CompareWithASCIIString(), because if arguments are not equal it can simply return false instead of determining what is larger. It was the main reason of introducing it. It is also more convenient, because you do not need to add == 0 or != 0 after the call (and if it is not added, it is difficult to read).

I propose to add the latter function to the public C API, but also extend it to support UTF-8 encoded C strings. While most of use cases are ASCII-only, formally almost all C strings in the C API are UTF-8 encoded. PyUnicode_FromString() and PyUnicode_AsUTF8AndSize() used to convert between Python and C strings use UTF-8 encoding. PyTypeObject.tp_name, PyMethodDef.ml_name, PyDescrObject.d_name all are UTF-8 encoded. PyUnicode_CompareWithASCIIString() cannot be used to compare Python string with such names.

For PyASCIIObject objects the new function will be as fast as _PyUnicode_EqualToASCIIString().

Linked PRs

  • gh-110289: C API: Add PyUnicode_EqualToUTF8() function #110297

Activity

  1. serhiy-storchaka
    added
    type-featureA feature request or enhancementA feature request or enhancement
    topic-C-API
    on Oct 3, 2023
  2. serhiy-storchaka
    added 2 commits that reference this issue on Oct 3, 2023

    pythongh-110289: C API: Add PyUnicode_EqualToString() function

    f736862

    pythongh-110289: C API: Add PyUnicode_EqualToString() function

    d39945e
  3. serhiy-storchaka
    added
    topic-unicode
    on Oct 3, 2023
  4. serhiy-storchaka
    changed the title [-]C API: Add PyUnicode_EqualToString() function[/-] [+]C API: Add PyUnicode_EqualToUTF8() function[/+] on Oct 4, 2023
  5. serhiy-storchaka
    added a commit that references this issue on Oct 11, 2023

    gh-110289: C API: Add PyUnicode_EqualToUTF8() and PyUnicode_EqualToUT…

    eb50cd3
  6. hugovk commented on Nov 9, 2023

    @hugovk
    hugovk
    on Nov 9, 2023
    Member
    More actions

    Thanks!

  7. hugovk
    closed this as completedon Nov 9, 2023
  8. hauntsaninja
    added a commit that references this issue on Jul 7, 2024

    Fix `PyUnicode` functions for Python 3.13 (#17504)

    966d6d3
  9. Glyphack
    added a commit that references this issue on Sep 2, 2024

    pythongh-110289: C API: Add PyUnicode_EqualToUTF8() and PyUnicode_Equ…

    ee0b11b
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

    topic-C-APItopic-unicodetype-featureA feature request or enhancementA feature request or enhancement

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402503 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.