3

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

10
  • 1
    If it only happens sometimes: is it possible that elsewhere in your code you have called a variable str? Commented Jan 19, 2019 at 22:22
  • I don't see anywhere that I'm defining a variable called str... Commented Jan 20, 2019 at 14:55
  • 1
    The only way I can reproduce your message in NameError: name 'str' is not defined in CPython 3 is to attempt del(str), which fails with that message. But IronPython isn't CPython so that may not mean much. Clearly something strange is going on in the mods to __builtins__, but that isn't telling you anything you didn't already suspect. Commented Jan 20, 2019 at 15:22
  • Yeah, if I remove the first four line which aren't really doing anything, then the script works. But that won't work if I have actual code to import into builtins... Commented Jan 20, 2019 at 15:49
  • Hmm, still broken if I do the following steps: 1. start a new single player game, 2. process a turn, 3. open planet report - doesn't happen if I load a game without processing a turn! Commented Jan 21, 2019 at 3:16

1 Answer 1

3
+50

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.

Sign up to request clarification or add additional context in comments.

7 Comments

Wait is it __builtin__ or __builtins__? Maybe that's screwing me up somehow, if I spelled it wrong...
no, both are related on python 2.7. See stackoverflow.com/questions/11181519/…. One is the dict of the other. But those lines are useless when not interactive.
Hmm, I tried getting str from the __builtins__ and I got an error saying __builtins__ was not defined! Tried it also with __builtin__ and got a similar error...
As for the format call, that's going to be tricky because I'm generating the script from a string interpolation sort of thing, e.g. "Mines {Amount1} minerals per turn"...
check my last edit, you can do that all right. and thanks for the bounty, it's my first!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.