Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Add close method to Reader #107

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
Aug 15, 2017
Merged

Conversation

djhoese
Copy link
Contributor

@djhoese djhoese commented Aug 11, 2017

Some testing frameworks and other utilities will print a warning when files are left open at the end of a test. I've added a close method to the Reader class that closes any files that may have been opened. This close method is also called from __del__ if the Reader object is deleted/garbage collected. The warning I was getting is:

  s = shapefile.Reader(shapefilename)
my_code.py:688: ResourceWarning: unclosed file <_io.BufferedReader name='my_shape.shx'>

I'm not super familiar with the internals of Reader so there could be a chance that this could break anything returned by the Reader that references shp, shx, and dbf files and depends on them staying open.

@karimbahgat
Copy link
Collaborator

karimbahgat commented Aug 15, 2017

This is great and makes sense to me! I don't see any issues with it, having called close() one should only expect that it's not possible to do anything on the file anymore.

A possible extension of this would be to do the same for the Writer class, and implement __enter__ and __exit__ to allow with statements.

Thanks!

@karimbahgat karimbahgat merged commit 91c4a7d into GeospatialPython:master Aug 15, 2017
megies added a commit to megies/pyshp that referenced this pull request Oct 15, 2017
karimbahgat pushed a commit that referenced this pull request Jun 6, 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.

2 participants