I'm trying to run an IronPython script in C# but sometimes when I run it I get this error:
IronPython.Runtime.UnboundNameException: name 'str' is not defined
I can't figure out why this is happening; it only happens sometimes; right now it happens when I click a planet in my game to view the report on its abilities.
Here's the script I'm trying to run:
'Mines ' + str(Amount1) + ' minerals each turn (modified by planet value).'
As you can see I'm trying to concatenate some strings, one of which is a stringified Amount1 variable (Amount1 I think is the number 800 but it might be a more complex expression equal to 800, I'd have to check on that), but for some reason I'm getting an error saying the str function is undefined, which makes no sense!
There are other scripts being included before this script runs; if you like I can find those and paste them here in case that might help...
edit: here's the full script that I'm running:
import sys, imp;
import __builtin__ as builtins;
builtins_code = """""";
exec builtins_code in builtins.__dict__;
import clr
clr.AddReference('System.Core')
import System
clr.ImportExtensions(System.Linq)
clr.AddReference('FrEee.Core')
import FrEee
import FrEee.Utility
clr.ImportExtensions(FrEee.Utility.Extensions)
from FrEee.Modding import Mod
from FrEee.Game.Objects.Space import Galaxy
from FrEee.Game.Objects.Civilization import Empire
'Mines ' + str(Amount1) + ' minerals each turn (modified by planet value).'
Could the exec builtins_code line be deleting the str function? If so, how can I make sure that builtins_code gets added to builtins rather than replacing it?
edit2: nope, even if I remove the first four lines, it crashes when I process a turn in a single player game and then click my homeworld!
edit3: if I put the script in a text file and run it as a watch (see below) it runs just fine, even on the breakpoint where it crashes:
FrEee.Modding.ScriptEngine.EvaluateExpression<string>(System.IO.File.ReadAllText("c:/users/edkol/desktop/test.py")), ac
The line:
exec builtins_code in builtins.__dict__
just adds _ to the interactive environment so it repeats the last evaluated variable. Without that line, _ isn't defined. Note that this feature isn't very useful in a script and could be removed. This seems unrelated to your issue.
It looks like the sole interest of IronPython is to be able to interact easily with Microsoft/C# stuff, but it remains "stable" in 2.7 version and it's probably not as bug-free as the official python versions (similar bug was encountered and not solved on reddit, not that it helps, but you know you're not the only one...).
It has to be a bug because you cannot delete a builtin when it's not overridden:
>>> str
<type 'str'>
>>> del str
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'str' is not defined
Now let's be pragmatic: there's no way someone is going to fix that bug in the near future.
So you could try to "restore" the str name by doing:
str = __builtins__.get('str')
I didn't test this, maybe it's not going to work either once you're in this strange state, but there are other workarounds to get hold of "lost" class names, using an instance and __class__ attribute:
>>> "".__class__(34) # "".__class__ is str
'34'
>>> 0 .__class__(1.4) # note the space to help the parser :)
1
The only problem is that is going to look weird in your code. Don't forget to comment!
Anyway, you don't really need str for this, you can workaround your issue by using str.format on a format string (positional):
'Mines {} minerals each turn (modified by planet value).'.format(Amount1)
or by keyword:
'Mines {Amount1} minerals each turn (modified by planet value).'.format(Amount1=Amount1)
That method doesn't involve str built-in. Not saying that you're not going to have other problems, but even when str is available, this is one prefered way to format a string.
Work your way out by avoiding such errors. Fortunately, python exceptions are explicit and can be catched with a nice traceback.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With