After some struggle, I managed to port the Baofeng UV-B5 and TYT TH-350 (subclass of UV-B5) drivers to Python 3, along with various other fixes to the codebase. Attached is the patch.
Comments below:
# HG changeset patch # User Zhaofeng Li hello@zhaofeng.li # Date 1549691138 28800 # Fri Feb 08 21:45:38 2019 -0800 # Branch py3 # Node ID 838216796b5a7e014674af9b16751cec87fb73e9 # Parent 7d5cf3c3432794c5ea4b1d1d2cc65d6038d3a73b [py3] bitwise.py: Python 3-ize
Note that I've been using issue #495 to track all pure python3 conversions, as that is the earliest related issue I could find. I'll add it to your patches.
Also, this patch breaks the settings tests on uvb5 on python2, and I want to try to keep all of those working as much as possible as that is one of the indicators of when we'll be safe to merge with the main branch.
diff -r 7d5cf3c34327 chirp/bitwise.py --- a/chirp/bitwise.py Fri Feb 08 20:30:10 2019 -0800 +++ b/chirp/bitwise.py Sat Feb 09 08:18:14 2019 -0800 @@ -232,51 +232,51 @@
class arrayDataElement(DataElement):
<snip>
def get_raw(self):
return "".join([item.get_raw() for item in self.__items])
return b"".join([item.get_raw() for item in self.__items])
I don't think we can make this change because everywhere else that depends on this returning strings will break. Obviously there are other things in bitwise that need to get cleaned up like this as well.
How about we make the function take a bytes=False, which will control what it returns? It's not ideal, but we can put a comment there and use it later to track down things still calling it looking for strings. I think we also need to make sure that whatever it is iterating also returns bytes, because in the case of a string array this will try to join bytes and str. I can take a look at fixing that.
It would be nicer to be able to control bitwise's return values as a whole at parse-time, but that whole thing is a bit of a mess and I don't really want to mess with it.
# HG changeset patch # User Zhaofeng Li hello@zhaofeng.li # Date 1549691367 28800 # Fri Feb 08 21:49:27 2019 -0800 # Branch py3 # Node ID 3434f35e76cfc7dd481a46cc61b9c9c999d01e0f # Parent aa4dd87eb5d0640e715aa2e70387161138b26885 [py3] uvb5.py: 2to3 with manual fixes
<snip>
def get_memory(self, number): _mem, _nam = self._get_memobjs(number) mem = chirp_common.Memory() if isinstance(number, str): mem.number = self.SPECIALS[number] mem.extd_number = number else: mem.number = number
if _mem.freq.get_raw()[0] == "\xFF":
if _mem.freq.get_raw()[0] == 0xff:
This won't work on python2:
$ python -c 'print(b"\x00"[0] == 0)' False $ python3 -c 'print(b"\x00"[0] == 0)' True
This is one of the super frustrating parts of the conversion, because while 2.7 is supposed to be compatible with 3, these kinds of things will quietly introduce bugs :(
# HG changeset patch # User Zhaofeng Li hello@zhaofeng.li # Date 1549694726 28800 # Fri Feb 08 22:45:26 2019 -0800 # Branch py3 # Node ID d3ba01e9b6ef486bc7b66bb2d07602ff65f48124 # Parent 8d77855fe3ecb0503388d628014eac9a274d7822 [py3] th350.py: 2to3, with many manual fixes
This breaks a couple of tests on on the th350 driver for python2.
Okay, so I'm going to apply these:
[py3] chirp_common.py: Fix file saving [py3] memmap.py: Fix slicing [py3] memedit.py: Convert power to str
and hold off on:
[py3] bitwise.py: Python 3-ize [py3] uvb5.py: 2to3 with manual fixes [py3] uvb5.py: Adapt for new Serial [py3] th350.py: 2to3, with many manual fixes
for the moment. I haven't looked at what the problem is with the first bitwise patch, but I will, along with working on the get_raw(bytes=true) stuff.
Thanks!
--Dan