Ported context.py from 2.x