dlenski / python-zxing

python wrapper for the ZXing barcode library
GNU Lesser General Public License v3.0
121 stars 36 forks source link

UnboundLocalError: local variable 'bc' referenced before assignment #29

Closed zeusin closed 4 months ago

zeusin commented 7 months ago

Hello,

I have installed zxing through pip. this version is installed:

$zxing -V
zxing v1.0
using Java ZXing library version v3.5.1

I have different output running the command from ssh and from graphical.... If I run the command in gnome-terminal I get this output:

$zxing /alb0001.tiff 
/alb0001.tiff
=============
Traceback (most recent call last):
  File "/home/user/.local/bin/zxing", line 8, in <module>
    sys.exit(main())
  File "/home/user/.local/lib/python3.9/site-packages/zxing/__main__.py", line 59, in main
    if not bc:
UnboundLocalError: local variable 'bc' referenced before assignment

but running the same command from ssh on another server, or even running ssh in same server with 127.0.0.1 ( and even in the same gnome-terminal that was not working previously ) is working....

$ssh user@127.0.0.1 'zxing /alb0001.tiff'
user@127.0.0.1's password: 
/alb0001.tiff
=============
  Decoded TEXT barcode in PDF_417 format.
  Raw text:    '1234|000'
  Parsed text: '1234|000'

Host is almalinux 9.

Is there anything I can do to get the command running from a graphical terminal?

zeusin commented 7 months ago

Hello,

I tried to debug with a friend what is happening... It seems the function "bcr.decode" is not working properly when running on terminal on a graphical environment.

But we don't know how to make it work.

We added a print behind bc definition:

            print("**",bcr.decode(ff))
            bc = bcr.decode(ff, try_harder=args.try_harder)

this is working through ssh:

zxing /home/user/alb0008.tiff
** BarCode(raw='1234|000', parsed='1234|000', path='/home/user/alb0008.tiff', format='PDF_417', type='TEXT', points=[(2425.0, 814.0), (2426.0, 925.0), (2914.0, 816.0), (2914.0, 926.0), (2494.0, 814.0), (2495.0, 925.0), (2840.0, 816.0), (2841.0, 926.0)])
/home/user/alb0008.tiff
======================
  Decoded TEXT barcode in PDF_417 format.
  Raw text:    '1234|000'
  Parsed text: '1234|000'

and this output is through graphical:

zxing /home/user/alb0008.tiff 
/home/user/alb0008.tiff
======================
Traceback (most recent call last):
  File "/usr/local/bin/zxing", line 8, in <module>
    sys.exit(main())
             ^^^^^^
  File "/usr/local/lib/python3.11/site-packages/zxing/__main__.py", line 66, in main
    if not bc:
           ^^
UnboundLocalError: cannot access local variable 'bc' where it is not associated with a value

line 66 is not real, because we added more comments to the code

dlenski commented 7 months ago

I have different output running the command from ssh and from graphical....

:raised_eyebrow: Are you sure you are running the same version of ZXing both via ssh and via the local "graphical" terminal? I'm skeptical.

I'm guessing that in at least one place, you're running a version of python-zxing that does not include the bugfix in 980a045444b2d798d7fb48acbc4a4c3226fa0552.

zeusin commented 7 months ago

Hello, thank you for taking your time to look over my question.

After lot of changes I'm going to uninstall everything and start from zero:

