Package translate :: Package misc :: Module context

[hide private]
[frames] | no frames]

# Source Code for Module translate.misc.context

     1  #!/usr/bin/env python
     2  # -*- coding: utf-8 -*-
     3  #
     4  # Copyright 2002-2006 Zuza Software Foundation
     5  #
     6  # This file is part of translate.
     7  #
     8  # translate is free software; you can redistribute it and/or modify
     9  # it under the terms of the GNU General Public License as published by
    10  # the Free Software Foundation; either version 2 of the License, or
    11  # (at your option) any later version.
    12  #
    13  # translate is distributed in the hope that it will be useful,
    14  # but WITHOUT ANY WARRANTY; without even the implied warranty of
    15  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    16  # GNU General Public License for more details.
    17  #
    18  # You should have received a copy of the GNU General Public License
    19  # along with translate; if not, write to the Free Software
    20  # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    21  #
    22
    23  import sys
    24
    25
    26 -def with_(mgr, body):
    27      """A function to mimic the with statement introduced in Python 2.5
    28
    29      The code below was taken from http://www.python.org/dev/peps/pep-0343/
    30      """
    31      exit = mgr.__exit__  # Not calling it yet
    32      value = mgr.__enter__()
    33      exc = True
    34      try:
    35          try:
    36              if isinstance(value, (tuple, list)):
    37                  return body(*value)
    38              else:
    39                  return body(value)
    40          except:
    41              # The exceptional case is handled here
    42              exc = False
    43              if not exit(*sys.exc_info()):
    44                  raise
    45              # The exception is swallowed if exit() returns true
    46      finally:
    47          # The normal and non-local-goto cases are handled here
    48          if exc:
    49              exit(None, None, None)
    50