Js2Py icon indicating copy to clipboard operation
Js2Py copied to clipboard

Title: KeyError: 3 in `append_arguments` function of `utils/injector.py`

Open Foadsf opened this issue 1 year ago • 7 comments

Description:

I encountered a KeyError while trying to use pipwin to install the pyppf package. The error seems to originate from the js2py package, specifically in the append_arguments function of the utils/injector.py module.

Traceback:

Traceback (most recent call last):
  File "<frozen runpy>", line 198, in _run_module_as_main
  File "<frozen runpy>", line 88, in _run_code
  File "C:\Users\foobar\AppData\Roaming\Python\Python312\Scripts\pipwin.exe\__main__.py", line 4, in <module>
  File "C:\Users\foobar\AppData\Roaming\Python\Python312\site-packages\pipwin\command.py", line 28, in <module>
    from . import pipwin, __version__
  File "C:\Users\foobar\AppData\Roaming\Python\Python312\site-packages\pipwin\pipwin.py", line 13, in <module>
    import js2py
  File "C:\Users\foobar\AppData\Roaming\Python\Python312\site-packages\js2py\__init__.py", line 72, in <module>
    from .base import PyJsException
  File "C:\Users\foobar\AppData\Roaming\Python\Python312\site-packages\js2py\base.py", line 2965, in <module>
    @Js
     ^^
  File "C:\Users\foobar\AppData\Roaming\Python\Python312\site-packages\js2py\base.py", line 165, in Js
    return PyJsFunction(val, FunctionPrototype)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\foobar\AppData\Roaming\Python\Python312\site-packages\js2py\base.py", line 1377, in __init__
    cand = fix_js_args(func)
           ^^^^^^^^^^^^^^^^^
  File "C:\Users\foobar\AppData\Roaming\Python\Python312\site-packages\js2py\utils\injector.py", line 27, in fix_js_args
    code = append_arguments(six.get_function_code(func), ('this', 'arguments'))
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\foobar\AppData\Roaming\Python\Python312\site-packages\js2py\utils\injector.py", line 121, in append_arguments
    arg = name_translations[inst.arg]
          ~~~~~~~~~~~~~~~~~^^^^^^^^^^
KeyError: 3

Steps to Reproduce:

  1. Install pipwin using pip install pipwin.
  2. Run pipwin install pyppf.

Environment:

  • Operating System: Windows 10
  • Python Version: 3.12
  • js2py Version: 0.71 (latest as of the issue date)
  • pipwin Version: Latest available

Additional Information:

The error occurs because the name_translations dictionary does not contain the key 3. This leads to a KeyError in the append_arguments function of the utils/injector.py module.

Potential Fix:

A possible fix would be to handle the missing key scenario in the append_arguments function. For instance:

def append_arguments(code, names):
    # existing code ...
    try:
        arg = name_translations[inst.arg]
    except KeyError:
        continue  # or handle the missing key appropriately
    # remaining code ...

I hope this information helps in resolving the issue. Thank you for your assistance!

Foadsf avatar May 30 '24 21:05 Foadsf