[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH] tools/libxl: Make gentypes.py compatible with older Python


  • To: <jbeulich@xxxxxxxx>
  • From: Jason Andryuk <jason.andryuk@xxxxxxx>
  • Date: Thu, 16 Oct 2025 22:06:13 -0400
  • Arc-authentication-results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=suse.com smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); arc=none (0)
  • Arc-message-signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=DxI9lOxDCuyTUAzAL7oPKcrGR+xyO4sju64Gbv2QO7g=; b=yci0z1d8yDxlL/x3Kdc0slPsZsgCLyr/0/qlPC5vNEQWSJ9GjL8NU6TwnbbGb3/KnNlcJ5h+zH/fDIRXF0hDwmleYUi3ttHSNAU7NtW03SYfUOQn3WSovbw+v+RjGReN6HWCnL1S3yI1pyUZQfR4dEPqCUznyQU8S1TWfJL31ZBhosTGgT9aoOyF2cnGm/crsNamelaKFyaKR0jxb6Ef+AxLGCM9c8v7fmQ406+4pyvUqiwBLTyv4g1BmyUoSX5QCIBzAsFHizsrzznmFAUeQ628U4Rj5bbow169e3FZP//CTyeniA6SlLI5FQ9YgWp0McS6T0G2hCRGcvqJ0BzblA==
  • Arc-seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=TOan3pdaA3JLa5TW3wY6NzXlzZzZWvQUzMTggOj5FNuEZtX7xT69Rv/XVTtx/dhBhBVqrWjg7Um3ihwyB41REvcPF2zlKhym56796x+avTMaJJk1bbrFZq7bY5TWMlThyUWLbbXbsoTDaKd35REcoUvcCteHw79xiPE7UrsLFVPiT8GaQWXLAhE6/tWOWYmIl6pMUvIqYcFPNMsT0VQZdlupcID7XuUDBAkMtDLiiEywu9BNxs/X1hRbKjcq1mOIljtErYHyuXCHUsPaNKTJDKV3MolYeEPNLOg4HHaw0yPqkECFAdgMX34E0gNUg7xRprzy0RUbljAwPnSxnNhENg==
  • Cc: <andrew.cooper3@xxxxxxxxxx>, <anthony.perard@xxxxxxxxxx>, <jgross@xxxxxxxx>, <oleksii.kurochko@xxxxxxxxx>, <xen-devel@xxxxxxxxxxxxxxxxxxxx>, Jason Andryuk <jason.andryuk@xxxxxxx>
  • Delivery-date: Fri, 17 Oct 2025 02:06:36 +0000
  • List-id: Xen developer discussion <xen-devel.lists.xenproject.org>

removeprefix is only added in Python 3.9.

Instead of the prefix removal, switch to passing in a "depth" parameter,
and incrementing it for each level.

There is a slight change in the generated _libxl_types.c.  instances of
KeyedUnion increment depth without outputing any code.  The net result
is some cases where jso_sub_1 is followed by jso_sub_3.  As an example:

_libxl_types.c
_libxl_types.c
@@ -5535,12 +5535,12 @@
                 if (!jso_sub_1)
                     goto out;
                 if (!libxl__string_is_default(&p->u.pty.path)) {
-                    json_object *jso_sub_2 = NULL;
-                    rc = libxl__string_gen_jso(&jso_sub_2, p->u.pty.path);
+                    json_object *jso_sub_3 = NULL;
+                    rc = libxl__string_gen_jso(&jso_sub_3, p->u.pty.path);
                     if (rc)
                         goto out;
-                    if (json_object_object_add(jso_sub_1, "path", jso_sub_2)) {
-                        json_object_put(jso_sub_2);
+                    if (json_object_object_add(jso_sub_1, "path", jso_sub_3)) {
+                        json_object_put(jso_sub_3);
                         goto out;
                     }
                 }

Signed-off-by: Jason Andryuk <jason.andryuk@xxxxxxx>
---
Here's an alternative approach to workaround removeprefix.

 tools/libs/light/gentypes.py | 14 ++++++++------
 1 file changed, 8 insertions(+), 6 deletions(-)

diff --git a/tools/libs/light/gentypes.py b/tools/libs/light/gentypes.py
index 006bea170a..0e45c04f49 100644
--- a/tools/libs/light/gentypes.py
+++ b/tools/libs/light/gentypes.py
@@ -377,15 +377,16 @@ def get_default_expr(f, nparent, fexpr):
     return "%s" % fexpr
 
 # For json-c gen_json functions
-def libxl_C_type_gen_jso(ty, v, indent = "    ", parent = None, scope_object = 
"jso"):
+def libxl_C_type_gen_jso(ty, v, indent = "    ", parent = None, scope_object = 
"jso", depth = 0):
     s = ""
     if parent is None:
         s += "json_object *jso;\n"
         s += "int rc;\n"
-        sub_scope_object = "jso_sub_1"
+        depth = 1
     else:
-        sub_scope_object = "jso_sub_%d" % 
(1+int(scope_object.removeprefix("jso_sub_")))
+        depth += 1
 
+    sub_scope_object = "jso_sub_%d" % depth
     if isinstance(ty, idl.Array):
         if parent is None:
             raise Exception("Array type must have a parent")
@@ -398,7 +399,8 @@ def libxl_C_type_gen_jso(ty, v, indent = "    ", parent = 
None, scope_object = "
         s += "        json_object *%s;\n" % (sub_scope_object)
         # remove some indent, it's over indented at least in one case 
libxl_vcpu_sched_params_gen_json
         s += libxl_C_type_gen_jso(ty.elem_type, v+"[i]",
-                                   indent + "    ", parent, sub_scope_object)
+                                  indent + "    ", parent, sub_scope_object,
+                                  depth)
         s += "        if (json_object_array_add(%s, %s)) {\n" % (scope_object, 
sub_scope_object)
         s += "            json_object_put(%s);\n" % (sub_scope_object)
         s += "            goto out;\n"
@@ -417,7 +419,7 @@ def libxl_C_type_gen_jso(ty, v, indent = "    ", parent = 
None, scope_object = "
             (nparent,fexpr) = ty.member(v, f, parent is None)
             s += "case %s:\n" % f.enumname
             if f.type is not None:
-                s += libxl_C_type_gen_jso(f.type, fexpr, indent + "    ", 
nparent, scope_object)
+                s += libxl_C_type_gen_jso(f.type, fexpr, indent + "    ", 
nparent, scope_object, depth)
             else:
                 s += "    %s = json_object_new_object();\n" % (scope_object)
                 s += "    if (!%s)\n" % (scope_object)
@@ -433,7 +435,7 @@ def libxl_C_type_gen_jso(ty, v, indent = "    ", parent = 
None, scope_object = "
             default_expr = get_default_expr(f, nparent, fexpr)
             s += "if (%s) {\n" % default_expr
             s += "    json_object *%s = NULL;\n" % (sub_scope_object)
-            s += libxl_C_type_gen_jso(f.type, fexpr, "    ", nparent, 
sub_scope_object)
+            s += libxl_C_type_gen_jso(f.type, fexpr, "    ", nparent, 
sub_scope_object, depth)
             s += libxl_C_type_gen_jso_map_key(f, nparent, "    ", 
scope_object, sub_scope_object)
 
             s += "}\n"
-- 
2.51.0




 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.