linux-integrity.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] selftests/tpm2: Change exception handling to be Python 3 compatible
@ 2020-04-12 14:36 Jarkko Sakkinen
  2020-04-12 15:02 ` Ezra Buehler
  0 siblings, 1 reply; 9+ messages in thread
From: Jarkko Sakkinen @ 2020-04-12 14:36 UTC (permalink / raw)
  To: Shuah Khan
  Cc: linux-kselftest, linux-integrity, Jarkko Sakkinen, Tadeusz Struk,
	open list

I need more time to fix all the byte array / string related stuff but
it makes sense to fix the exceptions as it is fairly mechanical
procedure:

1,$s/except \(.*\), \(.*\):/except \1(\2):/g

I.e. fix the low hanging fruit first and the rest later.

Signed-off-by: Jarkko Sakkinen <jarkko.sakkinen@linux.intel.com>
---
 tools/testing/selftests/tpm2/tpm2_tests.py | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)

diff --git a/tools/testing/selftests/tpm2/tpm2_tests.py b/tools/testing/selftests/tpm2/tpm2_tests.py
index 728be7c69b76..c3c06899e042 100644
--- a/tools/testing/selftests/tpm2/tpm2_tests.py
+++ b/tools/testing/selftests/tpm2/tpm2_tests.py
@@ -65,7 +65,7 @@ class SmokeTest(unittest.TestCase):
         blob = self.client.seal(self.root_key, data, auth, None)
         try:
             result = self.client.unseal(self.root_key, blob, auth[:-1] + 'B', None)
-        except ProtocolError, e:
+        except ProtocolError(e):
             rc = e.rc
 
         self.assertEqual(rc, tpm2.TPM2_RC_AUTH_FAIL)
@@ -119,7 +119,7 @@ class SmokeTest(unittest.TestCase):
             self.client.policy_password(handle)
 
             result = self.client.unseal(self.root_key, blob, auth, handle)
-        except ProtocolError, e:
+        except ProtocolError(e):
             rc = e.rc
             self.client.flush_context(handle)
         except:
@@ -136,7 +136,7 @@ class SmokeTest(unittest.TestCase):
         rc = 0
         try:
             blob = self.client.seal(self.root_key, data, auth, None)
-        except ProtocolError, e:
+        except ProtocolError(e):
             rc = e.rc
 
         self.assertEqual(rc, tpm2.TPM2_RC_SIZE)
@@ -152,7 +152,7 @@ class SmokeTest(unittest.TestCase):
                               0xDEADBEEF)
 
             self.client.send_cmd(cmd)
-        except IOError, e:
+        except IOError(e):
             rejected = True
         except:
             pass
@@ -212,7 +212,7 @@ class SmokeTest(unittest.TestCase):
             self.client.tpm.write(cmd)
             rsp = self.client.tpm.read()
 
-        except IOError, e:
+        except IOError(e):
             # read the response
             rsp = self.client.tpm.read()
             rejected = True
@@ -283,7 +283,7 @@ class SpaceTest(unittest.TestCase):
         rc = 0
         try:
             space1.send_cmd(cmd)
-        except ProtocolError, e:
+        except ProtocolError(e):
             rc = e.rc
 
         self.assertEqual(rc, tpm2.TPM2_RC_COMMAND_CODE |
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 9+ messages in thread

* Re: [PATCH] selftests/tpm2: Change exception handling to be Python 3 compatible
  2020-04-12 14:36 [PATCH] selftests/tpm2: Change exception handling to be Python 3 compatible Jarkko Sakkinen
@ 2020-04-12 15:02 ` Ezra Buehler
  2020-04-12 17:07   ` Jarkko Sakkinen
  0 siblings, 1 reply; 9+ messages in thread
From: Ezra Buehler @ 2020-04-12 15:02 UTC (permalink / raw)
  To: Jarkko Sakkinen
  Cc: Shuah Khan, linux-kselftest, linux-integrity, Tadeusz Struk, open list

Hi Jarkkon,

> On 12 Apr 2020, at 16:36, Jarkko Sakkinen <jarkko.sakkinen@linux.intel.com> wrote:
> +        except ProtocolError(e):

Should this not be

        except ProtocolError as e:

?

Cheers,
Ezra.


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH] selftests/tpm2: Change exception handling to be Python 3 compatible
  2020-04-12 15:02 ` Ezra Buehler
@ 2020-04-12 17:07   ` Jarkko Sakkinen
  2020-04-13  5:02     ` Ezra Buehler
  0 siblings, 1 reply; 9+ messages in thread
From: Jarkko Sakkinen @ 2020-04-12 17:07 UTC (permalink / raw)
  To: Ezra Buehler
  Cc: Shuah Khan, linux-kselftest, linux-integrity, Tadeusz Struk, open list

On Sun, Apr 12, 2020 at 05:02:27PM +0200, Ezra Buehler wrote:
> Hi Jarkkon,
> 
> > On 12 Apr 2020, at 16:36, Jarkko Sakkinen <jarkko.sakkinen@linux.intel.com> wrote:
> > +        except ProtocolError(e):
> 
> Should this not be
> 
>         except ProtocolError as e:

Unless there is a functional difference, does it matter?

/Jarkko

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH] selftests/tpm2: Change exception handling to be Python 3 compatible
  2020-04-12 17:07   ` Jarkko Sakkinen
@ 2020-04-13  5:02     ` Ezra Buehler
  2020-04-13 18:04       ` Jarkko Sakkinen
  0 siblings, 1 reply; 9+ messages in thread
From: Ezra Buehler @ 2020-04-13  5:02 UTC (permalink / raw)
  To: Jarkko Sakkinen
  Cc: Shuah Khan, linux-kselftest, linux-integrity, Tadeusz Struk, open list

Hi Jarkko,

On 12 Apr 2020, at 19:07, Jarkko Sakkinen <jarkko.sakkinen@linux.intel.com> wrote:
> 
> On Sun, Apr 12, 2020 at 05:02:27PM +0200, Ezra Buehler wrote:
>> Hi Jarkkon,
>> 
>>> On 12 Apr 2020, at 16:36, Jarkko Sakkinen <jarkko.sakkinen@linux.intel.com> wrote:
>>> +        except ProtocolError(e):
>> 
>> Should this not be
>> 
>>        except ProtocolError as e:
> 
> Unless there is a functional difference, does it matter?
> 
> /Jarkko

Well, your patch confuses me a lot. It looks to me like you are passing
the undefined `e` variable to the constructor.

When I run flake8 on it I get following error (among others):

    F821 undefined name 'e'

What I suggested is the standard syntax:
https://docs.python.org/3/tutorial/errors.html

Did you test this? You should get an error as soon as an exception
occurs.

Cheers,
Ezra.


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH] selftests/tpm2: Change exception handling to be Python 3 compatible
  2020-04-13  5:02     ` Ezra Buehler
@ 2020-04-13 18:04       ` Jarkko Sakkinen
  2020-04-14  5:45         ` Ezra Buehler
  0 siblings, 1 reply; 9+ messages in thread
From: Jarkko Sakkinen @ 2020-04-13 18:04 UTC (permalink / raw)
  To: Ezra Buehler
  Cc: Shuah Khan, linux-kselftest, linux-integrity, Tadeusz Struk, open list

On Mon, Apr 13, 2020 at 07:02:20AM +0200, Ezra Buehler wrote:
> Hi Jarkko,
> 
> On 12 Apr 2020, at 19:07, Jarkko Sakkinen <jarkko.sakkinen@linux.intel.com> wrote:
> > 
> > On Sun, Apr 12, 2020 at 05:02:27PM +0200, Ezra Buehler wrote:
> >> Hi Jarkkon,
> >> 
> >>> On 12 Apr 2020, at 16:36, Jarkko Sakkinen <jarkko.sakkinen@linux.intel.com> wrote:
> >>> +        except ProtocolError(e):
> >> 
> >> Should this not be
> >> 
> >>        except ProtocolError as e:
> > 
> > Unless there is a functional difference, does it matter?
> > 
> > /Jarkko
> 
> Well, your patch confuses me a lot. It looks to me like you are passing
> the undefined `e` variable to the constructor.
> 
> When I run flake8 on it I get following error (among others):
> 
>     F821 undefined name 'e'

