exec语句用来执行储存在字符串或文件中的Python语句
>>> exec 'print "Hello World"'
Hello World
注意例子中exec语句的用法和eval_r(), execfile()是不一样的. exec是一个语句(就象print或while), 而eval_r()和execfile()则是内建函数.
check_list = [
'import uno',
'import unohelper',
]
def check_deps(check_list):
error = False
import_errors = []
for imp in check_list:
try:
exec imp in {}
except ImportError,e:
error = True
import_errors.append(str(e))
if error:
raise osv.except_osv(_('Warning!')+' '+_('Unmet python dependencies!'), '\n'.join(import_errors))
>>> a={}
>>> print a
{}
>>> exec 'print "hello"' in a
hello
>>> print a
{'__builtins__': {'bytearray':
, 'IndexError': , 'all': , 'help': Type help() for interactive help, or help(object) for help about object., 'vars': , 'SyntaxError': , 'unicode': , 'UnicodeDecodeError': , 'memoryview': , 'isinstance': , 'copyright': Copyright (c) 2001-2014 Python Software Foundation.
All Rights Reserved.
Copyright (c) 2000 BeOpen.com.
All Rights Reserved.
Copyright (c) 1995-2001 Corporation for National Research Initiatives.
All Rights Reserved.
Copyright (c) 1991-1995 Stichting Mathematisch Centrum, Amsterdam.
All Rights Reserved., 'NameError': , 'BytesWarning': , 'dict': , 'input': , 'oct': , 'bin': , 'SystemExit': , 'StandardError': , 'format': , 'repr': , 'sorted': , 'False': False, 'RuntimeWarning': , 'list': , 'iter': , 'reload': , 'Warning': , '__package__': None, 'round': , 'dir': , 'cmp': , 'set': , 'bytes': , 'reduce': , 'intern': , 'issubclass': , 'Ellipsis': Ellipsis, 'EOFError': , 'locals': , 'BufferError': , 'slice': , 'FloatingPointError': , 'sum': , 'getattr': , 'abs': , 'exit': Use exit() or Ctrl-D (i.e. EOF) to exit, 'print': , 'True': True, 'FutureWarning': , 'ImportWarning': , 'None': None, 'hash': , 'ReferenceError': , 'len': , 'credits': Thanks to CWI, CNRI, BeOpen.com, Zope Corporation and a cast of thousands
for supporting Python development. See for more information., 'frozenset': , '__name__': '__builtin__', 'ord': , 'super': , '_': False, 'TypeError': , 'license': Type license() to see the full license text, 'KeyboardInterrupt': , 'UserWarning': , 'filter': , 'range': , 'staticmethod': , 'SystemError': , 'BaseException': , 'pow': , 'RuntimeError': , 'float': , 'MemoryError': , 'StopIteration': , 'globals': , 'divmod': , 'enumerate': , 'apply': , 'LookupError': , 'open': , 'quit': Use quit() or Ctrl-D (i.e. EOF) to exit, 'basestring': , 'UnicodeError': , 'zip': , 'hex': , 'long': , 'next': , 'ImportError': , 'chr': , 'xrange': , 'type': , '__doc__': "Built-in functions, exceptions, and other objects.\n\nNoteworthy: None is the `nil' object; Ellipsis represents `...' in slices.", 'Exception': , 'tuple': , 'UnicodeTranslateError': , 'reversed': , 'UnicodeEncodeError': , 'IOError': , 'hasattr': , 'delattr': , 'setattr': , 'raw_input': , 'SyntaxWarning': , 'compile': , 'ArithmeticError': , 'str': , 'property': , 'GeneratorExit': , 'int': , '__import__': , 'KeyError': , 'coerce': , 'PendingDeprecationWarning': , 'file': , 'EnvironmentError': , 'unichr': , 'id': , 'OSError': , 'DeprecationWarning': , 'min': , 'UnicodeWarning': , 'execfile': , 'any': , 'complex': , 'bool': , 'ValueError': , 'NotImplemented': NotImplemented, 'map': , 'buffer': , 'max': , 'object': , 'TabError': , 'callable': , 'ZeroDivisionError': , 'eval': , '__debug__': True, 'IndentationError': , 'AssertionError': , 'classmethod': , 'UnboundLocalError': , 'NotImplementedError': , 'AttributeError': , 'OverflowError': }}
阅读(4764) | 评论(0) | 转发(0) |