py-junos-eznc icon indicating copy to clipboard operation
py-junos-eznc copied to clipboard

StartShell UnboundLocalError: local variable 'data' referenced before assignment

Open ipmonk opened this issue 2 years ago • 2 comments

getting an exception when calling StartShell run() using PyEZ 2.6.4+. There is no data in the stdout buffer to read for several iterations of the loop...

#!/usr/bin/env python3
from jnpr.junos import Device
from jnpr.junos.utils.start_shell import StartShell

def try_run():
    dev = Device("foobar", user="foo", passwd="bar",port=22)
    with StartShell(dev) as ss:
        result = ss.run("hostname")
        print(result)
if __name__ == "__main__":
    try_run()
Traceback (most recent call last):
  File "repro.py", line 24, in <module>
    try_run()
  File "repro.py", line 21, in try_run
    result = ss.run("hostname")
  File "/Users/foobar/scripts/pyez_test_scripts/venv/lib/python3.6/site-packages/jnpr/junos/utils/start_shell.py", line 185, in run
    got = "".join(self.wait_for(this, timeout, sleep=sleep))
  File "/Users/foobar/scripts/pyez_test_scripts/venv/lib/python3.6/site-packages/jnpr/junos/utils/start_shell.py", line 82, in wait_for
    if isinstance(data, bytes):
UnboundLocalError: local variable 'data' referenced before assignment

Possible solution:

                data = b""       <<<< add this line
                rd, wr, err = select([chan], [], [], _SELECT_WAIT)
                if rd:
                    data = chan.recv(_RECVSZ)

ipmonk avatar Aug 26 '22 07:08 ipmonk

I was getting same error then I downgraded to version 2.6.3

SanjayPatel39 avatar Sep 01 '22 04:09 SanjayPatel39

Fix is under review https://github.com/Juniper/py-junos-eznc/pull/1207

chidanandpujar avatar Sep 02 '22 05:09 chidanandpujar

Hi,

Could you please share the steps reproduce this issue . issue is not reproducible with about script .

Thanks & Regards Chidanand

chidanandpujar avatar Nov 30 '22 10:11 chidanandpujar

Hi,

It reproduces in v2.6.4 no problem. It will not reproduce after the commit made on Sep 2nd for issue #1207.

(venv-3.10.4) foo@foo-mbp venv-3.10.4 % python3 foo.py
Traceback (most recent call last):
  File "/Users/foo/venv-3.10.4/foo.py", line 11, in <module>
    try_run()
  File "/Users/foo/venv-3.10.4/foo.py", line 7, in try_run
    with StartShell(dev) as ss:
  File "/Users/foo/venv-3.10.4/lib/python3.10/site-packages/jnpr/junos/utils/start_shell.py", line 203, in __enter__
    self.open()
  File "/Users/foo/venv-3.10.4/lib/python3.10/site-packages/jnpr/junos/utils/start_shell.py", line 129, in open
    self.wait_for(_SHELL_PROMPT)
  File "/Users/foo/venv-3.10.4/lib/python3.10/site-packages/jnpr/junos/utils/start_shell.py", line 82, in wait_for
    if isinstance(data, bytes):
UnboundLocalError: local variable 'data' referenced before assignment

(venv-3.10.4) foo@foo-mbp venv-3.10.4 % python3 -m pip install git+https://github.com/Juniper/py-junos-eznc.git@a0c0bac20c10dbd34453a67046e95a265f4e5517
Collecting git+https://github.com/Juniper/py-junos-eznc.git@a0c0bac20c10dbd34453a67046e95a265f4e5517
  Cloning https://github.com/Juniper/py-junos-eznc.git (to revision a0c0bac20c10dbd34453a67046e95a265f4e5517) to /private/var/folders/4_/tjz6vmrj5g7b7dk0xlfr65qc0000gp/T/pip-req-build-r6eiknxl
  Running command git clone --filter=blob:none --quiet https://github.com/Juniper/py-junos-eznc.git /private/var/folders/4_/tjz6vmrj5g7b7dk0xlfr65qc0000gp/T/pip-req-build-r6eiknxl
  Running command git rev-parse -q --verify 'sha^a0c0bac20c10dbd34453a67046e95a265f4e5517'
  Running command git fetch -q https://github.com/Juniper/py-junos-eznc.git a0c0bac20c10dbd34453a67046e95a265f4e5517
  Running command git checkout -q a0c0bac20c10dbd34453a67046e95a265f4e5517
  Resolved https://github.com/Juniper/py-junos-eznc.git to commit a0c0bac20c10dbd34453a67046e95a265f4e5517
  Preparing metadata (setup.py) ... done
