IronLanguages / ironpython3

Implementation of Python 3.x for .NET Framework that is built on top of the Dynamic Language Runtime.
Apache License 2.0
2.43k stars 284 forks source link

"Index was outside the bounds of the array" exception thrown when importing a class with hundreds of properties #1793

Closed Ridleh closed 2 months ago

Ridleh commented 2 months ago

Description

I have a python file called CardLookup that contains a class also called CardLookup that looks like this: image The class has over 300 variables in it that look like the above. This file was generated using a separate script.

In another file, I am trying to import this class and use a variable: image

However, I get an error whenever IronPython tries to import the class, " Index was outside the bounds of the array": image

I have similar files like CardLookup, where they only have one class inside that holds constants. Except none of them have over 300 variables. The second most is around 25 I think.

If I split the CardLookup class into smaller classes and import the one I need it works, so my hunch is the issue has something to deal with the number of variables a class has.

Steps to Reproduce

  1. Create a class inside of a python file
  2. Add over 300 properties to the class
  3. In another file, attempt to import the class directly i.e. From Class import Class

Expected behavior: The import should succeed

Actual behavior: An exception is thrown

Version Information

I am using IronPython version 3.4.0, on Windows 11. Im running IronPython inside of a Godot game

The output of sys.version is: 3.4.0 (3.4.0.1000) [.NETFramework,Version=v4.6.2 on Mono 6.12.0 ((no/5ce143a1) (64-bit)]

Ridleh commented 2 months ago

Got a different, hopefully more helpful, error this time image

slozier commented 2 months ago

Hmm, so far I have not had any luck in reproducing the issue. The second exception looks like it might be a Python error, something like this would produce the same exception:

class Test:
   a, b = 1, 2, 3

Any chance you could try to output the Python traceback? Might point to the issue. For example:

using IronPython.Hosting;
using Microsoft.Scripting.Hosting;

var engine = Python.CreateEngine();
try {
    engine.Execute("class Test:\n    a, b = 1, 2, 3");
}
catch (Exception e) {
    Console.WriteLine(engine.GetService<ExceptionOperations>().FormatException(e));
}
Ridleh commented 2 months ago

Any chance you could try to output the Python traceback? Might point to the issue

Yep, that helped me find the issue. One of the variable names had an apostrophe in it. After removing it I'm not getting either of the above issues anymore. Thanks for the help