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 — bpo-44024: Improve the TypeError message for non-string second arguments passed to the built-in functions getattr and hasattr by geryogam · Pull Request #25863 · python/cpython

Conversation

@geryogam

@geryogam geryogam commented May 3, 2021 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

Problem

Actual behaviour:

>>> getattr('foobar', 123)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: getattr(): attribute name must be string
>>> hasattr('foobar', 123)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: hasattr(): attribute name must be string

Expected behaviour:

>>> getattr('foobar', 123)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: attribute name must be string, not 'int'
>>> hasattr('foobar', 123)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: attribute name must be string, not 'int'

Motivation:

>>> setattr('foobar', 123, 'baz')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: attribute name must be string, not 'int'
>>> delattr('foobar', 123)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: attribute name must be string, not 'int'

Solution

In the function builtin_getattr defined in Python/bltinmodule.c, we remove the following lines:

    if (!PyUnicode_Check(name)) {
        PyErr_SetString(PyExc_TypeError,
                        "getattr(): attribute name must be string");
        return NULL;
    }

because the expected TypeError message is already implemented in the subsequent call to the functions _PyObject_LookupAttr and PyObject_GetAttr defined in Objects/object.c:

PyObject *
PyObject_GetAttr(PyObject *v, PyObject *name)
{
    PyTypeObject *tp = Py_TYPE(v);
    if (!PyUnicode_Check(name)) {
        PyErr_Format(PyExc_TypeError,
                     "attribute name must be string, not '%.200s'",
                     Py_TYPE(name)->tp_name);
        return NULL;
    }

[…]

int
_PyObject_LookupAttr(PyObject *v, PyObject *name, PyObject **result)
{
    PyTypeObject *tp = Py_TYPE(v);

    if (!PyUnicode_Check(name)) {
        PyErr_Format(PyExc_TypeError,
                     "attribute name must be string, not '%.200s'",
                     Py_TYPE(name)->tp_name);
        *result = NULL;
        return -1;
    }

[…]

Likewise, in the function builtin_hasattr_impl defined in Python/bltinmodule.c, we remove the following lines:

    if (!PyUnicode_Check(name)) {
        PyErr_SetString(PyExc_TypeError,
                        "hasattr(): attribute name must be string");
        return NULL;
    }

because the expected TypeError message is already implemented in the subsequent call to the function _PyObject_LookupAttr defined in Objects/object.c.

https://bugs.python.org/issue44024

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@geryogam
Use common TypeError message for built-in functions getattr and hasattr
33a38b3
@bedevere-bot bedevere-bot added the awaiting review label May 3, 2021
@the-knights-who-say-ni the-knights-who-say-ni added the CLA signed label May 3, 2021
JelleZijlstra
JelleZijlstra reviewed May 4, 2021
View reviewed changes

@JelleZijlstra JelleZijlstra left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a test for the error message and a news entry for the change?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@geryogam geryogam changed the title bpo-44024: Use common TypeError message for built-in functions getattr and hasattr bpo-44024: Use the common TypeError message for non-string second arguments passed to the built-in functions getattr and hasattr May 4, 2021
geryogam added 2 commits May 4, 2021 22:08
@geryogam
Add unit tests for TypeError messages
12ce6ca
@geryogam
Add a NEWS entry
caefee1
@geryogam
geryogam force-pushed the patch-27 branch from 98517a6 to caefee1 Compare May 4, 2021 20:09
JelleZijlstra
JelleZijlstra reviewed May 4, 2021
View reviewed changes
Comment thread Misc/NEWS.d/next/Core and Builtins/2021-05-04-21-55-49.bpo-44024.M9m8Qd.rst Resolved

Uh oh!

There was an error while loading. Please reload this page.

JelleZijlstra
JelleZijlstra reviewed May 4, 2021
View reviewed changes
Comment thread Lib/test/test_call.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

@geryogam @JelleZijlstra
Update Misc/NEWS.d/next/Core and Builtins/2021-05-04-21-55-49.bpo-440…
46c3a0f
…24.M9m8Qd.rst

Co-authored-by: Jelle Zijlstra <[email protected]>
JelleZijlstra
JelleZijlstra approved these changes May 4, 2021
View reviewed changes
@bedevere-bot bedevere-bot added awaiting core review and removed awaiting review labels May 4, 2021
@geryogam geryogam changed the title bpo-44024: Use the common TypeError message for non-string second arguments passed to the built-in functions getattr and hasattr bpo-44024: Improve the error message for non-string second arguments passed to the built-in functions getattr and hasattr May 4, 2021
@geryogam

geryogam commented May 4, 2021

Copy link
Copy Markdown
Contributor Author

Thanks for the review @JelleZijlstra!

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@geryogam
Update 2021-05-04-21-55-49.bpo-44024.M9m8Qd.rst
87813e8
@geryogam geryogam changed the title bpo-44024: Improve the error message for non-string second arguments passed to the built-in functions getattr and hasattr bpo-44024: Improve the TypeError message for non-string second arguments passed to the built-in functions getattr and hasattr May 4, 2021
@geryogam
Update 2021-05-04-21-55-49.bpo-44024.M9m8Qd.rst
ad724dd
@github-actions

github-actions Bot commented Jun 25, 2021

Copy link
Copy Markdown

This PR is stale because it has been open for 30 days with no activity.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@github-actions github-actions Bot added the stale Stale PR or inactive for long period of time. label Jun 25, 2021
@merwok

merwok commented Jan 5, 2022

Copy link
Copy Markdown
Member

Looks good, thanks! Could you pull and merge upstream in your branch?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

serhiy-storchaka
serhiy-storchaka reviewed Jan 5, 2022
View reviewed changes

@serhiy-storchaka serhiy-storchaka left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The change LGTM, but tests are added in wrong place.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread Lib/test/test_call.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

@geryogam
geryogam force-pushed the patch-27 branch from f3835ca to ad724dd Compare January 5, 2022 10:39
@hugovk hugovk removed the stale Stale PR or inactive for long period of time. label Jan 5, 2022
geryogam and others added 2 commits January 5, 2022 13:01
@geryogam
Move tests from test_call.py to test_builtin.py
4f17537
@serhiy-storchaka
Remove change in unrelated file.
c648edc
serhiy-storchaka
serhiy-storchaka approved these changes Jan 10, 2022
View reviewed changes

@serhiy-storchaka serhiy-storchaka left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@bedevere-bot bedevere-bot added awaiting merge and removed awaiting core review labels Jan 10, 2022
@serhiy-storchaka
serhiy-storchaka merged commit 16bf9bd into python:main Jan 18, 2022
@bedevere-bot bedevere-bot removed the awaiting merge label Jan 18, 2022
@geryogam
geryogam deleted the patch-27 branch January 18, 2022 21:41
geryogam added a commit to geryogam/cpython that referenced this pull request May 4, 2022
@geryogam
Remove duplicate test (see python#25863)
42da011
@geryogam geryogam mentioned this pull request May 4, 2022
bpo-43857: Improve the AttributeError message when deleting a missing attribute #25424
Merged
@rlamy rlamy mentioned this pull request May 30, 2025
Fix test failures in test_builtin.py pypy/pypy#5285
Merged
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@erlend-aasland erlend-aasland erlend-aasland left review comments
@JelleZijlstra JelleZijlstra JelleZijlstra approved these changes
@serhiy-storchaka serhiy-storchaka serhiy-storchaka approved these changes

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

8 participants

@geryogam @merwok @JelleZijlstra @serhiy-storchaka @erlend-aasland @hugovk @the-knights-who-say-ni @bedevere-bot
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.