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

ekolis
  • 6,270
  • 12
  • 50
  • 101
  • 1
    If it only happens sometimes: is it possible that elsewhere in your code you have called a variable `str`? – BoarGules Jan 19 '19 at 22:22
  • I don't see anywhere that I'm defining a variable called `str`... – ekolis Jan 20 '19 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. – BoarGules Jan 20 '19 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... – ekolis Jan 20 '19 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! – ekolis Jan 21 '19 at 03:16
  • Why do you want to `exec` anything *in* `__builtin__`? – Davis Herring Jan 21 '19 at 15:38
  • I want to put functions and variables in builtin so they can be accessed without a prefix in my scripts. – ekolis Jan 23 '19 at 03:09
  • If there is no problem when not processing a turn, then maybe there's a problem in that part of the code. Could you share it? – Tobias Brösamle Jan 29 '19 at 09:30
  • Unfortunately the ProcessTurn method is very long... – ekolis Jan 29 '19 at 13:29
  • a workaround would be to use `'Mines {} minerals each turn (modified by planet value).'.format(Amount1)` – Jean-François Fabre Jan 29 '19 at 20:12

1 Answers1

3

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.

Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
  • Wait is it `__builtin__` or `__builtins__`? Maybe that's screwing me up somehow, if I spelled it wrong... – ekolis Jan 29 '19 at 23:52
  • 1
    no, both are related on python 2.7. See https://stackoverflow.com/questions/11181519/python-whats-the-difference-between-builtin-and-builtins. One is the dict of the other. But those lines are useless when not interactive. – Jean-François Fabre Jan 30 '19 at 08:01
  • 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... – ekolis Jan 31 '19 at 03:27
  • 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"`... – ekolis Jan 31 '19 at 03:32
  • check my last edit, you can do that all right. and thanks for the bounty, it's my first! – Jean-François Fabre Jan 31 '19 at 09:15
  • Assigning to `str` the value of `"".__class__` did the trick - thanks! :D – ekolis Feb 02 '19 at 01:12
  • that's great! the class name had been lost ut the class itself still existed in the instances. Glad you got it solved. – Jean-François Fabre Feb 02 '19 at 08:22