Repository navigation
Add support for function argument defaults #100
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
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
2cecd36
Add support for defaults to bytecode, compiler and VM
OddBloke 85d2dd2
Add support for default argument syntax
OddBloke 51e2db6
Add snippet to test default arguments
OddBloke cb173c1
Update and expand Rust tests for function parsing
OddBloke File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| def no_args(): | ||
| pass | ||
|
|
||
| no_args() | ||
|
|
||
| try: | ||
| no_args('one_arg') | ||
| except TypeError: | ||
| pass | ||
| else: | ||
| assert False, 'no TypeError raised: 1 arg to no_args' | ||
|
|
||
|
|
||
| def one_arg(arg): | ||
| pass | ||
|
|
||
| one_arg('one_arg') | ||
|
|
||
| try: | ||
| one_arg() | ||
| except TypeError: | ||
| pass | ||
| else: | ||
| assert False, 'no TypeError raised: no args to one_arg' | ||
|
|
||
| try: | ||
| one_arg('one_arg', 'two_arg') | ||
| except TypeError: | ||
| pass | ||
| else: | ||
| assert False, 'no TypeError raised: two args to one_arg' | ||
|
|
||
|
|
||
| def one_default_arg(arg="default"): | ||
| return arg | ||
|
|
||
| assert 'default' == one_default_arg() | ||
| assert 'arg' == one_default_arg('arg') | ||
|
|
||
| try: | ||
| one_default_arg('one_arg', 'two_arg') | ||
| except TypeError: | ||
| pass | ||
| else: | ||
| assert False, 'no TypeError raised: two args to one_default_arg' | ||
|
|
||
|
|
||
| def one_normal_one_default_arg(pos, arg="default"): | ||
| return pos, arg | ||
|
|
||
| assert ('arg', 'default') == one_normal_one_default_arg('arg') | ||
| assert ('arg', 'arg2') == one_normal_one_default_arg('arg', 'arg2') | ||
|
|
||
| try: | ||
| one_normal_one_default_arg() | ||
| except TypeError: | ||
| pass | ||
| else: | ||
| assert False, 'no TypeError raised: two args to one_default_arg' | ||
|
|
||
| try: | ||
| one_normal_one_default_arg('one', 'two', 'three') | ||
| except TypeError: | ||
| pass | ||
| else: | ||
| assert False, 'no TypeError raised: two args to one_default_arg' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,6 +4,7 @@ version = "0.1.0" | |
| authors = ["Shing Lyu <[email protected]>"] | ||
|
|
||
| [dependencies] | ||
| bitflags = "1.0.4" | ||
| log = "0.3" | ||
| rustpython_parser = {path = "../parser"} | ||
| serde = "1.0.66" | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
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.
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.
I think I would go for an extra boolean in the function:
has_defaults. So no extra packages.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.
This will eventually need to carry 4 values (per https://docs.python.org/3/library/dis.html#opcode-MAKE_FUNCTION), which is why I went with the bitflags approach. It also maintains symmetry with the CPython implementation.
If you'd still prefer for me to change it, I'd be happy to do so.
(FWIW, we also already pull bitflags in due to a couple of dependencies, so this has no net impact of our dependency tree.)
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.
My reasons would be that using booleans would allow the rust compiler to implement bitfields by itself. I think bitfields are an optimization from times where memory was scarce and compilers did not implement this. I would not be surprised if rust under the hood implemented booleans as bitfields.
Leave it as it is for now :)