Using kwargs with format() (which points to a join() operation) causes an index error
Using kwargs and join() with format() fails in IronPython. The following code fails on IronPython 2.7.5 but runs fine on CPython 2.7.9. If I change it to use positional arguments it works fine,
The script with kwargs.
strings = set()
strings.update({"One"})
strings.update({"Two"})
strings.update({"Three"})
strings.update({"Four"})
print '({args})'.format(args = (', '.join(strings)))
Running using kwargs.
PS C:\Users\\desktop> ipy test.py
Traceback (most recent call last):
File "test.py", line 7, in <module>
IndexError: Index was outside the bounds of the array.
PS C:\Users\\desktop> python test.py
(Four, Three, Two, One)
The script with positional args.
strings = set()
strings.update({"One"})
strings.update({"Two"})
strings.update({"Three"})
strings.update({"Four"})
print '({0})'.format((', '.join(strings)))
Running with positional args.
PS C:\Users\\desktop> ipy test.py
(One, Three, Two, Four)
PS C:\Users\\desktop> python test.py
(Four, Three, Two, One)
No need for .join, just using kwargs at all has the same issue:
IronPython 2.7.6a0 DEBUG (2.7.6.0) on .NET 4.0.30319.34209 (32-bit)
Type "help", "copyright", "credits" or "license" for more information.
>>> '({args})'.format(args = 1)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: Index was outside the bounds of the array.
Specifically, it's if the kwarg is called 'args'. The call doesn't even get to the formatting code; it appears to be an issue with argument binding. Setting a first-chance exception on IndexOutOfRangeException will trap it.
FWIW kwargs and format_string also have issues, although different ones in each case. These are the parameter names for the StringOps.format method. There might be a more general problem here with binding kwargs and other parameter names. It's probably a case of needing the \u00F8 hack again but it would be nice to have a more general solution.