#pip3.11 uninstall zxing
Found existing installation: zxing 1.0
Uninstalling zxing-1.0:
  Would remove:
    /usr/local/bin/zxing
    /usr/local/lib/python3.11/site-packages/zxing-1.0.dist-info/*
    /usr/local/lib/python3.11/site-packages/zxing/*
Proceed (Y/n)? y
  Successfully uninstalled zxing-1.0

# locate zxing
/usr/local/bin/zxing
/usr/local/lib/python3.11/site-packages/zxing
/usr/local/lib/python3.11/site-packages/zxing-1.0.dist-info
# updatedb 
# locate zxing
# pip3.11 cache purge
Files removed: 3

Now I'm going to install it again with python 3.11. When issue was open, zxing was installed with python3.9

# pip3.11 install zxing --proxy http://192.168.x.x:8080
Collecting zxing
  Downloading zxing-1.0-py3-none-any.whl (680 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 680.1/680.1 kB 3.5 MB/s eta 0:00:00
Installing collected packages: zxing
Successfully installed zxing-1.0

now let's see line 52 in main.py:

    48          else:
    49              ff = fn
    50  
    51          try:
    52              bc = bcr.decode(ff, try_harder=args.try_harder)
    53          except BarCodeReaderException as e:
    54              p.error(e.message + ((': ' + e.filename) if e.filename else '') + (('\n\tCaused by: ' + repr(e.__cause__) if e.__cause__ else '')))

there is no "bc=none", so I added it in line 52 and running zxing command through ssh and graphical terminal

so:

    48          else:
    49              ff = fn
    50  
    51          bc = None
    52          try:
    53              bc = bcr.decode(ff, try_harder=args.try_harder)
    54          except BarCodeReaderException as e:

ssh:

# zxing /alb0001.tiff 
/alb0001.tiff
=============
  Decoded TEXT barcode in PDF_417 format.
  Raw text:    '1234|000'
  Parsed text: '1234|000'

gnome-terminal:

# zxing /alb0001.tiff 
/alb0001.tiff
=============
  ERROR: Failed to decode barcode (using Java ZXing library v3.5.1).

I think is failing to decode barcode because "bcr.decode" function is not working in graphical... ( test made in answer 2 )

EDIT: add "print("**",bcr.decode(ff))" to test:

ssh:

#zxing /alb0001.tiff 
** BarCode(raw='1234|000', parsed='1234|000', path='/alb0001.tiff', format='PDF_417', type='TEXT', points=[(1372.0, 791.0), (1372.0, 901.0), (1860.0, 794.0), (1861.0, 902.0), (1441.0, 791.0), (1442.0, 901.0), (1787.0, 794.0), (1787.0, 902.0)])
/alb0001.tiff
=============
  Decoded TEXT barcode in PDF_417 format.
  Raw text:    '1234|000'
  Parsed text: '1234|000'

gnome-terminal

# zxing /alb0001.tiff 
/alb0001.tiff
=============
  ERROR: Failed to decode barcode (using Java ZXing library v3.5.1).
zeusin commented 7 months ago

Ok, I think I have found the problem:

Trying messing with python in graphical terminal:

>>> barcode = reader.decode("/alb0001.tiff")
RuntimeError: b'Exception in thread "main" java.lang.UnsatisfiedLinkError: Can\'t load library: /usr/lib/jvm/java-11-openjdk-11.0.16.1.1-1.el9_0.x86_64/lib/libawt_xawt.so\n\tat java.base/java.lang.ClassLoader.loadLibrary(ClassLoader.java:2633)\n\tat java.base/java.lang.Runtime.load0(Runtime.java:768)\n\tat java.base/java.lang.System.load(System.java:1837)\n\tat java.base/java.lang.ClassLoader$NativeLibrary.load0(Native Method)\n\tat java.base/java.lang.ClassLoader$NativeLibrary.load(ClassLoader.java:2445)\n\tat java.base/java.lang.ClassLoader$NativeLibrary.loadLibrary(ClassLoader.java:2501)\n\tat java.base/java.lang.ClassLoader.loadLibrary0(ClassLoader.java:2700)\n\tat java.base/java.lang.ClassLoader.loadLibrary(ClassLoader.java:2651)\n\tat java.base/java.lang.Runtime.loadLibrary0(Runtime.java:830)\n\tat java.base/java.lang.System.loadLibrary(System.java:1873)\n\tat java.desktop/java.awt.Toolkit$3.run(Toolkit.java:1399)\n\tat java.desktop/java.awt.Toolkit$3.run(Toolkit.java:1397)\n\tat java.base/java.security.AccessController.doPrivileged(Native Method)\n\tat java.desktop/java.awt.Toolkit.loadLibraries(Toolkit.java:1396)\n\tat java.desktop/java.awt.Toolkit.<clinit>(Toolkit.java:1429)\n\tat java.desktop/sun.awt.AppContext$2.run(AppContext.java:282)\n\tat java.desktop/sun.awt.AppContext$2.run(AppContext.java:271)\n\tat java.base/java.security.AccessController.doPrivileged(Native Method)\n\tat java.desktop/sun.awt.AppContext.initMainAppContext(AppContext.java:271)\n\tat java.desktop/sun.awt.AppContext$3.run(AppContext.java:326)\n\tat java.desktop/sun.awt.AppContext$3.run(AppContext.java:309)\n\tat java.base/java.security.AccessController.doPrivileged(Native Method)\n\tat java.desktop/sun.awt.AppContext.getAppContext(AppContext.java:308)\n\tat java.desktop/javax.imageio.spi.IIORegistry.getDefaultInstance(IIORegistry.java:129)\n\tat java.desktop/javax.imageio.ImageIO.<clinit>(ImageIO.java:66)\n\tat com.google.zxing.client.j2se.ImageReader.readImage(ImageReader.java:44)\n\tat com.google.zxing.client.j2se.DecodeWorker.decode(DecodeWorker.java:125)\n\tat com.google.zxing.client.j2se.DecodeWorker.call(DecodeWorker.java:76)\n\tat com.google.zxing.client.j2se.CommandLineRunner.main(CommandLineRunner.java:99)\n'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.11/site-packages/zxing/__init__.py", line 129, in decode
    raise BarCodeReaderException("Unknown Java exception", self.java) from RuntimeError(stdout)
zxing.BarCodeReaderException: ('Unknown Java exception', 'java')

After this error I upgrade "java-11-openjdk" and now is working both ssh and gnome-terminal