Changes between Version 4 and Version 5 of Python3Support


Ignore:
Timestamp:
Aug 10, 2018, 2:50:00 PM (6 years ago)
Author:
sanjeet
Comment:

Added tables for py2p3 differences section

Legend:

Unmodified
Added
Removed
Modified
  • Python3Support

    v4 v5  
    9696 * Unicodes == Strings
    9797
     98{{{#!th style="background: #ddd"
     99'''Label'''
     100}}}
     101{{{#!th style="background: #ddd"
     102'''Python 2'''
     103}}}
     104{{{#!th style="background: #ddd"
     105'''Python 3'''
     106}}}
     107|-----------------------
     108{{{#!td
     109String:
     110}}}
     111{{{#!td
     112{{{
     113>>> 'sample'
     114'sample'
     115}}}
     116}}}
     117{{{#!td
     118{{{
     119>>> 'sample'
     120'sample'
     121}}}
     122}}}
     123|-----------------------
     124{{{#!td
     125Unicode:
     126}}}
     127{{{#!td
     128{{{
     129>>> 'sample'
     130u'sample'
     131}}}
     132}}}
     133{{{#!td
     134{{{
     135>>> 'sample'
     136'sample'
     137}}}
     138}}}
     139|-----------------------
     140{{{#!td
     141Bytes:
     142}}}
     143{{{#!td
     144{{{
     145>>> b'sample'
     146'sample'
     147}}}
     148}}}
     149{{{#!td
     150{{{
     151>>> b'sample'
     152b'sample'
     153}}}
     154}}}
     155|-----------------------
     156{{{#!td
     157Types:
     158}}}
     159{{{#!td
     160{{{
     161>>> type('xx'), type(u'xx'), type(b'xx')
     162(<type 'str'>, <type 'unicode'>, <type 'str'>)
     163}}}
     164}}}
     165{{{#!td
     166{{{
     167>>> type('xx'), type(u'xx'), type(b'xx')
     168(<class 'str'>, <class 'str'>, <class 'bytes'>)
     169}}}
     170}}}
     171
     172When special characters are involved:
     173
     174{{{#!th style="background: #ddd"
     175'''Label'''
     176}}}
     177{{{#!th style="background: #ddd"
     178'''Python 2'''
     179}}}
     180{{{#!th style="background: #ddd"
     181'''Python 3'''
     182}}}
     183|-----------------------
     184{{{#!td
     185String:
     186}}}
     187{{{#!td
     188{{{
     189>>> 'Příšerný kůň'
     190'P\xc5\x99\xc3\xad\xc5\xa1ern\xc3\xbd k\xc5\xaf\xc5\x88'
     191}}}
     192}}}
     193{{{#!td
     194{{{
     195>>> 'Příšerný kůň'
     196'Příšerný kůň'
     197}}}
     198}}}
     199|-----------------------
     200{{{#!td
     201Unicode:
     202}}}
     203{{{#!td
     204{{{
     205>>> u'Příšerný kůň'
     206u'P\u0159\xed\u0161ern\xfd k\u016f\u0148'
     207}}}
     208}}}
     209{{{#!td
     210{{{
     211>>> u'Příšerný kůň'
     212'Příšerný kůň'
     213}}}
     214}}}
     215|-----------------------
     216{{{#!td
     217Bytes:
     218}}}
     219{{{#!td
     220{{{
     221>>> b'Příšerný kůň'
     222'P\xc5\x99\xc3\xad\xc5\xa1ern\xc3\xbd k\xc5\xaf\xc5\x88'
     223}}}
     224}}}
     225{{{#!td
     226{{{
     227>>> b'Příšerný kůň'
     228SyntaxError: bytes can only contain ASCII literal characters.
     229}}}
     230}}}
     231
    98232For Python 3, bytes objects can not contain character literals other than ASCII, therefore, we use bytes() to convert from unicode/string to byte object.
    99233{{{
     
    107241}}}
    108242We already have encode and decode functions available in (from grass.script.utils import encode, decode) lib/python/script/utils.py that makes it easy for us to convert back and forth. To make it work with Python3, made changes in those functions to avoid syntax errors and exceptions.
    109 
    110243
    111244== How to write Python 3 compatible code