On Mon, Jun 17, 2019 at 10:45 PM Igor Korot <ikoro...@gmail.com> wrote: > > Hi, Chris, > > On Mon, Jun 17, 2019 at 7:31 AM Chris Angelico <ros...@gmail.com> wrote: > > > > On Mon, Jun 17, 2019 at 10:15 PM Igor Korot <ikoro...@gmail.com> wrote: > > > > > > Hi, > > > Is there a place where there is a full list of incompatibilities between > > > python 2 and python 3 is available and how to fix them? > > > > > > I'm looking for a way to fix following code, which runs perfectly with > > > python 2 > > > (and make it work with both python 2 and python 3): > > > > > > if bytes[0:16].tostring() != '<some_string>': > > > > > > I know there are automated tools that can help you do the upgrade, but > > > automated tools can do only so much.... > > > > > > And I am not sure if they can just add python 3 code and include version > > > check. > > > > If "bytes" here refers to a byte string, and not to the actual type > > called "bytes" (the same as "str" in Py2), the way I'd do it is: > > > > if bytes[:16] != b"<some_string>": > > > > However, I have no idea what your .tostring() method is, since it > > doesn't seem to be a method on the Py2 str object, nor of the > > bytearray object (my next guess). So further details/context would be > > needed. > > bytes = array.array('B', open(path, "rb").read()) > count = len(bytes) > > This is where bytes come from - sorry about that. > > So, how do I write the code compatible with both python 2 and python 3 > in this case?
Where's the incompatibility coming from? I tried this very simple example, and it seems fine. Python 2.7.13 (default, Sep 26 2018, 18:42:22) [GCC 6.3.0 20170516] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import array >>> b = array.array("B", b"asdfqwerzxcv") >>> b[:4] array('B', [97, 115, 100, 102]) >>> b[:4].tostring() 'asdf' Python 3.9.0a0 (heads/master:19a1e1eb86, Jun 5 2019, 09:02:06) [GCC 6.3.0 20170516] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import array >>> b = array.array("B", b"asdfqwerzxcv") >>> b[:4] array('B', [97, 115, 100, 102]) >>> b[:4].tostring() b'asdf' Either way, tostring() is giving you back a byte string, which you can easily compare to any byte string literal eg b"test string". Can you show more code, and where the actual failure (exception or wrong result) is coming from? ChrisA -- https://mail.python.org/mailman/listinfo/python-list