Reputation: 2244
Here's my code exactly as it appears (I'm working on a Numerical Analysis homework problem):
import integration as I
from math import *
import numpy as np
problem = 4
<snip/>
elif problem == 4:
h = 0.001
X = np.linspace(0,1, 1//h+2)
def f(x):
if x==0 or x==1:
return 0
try:
return x**3/( (1-x)**5 * (e**(x/(1-x))-1) )
except OverflowError:
return 0
try:
Y = list(map(f,X))
except:
print("That failed.")
Y = list(map(f,X))
print("Integral: %s"%I.Simpsons_L(Y, h))
Here's the output to shell:
>>>
That failed.
Integral: 6.49393940227
>>>
If I don't have the except
clause in there, then this is the error I get:
Traceback (most recent call last):
File "<snip/>", line 150, in <module>
Y = list(map(f,X))
File "<snip/>", line 145, in f
return x**3/( (1-x)**5 * (e**(x/(1-x))-1) )
File "C:\Python33\lib\idlelib\PyShell.py", line 60, in idle_showwarning
file.write(warnings.formatwarning(message, category, filename,
AttributeError: 'NoneType' object has no attribute 'write'
Edit 1
If I run my program from the command line, this is what I get:
NA_homework3problems.py:145: RuntimeWarning: overflow encountered in double_scalars
return x**3/( (1-x)**5 * (e**(x/(1-x))-1) )
Clearly, I have a workaround for my problem, so that's not why I'm asking this question. I'm asking why there's a problem in the first place. I am literally executing the same functions twice, and it works the second time but not the first. Also, this issue doesn't come up if I only have map
by itself, and if I attempt to use list
on the same map twice, the second one is empty, which I think is by design. So, what's going on here?
Upvotes: 1
Views: 862
Reputation: 2244
I did some more hunting and pinned down what causes the problem. Somehow, it is not my code, but rather numpy's. The following lines of code are sufficient to throw the error the first time but not the second.
>>> import numpy
>>> 1/numpy.float64(0)
Traceback (most recent call last):
File "<pyshell#84>", line 1, in <module>
1/numpy.float64(0)
File "C:\Python33\lib\idlelib\PyShell.py", line 64, in idle_showwarning
file.write(warnings.formatwarning(message, category, filename,
AttributeError: 'NoneType' object has no attribute 'write'
>>> 1/numpy.float64(0)
inf
Somehow, sys.__stderr__
gets set to None
. I have submitted a bug report for this issue: #5289. Hopefully it'll be fixed soon.
Upvotes: 0
Reputation: 304127
Something you've done previously has replaced the file
reference in pyshell with None
During your first run through the map, an exception and Warning is being generated
eg
RuntimeWarning: overflow encountered in double_scalars
The exception you are seeing is due to the attempt to print this warning failing.
The normal way warnings work is that once you've been alerted the warning is silenced for the remainder of the session
The second run also triggers the overflow, but since the warning is suppressed it doesn't try to print the error and so the pyshell error is also not encountered
You should look through your code and figure out what you are doing that's blowing away the file
variable in pyshell
Upvotes: 3