Skip to content

bpo-34246: Use no mutable default args in smtplib #8554

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Sep 7, 2018

Conversation

scorphus
Copy link
Contributor

@scorphus scorphus commented Jul 29, 2018

Some methods of the SMTP class use mutable default arguments. Specially send_message is affected in which it mutates one of the args by appending items to it, whic has side effects on further calls.

https://bugs.python.org/issue34246

@serhiy-storchaka
Copy link
Member

There is nothing wrong with using mutable default arguments if they are not mutated inside function and are not exposed outside of it.

For mail_options I suggest to use an empty tuple. And instead of mail_options += ['SMTPUTF8', 'BODY=8BITMIME'] use mail_options = (*mail_options, 'SMTPUTF8', 'BODY=8BITMIME').

Some methods of the SMTP class use mutable default arguments. Specially
`send_message` is affected in which it mutates one of the args by
appending items to it, whic has side effects on further calls.
@scorphus
Copy link
Contributor Author

scorphus commented Aug 6, 2018

Hello, @serhiy-storchaka.

Thanks so much for reviewing my changes. It's now cleaner with tuples 👍

I'm not sure whether I should ping you or if the new commit (67f70fd) triggers that automatically.

@serhiy-storchaka
Copy link
Member

Please don't squash commits, this makes reviewing new changes more difficult. I have not received a notification because you edited your commit instead of adding a new commit.

@@ -0,0 +1,2 @@
Use an empty tuple as the default argument for mail_options and avoid it from
Copy link
Member

Choose a reason for hiding this comment

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

The argument for what function? Using an empty tuple is an implementation detail, please describe the user visible behavior change. For example ":meth:`smtplib.SMTP.send_message` no longer modifies the content of the *mail_options* argument." or something like.

Add also "Patch by Pablo S. Blum de Aguiar."

@@ -958,7 +958,7 @@ def send_message(self, msg, from_addr=None, to_addrs=None,
if international:
g = email.generator.BytesGenerator(
bytesmsg, policy=msg.policy.clone(utf8=True))
mail_options += ['SMTPUTF8', 'BODY=8BITMIME']
mail_options = (*mail_options, 'SMTPUTF8', 'BODY=8BITMIME')
Copy link
Member

@methane methane Aug 6, 2018

Choose a reason for hiding this comment

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

I don't like this change. += seems more common/readable, and it's valid for immutable objects
like tuple and str. (You need to change [] to () in this case)

I'm sorry, I was wrong. When caller passed list, list += tuple will raise TypeError.

Just to ensure `smtplib.SMTP.send_message` no longer modifies the
content of the *mail_options* argument
@scorphus
Copy link
Contributor Author

Hey, I hope the 2 new commits I added 10 days ago triggered a notification. I also hope this message isn't just additional noise – apologies if it is, I'm still new to contrubuting to CPython.

@serhiy-storchaka serhiy-storchaka added type-bug An unexpected behavior, bug, or error needs backport to 3.6 labels Aug 16, 2018
@serhiy-storchaka
Copy link
Member

Thank you for your reminder @scorphus. It is not a noise. This PR LGTM, but we need to get an approval from @bitdancer or @warsaw, the email module experts.

Copy link
Member

@serhiy-storchaka serhiy-storchaka left a comment

Choose a reason for hiding this comment

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

Please update also the signature in Doc/library/smtplib.rst.

@bedevere-bot
Copy link

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

@scorphus
Copy link
Contributor Author

I have made the requested changes; please review again.

Regarding #5334 (and issue32657) it proposes a change that was already discussed here and droped in favour of the current status of this very PR (#8554).

@bedevere-bot
Copy link

Thanks for making the requested changes!

@serhiy-storchaka: please review the changes made to this pull request.

@serhiy-storchaka
Copy link
Member

If rcpt_options expected to be a sequence, not mapping, it is better to make a default value for options in send_message(), sendmail() and rcpt() an empty tuple than an empty list or dict.

@scorphus
Copy link
Contributor Author

Yeah, I thought so – and actually did exactly that in my initial change – but decided not to, as rcpt_options is not mutated anywhere – it's just joined. Anyways, I agree it won't hurt :-)

@scorphus
Copy link
Contributor Author

Done, @serhiy-storchaka

How about mail's default value for options?

@serhiy-storchaka
Copy link
Member

serhiy-storchaka commented Aug 20, 2018

It won't hurt :-)

Making the default value for options in mail() an empty tuple LGTM.

@scorphus
Copy link
Contributor Author

Cool! Thanks, @serhiy-storchaka.

Copy link
Member

@serhiy-storchaka serhiy-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! Thank you for your contribution Pablo!

@scorphus
Copy link
Contributor Author

YAY! Thanks for reviewing! I'm very happy to contribute!

Copy link
Member

@vstinner vstinner left a comment

Choose a reason for hiding this comment

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

LGTM.

@pablogsal pablogsal merged commit d5fbe9b into python:master Sep 7, 2018
@miss-islington
Copy link
Contributor

Thanks @scorphus for the PR, and @pablogsal for merging it 🌮🎉.. I'm working now to backport this PR to: 3.6, 3.7.
🐍🍒⛏🤖

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Sep 7, 2018
Some methods of the SMTP class use mutable default arguments. Specially
`send_message` is affected as it mutates one of the args by appending items
to it, which has side effects on further calls.
(cherry picked from commit d5fbe9b)

Co-authored-by: Pablo Aguiar <scorphus@gmail.com>
@bedevere-bot
Copy link

GH-9111 is a backport of this pull request to the 3.7 branch.

@miss-islington
Copy link
Contributor

Sorry, @scorphus and @pablogsal, I could not cleanly backport this to 3.6 due to a conflict.
Please backport using cherry_picker on command line.
cherry_picker d5fbe9b1a3d65ceeb9159c5ba999ee966a945f76 3.6

miss-islington added a commit that referenced this pull request Sep 7, 2018
Some methods of the SMTP class use mutable default arguments. Specially
`send_message` is affected as it mutates one of the args by appending items
to it, which has side effects on further calls.
(cherry picked from commit d5fbe9b)

Co-authored-by: Pablo Aguiar <scorphus@gmail.com>
@bedevere-bot
Copy link

GH-9112 is a backport of this pull request to the 3.6 branch.

pablogsal pushed a commit to pablogsal/cpython that referenced this pull request Sep 8, 2018
Some methods of the SMTP class use mutable default arguments. Specially
`send_message` is affected as it mutates one of the args by appending items
to it, which has side effects on further calls..
(cherry picked from commit d5fbe9b)

Co-authored-by: Pablo Aguiar <scorphus@gmail.com>
pablogsal added a commit that referenced this pull request Sep 8, 2018
)

Some methods of the SMTP class use mutable default arguments. Specially
`send_message` is affected as it mutates one of the args by appending items
to it, which has side effects on further calls..
(cherry picked from commit d5fbe9b)

Co-authored-by: Pablo Aguiar <scorphus@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants