Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

bpo-44024: Improve the TypeError message for non-string second arguments passed to the built-in functions getattr and hasattr#25863

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
serhiy-storchaka merged 8 commits intopython:mainfromgeryogam:patch-27
Jan 18, 2022

Conversation

geryogam
Copy link
Contributor

@geryogamgeryogam commentedMay 3, 2021
edited
Loading

Problem

Actual behaviour:

>>>getattr('foobar',123)Traceback (mostrecentcalllast):File"<stdin>",line1,in<module>TypeError:getattr():attributenamemustbestring>>>hasattr('foobar',123)Traceback (mostrecentcalllast):File"<stdin>",line1,in<module>TypeError:hasattr():attributenamemustbestring

Expected behaviour:

>>>getattr('foobar',123)Traceback (mostrecentcalllast):File"<stdin>",line1,in<module>TypeError:attributenamemustbestring,not'int'>>>hasattr('foobar',123)Traceback (mostrecentcalllast):File"<stdin>",line1,in<module>TypeError:attributenamemustbestring,not'int'

Motivation:

>>>setattr('foobar',123,'baz')Traceback (mostrecentcalllast):File"<stdin>",line1,in<module>TypeError:attributenamemustbestring,not'int'>>>delattr('foobar',123)Traceback (mostrecentcalllast):File"<stdin>",line1,in<module>TypeError:attributenamemustbestring,not'int'

Solution

In the functionbuiltin_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");returnNULL;    }

because the expectedTypeError message is already implemented in the subsequent call to the functions_PyObject_LookupAttr andPyObject_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);returnNULL;    }[…]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 functionbuiltin_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");returnNULL;    }

because the expectedTypeError message is already implemented in the subsequent call to the function_PyObject_LookupAttr defined in Objects/object.c.

https://bugs.python.org/issue44024

Copy link
Member

@JelleZijlstraJelleZijlstra left a 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?

@geryogamgeryogam changed the titlebpo-44024: Use common TypeError message for built-in functions getattr and hasattrbpo-44024: Use the common TypeError message for non-string second arguments passed to the built-in functions getattr and hasattrMay 4, 2021
…24.M9m8Qd.rstCo-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
@geryogamgeryogam changed the titlebpo-44024: Use the common TypeError message for non-string second arguments passed to the built-in functions getattr and hasattrbpo-44024: Improve the error message for non-string second arguments passed to the built-in functions getattr and hasattrMay 4, 2021
@geryogam
Copy link
ContributorAuthor

Thanks for the review@JelleZijlstra!

@geryogamgeryogam changed the titlebpo-44024: Improve the error message for non-string second arguments passed to the built-in functions getattr and hasattrbpo-44024: Improve the TypeError message for non-string second arguments passed to the built-in functions getattr and hasattrMay 4, 2021
@github-actions
Copy link

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

@github-actionsgithub-actionsbot added the staleStale PR or inactive for long period of time. labelJun 25, 2021
@merwok
Copy link
Member

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

Copy link
Member

@serhiy-storchakaserhiy-storchaka left a 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.

@hugovkhugovk removed the staleStale PR or inactive for long period of time. labelJan 5, 2022
Copy link
Member

@serhiy-storchakaserhiy-storchaka left a comment

Choose a reason for hiding this comment

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

LGTM.

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@erlend-aaslanderlend-aaslanderlend-aasland left review comments

@JelleZijlstraJelleZijlstraJelleZijlstra approved these changes

@serhiy-storchakaserhiy-storchakaserhiy-storchaka approved these changes

Assignees
No one assigned
Labels
None yet
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

8 participants
@geryogam@merwok@JelleZijlstra@serhiy-storchaka@erlend-aasland@hugovk@the-knights-who-say-ni@bedevere-bot

[8]ページ先頭

©2009-2025 Movatter.jp