Subject: Re: bug#70722: [PATCH] ; Migrate to python3 for (package-test-update-archives-async)

From: Lin Sun <[email-masked]>
Date: Mon, 6 May 2024 00:28:07 +0000

On Thu, May 2, 2024 at 6:55 PM Eli Zaretskii <[email-masked]> wrote:
>
> > From: Lin Sun <[email-masked]>
> > Date: Thu, 2 May 2024 18:11:04 +0000
> >
> > The test case "package-test-update-archives-async" still tries to
> > search "python2" for testing, while python2 was at the end of its life
> > years ago.
> > So move the test case from python2 to python3.
>
> I'd prefer to make the test support both Python 2.x and Python 3.x.
> The fact that python.org end-of-life'd Python 2 doesn't mean we have
> to jump to attention and follow suit.  Old systems might still have
> Python 2 installed, and there's no need to drop them.
>
> I do agree that we should try looking for Python 3 first, and only
> afterwards for Python 2.
>
> Also, I think the test should start by looking for just "python",
> before the versioned variants.
>
> E.g., on my system (executable-find "python3") returns nil, although I
> have Python 3.x installed -- but it's installed under the name
> "python".
Thanks for the comment, and it's true that python2 will still exist
for many years.
And I had changed the test case to adapt python/python3/python2 automatically.
Please help review the new patch. Thanks.
From 814d21df047460be1eae004ad2031a97059f4665 Mon Sep 17 00:00:00 2001
From: Lin Sun <[email-masked]>
Date: Thu, 2 May 2024 06:52:09 +0000
Subject: [PATCH] ; Support python3 for (package-test-update-archives-async)
 (bug#70722)

* lisp/emacs-lisp/package-tests.el (package-test-update-archives-async):
  Searching python/python3/pyhton2 and decides coresponding python
  script file.
* test/lisp/emacs-lisp/package-resources/package-test-server.py3:
  Python3 script for package test server.
---
 .../package-resources/package-test-server.py3 | 17 ++++++++++++
 test/lisp/emacs-lisp/package-tests.el         | 26 +++++++++++++++----
 2 files changed, 38 insertions(+), 5 deletions(-)
 create mode 100644 test/lisp/emacs-lisp/package-resources/package-test-server.py3

diff --git a/test/lisp/emacs-lisp/package-resources/package-test-server.py3 b/test/lisp/emacs-lisp/package-resources/package-test-server.py3
new file mode 100644
index 00000000000..8f2d2db5f96
--- /dev/null
+++ b/test/lisp/emacs-lisp/package-resources/package-test-server.py3
@@ -0,0 +1,17 @@
+## package-test-server.py3 -*- mode: python; coding: utf-8 -*-
+import sys
+from http.server import HTTPServer, SimpleHTTPRequestHandler
+
+
+HandlerClass = SimpleHTTPRequestHandler
+HandlerClass.protocol_version = "HTTP/1.0"
+server_address = ('127.0.0.1', int(sys.argv[1]) if sys.argv[1:] else 0)
+httpd = HTTPServer(server_address, HandlerClass)
+
+ip, port = httpd.socket.getsockname()[0:2]
+print ("Server started, http://%s:%s/" % (ip, port))
+# Flush in case we're in full buffering mode (instead of line
+# buffering), this might happen if python is a cygwin program and we
+# run it from a native w32 program.
+sys.stdout.flush()
+httpd.serve_forever()
diff --git a/test/lisp/emacs-lisp/package-tests.el b/test/lisp/emacs-lisp/package-tests.el
index d95b94f2145..3309ba32e0d 100644
--- a/test/lisp/emacs-lisp/package-tests.el
+++ b/test/lisp/emacs-lisp/package-tests.el
@@ -634,14 +634,30 @@ but with a different end of line convention (bug#48137)."
 (ert-deftest package-test-update-archives-async ()
   "Test updating package archives asynchronously."
   :tags '(:expensive-test)
-  (skip-unless (executable-find "python2"))
   (let* ((package-menu-async t)
          (default-directory package-test-data-dir)
-         (process (start-process
+         python-interpreter python-test-script
+         process addr)
+    (when-let* ((py-interpreter (executable-find "python"))
+                (py-ver (shell-command-to-string "python --version")))
+      (setq python-interpreter py-interpreter
+            python-test-script (if (string-match-p "Python 2" py-ver)
+                                 "package-test-server.py"
+                               "package-test-server.py3")))
+    (when-let* (((not python-interpreter))
+                (py-interpreter (executable-find "python3")))
+      (setq python-interpreter py-interpreter
+            python-test-script "package-test-server.py3"))
+    (when-let* (((not python-interpreter))
+                (py-interpreter (executable-find "python2")))
+      (setq python-interpreter py-interpreter
+            python-test-script "package-test-server.py"))
+
+    (skip-unless python-interpreter)
+
+    (setq process (start-process
                    "package-server" "package-server-buffer"
-                   (executable-find "python2")
-                   "package-test-server.py"))
-         (addr nil))
+                   python-interpreter python-test-script))
     (unwind-protect
         (progn
           (with-current-buffer "package-server-buffer"
-- 
2.20.5