[systemd-devel] [PATCH] analyze: Python 3 compatibility

Zbigniew Jędrzejewski-Szmek zbyszek at in.waw.pl
Fri Jun 15 01:28:20 PDT 2012


On 06/15/2012 12:08 AM, Shawn Landden wrote:
> Plot verb doesn't currently work b/c Cairo bug sending to stdout in python 3.

Hi,
since you're using 'except ... as', this is compatible with python 2.6
or higher. Makes sense. This also means, that print function can be used
instead os sys.stdout.write. This will minimize changes.

If you are changing print to something which requires (), either
sys.stdout.write() or print(), then the line-continuation backslashes
can be dropped.

My 0.2 ¢.

Zbyszek


>  src/analyze/systemd-analyze |   16 ++++++++--------
>  1 file changed, 8 insertions(+), 8 deletions(-)
> 
> diff --git a/src/analyze/systemd-analyze b/src/analyze/systemd-analyze
> index 5d451c3..76eceee 100755
> --- a/src/analyze/systemd-analyze
> +++ b/src/analyze/systemd-analyze
> @@ -91,16 +91,16 @@ def time():
>          initrd_time, start_time, finish_time = acquire_start_time()
>  
>          if initrd_time > 0:
> -                print "Startup finished in %lums (kernel) + %lums (initramfs) + %lums (userspace) = %lums" % ( \
> +                sys.stdout.write("Startup finished in %lums (kernel) + %lums (initramfs) + %lums (userspace) = %lums\n" % ( \
>                          initrd_time/1000, \
>                          (start_time - initrd_time)/1000, \
>                          (finish_time - start_time)/1000, \
> -                        finish_time/1000)
> +                        finish_time/1000))
>          else:
> -                print "Startup finished in %lums (kernel) + %lums (userspace) = %lums" % ( \
> +                sys.stdout.write("Startup finished in %lums (kernel) + %lums (userspace) = %lums\n" % ( \
>                          start_time/1000, \
>                          (finish_time - start_time)/1000, \
> -                        finish_time/1000)
> +                        finish_time/1000))
>  
>  
>  def blame():
> @@ -161,7 +161,7 @@ def plot():
>          context.set_line_width(1)
>          context.set_source_rgb(0.7, 0.7, 0.7)
>  
> -        for x in range(0, finish_time/10000 + 100, 100):
> +        for x in range(0, int(finish_time/10000) + 100, 100):
>                  context.move_to(x, 0)
>                  context.line_to(x, height-border*2)
>  
> @@ -185,7 +185,7 @@ def plot():
>          banner = "{} {} ({} {}) {}".format(osrel, *(os.uname()[1:5]))
>          draw_text(context, 0, -15, banner, hcenter = 0, vcenter = 1)
>  
> -        for x in range(0, finish_time/10000 + 100, 100):
> +        for x in range(0, int(finish_time/10000) + 100, 100):
>                  draw_text(context, x, -5, "%lus" % (x/100), vcenter = 0, hcenter = 0)
>  
>          y = 0
> @@ -285,8 +285,8 @@ bus = dbus.SystemBus()
>  
>  try:
>          opts, args = getopt.gnu_getopt(sys.argv[1:], "h", ["help", "user"])
> -except getopt.GetoptError, err:
> -        print str(err)
> +except getopt.GetoptError as err:
> +        sys.stdout.write(str(err) + "\n")
>          usage()
>          sys.exit(2)
>  for o, a in opts:



More information about the systemd-devel mailing list