Skip to content
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

File left open in sitecustomize when executing it in Python 3 #3067

Closed
ekandrot opened this issue Mar 17, 2016 · 5 comments
Closed

File left open in sitecustomize when executing it in Python 3 #3067

ekandrot opened this issue Mar 17, 2016 · 5 comments

Comments

@ekandrot
Copy link

Description of your problem

When I run in Windows10, I get the following error every time I execute file from the UI:

C:\Users\Edward\Anaconda3\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py:88: ResourceWarning: unclosed file <_io.BufferedReader name='C:/Users/Edward/test.py'>
exec(compile(open(filename, 'rb').read(), filename, 'exec'), namespace)

What steps will reproduce the problem?

  1. Run with the latest download on spyder installed via Anaconda.
  2. Execute any .py file via the "Run File" button

What is the expected output? What do you see instead?
No warning with each run - it crowds out my results.

Please provide any additional information below

I fixed it by changing line 88 in the file to:
with open(filename, 'rb') as ff:
exec(compile(ff.read(), filename, 'exec'), namespace)

which will then properly close the file.

Versions and main components

  • Spyder Version: 2.3.8
  • Python Version: 3.5.1
  • Operating system: Windows 10

Dependencies

Please go to the menu entry Help > Optional Dependencies (or
Help > Dependencies), press the button Copy to clipboard
and paste the contents below:

IPython >=1.0 : 4.1.2 (OK)
jedi >=0.8.1;<0.9.0: 0.9.0 (NOK)
matplotlib >=1.0 : 1.5.1 (OK)
pandas >=0.13.1 : 0.18.0 (OK)
pep8 >=0.6 : 1.7.0 (OK)
pyflakes >=0.6.0 : 1.1.0 (OK)
pygments >=1.6 : 2.1.1 (OK)
pylint >=0.25 : None (NOK)
qtconsole >=4.0 : 4.1.1 (OK)
rope >=0.9.2 : 0.9.4-1 (OK)
sphinx >=0.6.6 : 1.3.1 (OK)
sympy >=0.7.3 : 1.0 (OK)
zmq >=2.1.11 : 15.2.0 (OK)

@ccordoba12
Copy link
Member

Thanks for reporting. Could you show us a simple code example that reproduce this problem? Thanks :-)

@ekandrot
Copy link
Author

Thank you for looking into this! I narrowed down the cause to these lines:

import praw
user_agent = 'Test Agent'
r = praw.Reddit(user_agent=user_agent)
print ('Hello')

After running this code once (using the reddit api library, praw), all future Executes return the aforementioned error. If I change the code in sitecustomize.py as per the description above, this error no longer happens.

It is probably that praw isn't closing something? Either way, the standard for opening a file to guarantee that it is closed is with a 'with' statement, from my reading of current Python docs (I'm converting my code over to this style now). So it seems that changing the code in sitecustomize.py fixes this for libraries like praw.

Before I filed this bug, I did a lot of searching and found a few people with the same issue with Spyder running their files, but no solution. I hope this change will fix all of their problems as well.

@Nodd
Copy link
Contributor

Nodd commented Mar 18, 2016

Looking at the code extract, looks like your solution is correct. Could you create a pull request with hese modifications ?

That said, the file should be closed when the file object is destroyed, which should happen quick because it's a temporary value.

@ccordoba12 ccordoba12 changed the title File left open in Python3 in spyder/spyderlib/widgets/externalshell/sitecustomize.py:88 File left open in sitecustomize Mar 20, 2016
@ccordoba12
Copy link
Member

@ekandrot, don't worry, since this is a simple fix I'll do it myself :-)

@ccordoba12 ccordoba12 changed the title File left open in sitecustomize File left open in sitecustomize when executing a file in Python 3 Mar 20, 2016
@ccordoba12 ccordoba12 changed the title File left open in sitecustomize when executing a file in Python 3 File left open in sitecustomize when executing it in Python 3 Mar 20, 2016
ccordoba12 added a commit that referenced this issue Mar 20, 2016
@ekandrot
Copy link
Author

Thanks! I was away this weekend and just saw this. Next time I'll submit a pull request.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants