~fginther/landscape-client/fix-missing-install-dirs

« back to all changes in this revision

Viewing changes to landscape/compat.py

  • Committer: 🤖 Landscape Builder
  • Author(s): Steffen Allner
  • Date: 2017-04-05 15:59:20 UTC
  • mfrom: (997.2.3 py3-cleanup)
  • Revision ID: _landscape_builder-20170405155920-ffx0c6wpxo26i9xd
Merge py3-cleanup [f=] [r=ericsnowcurrently,landscape-builder,nilo] [a=Gocept]
Cleanups related to landscape.compat (and lint).

This MP cleans up small residues from the port to Python 2/3 compatibility. It removes the unwanted coerce_unicode() helper method, which was replaced by individual solutions at the respective place of use. Furthermore unused imports have been removed and the usage (c)StringIO has been investigated, whether it could be replaced by a common io.{String,Bytes}IO() call in Python 2 and 3. This was often not the case, as this was also connected with the logging setup in the tests, which should not be addressed here.

Show diffs side-by-side

added added

removed removed

Lines of Context:
22
22
    stringio = StringIO
23
23
    from cStringIO import StringIO as cstringio
24
24
    input = raw_input
25
 
 
26
 
 
27
 
def coerce_unicode(s, encoding='ascii', errors='strict'):
28
 
    """
29
 
    Coerce byte strings into unicode for Python 2.
30
 
 
31
 
    In Python 2, decodes a byte string L{s} into unicode using the L{encoding},
32
 
    returns unmodified if any other type. In Python 3, raises a L{TypeError}
33
 
    when passed a byte string in L{s}, returns unmodified otherwise.
34
 
 
35
 
    @param s: The string to be converted to unicode.
36
 
    @type s: L{bytes} or L{unicode}
37
 
    @raise UnicodeError: The input L{bytes} is not decodable
38
 
        with given encoding.
39
 
    @raise TypeError: The input is L{bytes} on Python 3.
40
 
    """
41
 
    # In Python 2 C{unicode(b'bytes')} returns a unicode string C{'bytes'}. In
42
 
    # Python 3, the equivalent C{str(b'bytes')} will return C{"b'bytes'"}
43
 
    # instead.
44
 
    if isinstance(s, bytes):
45
 
        if _PY3:
46
 
            raise TypeError("Expected str not %r (bytes)" % (s,))
47
 
        else:
48
 
            return s.decode(encoding, errors)
49
 
    else:
50
 
        return s