home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2011 July / maximum-cd-2011-07.iso / DiscContents / LibO_3.3.2_Win_x86_install_multi.exe / libreoffice1.cab / fix_execfile.py < prev    next >
Encoding:
Python Source  |  2011-03-15  |  1.1 KB  |  38 lines

  1. # Copyright 2006 Google, Inc. All Rights Reserved.
  2. # Licensed to PSF under a Contributor Agreement.
  3.  
  4. """Fixer for execfile.
  5.  
  6. This converts usages of the execfile function into calls to the built-in
  7. exec() function.
  8. """
  9.  
  10. from .. import pytree
  11. from .. import fixer_base
  12. from ..fixer_util import Comma, Name, Call, LParen, RParen, Dot
  13.  
  14.  
  15. class FixExecfile(fixer_base.BaseFix):
  16.  
  17.     PATTERN = """
  18.     power< 'execfile' trailer< '(' arglist< filename=any [',' globals=any [',' locals=any ] ] > ')' > >
  19.     |
  20.     power< 'execfile' trailer< '(' filename=any ')' > >
  21.     """
  22.  
  23.     def transform(self, node, results):
  24.         assert results
  25.         syms = self.syms
  26.         filename = results["filename"]
  27.         globals = results.get("globals")
  28.         locals = results.get("locals")
  29.         args = [Name('open'), LParen(), filename.clone(), RParen(), Dot(),
  30.                 Name('read'), LParen(), RParen()]
  31.         args[0].set_prefix("")
  32.         if globals is not None:
  33.             args.extend([Comma(), globals.clone()])
  34.         if locals is not None:
  35.             args.extend([Comma(), locals.clone()])
  36.  
  37.         return Call(Name("exec"), args, prefix=node.get_prefix())
  38.