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

Added protections to AttributeErrors raised within properties#9455

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

Conversation

james-mchugh
Copy link
Contributor

@james-mchughjames-mchugh commentedJun 29, 2024
edited
Loading

Description of Changes

Updates theRequest class'sdata,FILES, andPOST properties to utilize thewrap_attributeerrors context manager to ensure any properties that run_load_data_and_files in theRequest class properly handle anyAttributeErrors raised internally.

Updates the__getattr__ function to explicitly raise anAttributeError rather than calling__getattribute__ again, as calling__getattribute__ can have unexpected side effects (see#9433).

Adds a unit test to cover the case where a Parser raises an attribute error when theRequest.data property is accessed.

Benefits

AttributeErrors raised in properties such as from parsing will now produce sane error messages and will no longer result in errors being suppressed.

Possible drawbacks

None that I am currently aware of.

Applicable issues

Additional information

There appears to already be a precedent for this type of handling in request.py with thewrap_attributeerrors context manager that is currently only used for authentication. This extends that behavior to other properties via a commonsafe_property decorator that can be used instead of the builtinproperty decorator.

I had originally utilized asafe_property decorator to automatically add safe attribute handling to all properties, but that a significant performance penalty when accessing the properties as pointed out by@reddytocode. It has been dropped.

Copy link

@reddytocodereddytocode left a comment

Choose a reason for hiding this comment

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

Did a timing analysis onrequest.data similar to the one madehere, and observed a time increase.

Timingsrequests.data
before0.022 s
after0.106 s

I don't think that@safe_property should be used on every property.
Usingwith wrap_attributeerrors: to thedata should be sufficient IMO.

james-mchugh reacted with eyes emoji
@james-mchugh
Copy link
ContributorAuthor

james-mchugh commentedJul 7, 2024
edited
Loading

Did a timing analysis onrequest.data similar to the one madehere, and observed a time increase.

Timingsrequests.data
before0.022 s
after0.106 s

Good call running the timings. I didn't realize a custom property could cause that much of a slow down. That's good to know. I'll make the necessary updates.

I don't think that@safe_property should be used on every property. Usingwith wrap_attributeerrors: to thedata should be sufficient IMO.

As for which properties need it, it could definitely be dropped for some of the simple properties likecontent_type. However, any property that runs_load_data_and_files, which would includedata,POST, andFILESis susceptible to the original issue. If we want to drop the property, I could just explicitly add the context manager to those.

I think the custom property is useful, as it creates a repeatable pattern for how properties can be safely created inRequest class without having to manage the pitfalls of using__getattr__ andproperty together for each property individually, but it's not worth it if it slows things down so much.

@james-mchughjames-mchughforce-pushed thefix-request-attribute-handling branch 2 times, most recently fromb6084b1 tofca7e6bCompareJuly 7, 2024 04:46
@james-mchugh
Copy link
ContributorAuthor

@reddytocode Thank you for the review. I've dropped thesafe_property decorator and updated the properties that call_load_data_and_files to use thewrap_attributeerrors contextmanager.

I also moved the unit test under theTestContentParsing class.

Please let me know if any additional updates are needed

reddytocode reacted with thumbs up emoji

@auvipyauvipy requested a review froma teamJuly 7, 2024 07:23
Copy link
Contributor

@peterthomassenpeterthomassen left a comment

Choose a reason for hiding this comment

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

pls squash.

james-mchugh reacted with thumbs up emoji
Signed-off-by: James Riley McHugh <mchugh_james@bah.com>
@james-mchughjames-mchughforce-pushed thefix-request-attribute-handling branch fromfca7e6b tobaf9a1fCompareJuly 8, 2024 13:13
@james-mchugh
Copy link
ContributorAuthor

pls squash.

Done!

@james-mchugh
Copy link
ContributorAuthor

pls squash.

Just in case you weren't aware, Github allows projects to change the merge strategies used for PRs. For some of the projects I maintain, we've disabled merge methods other than squash, making it so all PR branches are automatically squashed when merged. You can configure it to pull the squashed commit message from the PR title and description too. It's pretty handy. I definitely recommend checking it out if you guys haven't and if this is a common requirement for PRs.

@peterthomassen
Copy link
Contributor

I know the feature, but thanks for pointing it out. The reason for my squashing request was rather that the previous commits make changes that were undone one or two commits later, and it was difficult to review.

@james-mchugh
Copy link
ContributorAuthor

Oh, interesting. Do you typically review PRs by looking at individual commits rather than the overall diff? I typically just look at the overall diff, but I'm wondering if I should start paying closer attention to individual commits as well.

Also, please let me know if any additional changes are needed.@auvipy has approved, but it looks like your request for changes is still preventing the PR from being merged. Please let me know if additional changes are needed besides squashing the commits.

@peterthomassen
Copy link
Contributor

Oh, interesting. Do you typically review PRs by looking at individual commits rather than the overall diff?

Some PRs are larger, and some people organize them such that each commit is a mental block, and that aids comprehension during reviewing things one by one. It kinda works if a commit contains to mental block (e.g. a test and a docs change), but it does not work so well if one commit undoes changes of a previous one.

Also, reviewing the combination only allows sneaking unreviewed code into the codebase (although not at the HEAD of the branch), and if the merge doesn't squash (which I don't usually assume), then those unreviewed commits show up in the main branch history.

For these two reasons, I like to review commits explicitly. But I get it that that's a matter of taste, and others may do it differently.

@auvipy has approved, but it looks like your request for changes is still preventing the PR from being merged.

Sorry! Approved.

james-mchugh reacted with thumbs up emoji

@auvipyauvipy merged commit8e304e1 intoencode:masterJul 17, 2024
7 checks passed
@browniebrokebrowniebroke added this to the3.16 milestoneJan 16, 2025
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@reddytocodereddytocodereddytocode left review comments

@peterthomassenpeterthomassenpeterthomassen approved these changes

@auvipyauvipyauvipy approved these changes

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

Successfully merging this pull request may close these issues.

Attribute Errors raised in Parser Silently Ignored
5 participants
@james-mchugh@peterthomassen@auvipy@reddytocode@browniebroke

[8]ページ先頭

©2009-2025 Movatter.jp