Skip to content

Add support for print to file and file flush. #27

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 1 commit into from
Sep 12, 2018

Conversation

raff
Copy link
Contributor

@raff raff commented Sep 8, 2018

No description provided.

@codecov-io
Copy link

codecov-io commented Sep 8, 2018

Codecov Report

Merging #27 into master will increase coverage by 0.18%.
The diff coverage is 59.45%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master      #27      +/-   ##
==========================================
+ Coverage   64.67%   64.86%   +0.18%     
==========================================
  Files          55       55              
  Lines       10044    10074      +30     
==========================================
+ Hits         6496     6534      +38     
+ Misses       3090     3079      -11     
- Partials      458      461       +3
Impacted Files Coverage Δ
py/file.go 53.14% <55%> (+2.35%) ⬆️
builtin/builtin.go 78.67% <64.7%> (+3.85%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 8cee534...faf6c54. Read the comment docs.

@raff raff force-pushed the print_to_file branch 2 times, most recently from fb4a4f2 to d8a60c1 Compare September 10, 2018 18:58
Copy link
Collaborator

@ncw ncw left a comment

Choose a reason for hiding this comment

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

This looks really good - thank you. Great to have the print working properly.

A few little things inline and a a few queries.

if err != nil {
return nil, err
}

// FIXME ignoring file and flush
Copy link
Collaborator

Choose a reason for hiding this comment

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

This comment is now out of date :-)

if shouldFlush, _ := py.MakeBool(flush); shouldFlush == py.True {
fflush, err := py.GetAttrString(file, "flush")
if err == nil {
py.Call(fflush, nil, nil)
Copy link
Collaborator

Choose a reason for hiding this comment

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

This should be returning an error on failure I think?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'll see if I can reproduce a flush error in python 3 and fix if needed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added check for "file already closed". I don't think flush can return any other error.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I'd say we should as a matter of course return all the errors otherwise tools like errcheck (which I plan to run the codebase through) will complain that we aren't handling errors properly.

The error may need translating, that is for sure, but I think we should always return all errors!

Maybe we should make a generic error translator rather like the one here (from the open code)

        if err != nil {
                // XXX: should check for different types of errors
                switch {
                case os.IsExist(err):
                        return nil, ExceptionNewf(FileExistsError, err.Error())

                case os.IsNotExist(err):
                        return nil, ExceptionNewf(FileNotFoundError, err.Error())
                }
        }

(Note that this particular bit of code is ignoring the err if it wasn't one of those two errors which it shouldn't be - that needs to be fixed)

py/file.go Outdated
@@ -138,6 +141,20 @@ func (o *File) Close() (Object, error) {
return None, nil
}

func (o *File) Flush() (Object, error) {
_ = o.File.Sync()
Copy link
Collaborator

Choose a reason for hiding this comment

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

Maybe this should return None, o.File.Sync() otherwise we are ignoring the error on Sync()

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'll see if I can reproduce a flush error in python 3 and fix if needed.

py/file.go Outdated

func (o *File) M__exit__(exc_type, exc_value, traceback Object) (Object, error) {
o.Close()
return None, nil
Copy link
Collaborator

Choose a reason for hiding this comment

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

Likewise here, return None, o.Close()

Copy link
Contributor Author

Choose a reason for hiding this comment

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

o.Close() always return None - python File.close() never return an error.

py/file.go Outdated
@@ -250,3 +267,6 @@ func OpenFile(filename, mode string, buffering int) (Object, error) {
}

// Check interface is satisfied
var _ I__enter__ = (*File)(nil)
var _ I__exit__ = (*File)(nil)
var _ I__exit__ = (*File)(nil)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Duplicated __exit__ check?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry, I'll fix it.

@raff raff force-pushed the print_to_file branch 2 times, most recently from e5b841e to 24e39a3 Compare September 11, 2018 15:59
@raff
Copy link
Contributor Author

raff commented Sep 11, 2018

Now all failed calls should properly return an error

@ncw
Copy link
Collaborator

ncw commented Sep 12, 2018

This looks great now thanks. Will merge :-)

@ncw ncw merged commit 4f66e54 into go-python:master Sep 12, 2018
@raff raff deleted the print_to_file branch October 4, 2018 20:20
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.

3 participants