From 24fb774485f719df1e84dda31605d3f69202d69f Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Fran=C3=A7ois-Simon=20Fauteux-Chapleau?=
 <francois-simon.fauteux-chapleau@savoirfairelinux.com>
Date: Thu, 8 Aug 2024 14:59:17 -0400
Subject: [PATCH] qtwebengine: enable building with Python 3.12

Replace the deprecated imp module by importlib:
https://codereview.qt-project.org/c/qt/qtwebengine-chromium/+/524014
https://codereview.qt-project.org/c/qt/qtwebengine-chromium/+/534568

Update six to fix html5lib import failure:
https://codereview.qt-project.org/c/qt/qtwebengine-chromium/+/535605
https://issues.chromium.org/issues/40286977
---
 .../protobufs/binary_proto_generator.py          |  8 ++++++--
 .../mojo/public/tools/mojom/mojom/fileutil.py    |  1 -
 .../tools/mojom/mojom/fileutil_unittest.py       |  5 +----
 .../mojom/mojom/generate/generator_unittest.py   |  7 ++-----
 .../mojom/mojom/generate/translate_unittest.py   |  4 ----
 .../tools/mojom/mojom/parse/ast_unittest.py      |  6 ------
 .../mojom/parse/conditional_features_unittest.py |  8 ++------
 .../mojo/public/tools/mojom/mojom/parse/lexer.py |  1 -
 .../tools/mojom/mojom/parse/lexer_unittest.py    |  7 ++-----
 .../tools/mojom/mojom/parse/parser_unittest.py   |  5 -----
 .../third_party/catapult/third_party/six/six.py  | 16 ++++++++++++++++
 11 files changed, 29 insertions(+), 39 deletions(-)

diff --git a/qtwebengine/src/3rdparty/chromium/components/resources/protobufs/binary_proto_generator.py b/qtwebengine/src/3rdparty/chromium/components/resources/protobufs/binary_proto_generator.py
index 2a1802dccdc..8b9de65ed0b 100755
--- a/qtwebengine/src/3rdparty/chromium/components/resources/protobufs/binary_proto_generator.py
+++ b/qtwebengine/src/3rdparty/chromium/components/resources/protobufs/binary_proto_generator.py
@@ -9,7 +9,7 @@
 """
 from __future__ import print_function
 import abc
-import imp
+from importlib import util as imp_util
 import optparse
 import os
 import re
@@ -68,7 +68,11 @@ class GoogleProtobufModuleImporter:
       raise ImportError(fullname)
 
     filepath = self._fullname_to_filepath(fullname)
-    return imp.load_source(fullname, filepath)
+    spec = imp_util.spec_from_file_location(fullname, filepath)
+    loaded = imp_util.module_from_spec(spec)
+    spec.loader.exec_module(loaded)
+
+    return loaded
 
 class BinaryProtoGenerator:
 
diff --git a/qtwebengine/src/3rdparty/chromium/mojo/public/tools/mojom/mojom/fileutil.py b/qtwebengine/src/3rdparty/chromium/mojo/public/tools/mojom/mojom/fileutil.py
index 29daec367c5..124f12c134b 100644
--- a/qtwebengine/src/3rdparty/chromium/mojo/public/tools/mojom/mojom/fileutil.py
+++ b/qtwebengine/src/3rdparty/chromium/mojo/public/tools/mojom/mojom/fileutil.py
@@ -3,7 +3,6 @@
 # found in the LICENSE file.
 
 import errno
-import imp
 import os.path
 import sys
 
diff --git a/qtwebengine/src/3rdparty/chromium/mojo/public/tools/mojom/mojom/fileutil_unittest.py b/qtwebengine/src/3rdparty/chromium/mojo/public/tools/mojom/mojom/fileutil_unittest.py
index 48eaf4eca94..c93d22898d2 100644
--- a/qtwebengine/src/3rdparty/chromium/mojo/public/tools/mojom/mojom/fileutil_unittest.py
+++ b/qtwebengine/src/3rdparty/chromium/mojo/public/tools/mojom/mojom/fileutil_unittest.py
@@ -2,19 +2,16 @@
 # Use of this source code is governed by a BSD-style license that can be
 # found in the LICENSE file.
 
-import imp
 import os.path
 import shutil
-import sys
 import tempfile
 import unittest
 
 from mojom import fileutil
 
-
 class FileUtilTest(unittest.TestCase):
   def testEnsureDirectoryExists(self):
-    """Test that EnsureDirectoryExists fuctions correctly."""
+    """Test that EnsureDirectoryExists functions correctly."""
 
     temp_dir = tempfile.mkdtemp()
     try:
diff --git a/qtwebengine/src/3rdparty/chromium/mojo/public/tools/mojom/mojom/generate/generator_unittest.py b/qtwebengine/src/3rdparty/chromium/mojo/public/tools/mojom/mojom/generate/generator_unittest.py
index 76cda3981f3..7143e07c4d7 100644
--- a/qtwebengine/src/3rdparty/chromium/mojo/public/tools/mojom/mojom/generate/generator_unittest.py
+++ b/qtwebengine/src/3rdparty/chromium/mojo/public/tools/mojom/mojom/generate/generator_unittest.py
@@ -2,12 +2,11 @@
 # Use of this source code is governed by a BSD-style license that can be
 # found in the LICENSE file.
 
-import imp
+import importlib.util
 import os.path
 import sys
 import unittest
 
-
 def _GetDirAbove(dirname):
   """Returns the directory "above" this file containing |dirname| (which must
   also be "above" this file)."""
@@ -20,12 +19,11 @@ def _GetDirAbove(dirname):
 
 
 try:
-  imp.find_module("mojom")
+  importlib.util.find_spec("mojom")
 except ImportError:
   sys.path.append(os.path.join(_GetDirAbove("pylib"), "pylib"))
 from mojom.generate import generator
 
-
 class StringManipulationTest(unittest.TestCase):
   """generator contains some string utilities, this tests only those."""
 
@@ -69,6 +67,5 @@ class StringManipulationTest(unittest.TestCase):
     self.assertEquals("SNAKE_D3D11_CASE",
                       generator.ToUpperSnakeCase("snakeD3d11Case"))
 
-
 if __name__ == "__main__":
   unittest.main()
diff --git a/qtwebengine/src/3rdparty/chromium/mojo/public/tools/mojom/mojom/generate/translate_unittest.py b/qtwebengine/src/3rdparty/chromium/mojo/public/tools/mojom/mojom/generate/translate_unittest.py
index 4259374513f..558e71e1193 100644
--- a/qtwebengine/src/3rdparty/chromium/mojo/public/tools/mojom/mojom/generate/translate_unittest.py
+++ b/qtwebengine/src/3rdparty/chromium/mojo/public/tools/mojom/mojom/generate/translate_unittest.py
@@ -2,16 +2,12 @@
 # Use of this source code is governed by a BSD-style license that can be
 # found in the LICENSE file.
 
-import imp
-import os.path
-import sys
 import unittest
 
 from mojom.generate import module as mojom
 from mojom.generate import translate
 from mojom.parse import ast
 
-
 class TranslateTest(unittest.TestCase):
   """Tests |parser.Parse()|."""
 
diff --git a/qtwebengine/src/3rdparty/chromium/mojo/public/tools/mojom/mojom/parse/ast_unittest.py b/qtwebengine/src/3rdparty/chromium/mojo/public/tools/mojom/mojom/parse/ast_unittest.py
index c36376712e7..b289f7b11f6 100644
--- a/qtwebengine/src/3rdparty/chromium/mojo/public/tools/mojom/mojom/parse/ast_unittest.py
+++ b/qtwebengine/src/3rdparty/chromium/mojo/public/tools/mojom/mojom/parse/ast_unittest.py
@@ -2,14 +2,10 @@
 # Use of this source code is governed by a BSD-style license that can be
 # found in the LICENSE file.
 
-import imp
-import os.path
-import sys
 import unittest
 
 from mojom.parse import ast
 
-
 class _TestNode(ast.NodeBase):
   """Node type for tests."""
 
@@ -20,13 +16,11 @@ class _TestNode(ast.NodeBase):
   def __eq__(self, other):
     return super().__eq__(other) and self.value == other.value
 
-
 class _TestNodeList(ast.NodeListBase):
   """Node list type for tests."""
 
   _list_item_type = _TestNode
 
-
 class ASTTest(unittest.TestCase):
   """Tests various AST classes."""
 
diff --git a/qtwebengine/src/3rdparty/chromium/mojo/public/tools/mojom/mojom/parse/conditional_features_unittest.py b/qtwebengine/src/3rdparty/chromium/mojo/public/tools/mojom/mojom/parse/conditional_features_unittest.py
index 5fc582025ee..2fa5d2be6ab 100644
--- a/qtwebengine/src/3rdparty/chromium/mojo/public/tools/mojom/mojom/parse/conditional_features_unittest.py
+++ b/qtwebengine/src/3rdparty/chromium/mojo/public/tools/mojom/mojom/parse/conditional_features_unittest.py
@@ -2,12 +2,11 @@
 # Use of this source code is governed by a BSD-style license that can be
 # found in the LICENSE file.
 
-import imp
+import importlib.util
 import os
 import sys
 import unittest
 
-
 def _GetDirAbove(dirname):
   """Returns the directory "above" this file containing |dirname| (which must
   also be "above" this file)."""
@@ -18,9 +17,8 @@ def _GetDirAbove(dirname):
     if tail == dirname:
       return path
 
-
 try:
-  imp.find_module('mojom')
+  importlib.util.find_spec("mojom")
 except ImportError:
   sys.path.append(os.path.join(_GetDirAbove('pylib'), 'pylib'))
 import mojom.parse.ast as ast
@@ -29,7 +27,6 @@ import mojom.parse.parser as parser
 
 ENABLED_FEATURES = frozenset({'red', 'green', 'blue'})
 
-
 class ConditionalFeaturesTest(unittest.TestCase):
   """Tests |mojom.parse.conditional_features|."""
 
@@ -356,6 +353,5 @@ class ConditionalFeaturesTest(unittest.TestCase):
                       conditional_features.RemoveDisabledDefinitions,
                       definition, ENABLED_FEATURES)
 
-
 if __name__ == '__main__':
   unittest.main()
diff --git a/qtwebengine/src/3rdparty/chromium/mojo/public/tools/mojom/mojom/parse/lexer.py b/qtwebengine/src/3rdparty/chromium/mojo/public/tools/mojom/mojom/parse/lexer.py
index 73ca15df94c..1083a1af7bb 100644
--- a/qtwebengine/src/3rdparty/chromium/mojo/public/tools/mojom/mojom/parse/lexer.py
+++ b/qtwebengine/src/3rdparty/chromium/mojo/public/tools/mojom/mojom/parse/lexer.py
@@ -2,7 +2,6 @@
 # Use of this source code is governed by a BSD-style license that can be
 # found in the LICENSE file.
 
-import imp
 import os.path
 import sys
 
diff --git a/qtwebengine/src/3rdparty/chromium/mojo/public/tools/mojom/mojom/parse/lexer_unittest.py b/qtwebengine/src/3rdparty/chromium/mojo/public/tools/mojom/mojom/parse/lexer_unittest.py
index ce376da66e0..bc9f8354316 100644
--- a/qtwebengine/src/3rdparty/chromium/mojo/public/tools/mojom/mojom/parse/lexer_unittest.py
+++ b/qtwebengine/src/3rdparty/chromium/mojo/public/tools/mojom/mojom/parse/lexer_unittest.py
@@ -2,12 +2,11 @@
 # Use of this source code is governed by a BSD-style license that can be
 # found in the LICENSE file.
 
-import imp
+import importlib.util
 import os.path
 import sys
 import unittest
 
-
 def _GetDirAbove(dirname):
   """Returns the directory "above" this file containing |dirname| (which must
   also be "above" this file)."""
@@ -18,17 +17,15 @@ def _GetDirAbove(dirname):
     if tail == dirname:
       return path
 
-
 sys.path.insert(1, os.path.join(_GetDirAbove("mojo"), "third_party"))
 from ply import lex
 
 try:
-  imp.find_module("mojom")
+  importlib.util.find_spec("mojom")
 except ImportError:
   sys.path.append(os.path.join(_GetDirAbove("pylib"), "pylib"))
 import mojom.parse.lexer
 
-
 # This (monkey-patching LexToken to make comparison value-based) is evil, but
 # we'll do it anyway. (I'm pretty sure ply's lexer never cares about comparing
 # for object identity.)
diff --git a/qtwebengine/src/3rdparty/chromium/mojo/public/tools/mojom/mojom/parse/parser_unittest.py b/qtwebengine/src/3rdparty/chromium/mojo/public/tools/mojom/mojom/parse/parser_unittest.py
index 0513343ec7e..0a26307b1a3 100644
--- a/qtwebengine/src/3rdparty/chromium/mojo/public/tools/mojom/mojom/parse/parser_unittest.py
+++ b/qtwebengine/src/3rdparty/chromium/mojo/public/tools/mojom/mojom/parse/parser_unittest.py
@@ -2,16 +2,12 @@
 # Use of this source code is governed by a BSD-style license that can be
 # found in the LICENSE file.
 
-import imp
-import os.path
-import sys
 import unittest
 
 from mojom.parse import ast
 from mojom.parse import lexer
 from mojom.parse import parser
 
-
 class ParserTest(unittest.TestCase):
   """Tests |parser.Parse()|."""
 
@@ -1375,6 +1371,5 @@ class ParserTest(unittest.TestCase):
         r" *associated\? MyInterface& a;$"):
       parser.Parse(source3, "my_file.mojom")
 
-
 if __name__ == "__main__":
   unittest.main()
diff --git a/qtwebengine/src/3rdparty/chromium/third_party/catapult/third_party/six/six.py b/qtwebengine/src/3rdparty/chromium/third_party/catapult/third_party/six/six.py
index 83f69783d1a..5e7f0ce4437 100644
--- a/qtwebengine/src/3rdparty/chromium/third_party/catapult/third_party/six/six.py
+++ b/qtwebengine/src/3rdparty/chromium/third_party/catapult/third_party/six/six.py
@@ -71,6 +71,11 @@ else:
             MAXSIZE = int((1 << 63) - 1)
         del X
 
+if PY34:
+    from importlib.util import spec_from_loader
+else:
+    spec_from_loader = None
+
 
 def _add_doc(func, doc):
     """Add documentation to a function."""
@@ -186,6 +191,11 @@ class _SixMetaPathImporter(object):
             return self
         return None
 
+    def find_spec(self, fullname, path, target=None):
+        if fullname in self.known_modules:
+            return spec_from_loader(fullname, self)
+        return None
+
     def __get_module(self, fullname):
         try:
             return self.known_modules[fullname]
@@ -223,6 +233,12 @@ class _SixMetaPathImporter(object):
         return None
     get_source = get_code  # same as get_code
 
+    def create_module(self, spec):
+        return self.load_module(spec.name)
+
+    def exec_module(self, module):
+        pass
+
 _importer = _SixMetaPathImporter(__name__)
 
 
-- 
2.34.1

