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

Using warnings.warn() results in TypeError #2429

Closed
dopplershift opened this issue Sep 25, 2012 · 3 comments
Closed

Using warnings.warn() results in TypeError #2429

dopplershift opened this issue Sep 25, 2012 · 3 comments
Milestone

Comments

@dopplershift
Copy link
Contributor

The following code when run in the ipython terminal, qtconsole, and notebook will incorrectly produce a TypeError:

import warnings; warnings.warn('test')

Specifically, running it gives the following trace:

In [1]: import warnings; warnings.warn('test')
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-1-ae902b7aadb6> in <module>()
----> 1 import warnings; warnings.warn('test')

TypeError: expected string or Unicode object, NoneType found

Based on a git bisect, it cropped in with commit 2140420. Specifically, the offending line is 2459 (on master):

save_fname = self.user_ns.get('__file__',None)

If I change None to '', the error goes away and I see the warning. This is important, because it was resulting in tracebacks when numpy would issue a warning. I'm not sure if '' is the proper fix, so I haven't created a PR.

@bfroehle
Copy link
Contributor

Yes, see also #2279. I think directions to fix this can be taken from my post.

@dopplershift
Copy link
Contributor Author

Hadn't seen that one thanks. Your solution works great for me.

@bfroehle
Copy link
Contributor

Great, I'll close this issue as a duplicate.

Carreau added a commit that referenced this issue Sep 30, 2012
Revert #1831, the `__file__` injection in safe_execfile / safe_execfile_ipy.

This reverts commit 2717feb, reversing changes made to ea4f608.

Pull request #1831 (fix #1814 set __file__ when running .ipy files) has been the source of a lot of grief:

#2279: Setting __file__ to None breaks Mayavi import
#2429: Using warnings.warn() results in TypeError
In general the patch was inappropriate because it:
1. Fails to properly restore the context, by setting __file__ to None rather than deleting it.
2. Sets __file__ in the wrong dictionary (self.user_ns rather than where[0]).
@minrk minrk added this to the no action milestone Mar 26, 2014
mattvonrocketstein pushed a commit to mattvonrocketstein/ipython that referenced this issue Nov 3, 2014
Revert ipython#1831, the `__file__` injection in safe_execfile / safe_execfile_ipy.

This reverts commit 2717feb, reversing changes made to ea4f608.

Pull request ipython#1831 (fix ipython#1814 set __file__ when running .ipy files) has been the source of a lot of grief:

ipython#2279: Setting __file__ to None breaks Mayavi import
ipython#2429: Using warnings.warn() results in TypeError
In general the patch was inappropriate because it:
1. Fails to properly restore the context, by setting __file__ to None rather than deleting it.
2. Sets __file__ in the wrong dictionary (self.user_ns rather than where[0]).
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

No branches or pull requests

3 participants