Skip to content

Found a problem with the args of the Dictionary "items" function. #101

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

Closed
Sungmin-Joo opened this issue Sep 29, 2019 · 4 comments · Fixed by #115
Closed

Found a problem with the args of the Dictionary "items" function. #101

Sungmin-Joo opened this issue Sep 29, 2019 · 4 comments · Fixed by #115

Comments

@Sungmin-Joo
Copy link
Contributor

Sungmin-Joo commented Sep 29, 2019

Current Results
in gpython

>>>> d_list = {'a' : 1, 'b' : 2}
>>>> for k, v in d_list.items("hi"):
>>>>     print(k,':',v)
a : 1
b : 2

in python

>>>> d_list = {'a' : 1, 'b' : 2}
>>>> for k, v in d_list.items("hi"):
>>>>     print(k,':',v)
Traceback (most recent call last):
  File "test.py", line 2, in <module>
    for k, v in d_list.items("hi"):
TypeError: items() takes no arguments (1 given)

gpython/py/dict.go

Lines 30 to 39 in af17d7d

func init() {
StringDictType.Dict["items"] = MustNewMethod("items", func(self Object, args Tuple) (Object, error) {
sMap := self.(StringDict)
o := make([]Object, 0, len(sMap))
for k, v := range sMap {
o = append(o, Tuple{String(k), v})
}
return NewIterator(o), nil
}, 0, "items() -> list of D's (key, value) pairs, as 2-tuples")
}

I think we should add an error to this part of the code.

@ghost
Copy link

ghost commented Sep 29, 2019

For me it works in CPython 3.7.4 as in GPython.

@Sungmin-Joo
Copy link
Contributor Author

@Tim-St
I found a mistake in the code I used in the issue.
Please check again.

@ghost
Copy link

ghost commented Sep 29, 2019

Yes, the code requires the following check at the top:

err := UnpackTuple(args, nil, "items", 0, 0)
if err != nil {
    return nil, err
}

It could even be more correct by allowing to call dict.items(someDict), like I've implemented it in list.sort.

@Sungmin-Joo
Copy link
Contributor Author

@Tim-St
Ok.
When I finish my ongoing Pull Request, I'll do this right away.

corona10 pushed a commit that referenced this issue Nov 18, 2019
* Fix bug in "items" function

Fixes #101

* Update dict.py
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 a pull request may close this issue.

1 participant