• Raul Tambre's avatar
    build/toolchain/win/midl.py: Python 3 support · 56415b77
    Raul Tambre authored
    * Use bytestrings for operations in cases where files are read in binary mode.
    * Output from subprocess is a bytestring in Python 3. Need to decode it before using in normal string operations.
    * Due to use of bytestrings need to encode UUID string.
    * Had to use branching code for ord() due to string vs bytestring in Py2/3.
    * array() typecode 'c' (char) has been removed in Python 3. Use 'B' (unsigned char) instead.
    * Use '//' for division where necessary. Add 'from __future__ import division' to avoid this in the future.
    * Import reduce() for Python 3.
    * Fixed open(mode='U') DeprecationWarnings. Use io.open for universal newlines on both Py2/3.
    
    The changes are backwards compatible with Python 2.
    Formatting changes are a result of running "git cl format --python".
    
    Errors fixed:
    Traceback (most recent call last):
      File "../../build/toolchain/win/midl.py", line 240, in <module>
        sys.exit(main(*sys.argv[1:]))
      File "../../build/toolchain/win/midl.py", line 205, in main
        processing = set(os.path.basename(x)
      File "../../build/toolchain/win/midl.py", line 206, in <genexpr>
        for x in lines if x.startswith(prefixes))
    TypeError: a bytes-like object is required, not 'str'
    
    Traceback (most recent call last):
      File "../../build/toolchain/win/midl.py", line 239, in <module>
        sys.exit(main(*sys.argv[1:]))
      File "../../build/toolchain/win/midl.py", line 213, in main
        ZapTimestamp(os.path.join(tmp_dir, f))
      File "../../build/toolchain/win/midl.py", line 71, in ZapTimestamp
        contents = re.sub(
      File "C:\Program Files\Python38\lib\re.py", line 208, in sub
        return _compile(pattern, flags).sub(repl, string, count)
    TypeError: cannot use a string pattern on a bytes-like object
    
    Traceback (most recent call last):
      File "../../build/toolchain/win/midl.py", line 241, in <module>
        sys.exit(main(*sys.argv[1:]))
      File "../../build/toolchain/win/midl.py", line 167, in main
        overwrite_cls_guid(os.path.join(outdir, h),
      File "../../build/toolchain/win/midl.py", line 152, in overwrite_cls_guid
        overwrite_cls_guid_h(h_file, dynamic_guid)
      File "../../build/toolchain/win/midl.py", line 99, in overwrite_cls_guid_h
        br'class DECLSPEC_UUID("%s")' % str(dynamic_guid), contents)
    TypeError: %b requires a bytes-like object, or an object that implements __bytes__, not 'str'
    
    Traceback (most recent call last):
      File "../../build/toolchain/win/midl.py", line 241, in <module>
        sys.exit(main(*sys.argv[1:]))
      File "../../build/toolchain/win/midl.py", line 167, in main
        overwrite_cls_guid(os.path.join(outdir, h),
      File "../../build/toolchain/win/midl.py", line 153, in overwrite_cls_guid
        overwrite_cls_guid_iid(iid_file, dynamic_guid)
      File "../../build/toolchain/win/midl.py", line 106, in overwrite_cls_guid_iid
        hexuuid += ','.join('0x%02x' % ord(b) for b in dynamic_guid.bytes[8:])
      File "../../build/toolchain/win/midl.py", line 106, in <genexpr>
        hexuuid += ','.join('0x%02x' % ord(b) for b in dynamic_guid.bytes[8:])
    TypeError: ord() expected string of length 1, but int found
    
    Traceback (most recent call last):
      File "../../build/toolchain/win/midl.py", line 241, in <module>
        sys.exit(main(*sys.argv[1:]))
      File "../../build/toolchain/win/midl.py", line 167, in main
        overwrite_cls_guid(os.path.join(outdir, h),
      File "../../build/toolchain/win/midl.py", line 154, in overwrite_cls_guid
        overwrite_cls_guid_tlb(tlb_file, dynamic_guid)
      File "../../build/toolchain/win/midl.py", line 120, in overwrite_cls_guid_tlb
        assert ord(contents[type_off]) == 0x25, "expected coclass"
    TypeError: ord() expected string of length 1, but int found
    
    Traceback (most recent call last):
      File "../../build/toolchain/win/midl.py", line 241, in <module>
        sys.exit(main(*sys.argv[1:]))
      File "../../build/toolchain/win/midl.py", line 167, in main
        overwrite_cls_guid(os.path.join(outdir, h),
      File "../../build/toolchain/win/midl.py", line 154, in overwrite_cls_guid
        overwrite_cls_guid_tlb(tlb_file, dynamic_guid)
      File "../../build/toolchain/win/midl.py", line 125, in overwrite_cls_guid_tlb
        contents = array.array('c', contents)
    ValueError: bad typecode (must be b, B, u, h, H, i, I, l, L, q, Q, f or d)
    
    Traceback (most recent call last):
      File "../../build/toolchain/win/midl.py", line 241, in <module>
        sys.exit(main(*sys.argv[1:]))
      File "../../build/toolchain/win/midl.py", line 167, in main
        overwrite_cls_guid(os.path.join(outdir, h),
      File "../../build/toolchain/win/midl.py", line 154, in overwrite_cls_guid
        overwrite_cls_guid_tlb(tlb_file, dynamic_guid)
      File "../../build/toolchain/win/midl.py", line 131, in overwrite_cls_guid_tlb
        hashtab = [0xffffffff] * (0x80 / 4)
    TypeError: can't multiply sequence by non-int of type 'float'
    
    Traceback (most recent call last):
      File "../../build/toolchain/win/midl.py", line 242, in <module>
        sys.exit(main(*sys.argv[1:]))
      File "../../build/toolchain/win/midl.py", line 168, in main
        overwrite_cls_guid(os.path.join(outdir, h),
      File "../../build/toolchain/win/midl.py", line 155, in overwrite_cls_guid
        overwrite_cls_guid_tlb(tlb_file, dynamic_guid)
      File "../../build/toolchain/win/midl.py", line 138, in overwrite_cls_guid_tlb
        guidhash = reduce(operator.xor, [w for w in words]) % (0x80 // 4)
    NameError: name 'reduce' is not defined
    
    ../../build/toolchain/win/midl.py:231: DeprecationWarning: 'U' mode is deprecated
      open(fromfile, 'bU').readlines(),
    ../../build/toolchain/win/midl.py:232: DeprecationWarning: 'U' mode is deprecated
      open(tofile, 'bU').readlines(), fromfile, tofile)))
    
    Bug: 941669
    Change-Id: I3e5985c732db114eff2209e428ec9ee3f209e3b6
    Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1829205
    Commit-Queue: Raul Tambre <raul@tambre.ee>
    Reviewed-by: default avatarDirk Pranke <dpranke@chromium.org>
    Auto-Submit: Raul Tambre <raul@tambre.ee>
    Cr-Commit-Position: refs/heads/master@{#705426}
    56415b77
midl.py 11.1 KB