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 -def with_(mgr, body):
    26      """A function to mimic the with statement introduced in Python 2.5
    27
    28      The code below was taken from http://www.python.org/dev/peps/pep-0343/
    29      """
    30      exit = mgr.__exit__  # Not calling it yet
    31      value = mgr.__enter__()
    32      exc = True
    33      try:
    34          try:
    35              if isinstance(value, (tuple, list)):
    36                  return body(*value)
    37              else:
    38                  return body(value)
    39          except:
    40              # The exceptional case is handled here
    41              exc = False
    42              if not exit(*sys.exc_info()):
    43                  raise
    44              # The exception is swallowed if exit() returns true
    45      finally:
    46          # The normal and non-local-goto cases are handled here
    47          if exc:
    48              exit(None, None, None)
    49