Skip to contentSkip to conversation

Adjust odd Python bindings

PATCH In review Elliott Mitchell 2 participants 4 messages Apr 22, 2022 Arm/actions semi architecture

lore
Share & tools
lore
#0 lore raw

cover Adjust odd Python bindings

First off, this isn't properly tested. I know it compiles, but that may not mean much when this encounters the Real World.

I'm doing initial exploration for a project involving Xen. Python seems a good language for the project, so I'm trying out the Python bindings for Xen.

What I've noticed is the bindings seem wrong. Importing xen.lowlevel.xc, then creating a xen.lowlevel.xc.xc() object has too many layers. The extra ".xc" is a distinct mismatch for comparable tools. As such I'm trying to get rid of the duplicated layer for both xc and xs.

The Python documentation indicates the name given to Extension() should be the full name, not just the package name. As such that is being adjusted too.

I haven't yet done testing beyond confirming this builds, since further checking will take some time to do...

I'm unsure how widely the Python bindings are used. Certainly they were used for PyGRUB. Have any other projects used them so far? What I'm doing is likely to be contributed to the Xen Project, and I suspect the existence of other projects would be known...

Elliott Mitchell (1):
tools/python: move lowlevel packages one level up

 tools/pygrub/src/pygrub                 |  4 ++--
 tools/python/setup.py                   | 12 ++++++------
 tools/python/xen/lowlevel/{xc => }/xc.c |  0
 tools/python/xen/lowlevel/{xs => }/xs.c |  0
 4 files changed, 8 insertions(+), 8 deletions(-)
 rename tools/python/xen/lowlevel/{xc => }/xc.c (100%)
 rename tools/python/xen/lowlevel/{xs => }/xs.c (100%)
Signature · 1 line
2.30.2
#1 lore raw

PATCH 1/1 tools/python: move lowlevel packages one level up

Prior to this a Python script first needed to import 3 levels of packages, then invoke xen.lowlevel.$tool.$tool() to get a handle. Now only 2 levels of packages, then xen.lowlevel.$tool() gets a handle.

---
 tools/pygrub/src/pygrub                 |  4 ++--
 tools/python/setup.py                   | 12 ++++++------
 tools/python/xen/lowlevel/{xc => }/xc.c |  0
 tools/python/xen/lowlevel/{xs => }/xs.c |  0
 4 files changed, 8 insertions(+), 8 deletions(-)
 rename tools/python/xen/lowlevel/{xc => }/xc.c (100%)
 rename tools/python/xen/lowlevel/{xs => }/xs.c (100%)
tools/pygrub/src/pygrub +2 -2
index ce7ab0eb8c..6b1989c7d8 100755
@@ -18,7 +18,7 @@ import os, sys, string, struct, tempfile, re, traceback, stat, errno
 import copy
 import logging
 import platform
-import xen.lowlevel.xc
+import xen.lowlevel
 
 import curses, _curses, curses.textpad, curses.ascii
 import getopt
@@ -669,7 +669,7 @@ def run_grub(file, entry, fs, cfg_args):
     return grubcfg
 
 def supports64bitPVguest():
-    xc = xen.lowlevel.xc.xc()
+    xc = xen.lowlevel.xc()
     caps = xc.xeninfo()['xen_caps'].split(" ")
     for cap in caps:
         if cap == "xen-3.0-x86_64":
tools/python/setup.py +6 -6
index 8c95db7769..77546335b8 100644
@@ -17,28 +17,28 @@ PATH_LIBXENCTRL = XEN_ROOT + "/tools/libs/ctrl"
 PATH_LIBXENGUEST = XEN_ROOT + "/tools/libs/guest"
 PATH_XENSTORE = XEN_ROOT + "/tools/libs/store"
 
-xc = Extension("xc",
+xc = Extension("xen.lowlevel.xc",
                extra_compile_args = extra_compile_args,
                include_dirs       = [ PATH_XEN,
                                       PATH_LIBXENTOOLLOG + "/include",
                                       PATH_LIBXENEVTCHN + "/include",
                                       PATH_LIBXENCTRL + "/include",
                                       PATH_LIBXENGUEST + "/include",
-                                      "xen/lowlevel/xc" ],
+                                      "xen/lowlevel" ],
                library_dirs       = [ PATH_LIBXENCTRL, PATH_LIBXENGUEST ],
                libraries          = [ "xenctrl", "xenguest" ],
                depends            = [ PATH_LIBXENCTRL + "/libxenctrl.so", PATH_LIBXENGUEST + "/libxenguest.so" ],
                extra_link_args    = SHLIB_libxenctrl + SHLIB_libxenguest,
-               sources            = [ "xen/lowlevel/xc/xc.c" ])
+               sources            = [ "xen/lowlevel/xc.c" ])
 
-xs = Extension("xs",
+xs = Extension("xen.lowlevel.xs",
                extra_compile_args = extra_compile_args,
-               include_dirs       = [ PATH_XEN, PATH_XENSTORE + "/include", "xen/lowlevel/xs" ],
+               include_dirs       = [ PATH_XEN, PATH_XENSTORE + "/include", "xen/lowlevel" ],
                library_dirs       = [ PATH_XENSTORE ],
                libraries          = [ "xenstore" ],
                depends            = [ PATH_XENSTORE + "/libxenstore.so" ],
                extra_link_args    = SHLIB_libxenstore,
-               sources            = [ "xen/lowlevel/xs/xs.c" ])
+               sources            = [ "xen/lowlevel/xs.c" ])
 
 plat = os.uname()[0]
 modules = [ xc, xs ]
tools/python/xen/lowlevel/xc.c renamed from tools/python/xen/lowlevel/xc/xc.c +0 -0
similarity index 100%
rename from tools/python/xen/lowlevel/xc/xc.c
rename to tools/python/xen/lowlevel/xc.c
tools/python/xen/lowlevel/xs.c renamed from tools/python/xen/lowlevel/xs/xs.c +0 -0
similarity index 100%
rename from tools/python/xen/lowlevel/xs/xs.c
rename to tools/python/xen/lowlevel/xs.c
Signature · 1 line
2.30.2
#2 lore raw
In reply to Elliott Mitchell
Quoted 4 lines · Elliott Mitchell

I'm unsure how widely the Python bindings are used. Certainly they were used for PyGRUB. Have any other projects used them so far? What I'm doing is likely to be contributed to the Xen Project, and I suspect the existence of other projects would be known...

Yes, Python bindings are used outside of xen.git. I'm aware of few places in Qubes OS, but I expect there are other users too. So, I'm afraid breaking the current import method isn't really an option. What you could try, is to make both versions work at the same time, but TBH I'm not sure how to do that with C extension.

Signature · 3 lines
Best Regards,
Marek Marczykowski-Górecki
Invisible Things Lab
#3 lore raw
Quoted 11 lines · Marek Marczykowski-G??recki
Quoted 4 lines · Elliott Mitchell

I'm unsure how widely the Python bindings are used. Certainly they were used for PyGRUB. Have any other projects used them so far? What I'm doing is likely to be contributed to the Xen Project, and I suspect the existence of other projects would be known...

Yes, Python bindings are used outside of xen.git. I'm aware of few places in Qubes OS, but I expect there are other users too. So, I'm afraid breaking the current import method isn't really an option. What you could try, is to make both versions work at the same time, but TBH I'm not sure how to do that with C extension.

Simplest would have been to address this in 2004, when the Python bindings had insignificant usage. Another handy time when things could have been broken was the Python 2->3 transition. Yet now both of those points have passed.

I suspect this could be done by adding some Python code to an __init__.py file. Simply redirect attempts to load the old path to the new path.

Other thing I noted is the Python documentation indicates the first argument to Extension() was wrong.

Signature · 4 lines
(\___(\___(\______          --=> 8-) EHM <=--          ______/)___/)___/)
 \BS (    |         [email protected]  PGP 87145445         |    )   /
  \_CS\   |  _____  -O #include <stddisclaimer.h> O-   _____  |   /  _/
8A19\___\_|_/58D2 7E3D DDF4 7BA6 <-PGP-> 41D1 B375 37D0 8714\_|_/___/5445