diff options
| author | Justus Winter <[email protected]> | 2016-06-07 17:31:10 +0000 | 
|---|---|---|
| committer | Justus Winter <[email protected]> | 2016-06-08 11:44:09 +0000 | 
| commit | e3c5913a33edcbd7329b8d154c669f95ce782038 (patch) | |
| tree | e8349c08388ca61dcfe2acb0b87c5fa5f675a061 /lang/python/tests/t-idiomatic.py | |
| parent | python: Fix error handling. (diff) | |
| download | gpgme-e3c5913a33edcbd7329b8d154c669f95ce782038.tar.gz gpgme-e3c5913a33edcbd7329b8d154c669f95ce782038.zip | |
python: Implement the context manager protocol.
* lang/python/pyme/core.py (Context.__del__): Make function
idemptotent.
(Context.{__enter__,__exit__}): Implement the context manager
protocol.
(Data.__del__): Make function idemptotent, drop debug print.
(Data.{__enter__,__exit__}): Implement the context manager
protocol.
* lang/python/tests/t-idiomatic.py: Demonstrate this.
Signed-off-by: Justus Winter <[email protected]>
Diffstat (limited to 'lang/python/tests/t-idiomatic.py')
| -rwxr-xr-x | lang/python/tests/t-idiomatic.py | 19 | 
1 files changed, 14 insertions, 5 deletions
| diff --git a/lang/python/tests/t-idiomatic.py b/lang/python/tests/t-idiomatic.py index 05a377e4..37cfb64a 100755 --- a/lang/python/tests/t-idiomatic.py +++ b/lang/python/tests/t-idiomatic.py @@ -23,7 +23,15 @@ from pyme import core, constants, errors  import support  support.init_gpgme(constants.PROTOCOL_OpenPGP) -c = core.Context() + +# Both Context and Data can be used as context manager: +with core.Context() as c, core.Data() as d: +    c.get_engine_info() +    d.write(b"Halloechen") +    leak_c = c +    leak_d = d +assert leak_c.wrapped == None +assert leak_d.wrapped == None  # Demonstrate automatic wrapping of file-like objects with 'fileno'  # method. @@ -33,10 +41,11 @@ with tempfile.TemporaryFile() as source, \      source.write(b"Hallo Leute\n")      source.seek(0, os.SEEK_SET) -    c.op_sign(source, signed, constants.SIG_MODE_NORMAL) -    signed.seek(0, os.SEEK_SET) -    c.op_verify(signed, None, sink) -    result = c.op_verify_result() +    with core.Context() as c: +        c.op_sign(source, signed, constants.SIG_MODE_NORMAL) +        signed.seek(0, os.SEEK_SET) +        c.op_verify(signed, None, sink) +        result = c.op_verify_result()      assert len(result.signatures) == 1, "Unexpected number of signatures"      sig = result.signatures[0] | 