I don't know what flake8 is.

> What I suggested is the standard syntax:
> https://docs.python.org/3/tutorial/errors.html

It passed the Python 3 interpreter.

> Did you test this? You should get an error as soon as an exception
> occurs.

Yes. Interpreter did not complain. I did not know that the language
is broken that way that you have to exercise the code path to get
a syntax error.

/Jarkko

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH] selftests/tpm2: Change exception handling to be Python 3 compatible
  2020-04-13 18:04       ` Jarkko Sakkinen
@ 2020-04-14  5:45         ` Ezra Buehler
  2020-04-14  7:38           ` Jarkko Sakkinen
  0 siblings, 1 reply; 9+ messages in thread
From: Ezra Buehler @ 2020-04-14  5:45 UTC (permalink / raw)
  To: Jarkko Sakkinen
  Cc: Shuah Khan, linux-kselftest, linux-integrity, Tadeusz Struk, open list

Hi Jarkko,

On 13 Apr 2020, at 20:04, Jarkko Sakkinen <jarkko.sakkinen@linux.intel.com> wrote:
> 
> On Mon, Apr 13, 2020 at 07:02:20AM +0200, Ezra Buehler wrote:
>> Hi Jarkko,
>> 
>> On 12 Apr 2020, at 19:07, Jarkko Sakkinen <jarkko.sakkinen@linux.intel.com> wrote:
>>> 
>>> On Sun, Apr 12, 2020 at 05:02:27PM +0200, Ezra Buehler wrote:
>>>> Hi Jarkkon,
>>>> 
>>>>> On 12 Apr 2020, at 16:36, Jarkko Sakkinen <jarkko.sakkinen@linux.intel.com> wrote:
>>>>> +        except ProtocolError(e):
>>>> 
>>>> Should this not be
>>>> 
>>>>       except ProtocolError as e:
>>> 
>>> Unless there is a functional difference, does it matter?
>>> 
>>> /Jarkko
>> 
>> Well, your patch confuses me a lot. It looks to me like you are passing
>> the undefined `e` variable to the constructor.
>> 
>> When I run flake8 on it I get following error (among others):
>> 
>>    F821 undefined name 'e'
> 
> I don't know what flake8 is.

https://flake8.pycqa.org/en/latest/

>> What I suggested is the standard syntax:
>> https://docs.python.org/3/tutorial/errors.html
> 
> It passed the Python 3 interpreter.

That is because it is technically valid syntax.

>> Did you test this? You should get an error as soon as an exception
>> occurs.
> 
> Yes. Interpreter did not complain. I did not know that the language
> is broken that way that you have to exercise the code path to get
> a syntax error.

That is due to the dynamic nature of Python. You won’t get a syntax
error. You will get an exception:

    NameError: name 'e' is not defined

Python has to assume that `e` might be defined at runtime. However,
style checkers will complain.

> 
> /Jarkko

Cheers,
Ezra.


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH] selftests/tpm2: Change exception handling to be Python 3 compatible
  2020-04-14  5:45         ` Ezra Buehler
@ 2020-04-14  7:38           ` Jarkko Sakkinen
  2020-04-14 11:14             ` Ezra Buehler
  0 siblings, 1 reply; 9+ messages in thread
From: Jarkko Sakkinen @ 2020-04-14  7:38 UTC (permalink / raw)
  To: Ezra Buehler
  Cc: Shuah Khan, linux-kselftest, linux-integrity, Tadeusz Struk, open list

On Tue, Apr 14, 2020 at 07:45:33AM +0200, Ezra Buehler wrote:
> Hi Jarkko,
> 
> On 13 Apr 2020, at 20:04, Jarkko Sakkinen <jarkko.sakkinen@linux.intel.com> wrote:
> > 
> > On Mon, Apr 13, 2020 at 07:02:20AM +0200, Ezra Buehler wrote:
> >> Hi Jarkko,
> >> 
> >> On 12 Apr 2020, at 19:07, Jarkko Sakkinen <jarkko.sakkinen@linux.intel.com> wrote:
> >>> 
> >>> On Sun, Apr 12, 2020 at 05:02:27PM +0200, Ezra Buehler wrote:
> >>>> Hi Jarkkon,
> >>>> 
> >>>>> On 12 Apr 2020, at 16:36, Jarkko Sakkinen <jarkko.sakkinen@linux.intel.com> wrote:
> >>>>> +        except ProtocolError(e):
> >>>> 
> >>>> Should this not be
> >>>> 
> >>>>       except ProtocolError as e:
> >>> 
> >>> Unless there is a functional difference, does it matter?
> >>> 
> >>> /Jarkko
> >> 
> >> Well, your patch confuses me a lot. It looks to me like you are passing
> >> the undefined `e` variable to the constructor.
> >> 
> >> When I run flake8 on it I get following error (among others):
> >> 
> >>    F821 undefined name 'e'
> > 
> > I don't know what flake8 is.
> 
> https://flake8.pycqa.org/en/latest/
> 
> >> What I suggested is the standard syntax:
> >> https://docs.python.org/3/tutorial/errors.html
> > 
> > It passed the Python 3 interpreter.
> 
> That is because it is technically valid syntax.
> 
> >> Did you test this? You should get an error as soon as an exception
> >> occurs.
> > 
> > Yes. Interpreter did not complain. I did not know that the language
> > is broken that way that you have to exercise the code path to get
> > a syntax error.
> 
> That is due to the dynamic nature of Python. You won’t get a syntax
> error. You will get an exception:
> 
>     NameError: name 'e' is not defined
> 
> Python has to assume that `e` might be defined at runtime. However,
> style checkers will complain.

OK, I'm aware about the dynamic nature but in this case it is somewhat
counter intuitive since it is part of the exception clause. You'd except
the Python interpreter to complain.

So, is Flake8 like the standard to be used?

