Skip to content

The problem implementing print in gpython. #91

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 26, 2019 · 5 comments
Closed

The problem implementing print in gpython. #91

Sungmin-Joo opened this issue Sep 26, 2019 · 5 comments
Labels
bug Something isn't working

Comments

@Sungmin-Joo
Copy link
Contributor

Sungmin-Joo commented Sep 26, 2019

Even if sep is not required, the option must be inserted.

gpython

with open("testfile","w") as f:
    print("1234", end = "@@@", file = f)
with open("testfile","r") as f:
    print(f.read())`

Traceback (most recent call last):
  File "test.py", line 3, in <module>
    FIXME line of source goes here
TypeError: 'print() argument 2 must be str, not file'
2019/09/26 11:21:21 TypeError: 'print() argument 2 must be str, not file'

when fixed code

with open("testfile","w") as f:
    print("1234", sep = '', end = "@@@", file = f)
with open("testfile","r") as f:
    print(f.read())

1234@@@
@Sungmin-Joo Sungmin-Joo changed the title There is a problem implementing print in gpython. The problem implementing print in gpython. Sep 27, 2019
@ncw
Copy link
Collaborator

ncw commented Sep 28, 2019

The gpython print is fairly minimal at the moment and certainly needs improving.

I think this particular bug is caused by this line doing the argument parsing wrong (not sure exactly why).

err := py.ParseTupleAndKeywords(nil, kwargs, "|ssOO:print", kwlist, &sepObj, &endObj, &file, &flush)

@ncw ncw added the bug Something isn't working label Sep 28, 2019
@Sungmin-Joo
Copy link
Contributor Author

Sungmin-Joo commented Sep 28, 2019

@ncw
There is a temporary way to modify the builtin_print.
But I don't think it's the right way.

func builtin_print(self py.Object, args py.Tuple, kwargs py.StringDict) (py.Object, error) {
    var (
        sepObj py.Object = py.String(" ")
        endObj py.Object = py.String("\n")
        file   py.Object = py.MustGetModule("sys").Globals["stdout"]
        flush  py.Object
    )
    kwlist := []string{"sep", "end", "file", "flush"}
    for _, kw := range kwlist {
        switch _, ok := kwargs[kw]; ok {
        case true:
            continue
        default:
            if kw == "sep" {
                kwargs[kw] = sepObj
            } else if kw == "end" {
                kwargs[kw] = endObj
            } else if kw == "file" {
                kwargs[kw] = file
            } else {
                kwargs[kw] = flush
            }
        }
    }
    err := py.ParseTupleAndKeywords(nil, kwargs, "|ssOO:print", kwlist, &sepObj, &endObj, &file, &flush)
    ...

@ncw
Copy link
Collaborator

ncw commented Sep 28, 2019

py.ParseTupleAndKeywords should be doing that for us...

The bug must be in this bit of code I think... No time to investigate further right now - over to you to have a look!

gpython/py/args.go

Lines 444 to 453 in 8c361a8

for i, kw := range kwlist {
if value, ok := kwargs[kw]; ok {
if len(args) > i {
return ExceptionNewf(TypeError, "%s() got multiple values for argument '%s'", name, kw)
}
args = append(args, value)
} else if keywordOnly {
args = append(args, nil)
}
}

@drew-512
Copy link
Contributor

suggest @sbinet close -- fixed w/ #151

@sbinet
Copy link
Member

sbinet commented Feb 22, 2022

fixed w/ #151.

@sbinet sbinet closed this as completed Feb 22, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants