From: Eduardo Habkost Subject: Re: [KVM-AUTOTEST PATCH 4/5] kvm_config: add helper to raise exception informing line number Date: Tue, 11 Jan 2011 10:52:06 -0200 Message-ID: <20110111125206.GA12161@otherpad.lan.raisama.net> References: <1294330356-17043-1-git-send-email-ehabkost@redhat.com> <1294330356-17043-5-git-send-email-ehabkost@redhat.com> <1294717702.15303.9.camel@freedom> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Cc: autotest@test.kernel.org, kvm@vger.kernel.org, Michael Goldish To: Lucas Meneghel Rodrigues Return-path: Received: from mx1.redhat.com ([209.132.183.28]:54519 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1755706Ab1AKMwJ (ORCPT ); Tue, 11 Jan 2011 07:52:09 -0500 Content-Disposition: inline In-Reply-To: <1294717702.15303.9.camel@freedom> Sender: kvm-owner@vger.kernel.org List-ID: On Tue, Jan 11, 2011 at 01:48:22AM -0200, Lucas Meneghel Rodrigues wrote: > On Thu, 2011-01-06 at 14:12 -0200, Eduardo Habkost wrote: > > + for num,line in enumerate(str.splitlines(), 1): > > ^ enumerate in py 2.4 takes exactly 1 argument, so it's not possible to > provide the enumerate start index like this, > > http://docs.python.org/library/functions.html#enumerate Oops! Right. > > so we have to do something like: > > + sequence = str.splitlines()[1:] > + for num,line in enumerate(sequence): > > To make it py2.4 compliant. No, that's not what the 'start' parameter does. You don't want to skip the first line from the input. However, we can simply do the following: diff --git a/client/tests/kvm/kvm_config.py b/client/tests/kvm/kvm_config.py index c206743..5acf247 100755 --- a/client/tests/kvm/kvm_config.py +++ b/client/tests/kvm/kvm_config.py @@ -549,7 +549,7 @@ class configreader: self.line_index = 0 self.lines = [] self.real_number = [] - for num,line in enumerate(str.splitlines(), 1): + for num,line in enumerate(str.splitlines()): line = line.rstrip().expandtabs() stripped_line = line.strip() indent = len(line) - len(stripped_line) @@ -558,7 +558,7 @@ class configreader: or stripped_line.startswith("//")): continue self.lines.append((line, stripped_line, indent)) - self.real_number.append(num) + self.real_number.append(num+1) def get_next_line(self): Updated patch below. -------------------- >From 09a1c35fa79127767de3c54dd02890658498e204 Mon Sep 17 00:00:00 2001 From: Eduardo Habkost Date: Wed, 5 Jan 2011 17:34:48 -0200 Subject: [PATCH] kvm_config: add helper to raise exception informing line number Useful for syntax or other errors on the config file. We want to tell the user on which file:line the error is located. Signed-off-by: Eduardo Habkost --- client/tests/kvm/kvm_config.py | 16 +++++++++++++++- 1 files changed, 15 insertions(+), 1 deletions(-) diff --git a/client/tests/kvm/kvm_config.py b/client/tests/kvm/kvm_config.py index 35e2ab9..5acf247 100755 --- a/client/tests/kvm/kvm_config.py +++ b/client/tests/kvm/kvm_config.py @@ -548,7 +548,8 @@ class configreader: self.filename = filename self.line_index = 0 self.lines = [] - for line in str.splitlines(): + self.real_number = [] + for num,line in enumerate(str.splitlines()): line = line.rstrip().expandtabs() stripped_line = line.strip() indent = len(line) - len(stripped_line) @@ -557,6 +558,7 @@ class configreader: or stripped_line.startswith("//")): continue self.lines.append((line, stripped_line, indent)) + self.real_number.append(num+1) def get_next_line(self): @@ -589,6 +591,18 @@ class configreader: """ self.line_index = index + def raise_error(self, msg): + """Raise an error related to the last line returned by get_next_line() + """ + if self.line_index == 0: # nothing was read. shouldn't happen, but... + line_id = 'BEGIN' + elif self.line_index >= len(self.lines): # past EOF + line_id = 'EOF' + else: + # line_index is the _next_ line. get the previous one + line_id = str(self.real_number[self.line_index-1]) + raise error.AutotestError("%s:%s: %s" % (self.filename, line_id, msg)) + # Array structure: # ---------------- -- 1.7.3.2 -- Eduardo