/Jarkko

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH] selftests/tpm2: Change exception handling to be Python 3 compatible
  2020-04-14  7:38           ` Jarkko Sakkinen
@ 2020-04-14 11:14             ` Ezra Buehler
  2020-04-14 16:05               ` Jarkko Sakkinen
  0 siblings, 1 reply; 9+ messages in thread
From: Ezra Buehler @ 2020-04-14 11:14 UTC (permalink / raw)
  To: Jarkko Sakkinen
  Cc: Shuah Khan, linux-kselftest, linux-integrity, Tadeusz Struk, open list

On 14 Apr 2020, at 09:38, Jarkko Sakkinen <jarkko.sakkinen@linux.intel.com> wrote:
> OK, I'm aware about the dynamic nature but in this case it is somewhat
> counter intuitive since it is part of the exception clause. You'd except
> the Python interpreter to complain.

I agree.

> So, is Flake8 like the standard to be used?

Pretty much, yes. There is also Pylint though. Among other things, they
both check for PEP 8 (official) coding style compliance.

Cheers,
Ezra.

> On 14 Apr 2020, at 09:38, Jarkko Sakkinen <jarkko.sakkinen@linux.intel.com> wrote:
> 
> On Tue, Apr 14, 2020 at 07:45:33AM +0200, Ezra Buehler wrote:
>> Hi Jarkko,
>> 
>> On 13 Apr 2020, at 20:04, Jarkko Sakkinen <jarkko.sakkinen@linux.intel.com> wrote:
>>> 
>>> On Mon, Apr 13, 2020 at 07:02:20AM +0200, Ezra Buehler wrote:
>>>> Hi Jarkko,
>>>> 
>>>> On 12 Apr 2020, at 19:07, Jarkko Sakkinen <jarkko.sakkinen@linux.intel.com> wrote:
>>>>> 
>>>>> On Sun, Apr 12, 2020 at 05:02:27PM +0200, Ezra Buehler wrote:
>>>>>> Hi Jarkkon,
>>>>>> 
>>>>>>> On 12 Apr 2020, at 16:36, Jarkko Sakkinen <jarkko.sakkinen@linux.intel.com> wrote:
>>>>>>> +        except ProtocolError(e):
>>>>>> 
>>>>>> Should this not be
>>>>>> 
>>>>>>      except ProtocolError as e:
>>>>> 
>>>>> Unless there is a functional difference, does it matter?
>>>>> 
>>>>> /Jarkko
>>>> 
>>>> Well, your patch confuses me a lot. It looks to me like you are passing
>>>> the undefined `e` variable to the constructor.
>>>> 
>>>> When I run flake8 on it I get following error (among others):
>>>> 
>>>>   F821 undefined name 'e'
>>> 
>>> I don't know what flake8 is.
>> 
>> https://flake8.pycqa.org/en/latest/
>> 
>>>> What I suggested is the standard syntax:
>>>> https://docs.python.org/3/tutorial/errors.html
>>> 
>>> It passed the Python 3 interpreter.
>> 
>> That is because it is technically valid syntax.
>> 
>>>> Did you test this? You should get an error as soon as an exception
>>>> occurs.
>>> 
>>> Yes. Interpreter did not complain. I did not know that the language
>>> is broken that way that you have to exercise the code path to get
>>> a syntax error.
>> 
>> That is due to the dynamic nature of Python. You won’t get a syntax
>> error. You will get an exception:
>> 
>>    NameError: name 'e' is not defined
>> 
>> Python has to assume that `e` might be defined at runtime. However,
>> style checkers will complain.
> 
> OK, I'm aware about the dynamic nature but in this case it is somewhat
> counter intuitive since it is part of the exception clause. You'd except
> the Python interpreter to complain.
> 
> So, is Flake8 like the standard to be used?
> 
> /Jarkko


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH] selftests/tpm2: Change exception handling to be Python 3 compatible
  2020-04-14 11:14             ` Ezra Buehler
@ 2020-04-14 16:05               ` Jarkko Sakkinen
  0 siblings, 0 replies; 9+ messages in thread
From: Jarkko Sakkinen @ 2020-04-14 16:05 UTC (permalink / raw)
  To: Ezra Buehler
  Cc: Shuah Khan, linux-kselftest, linux-integrity, Tadeusz Struk, open list

On Tue, Apr 14, 2020 at 01:14:11PM +0200, Ezra Buehler wrote:
> On 14 Apr 2020, at 09:38, Jarkko Sakkinen <jarkko.sakkinen@linux.intel.com> wrote:
> > OK, I'm aware about the dynamic nature but in this case it is somewhat
> > counter intuitive since it is part of the exception clause. You'd except
> > the Python interpreter to complain.
> 
> I agree.
> 
> > So, is Flake8 like the standard to be used?
> 
> Pretty much, yes. There is also Pylint though. Among other things, they
> both check for PEP 8 (official) coding style compliance.

Thank you for taking time explaining all this. I'll make sure to include
these to my process when I update my test from now on.

/Jarkko

^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2020-04-14 16:05 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-04-12 14:36 [PATCH] selftests/tpm2: Change exception handling to be Python 3 compatible Jarkko Sakkinen
2020-04-12 15:02 ` Ezra Buehler
2020-04-12 17:07   ` Jarkko Sakkinen
2020-04-13  5:02     ` Ezra Buehler
2020-04-13 18:04       ` Jarkko Sakkinen
2020-04-14  5:45         ` Ezra Buehler
2020-04-14  7:38           ` Jarkko Sakkinen
2020-04-14 11:14             ` Ezra Buehler
2020-04-14 16:05               ` Jarkko Sakkinen

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).