On 2016-06-09, Brett Cannon wrote:
> On Thu, 9 Jun 2016 at 14:56 Nick Coghlan <ncogh...@gmail.com> wrote:
> > Once you switch to those now recommended more conservative migration
> > tools, the tool suite you request already exists:
> >
> > - update your code with modernize or futurize
> > - check it still runs on Python 2.7
> > - check it doesn't generate warnings under 2.7's "-3" switch
> > - check it passes "pylint --py3k"
> > - check if it runs on Python 3.5
> >
> 
> `python3.5 -bb` is best to help keep Python 2.7 compatibility, otherwise
> what Nick said. :)

I have to wonder if you guys actually ported at lot of Python 2
code.  Maybe you somehow avoided the problematic behavior. Below is
a pretty trival set of functions.  The tools you recommend do not
help at all.  One problem is that the str literals should be bytes
literals.  Comparison with None needs to be avoided.

With Python 2 code runs successfully.  With Python 3 the code
crashes with a traceback.  With my modified Python 3.6, the code
runs successfully but generates the following warnings:

    test.py:13: DeprecationWarning: encoding bytes to str
      output.write('%d:' % len(s))
    test.py:14: DeprecationWarning: encoding bytes to str
      output.write(s)
    test.py:15: DeprecationWarning: encoding bytes to str
      output.write(',')
    test.py:5: DeprecationWarning: encoding bytes to str
      if c == ':':
    test.py:9: DeprecationWarning: encoding bytes to str
      size += c
    test.py:24: DeprecationWarning: encoding bytes to str
      data = data + s
    test.py:26: DeprecationWarning: encoding bytes to str
      if input.read(1) != ',':
    test.py:31: DeprecationWarning: default compare is depreciated
      if a > 0:

It is very easy for me to find code written for Python 2 that will
fail in the same way.  According to you guys, there is no problem
and we already have good enough tooling. ;-(
def ns_read_size(input):
    size = ''
    while 1:
        c = input.read(1)
        if c == ':':
            break
        elif not c:
            raise IOError('short netstring read')
        size += c
    return int(size)

def ns_write_string(s, output):
    output.write('%d:' % len(s))
    output.write(s)
    output.write(',')

def ns_read_string(input):
    size = ns_read_size(input)
    data = ''
    while size > 0:
        s = input.read(size)
        if not s:
            raise IOError('short netstring read')
        data = data + s
        size -= len(s)
    if input.read(1) != ',':
        raise IOError('missing netstring terminator')
    return data

def compare(a, b):
    if a > 0:
        return b + 10
    return 0

def main():
    import tempfile
    out = tempfile.TemporaryFile()
    ns_write_string('Hello world', out)
    out.seek(0)
    s = ns_read_string(out)
    if s != 'Hello world':
        print('Failed')
    else:
        print('Ok')
    if (compare(None, 5) == 0 and
            compare(1, 5) == 15):
        print('Ok')
    else:
        print('Failed')

if __name__ == '__main__':
    main()
_______________________________________________
Python-Dev mailing list
Python-Dev@python.org
https://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
https://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com

Reply via email to