airprint-generate icon indicating copy to clipboard operation
airprint-generate copied to clipboard

Script fails with Python 3.x when py-lxml installed

Open freultwah opened this issue 4 years ago • 4 comments

I stumbled upon a Python 2 to Python 3 conversion issue when trying to run the script with py-lxml installed.

# ./airprint-generate.py
Traceback (most recent call last):
  File "/root/./airprint-generate.py", line 279, in <module>
    apg.generate()
  File "/root/./airprint-generate.py", line 226, in generate
    tree.write(f, pretty_print=True, xml_declaration=True, encoding="UTF-8")
  File "src/lxml/etree.pyx", line 2057, in lxml.etree._ElementTree.write
  File "src/lxml/serializer.pxi", line 758, in lxml.etree._tofilelike
  File "src/lxml/etree.pyx", line 318, in lxml.etree._ExceptionContext._raise_if_stored
  File "src/lxml/serializer.pxi", line 682, in lxml.etree._FilelikeWriter.write
TypeError: write() argument must be str, not bytes

I fixed it by opening the file as binary:

--- airprint-generate.py.us.bak 2020-12-02 14:00:09.404088000 +0200
+++ airprint-generate.py        2020-12-02 14:05:06.222074000 +0200
@@ -220,7 +220,7 @@
                 if self.directory:
                     fname = os.path.join(self.directory, fname)
                 
-                f = open(fname, 'w')
+                f = open(fname, 'wb')
 
                 if etree:
                     tree.write(f, pretty_print=True, xml_declaration=True, encoding="UTF-8")

I could see no adverse effects when py-lxml was not installed.

freultwah avatar Dec 02 '20 12:12 freultwah

Same issue, and the fix above resolved it. Thanks!

abbsi avatar Dec 20 '20 11:12 abbsi

Ditto. Thanks!

jhayashi avatar Dec 27 '20 02:12 jhayashi

Same here. Is there any reason not to make this change globally? IOW, will it break something?

undrwater avatar Jan 05 '21 17:01 undrwater

pip3 uninstall lxml ./airprint-generate.py -d /etc/avahi/services/ ls /etc/avahi/services/

gearfox avatar Dec 09 '22 02:12 gearfox