Requirement already satisfied: lxml>=3.2.4 in ./lib/python3.10/site-packages (from junos-eznc==2.6.5+7.ga0c0bac) (4.9.1)
Requirement already satisfied: ncclient==0.6.13 in ./lib/python3.10/site-packages (from junos-eznc==2.6.5+7.ga0c0bac) (0.6.13)
Requirement already satisfied: paramiko>=1.15.2 in ./lib/python3.10/site-packages (from junos-eznc==2.6.5+7.ga0c0bac) (2.11.0)
Requirement already satisfied: scp>=0.7.0 in ./lib/python3.10/site-packages (from junos-eznc==2.6.5+7.ga0c0bac) (0.14.4)
Requirement already satisfied: jinja2>=2.7.1 in ./lib/python3.10/site-packages (from junos-eznc==2.6.5+7.ga0c0bac) (3.1.2)
Requirement already satisfied: PyYAML>=5.1 in ./lib/python3.10/site-packages (from junos-eznc==2.6.5+7.ga0c0bac) (6.0)
Requirement already satisfied: netaddr in ./lib/python3.10/site-packages (from junos-eznc==2.6.5+7.ga0c0bac) (0.8.0)
Requirement already satisfied: six in ./lib/python3.10/site-packages (from junos-eznc==2.6.5+7.ga0c0bac) (1.16.0)
Requirement already satisfied: pyserial in ./lib/python3.10/site-packages (from junos-eznc==2.6.5+7.ga0c0bac) (3.5)
Requirement already satisfied: yamlordereddictloader in ./lib/python3.10/site-packages (from junos-eznc==2.6.5+7.ga0c0bac) (0.4.0)
Requirement already satisfied: pyparsing in ./lib/python3.10/site-packages (from junos-eznc==2.6.5+7.ga0c0bac) (3.0.9)
Requirement already satisfied: transitions in ./lib/python3.10/site-packages (from junos-eznc==2.6.5+7.ga0c0bac) (0.9.0)
Requirement already satisfied: setuptools>0.6 in ./lib/python3.10/site-packages (from ncclient==0.6.13->junos-eznc==2.6.5+7.ga0c0bac) (58.1.0)
Requirement already satisfied: MarkupSafe>=2.0 in ./lib/python3.10/site-packages (from jinja2>=2.7.1->junos-eznc==2.6.5+7.ga0c0bac) (2.1.1)
Requirement already satisfied: pynacl>=1.0.1 in ./lib/python3.10/site-packages (from paramiko>=1.15.2->junos-eznc==2.6.5+7.ga0c0bac) (1.5.0)
Requirement already satisfied: bcrypt>=3.1.3 in ./lib/python3.10/site-packages (from paramiko>=1.15.2->junos-eznc==2.6.5+7.ga0c0bac) (3.2.2)
Requirement already satisfied: cryptography>=2.5 in ./lib/python3.10/site-packages (from paramiko>=1.15.2->junos-eznc==2.6.5+7.ga0c0bac) (37.0.2)
Requirement already satisfied: cffi>=1.1 in ./lib/python3.10/site-packages (from bcrypt>=3.1.3->paramiko>=1.15.2->junos-eznc==2.6.5+7.ga0c0bac) (1.15.0)
Requirement already satisfied: pycparser in ./lib/python3.10/site-packages (from cffi>=1.1->bcrypt>=3.1.3->paramiko>=1.15.2->junos-eznc==2.6.5+7.ga0c0bac) (2.21)
Using legacy 'setup.py install' for junos-eznc, since package 'wheel' is not installed.
Installing collected packages: junos-eznc
  Attempting uninstall: junos-eznc
    Found existing installation: junos-eznc 2.6.4
    Uninstalling junos-eznc-2.6.4:
      Successfully uninstalled junos-eznc-2.6.4
  Running setup.py install for junos-eznc ... done
Successfully installed junos-eznc-2.6.5+7.ga0c0bac
WARNING: There was an error checking the latest version of pip.
(venv-3.10.4) foo@foo-mbp venv-3.10.4 % python3 foo.py
(True, 'hostname\r\r\nbatman-re0\r\n% ')

ipmonk avatar Nov 30 '22 11:11 ipmonk