Re: [PATCH 2/4] bbremote: Convert to python3
On Tue, Sep 06, 2022 at 03:02:34PM +0200, Jan Lübbe wrote: > On Tue, 2022-09-06 at 12:20 +0200, Sascha Hauer wrote: > > This is the long overdue conversion from python2 to python3. > > > > Signed-off-by: Sascha Hauer > > --- > > scripts/bbremote | 2 +- > > scripts/remote/controller.py | 21 +++-- > > scripts/remote/main.py | 8 > > scripts/remote/messages.py | 15 +-- > > scripts/remote/ratp.py | 4 ++-- > > scripts/remote/ratpfs.py | 4 ++-- > > scripts/remote/threadstdio.py | 4 ++-- > > 7 files changed, 35 insertions(+), 23 deletions(-) > > > > diff --git a/scripts/bbremote b/scripts/bbremote > > index bc5351dbae..1eeabd08d1 100755 > > --- a/scripts/bbremote > > +++ b/scripts/bbremote > > @@ -1,3 +1,3 @@ > > -#!/usr/bin/env python2 > > +#!/usr/bin/env python3 > > > > import remote.main > > diff --git a/scripts/remote/controller.py b/scripts/remote/controller.py > > index b4493591dd..a3ae260558 100644 > > --- a/scripts/remote/controller.py > > +++ b/scripts/remote/controller.py > > @@ -1,4 +1,4 @@ > > -#!/usr/bin/env python2 > > +#!/usr/bin/env python3 > > # -*- coding: utf-8 -*- > > > > from __future__ import absolute_import, division, print_function > > @@ -8,7 +8,7 @@ import logging > > import sys > > import os > > from threading import Thread > > -from Queue import Queue, Empty > > +from queue import Queue, Empty > > from .ratpfs import RatpFSServer > > from .messages import * > > from .ratp import RatpError > > @@ -105,7 +105,7 @@ class Controller(Thread): > > self.rxq = None > > self.conn.connect(timeout=5.0) > > self._txq = Queue() > > - self._stop = False > > + self._stopit = False > > Is this rename needed? Yes. It seems the Thread class now has a member _stop itself, see: https://github.com/python/cpython/blob/3.10/Lib/threading.py#L1028 Without this rename we get: Traceback (most recent call last): File "/ptx/work/WORK_EIHEI/sha/projects/barebox/barebox/./scripts/bbremote", line 3, in import remote.main File "/ptx/work/WORK_EIHEI/sha/projects/barebox/barebox/scripts/remote/main.py", line 281, in res = args.func(args) File "/ptx/work/WORK_EIHEI/sha/projects/barebox/barebox/scripts/remote/main.py", line 185, in handle_console ctrl.stop() File "/ptx/work/WORK_EIHEI/sha/projects/barebox/barebox/scripts/remote/controller.py", line 239, in stop self.join() File "/usr/lib/python3.9/threading.py", line 1033, in join self._wait_for_tstate_lock() File "/usr/lib/python3.9/threading.py", line 1051, in _wait_for_tstate_lock self._stop() TypeError: 'bool' object is not callable > > > > def _unpack_payload(self, payload): > > + assert isinstance(payload, bytes) > > self.cmd = payload > > > > def _pack_payload(self): > > @@ -94,6 +97,8 @@ class BBPacketCommandReturn(BBPacket): > > > > class BBPacketConsoleMsg(BBPacket): > > def __init__(self, raw=None, text=None): > > + if text != None: > > Use 'if text is not None:'. Ok. > > +++ b/scripts/remote/ratpfs.py > > @@ -1,4 +1,4 @@ > > -#!/usr/bin/env python2 > > +#!/usr/bin/env python3 > > # -*- coding: utf-8 -*- > > > > from __future__ import absolute_import, division, print_function > > @@ -97,7 +97,7 @@ class RatpFSServer(object): > > os.O_TRUNC) > > path = params[4:] > > try: > > - f = os.open(self._resolve(path), flags, 0666) > > + f = os.open(self._resolve(path), flags, 0o666) > > This looks like an unrelated fix. It depends how you see it. This was accepted in python2, but no longer in python3, so this is a step to python3 conversion. Sascha -- Pengutronix e.K. | | Steuerwalder Str. 21 | http://www.pengutronix.de/ | 31137 Hildesheim, Germany | Phone: +49-5121-206917-0| Amtsgericht Hildesheim, HRA 2686 | Fax: +49-5121-206917- |
Re: [PATCH 2/4] bbremote: Convert to python3
On Tue, 2022-09-06 at 12:20 +0200, Sascha Hauer wrote: > This is the long overdue conversion from python2 to python3. > > Signed-off-by: Sascha Hauer > --- > scripts/bbremote | 2 +- > scripts/remote/controller.py | 21 +++-- > scripts/remote/main.py | 8 > scripts/remote/messages.py | 15 +-- > scripts/remote/ratp.py | 4 ++-- > scripts/remote/ratpfs.py | 4 ++-- > scripts/remote/threadstdio.py | 4 ++-- > 7 files changed, 35 insertions(+), 23 deletions(-) > > diff --git a/scripts/bbremote b/scripts/bbremote > index bc5351dbae..1eeabd08d1 100755 > --- a/scripts/bbremote > +++ b/scripts/bbremote > @@ -1,3 +1,3 @@ > -#!/usr/bin/env python2 > +#!/usr/bin/env python3 > > import remote.main > diff --git a/scripts/remote/controller.py b/scripts/remote/controller.py > index b4493591dd..a3ae260558 100644 > --- a/scripts/remote/controller.py > +++ b/scripts/remote/controller.py > @@ -1,4 +1,4 @@ > -#!/usr/bin/env python2 > +#!/usr/bin/env python3 > # -*- coding: utf-8 -*- > > from __future__ import absolute_import, division, print_function > @@ -8,7 +8,7 @@ import logging > import sys > import os > from threading import Thread > -from Queue import Queue, Empty > +from queue import Queue, Empty > from .ratpfs import RatpFSServer > from .messages import * > from .ratp import RatpError > @@ -105,7 +105,7 @@ class Controller(Thread): > self.rxq = None > self.conn.connect(timeout=5.0) > self._txq = Queue() > - self._stop = False > + self._stopit = False Is this rename needed? > self.fsserver = RatpFSServer() > > def _send(self, bbpkt): > @@ -147,24 +147,24 @@ class Controller(Thread): > return 0 > > def command(self, cmd): > - self._send(BBPacketCommand(cmd=cmd)) > + self._send(BBPacketCommand(cmd=cmd.encode())) > r = self._expect(BBPacketCommandReturn, timeout=None) > logging.info("Command: %r", r) > return r.exit_code > > def getenv(self, varname): > - self._send(BBPacketGetenv(varname=varname)) > + self._send(BBPacketGetenv(varname=varname.encode())) > r = self._expect(BBPacketGetenvReturn) > return r.text > > def md(self, path, addr, size): > - self._send(BBPacketMd(path=path, addr=addr, size=size)) > + self._send(BBPacketMd(path=path.encode(), addr=addr, size=size)) > r = self._expect(BBPacketMdReturn) > logging.info("Md return: %r", r) > return (r.exit_code,r.data) > > def mw(self, path, addr, data): > - self._send(BBPacketMw(path=path, addr=addr, data=data)) > + self._send(BBPacketMw(path=path.encode(), addr=addr, data=data)) > r = self._expect(BBPacketMwReturn) > logging.info("Mw return: %r", r) > return (r.exit_code,r.written) > @@ -208,7 +208,7 @@ class Controller(Thread): > def run(self): > assert self.rxq is not None > try: > - while not self._stop: > + while not self._stopit: > # receive > pkt = self.conn.recv() > if pkt: > @@ -235,15 +235,16 @@ class Controller(Thread): > Thread.start(self) > > def stop(self): > - self._stop = True > + self._stopit = True > self.join() > - self._stop = False > + self._stopit = False > self.rxq = None > > def send_async(self, pkt): > self._txq.put(pkt) > > def send_async_console(self, text): > + assert isinstance(text, bytes) > self._txq.put(BBPacketConsoleMsg(text=text)) > > def send_async_ping(self): > diff --git a/scripts/remote/main.py b/scripts/remote/main.py > index cef5d92ee2..b99dba180a 100644 > --- a/scripts/remote/main.py > +++ b/scripts/remote/main.py > @@ -1,4 +1,4 @@ > -#!/usr/bin/env python2 > +#!/usr/bin/env python3 > > from __future__ import absolute_import, division, print_function > > @@ -7,7 +7,7 @@ import os > import argparse > import binascii > import logging > -from Queue import Queue > +from queue import Queue > from .ratp import RatpError > > try: > @@ -162,8 +162,8 @@ def handle_console(args): > ctrl = get_controller(args) > ctrl.export(args.export) > ctrl.start(queue) > - ctrl.send_async_console('\r') > - cons = ConsoleInput(queue, exit='\x14') # CTRL-T > + ctrl.send_async_console(b'\r') > + cons = ConsoleInput(queue, exit=b'\x14') # CTRL-T > cons.start() > try: > while True: > diff --git a/scripts/remote/messages.py b/scripts/remote/messages.py > index abd331c8b6..ae36189d4c 100644 > --- a/scripts/remote/messages.py > +++ b/scripts/remote/messages.py > @@ -1,4 +1,4 @@ > -#!/usr/bin/env python2 > +#!/usr/bin/env python3 > # -*- coding: utf-8 -*- > > from __future__ import absolute_import, division, print_function > @@ -41,12 +41,13 @@ class
[PATCH 2/4] bbremote: Convert to python3
This is the long overdue conversion from python2 to python3. Signed-off-by: Sascha Hauer --- scripts/bbremote | 2 +- scripts/remote/controller.py | 21 +++-- scripts/remote/main.py| 8 scripts/remote/messages.py| 15 +-- scripts/remote/ratp.py| 4 ++-- scripts/remote/ratpfs.py | 4 ++-- scripts/remote/threadstdio.py | 4 ++-- 7 files changed, 35 insertions(+), 23 deletions(-) diff --git a/scripts/bbremote b/scripts/bbremote index bc5351dbae..1eeabd08d1 100755 --- a/scripts/bbremote +++ b/scripts/bbremote @@ -1,3 +1,3 @@ -#!/usr/bin/env python2 +#!/usr/bin/env python3 import remote.main diff --git a/scripts/remote/controller.py b/scripts/remote/controller.py index b4493591dd..a3ae260558 100644 --- a/scripts/remote/controller.py +++ b/scripts/remote/controller.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python2 +#!/usr/bin/env python3 # -*- coding: utf-8 -*- from __future__ import absolute_import, division, print_function @@ -8,7 +8,7 @@ import logging import sys import os from threading import Thread -from Queue import Queue, Empty +from queue import Queue, Empty from .ratpfs import RatpFSServer from .messages import * from .ratp import RatpError @@ -105,7 +105,7 @@ class Controller(Thread): self.rxq = None self.conn.connect(timeout=5.0) self._txq = Queue() -self._stop = False +self._stopit = False self.fsserver = RatpFSServer() def _send(self, bbpkt): @@ -147,24 +147,24 @@ class Controller(Thread): return 0 def command(self, cmd): -self._send(BBPacketCommand(cmd=cmd)) +self._send(BBPacketCommand(cmd=cmd.encode())) r = self._expect(BBPacketCommandReturn, timeout=None) logging.info("Command: %r", r) return r.exit_code def getenv(self, varname): -self._send(BBPacketGetenv(varname=varname)) +self._send(BBPacketGetenv(varname=varname.encode())) r = self._expect(BBPacketGetenvReturn) return r.text def md(self, path, addr, size): -self._send(BBPacketMd(path=path, addr=addr, size=size)) +self._send(BBPacketMd(path=path.encode(), addr=addr, size=size)) r = self._expect(BBPacketMdReturn) logging.info("Md return: %r", r) return (r.exit_code,r.data) def mw(self, path, addr, data): -self._send(BBPacketMw(path=path, addr=addr, data=data)) +self._send(BBPacketMw(path=path.encode(), addr=addr, data=data)) r = self._expect(BBPacketMwReturn) logging.info("Mw return: %r", r) return (r.exit_code,r.written) @@ -208,7 +208,7 @@ class Controller(Thread): def run(self): assert self.rxq is not None try: -while not self._stop: +while not self._stopit: # receive pkt = self.conn.recv() if pkt: @@ -235,15 +235,16 @@ class Controller(Thread): Thread.start(self) def stop(self): -self._stop = True +self._stopit = True self.join() -self._stop = False +self._stopit = False self.rxq = None def send_async(self, pkt): self._txq.put(pkt) def send_async_console(self, text): +assert isinstance(text, bytes) self._txq.put(BBPacketConsoleMsg(text=text)) def send_async_ping(self): diff --git a/scripts/remote/main.py b/scripts/remote/main.py index cef5d92ee2..b99dba180a 100644 --- a/scripts/remote/main.py +++ b/scripts/remote/main.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python2 +#!/usr/bin/env python3 from __future__ import absolute_import, division, print_function @@ -7,7 +7,7 @@ import os import argparse import binascii import logging -from Queue import Queue +from queue import Queue from .ratp import RatpError try: @@ -162,8 +162,8 @@ def handle_console(args): ctrl = get_controller(args) ctrl.export(args.export) ctrl.start(queue) -ctrl.send_async_console('\r') -cons = ConsoleInput(queue, exit='\x14') # CTRL-T +ctrl.send_async_console(b'\r') +cons = ConsoleInput(queue, exit=b'\x14') # CTRL-T cons.start() try: while True: diff --git a/scripts/remote/messages.py b/scripts/remote/messages.py index abd331c8b6..ae36189d4c 100644 --- a/scripts/remote/messages.py +++ b/scripts/remote/messages.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python2 +#!/usr/bin/env python3 # -*- coding: utf-8 -*- from __future__ import absolute_import, division, print_function @@ -41,12 +41,13 @@ class BBPacket(object): if raw is not None: self.unpack(raw) else: -self.payload = payload +self.payload = payload.encode() def __repr__(self): return "BBPacket(%i, %i)" % (self.p_type, self.p_flags) def _unpack_payload(self, data): +assert isinstance(data, bytes) self.payload = data