Skip to main content

If you’re a library or framework creator then it is nice to be able to create APIs that can be used either as decorators or context managers.

The contextdecorator module is a backport of new features added to the contextlib module in Python 3.2. contextdecorator works with Python 2.4+ including Python 3.

Context managers inheriting from ContextDecorator have to implement __enter__ and __exit__ as normal. __exit__ retains its optional exception handling even when used as a decorator.

Example:

from contextdecorator import ContextDecorator

class mycontext(ContextDecorator):
   def __enter__(self):
      print 'Starting'
      return self

   def __exit__(self, *exc):
      print 'Finishing'
      return False

>>> @mycontext()
... def function():
...    print 'The bit in the middle'
...
>>> function()
Starting
The bit in the middle
Finishing

>>> with mycontext():
...    print 'The bit in the middle'
...
Starting
The bit in the middle
Finishing

Existing context managers that already have a base class can be extended by using ContextDecorator as a mixin class:

from contextdecorator import ContextDecorator

class mycontext(ContextBaseClass, ContextDecorator):
   def __enter__(self):
      return self

   def __exit__(self, *exc):
      return False

contextdecorator also contains an implementation of contextlib.contextmanager that uses ContextDecorator. The context managers it creates can be used as decorators as well as in with statements.

from contextdecorator import contextmanager

@contextmanager
def mycontext(*args):
   print 'Started'
   try:
      yield
   finally:
      print 'Finished!'

>>> @mycontext('some', 'args')
... def function():
...    print 'In the middle'
...
Started
In the middle
Finished!

>>> with mycontext('some', 'args'):
...    print 'In the middle'
...
Started
In the middle
Finished!

Repository and issue tracker:

The project is available for download from PyPI so it can be easily installed:

pip install -U contextdecorator
easy_install -U contextdecorator

The tests require unittest2 to run.

Release files for contextdecorator 0.10.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distributions (sdists)

Source distribution for contextdecorator 0.10.0
File Size Uploaded
contextdecorator-0.10.0.zip 6.8 kB Details
contextdecorator-0.10.0.tar.gz 4.6 kB Details

Release files / contextdecorator-0.10.0.zip

Download URL contextdecorator-0.10.0.zip
Size 6.8 kB
Tags Source
SHA-256 checksum
How to use checksums
e6ee12393ea89a6222152cd103d8f55397ff13e08cb513f48ccc2739768b9171
BLAKE2b-256 checksum
How to use checksums
f68bbb30402c613cb77ab5822ed71ac00f4349231519161ed0265bfb5f5aaae4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / contextdecorator-0.10.0.tar.gz

Download URL contextdecorator-0.10.0.tar.gz
Size 4.6 kB
Tags Source
SHA-256 checksum
How to use checksums
44645affa722c72b4931d48a4ff9265a8df19373e20e12334bca4ec2104f8619
BLAKE2b-256 checksum
How to use checksums
774303f264fa07fb0f794bfe174751eb6e6e294a89fc53af87ea1cb0df26ac18
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

0.10.0 This release

2 release files

0.0

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page