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

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



commit 47268fb8c8cdeeddcd1cf5c5b33903e9926ecaad
Author:     Jason Andryuk <jason.andryuk@xxxxxxx>
AuthorDate: Thu Oct 16 22:06:13 2025 -0400
Commit:     Andrew Cooper <andrew.cooper3@xxxxxxxxxx>
CommitDate: Tue Oct 21 18:11:22 2025 +0100

    tools/libxl: Make gentypes.py compatible with older Python
    
    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>
    Reviewed-by: Anthony PERARD <anthony.perard@xxxxxxxxxx>
    Reviewed-by: Andrew Cooper <andrew.cooper3@xxxxxxxxxx>
    Release-Acked-by: Oleksii Kurochko <oleksii.kurochko@xxxxxxxxx>
---
 tools/libs/light/gentypes.py | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)

diff --git a/tools/libs/light/gentypes.py b/tools/libs/light/gentypes.py
index 006bea170a..47bca4bc57 100644
--- a/tools/libs/light/gentypes.py
+++ b/tools/libs/light/gentypes.py
@@ -377,15 +377,14 @@ 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"
-    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 +397,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 +417,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 +433,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"
--
generated by git-patchbot for /home/xen/git/xen.git#staging



 


Rackspace

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