[3.12] gh-82045: Correct and deduplicate "isprintable" docs; add test. (GH-130125)

We had the definition of what makes a character "printable" documented in three places, giving two different definitions.
The definition in the comment on `_PyUnicode_IsPrintable` was inverted; correct that.

With that correction, the two definitions turn out to be equivalent -- but to confirm that, you have to go look up, or happen to know, that those are the only five "Other" categories and only three "Separator" categories in the Unicode character database.  That makes it hard for the reader to tell whether they really are the same, or if there's some subtle difference in the intended semantics.

Fix that by cutting the C API docs' and the C comment's copies of the subtle details, in favor of referring to the Python-level docs. That ensures it's explicit that these are all meant to agree, and also lets us concentrate improvements to the wording in one place.

Speaking of which, borrow some ideas from the C comment, along with other tweaks, to hopefully add a bit more clarity to that one newly-centralized copy in the docs.

Also add a thorough test that the implementation agrees with this definition.

Co-authored-by: Greg Price <gnprice@gmail.com>
(cherry picked from commit 3402e133ef)
This commit is contained in:
Stan Ulbrych 2025-02-17 13:07:59 +00:00 committed by GitHub
parent fc1c35f44d
commit 8a598fb623
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 34 additions and 34 deletions

View file

@ -11859,15 +11859,14 @@ unicode_isidentifier_impl(PyObject *self)
/*[clinic input]
str.isprintable as unicode_isprintable
Return True if the string is printable, False otherwise.
Return True if all characters in the string are printable, False otherwise.
A string is printable if all of its characters are considered printable in
repr() or if it is empty.
A character is printable if repr() may use it in its output.
[clinic start generated code]*/
static PyObject *
unicode_isprintable_impl(PyObject *self)
/*[clinic end generated code: output=3ab9626cd32dd1a0 input=98a0e1c2c1813209]*/
/*[clinic end generated code: output=3ab9626cd32dd1a0 input=4e56bcc6b06ca18c]*/
{
Py_ssize_t i, length;
int kind;