Skip to content

Remove default dict parameters from api#236

Merged
non-Jedi merged 1 commit intomatrix-org:masterfrom
pcppcp:remove_default_dict
Jun 15, 2018
Merged

Remove default dict parameters from api#236
non-Jedi merged 1 commit intomatrix-org:masterfrom
pcppcp:remove_default_dict

Conversation

@pcppcp
Copy link
Contributor

@pcppcp pcppcp commented Jun 15, 2018

Using dict or list as a default argument generally causes havoc. This PR removes them.

kind (str): Specify kind="guest" to register as guest.
"""
if content is None:
content = {}
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm confused, it causes havoc how? And as far as I can tell the outcome is exactly the same here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

python will instantiate the argument once. After that any call to the method can mutate this single argument.
https://pythonconquerstheuniverse.wordpress.com/2012/02/15/mutable-default-arguments/

We've experienced this when running multiple clients in a single thread with gevent context switching. This has caused one client using access token of other client and other funny things.

Copy link
Collaborator

Choose a reason for hiding this comment

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

@pcppcp is this code doing gevent with multiple clients public anywhere by chance? Would be curious to see it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

we use matrix as an experimental transport layer in Raiden and since in the tests there are sometimes many clients running in a single thread, a lot of context switching occurs and this had revealed the problem with the default dict.

Copy link
Collaborator

Choose a reason for hiding this comment

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

very cool

Choose a reason for hiding this comment

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

@non-Jedi we plan to upstream some of our changes in the near future, but if you're interested on some concepts of our usage, you may look at some of our usages here

@non-Jedi
Copy link
Collaborator

Thanks! Been meaning to fix this well-known gotcha for a while now. Much appreciated. Could you please sign off per contributing.rst?

@pcppcp pcppcp force-pushed the remove_default_dict branch from 7f8ea2f to 1043cdc Compare June 15, 2018 17:13
@pcppcp
Copy link
Contributor Author

pcppcp commented Jun 15, 2018

@non-Jedi I've added this signed-off-by line to my commit, is it all that's required?

Signed-off-by: Jozef Henzl <popcorp86@gmail.com>
@pcppcp pcppcp force-pushed the remove_default_dict branch from 1043cdc to a135c05 Compare June 15, 2018 18:57
@non-Jedi
Copy link
Collaborator

@pcppcp yep. Thanks again!

@non-Jedi non-Jedi merged commit a135c05 into matrix-org:master Jun 15, 2018
@pcppcp pcppcp deleted the remove_default_dict branch June 15, 2018 20:23
palango pushed a commit to raiden-network/raiden-libs that referenced this pull request Jun 18, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants