{"id":350614,"date":"2023-07-21T15:02:32","date_gmt":"2023-07-21T15:02:32","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=350614"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=350614","title":{"rendered":"<span>\u041a\u0430\u043a \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u043e \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u043a\u0430\u043b\u044c\u043a\u0443\u043b\u044f\u0442\u043e\u0440 \u043d\u0430 \u043f\u0438\u0442\u043e\u043d\u0435 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e eval()<\/span>"},"content":{"rendered":"<div><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u0412\u00a0\u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u044f\u0445 \u043a\u00a0\u0441\u0442\u0430\u0442\u044c\u044f\u043c \u043f\u043e\u00a0\u0441\u0438\u043d\u0442\u0430\u043a\u0441\u0438\u0447\u0435\u0441\u043a\u043e\u043c\u0443 \u0430\u043d\u0430\u043b\u0438\u0437\u0443 \u044f \u0438\u043d\u043e\u0433\u0434\u0430 \u0432\u0438\u0436\u0443 \u0442\u0430\u043a\u0438\u0435:<\/p>\n<blockquote>\n<p>\u043d\u0430\u00a0\u043f\u0438\u0442\u043e\u043d\u0435 \u043a\u0430\u043b\u044c\u043a\u0443\u043b\u044f\u0442\u043e\u0440 \u043f\u0438\u0448\u0435\u0442\u0441\u044f \u043f\u0440\u043e\u0449\u0435 \u043f\u0440\u043e\u0441\u0442\u043e\u0433\u043e \u2014 <code>print(eval(input()))<\/code><\/p>\n<\/blockquote>\n<p>\u041d\u0443, \u0432\u043e\u0431\u0449\u0435\u043c\u2011\u0442\u043e\u00a0\u2014 \u0434\u0430, \u043d\u043e\u00a0\u0435\u0441\u043b\u0438, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0432\u044b \u043f\u0440\u0438\u043a\u0440\u0443\u0442\u0438\u0442\u0435 \u0442\u0430\u043a\u043e\u0439 \u043a\u0430\u043b\u044c\u043a\u0443\u043b\u044f\u0442\u043e\u0440 \u043a\u00a0\u0441\u0432\u043e\u0435\u043c\u0443 \u0441\u0430\u0439\u0442\u0443, \u0442\u043e \u043b\u044e\u0431\u043e\u0439\u00a0\u0436\u0435\u043b\u0430\u044e\u0449\u0438\u0439 \u0432\u043c\u0435\u0441\u0442\u043e <code>2+2*2<\/code> \u043c\u043e\u0436\u0435\u0442 \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c <code>exec(\"import os; os.removedirs('\/')\")<\/code>, \u043f\u0440\u0435\u0434\u0432\u0430\u0440\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u0438\u0437\u0443\u0447\u0438\u0432 \u0432\u0441\u0435 \u0432\u0430\u0448\u0438 \u0441\u0435\u043a\u0440\u0435\u0442\u043d\u044b\u0435 \u0444\u0430\u0439\u043b\u044b \u043f\u043e\u0434\u043e\u0431\u043d\u044b\u043c\u00a0\u0436\u0435 \u043e\u0431\u0440\u0430\u0437\u043e\u043c. \u0422\u0430\u043a\u0430\u044f \u043f\u0435\u0440\u0441\u043f\u0435\u043a\u0442\u0438\u0432\u0430 \u043d\u0435\u00a0\u043c\u043e\u0436\u0435\u0442 \u0440\u0430\u0434\u043e\u0432\u0430\u0442\u044c, \u043d\u043e\u00a0\u0438 \u043e\u0442\u043a\u0430\u0437\u044b\u0432\u0430\u0442\u044c\u0441\u044f \u043e\u0442 <code>eval()<\/code> \u0442\u043e\u0436\u0435 \u043d\u0435\u00a0\u0441\u0442\u043e\u0438\u0442.<\/p>\n<p>\u2014 \u0410\u00a0\u0447\u0442\u043e\u00a0\u0434\u0435\u043b\u0430\u0442\u044c\u2011\u0442\u043e?\u00a0\u2014 \u0441\u043f\u0440\u043e\u0441\u0438\u0442\u0435 \u0432\u044b. \u041e\u0442\u0432\u0435\u0442 \u043f\u0440\u043e\u0441\u0442\u043e\u0439: \u0432\u0430\u043b\u0438\u0434\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0432\u0445\u043e\u0434\u044f\u0449\u0438\u0435 \u0434\u0430\u043d\u043d\u044b\u0435, \u043a\u0430\u043a\u00a0\u0432\u044b \u044d\u0442\u043e \u0432\u0441\u0435\u0433\u0434\u0430 \u0434\u0435\u043b\u0430\u0435\u0442\u0435. \u0422\u043e\u043b\u044c\u043a\u043e \u043d\u0435\u00a0\u0442\u0435, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043f\u0435\u0440\u0435\u0434\u0430\u044e\u0442\u0441\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u0438 <code>eval()<\/code>\u00a0\u2014 \u0434\u043b\u044f\u00a0\u044d\u0442\u043e\u0433\u043e \u0432\u0430\u043c \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u043f\u0440\u0438\u0448\u043b\u043e\u0441\u044c \u0431\u044b \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0441\u0432\u043e\u0439 \u0441\u0438\u043d\u0442\u0430\u043a\u0441\u0438\u0447\u0435\u0441\u043a\u0438\u0439 \u0430\u043d\u0430\u043b\u0438\u0437\u0430\u0442\u043e\u0440, \u0430\u00a0\u0432\u043d\u0443\u0442\u0440\u0435\u043d\u043d\u0438\u0435 \u0434\u0430\u043d\u043d\u044b\u0435, \u043a\u043e\u0442\u043e\u0440\u044b\u043c\u0438 \u043e\u043f\u0435\u0440\u0438\u0440\u0443\u0435\u0442 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f <code>eval()<\/code>.<\/p>\n<p>\u2014 \u0410! \u042f \u0437\u043d\u0430\u044e\u00a0\u2014 \u0441\u043a\u0430\u0436\u0435\u0442\u0435 \u0432\u044b\u00a0\u2014 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c <code>compile()<\/code>, \u0432\u0430\u043b\u0438\u0434\u0438\u0440\u0443\u0435\u043c \u043a\u043e\u0434 \u0438 \u0442\u043e\u0433\u0434\u0430 \u0443\u0436\u0435 \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u043c <code>eval()<\/code>. \u0427\u0442\u043e-\u0436, \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e \u0438 \u0442\u0430\u043a. \u041d\u043e\u00a0\u044f \u043d\u0435\u00a0\u0437\u043d\u0430\u044e \u043f\u0440\u043e\u0441\u0442\u043e\u0433\u043e \u0441\u043f\u043e\u0441\u043e\u0431\u0430 \u0432\u0430\u043b\u0438\u0434\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0431\u0430\u0439\u0442\u2011\u043a\u043e\u0434. \u041d\u0435\u0442. \u041d\u0430\u043c \u043d\u0430\u0434\u043e \u0432\u0430\u043b\u0438\u0434\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u043f\u0438\u0442\u043e\u043d\u043e\u0432\u0441\u043a\u043e\u0433\u043e \u0441\u0438\u043d\u0442\u0430\u043a\u0441\u0438\u0447\u0435\u0441\u043a\u043e\u0433\u043e \u0430\u043d\u0430\u043b\u0438\u0437\u0430\u0442\u043e\u0440\u0430\u00a0\u2014 \u0430\u0431\u0441\u0442\u0440\u0430\u043a\u0442\u043d\u043e\u0435 \u0441\u0438\u043d\u0442\u0430\u043a\u0441\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u0434\u0435\u0440\u0435\u0432\u043e \u0438\u043b\u0438\u00a0AST. \u042d\u0442\u043e \u0433\u043e\u0440\u0430\u0437\u0434\u043e \u043f\u0440\u043e\u0449\u0435, \u0445\u043e\u0442\u044c \u0438 \u0437\u0432\u0443\u0447\u0438\u0442 \u043f\u0443\u0433\u0430\u044e\u0449\u0435.<\/p>\n<p>\u041d\u0430\u043f\u0438\u0448\u0435\u043c \u0441\u0432\u043e\u0439 <code>eval()<\/code> \u0442\u0430\u043a:<\/p>\n<pre><code class=\"python\">def my_eval(expression):     tree = ast.parse(expression, mode='eval')     code = compile(tree, filename='', mode='eval')     return eval(code)<\/code><\/pre>\n<p>\u041d\u0430\u043c, \u0435\u0441\u0442\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u043d\u043e, \u043f\u043e\u043d\u0430\u0434\u043e\u0431\u0438\u0442\u0441\u044f \u043c\u043e\u0434\u0443\u043b\u044c ast:<\/p>\n<pre><code>>>> import ast<\/code><\/pre>\n<p>\u041f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u043c &#8212; \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u043b\u0438?<\/p>\n<pre><code>>>> print(my_eval(input())) 2+2*2 6<\/code><\/pre>\n<p>\u0418\u0442\u0430\u043a, \u0443 \u043d\u0430\u0441 \u0435\u0441\u0442\u044c <code>tree<\/code>. \u0412\u0430\u043c, \u043a\u043e\u043d\u0435\u0447\u043d\u043e \u0436\u0435, \u043b\u044e\u0431\u043e\u043f\u044b\u0442\u043d\u043e, \u0447\u0442\u043e \u0442\u0430\u043c \u0432\u043d\u0443\u0442\u0440\u0438, \u0438 \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0442\u0435\u043b\u044c\u043d\u043e, \u043d\u0430\u0434\u043e \u0436\u0435 \u0437\u043d\u0430\u0442\u044c, \u0447\u0442\u043e \u043c\u044b \u0441\u043e\u0431\u0438\u0440\u0430\u0435\u043c\u0441\u044f \u0432\u0430\u043b\u0438\u0434\u0438\u0440\u043e\u0432\u0430\u0442\u044c. \u041d\u043e \u043f\u0440\u043e\u0441\u0442\u043e \u0442\u0430\u043a \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u043d\u0435 \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u0441\u044f \u2014 \u044d\u0442\u043e \u043e\u0431\u044a\u0435\u043a\u0442,  \u0438 <code>print()<\/code> \u0438\u043b\u0438 <code>pprint()<\/code>\u0432\u044b\u0434\u0430\u0434\u0443\u0442 \u0432\u0441\u0435\u0433\u043e \u043b\u0438\u0448\u044c <code>&lt;ast.Expression object at 0x7f6de7eb3c70><\/code> \u042f \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043b\u0441\u044f \u0442\u0430\u043a\u043e\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0435\u0439:<\/p>\n<pre><code class=\"python\">def dump(node):     def _format(node, indent):         if isinstance(node, ast.AST):             print('%sAST %s' % (' ' * indent, node.__class__.__name__))             for a, b in ast.iter_fields(node):                 print('%s%s' % (' ' * indent, a))                 _format(b, indent + 4)         elif isinstance(node, list):             print('%sLIST %s' % (' ' * indent, node.__class__.__name__))             for x in node:                 _format(x, indent)         else:             print('%s%s' % (' ' * indent, repr(node)))     _format(node, 0)<\/code><\/pre>\n<p>\u041c\u043e\u0436\u0435\u0442\u0435 \u0432\u0441\u0442\u0430\u0432\u0438\u0442\u044c <code>dump(tree)<\/code> \u0432 <code>my_eval(),<\/code> \u043f\u043e\u0438\u0433\u0440\u0430\u0442\u044c\u0441\u044f \u0438 \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c, \u043a\u0430\u043a\u043e\u0435 \u043e\u043d\u043e \u2014 \u044d\u0442\u043e \u0434\u0435\u0440\u0435\u0432\u043e. \u0422\u043e\u043b\u044c\u043a\u043e \u043d\u0435 \u043d\u0430\u0434\u043e \u043a\u043e\u043f\u0438\u043f\u0430\u0441\u0442\u0438\u0442\u044c \u043c\u043e\u0439 \u043f\u0440\u0438\u043c\u0435\u0440 \u0441 <code>removedirs()<\/code>.<\/p>\n<p>\u041a\u043e\u043c\u0443 \u0438\u0433\u0440\u0430\u0442\u044c\u0441\u044f \u043b\u0435\u043d\u0438\u0432\u043e \u2014 \u0432\u043e\u0442 \u043f\u0430\u0440\u0430 \u043f\u0440\u0438\u043c\u0435\u0440\u043e\u0432:<\/p>\n<pre><code>>>> print(my_eval(input())) 2+2*2 AST Expression body     AST BinOp     left         AST Constant         value             2         kind             None     op         AST Add     right         AST BinOp         left             AST Constant             value                 2             kind                 None         op             AST Mult         right             AST Constant             value                 2             kind                 None 6<\/code><\/pre>\n<pre><code>>>> print(my_eval(input())) print(globals()) AST Expression body     AST Call     func         AST Name         id             'print'         ctx             AST Load     args         LIST list         AST Call         func             AST Name             id                 'globals'             ctx                 AST Load         args             LIST list         keywords             LIST list     keywords         LIST list {'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': &lt;class '_frozen_importlib.BuiltinImporter'>, '__spec__': None, '__annotations__': {}, '__builtins__': &lt;module 'builtins' (built-in)>, 'my_eval': &lt;function my_eval at 0x7f6de9225480>, 'ast': &lt;module 'ast' from '\/usr\/lib\/python3.10\/ast.py'>, 'dump': &lt;function dump at 0x7f6de7efb490>} None<\/code><\/pre>\n<p>\u0412\u0438\u0434\u0438\u0442\u0435, \u0447\u0435\u043c \u043e\u0442\u043b\u0438\u0447\u0430\u044e\u0442\u0441\u044f \u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u044b\u0435 \u0432\u044b\u0440\u0430\u0436\u0435\u043d\u0438\u044f \u043e\u0442 \u043d\u0435\u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u044b\u0445? \u0411\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u044b\u0435 \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0442 \u0442\u043e\u043b\u044c\u043a\u043e <code>Constant<\/code> \u0438 <code>BinOp<\/code>, \u0430 \u043d\u0435\u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u044b\u0435 \u2014 \u0432\u0441\u044f\u043a\u0438\u0435 <code>Call<\/code> \u0438 <code>Name<\/code>. \u041f\u043e\u043b\u043d\u044b\u0439 \u0441\u043f\u0438\u0441\u043e\u043a \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439 \u0438 \u0434\u0440\u0443\u0433\u0438\u0445 \u0443\u0437\u043b\u043e\u0432 \u0434\u0435\u0440\u0435\u0432\u0430 \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u0432 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u0438 \u043a \u043c\u043e\u0434\u0443\u043b\u044e ast. \u042d\u0442\u043e \u043f\u043e\u043c\u043e\u0436\u0435\u0442 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c\u0441\u044f, \u0447\u0442\u043e \u0437\u0430\u043f\u0440\u0435\u0442\u0438\u0442\u044c, \u0430 \u0447\u0442\u043e \u0440\u0430\u0437\u0440\u0435\u0448\u0438\u0442\u044c. \u0414\u043b\u044f \u043f\u0440\u043e\u0441\u0442\u043e\u0433\u043e \u043a\u0430\u043b\u044c\u043a\u0443\u043b\u044f\u0442\u043e\u0440\u0430, \u044f \u0441\u0447\u0438\u0442\u0430\u044e, \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u0440\u0430\u0437\u0440\u0435\u0448\u0438\u0442\u044c \u043e\u0441\u043d\u043e\u0432\u043d\u044b\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 <code>BinOp<\/code> \u0438 <code>UnaryOp<\/code>. \u041f\u043b\u044e\u0441 <code>Constant<\/code>.<\/p>\n<p>\u041f\u043e\u0440\u0430 \u043f\u0438\u0441\u0430\u0442\u044c \u0432\u0430\u043b\u0438\u0434\u0430\u0442\u043e\u0440:<\/p>\n<pre><code class=\"python\">_allowed_nodes = (     # \u0431\u0430\u0437\u043e\u0432\u044b\u0435 \u0443\u0437\u043b\u044b:     ast.BinOp, ast.UnaryOp, ast.Constant,      # \u043e\u0441\u043d\u043e\u0432\u043d\u044b\u0435 BinOps:     ast.Add, ast.Sub, ast.Mult, ast.Div, ast.FloorDiv, ast.Mod, ast.Pow,      # \u043e\u0441\u043d\u043e\u0432\u043d\u044b\u0435 UnaryOps:     ast.UAdd, ast.USub )  def validate_ast(tree):      # \u0432\u0430\u043b\u0438\u0434\u0438\u0440\u0443\u0435\u043c \u043a\u043e\u0440\u0435\u043d\u044c \u0434\u0435\u0440\u0435\u0432\u0430     if not isinstance(tree, ast.Expression):         raise Exception('\u041d\u0435\u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u043e\u0435 \u0432\u044b\u0440\u0430\u0436\u0435\u043d\u0438\u0435')      # \u0432\u0430\u043b\u0438\u0434\u0438\u0440\u0443\u0435\u043c \u0443\u0437\u043b\u044b     def validate_children(node):         for child in ast.iter_child_nodes(node):             if not isinstance(child, _allowed_nodes):                 raise Exception('\u041d\u0435\u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u043e\u0435 \u0432\u044b\u0440\u0430\u0436\u0435\u043d\u0438\u0435')             validate_children(child)      validate_children(tree.body)<\/code><\/pre>\n<p>\u0412\u043e\u0442 \u0442\u0430\u043a \u0432\u0441\u0451 \u043f\u0440\u043e\u0441\u0442\u043e. \u0421\u043c\u0435\u043b\u043e \u0432\u044b\u0437\u044b\u0432\u0430\u0439\u0442\u0435 \u044d\u0442\u0443 \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u0438\u0437 <code>my_eval()<\/code> \u043f\u0435\u0440\u0435\u0434 <code>compile()<\/code>:<\/p>\n<pre><code class=\"python\">def my_eval(expression):     tree = ast.parse(expression, mode='eval')     validate_ast(tree)     code = compile(tree, filename='', mode='eval')     return eval(code)<\/code><\/pre>\n<p>\u0438 \u0431\u0443\u0434\u0435\u0442 \u0432\u0430\u043c \u0441\u0447\u0430\u0441\u0442\u044c\u0435. \u041d\u043e, \u0438\u043c\u0435\u0439\u0442\u0435 \u0432\u0432\u0438\u0434\u0443, \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u044f \u043a \u0444\u0443\u043d\u043a\u0446\u0438\u0438 <code>compile()<\/code> \u0441\u043e\u043e\u0431\u0449\u0430\u0435\u0442:<\/p>\n<blockquote>\n<p>Warning<\/p>\n<p>It is possible to crash the Python interpreter with a sufficiently large\/complex string when compiling to an AST object due to stack depth limitations in Python\u2019s AST compiler.<\/p>\n<\/blockquote>\n<p>\u0422\u0430\u043a \u0447\u0442\u043e \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0438\u0432\u0430\u0439\u0442\u0435 \u0434\u043b\u0438\u043d\u0443 \u0441\u0442\u0440\u043e\u043a\u0438, \u043f\u0435\u0440\u0435\u0434\u0430\u0432\u0430\u0435\u043c\u043e\u0439 <code>my_eval()<\/code>. \u0418 \u0442\u043e\u0433\u0434\u0430 \u0441\u0447\u0430\u0441\u0442\u044c\u0435 \u0431\u0443\u0434\u0435\u0442 \u043d\u0430\u0441\u0442\u043e\u044f\u0449\u0438\u043c.<\/p>\n<p>\u2014 \u041f\u043e\u0441\u0442\u043e\u0439\u0442\u0435! \u2014 \u0441\u043a\u0430\u0436\u0435\u0442\u0435 \u0432\u044b \u2014 \u041c\u043d\u0435 \u043d\u0435 \u043d\u0443\u0436\u0435\u043d \u043f\u0440\u043e\u0441\u0442\u043e\u0439 \u0431\u0443\u0445\u0433\u0430\u043b\u0442\u0435\u0440\u0441\u043a\u0438\u0439 \u043a\u0430\u043b\u044c\u043a\u0443\u043b\u044f\u0442\u043e\u0440! \u0413\u0434\u0435 \u0445\u043e\u0442\u044c \u043a\u0430\u043a\u043e\u0439-\u043d\u0438\u0431\u0443\u0434\u044c \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439 \u043d\u0430\u0431\u043e\u0440 \u043c\u0430\u0442\u0435\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u0445 \u0444\u0443\u043d\u043a\u0446\u0438\u0439? \u041d\u0435 \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0442\u044c \u0436\u0435 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u0432 \u043f\u0435\u0448\u0435\u0435 \u043f\u0443\u0442\u0435\u0448\u0435\u0441\u0442\u0432\u0438\u0435 \u0438\u0441\u043a\u0430\u0442\u044c \u043a\u043e\u0441\u0438\u043d\u0443\u0441 \u0444\u0438?<\/p>\n<p>\u041c-\u0434\u0430, \u043f\u043e\u0436\u0430\u043b\u0443\u0439, \u0441\u043e\u0433\u043b\u0430\u0448\u0443\u0441\u044c \u0441 \u0432\u0430\u043c\u0438. \u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u0440\u0430\u0437\u0440\u0435\u0448\u0438\u043c \u043a\u043e\u0435-\u0447\u0442\u043e \u0438\u0437 \u043c\u043e\u0434\u0443\u043b\u044f <code>math<\/code>. \u041d\u043e \u044d\u0442\u043e \u0431\u0443\u0434\u0443\u0442 \u0432\u044b\u0437\u043e\u0432\u044b \u0444\u0443\u043d\u043a\u0446\u0438\u0439, \u0430 \u0437\u043d\u0430\u0447\u0438\u0442 \u2014 \u043d\u0435\u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u044b\u0435 \u043e\u0431\u043b\u0430\u0441\u0442\u0438 \u0442\u044c\u043c\u044b. \u041d\u0430\u043c \u043f\u0440\u0438\u0434\u0451\u0442\u0441\u044f \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c <code>ast.Call<\/code>, <code>ast.Name<\/code> \u0438 <code>ast.Load<\/code> \u0432 <code>_allowed_nodes<\/code>, \u0438 \u044d\u0442\u043e \u043e\u0442\u043a\u0440\u044b\u0432\u0430\u0435\u0442 \u043f\u0443\u0442\u044c \u043a <code>exec()<\/code>. \u0427\u0442\u043e\u0431\u044b \u0438\u0437\u0431\u0435\u0436\u0430\u0442\u044c \u043d\u0435\u043f\u0440\u0438\u044f\u0442\u043d\u043e\u0441\u0442\u0435\u0439, \u043d\u0430\u0434\u043e \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0438\u0442\u044c \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u043c  \u0432\u0430\u0436\u043d\u043e \u043d\u0430\u043b\u0438\u0447\u0438\u0435 \u043f\u0443\u0441\u0442\u043e\u0433\u043e <code>__builtins__<\/code>:<\/p>\n<pre><code class=\"python\">def my_eval(expression):     tree = ast.parse(expression, mode='eval')     validate_ast(tree)     code = compile(tree, filename='', mode='eval')     context = {'__builtins__': {}}     return eval(code, context)<\/code><\/pre>\n<p>\u041f\u043b\u044e\u0441, \u043d\u0430\u0434\u043e \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u043a\u0443 \u0443\u0437\u043b\u043e\u0432-\u0441\u043f\u0438\u0441\u043a\u043e\u0432 \u0432 \u0432\u0430\u043b\u0438\u0434\u0430\u0442\u043e\u0440:<\/p>\n<pre><code class=\"python\">def validate_ast(tree):      # \u0432\u0430\u043b\u0438\u0434\u0438\u0440\u0443\u0435\u043c \u043a\u043e\u0440\u0435\u043d\u044c \u0434\u0435\u0440\u0435\u0432\u0430     if not isinstance(tree, ast.Expression):         raise Exception('\u041d\u0435\u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u043e\u0435 \u0432\u044b\u0440\u0430\u0436\u0435\u043d\u0438\u0435')      # \u0432\u0430\u043b\u0438\u0434\u0438\u0440\u0443\u0435\u043c \u0443\u0437\u043b\u044b     def validate_children(node):         for child in ast.iter_child_nodes(node):             if isinstance(child, list):                 for grandchild in child:                     validate_children(grandchild)             else:                 if not isinstance(child, _allowed_nodes):                     raise Exception('\u041d\u0435\u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u043e\u0435 \u0432\u044b\u0440\u0430\u0436\u0435\u043d\u0438\u0435')                 validate_children(child)      validate_children(tree.body)<\/code><\/pre>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c, \u0435\u0441\u043b\u0438 \u043c\u044b \u0432\u0432\u0435\u0434\u0451\u043c<\/p>\n<p><code>exec('print(42)')<\/code><\/p>\n<p>\u0442\u043e \u043f\u043e\u043b\u0443\u0447\u0438\u043c<\/p>\n<p><code>NameError: name 'exec' is not defined<\/code><\/p>\n<p>\u0423\u0440\u0430! \u041d\u0438\u0447\u0435\u0433\u043e \u043b\u0443\u0447\u0448\u0435\u0433\u043e \u044f, \u043a\u043e\u043d\u0435\u0447\u043d\u043e, \u043f\u0440\u0438\u0434\u0443\u043c\u0430\u0442\u044c \u043d\u0435 \u043c\u043e\u0433\u0443, \u043d\u043e \u043d\u0430\u0434\u0435\u044e\u0441\u044c \u0447\u0442\u043e\u00a0\u0442\u0435, \u043a\u0442\u043e \u0443\u043c\u043d\u0435\u0435 \u043c\u0435\u043d\u044f, \u043d\u0430\u043f\u0438\u0448\u0443\u0442 \u0432\u00a0\u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u044f\u0445 \u043a\u0430\u043a\u00a0\u044d\u0442\u043e \u0432\u0437\u043b\u043e\u043c\u0430\u0442\u044c \u0438, \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0435\u043d\u043d\u043e, \u0437\u0430\u0449\u0438\u0442\u0438\u0442\u044c\u0441\u044f.<\/p>\n<p>\u0418\u0442\u0430\u043a, \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0443 \u043d\u0430\u0441 \u0440\u0430\u0437\u0440\u0435\u0448\u0435\u043d\u044b, \u043d\u0430\u0434\u043e \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u043d\u0443\u0436\u043d\u044b\u0435 \u0432 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f. \u042f \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0443\u0441\u044c \u0441\u0438\u043d\u0443\u0441\u043e\u043c \u0438 \u043a\u043e\u0441\u0438\u043d\u0443\u0441\u043e\u043c, \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u043e\u0435 \u0432\u044b \u0438 \u0441\u0430\u043c\u0438 \u0441\u043c\u043e\u0436\u0435\u0442\u0435. \u041f\u043e \u0441\u0432\u043e\u0435\u043c\u0443 \u0432\u043a\u0443\u0441\u0443.<\/p>\n<pre><code class=\"python\">import math  def my_eval(expression):     tree = ast.parse(expression, mode='eval')     validate_ast(tree)     code = compile(tree, filename='', mode='eval')     context = {         '__builtins__': {},         'sin': math.sin,         'cos': math.cos     }     return eval(code, context)<\/code><\/pre>\n<p>\u041c\u043e\u0436\u0435\u0442 \u043f\u043e\u043a\u0430\u0437\u0430\u0442\u044c\u0441\u044f, \u0447\u0442\u043e \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442 \u0432\u0441\u0435\u0433\u043e \u043e\u0434\u0438\u043d \u0440\u0430\u0437, \u0432\u043d\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u0438, \u043a\u0430\u043a \u043c\u044b \u044d\u0442\u043e \u0441\u0434\u0435\u043b\u0430\u043b\u0438 \u0441 <code>_allowed_nodes<\/code> \u0432 \u0432\u0430\u043b\u0438\u0434\u0430\u0442\u043e\u0440\u0435, \u043d\u043e! \u2014 <code>_allowed_nodes<\/code> \u0443 \u043d\u0430\u0441 \u0432\u0441\u0451-\u0442\u0430\u043a\u0438 immutable, \u0430 \u0434\u043b\u044f \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0430 \u0435\u0441\u0442\u044c \u0440\u0438\u0441\u043a \u043c\u043e\u0434\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u0438, \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u043e\u0439 \u0438\u043b\u0438 \u043f\u0440\u0435\u0434\u043d\u0430\u043c\u0435\u0440\u0435\u043d\u043d\u043e\u0439. \u0418\u0437\u0432\u0435\u0441\u0442\u043d\u044b\u0435 \u0430\u043d\u0430\u043b\u043e\u0433\u0438\u0447\u043d\u044b\u0435 \u0433\u0440\u0430\u0431\u043b\u0438 \u2014 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 dict \u0438 list \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439 \u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e \u0434\u043b\u044f \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u043e\u0432. \u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u043b\u0443\u0447\u0448\u0435 \u0441\u043e\u0437\u0434\u0430\u0432\u0430\u0442\u044c \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442 \u043a\u0430\u0436\u0434\u044b\u0439 \u0440\u0430\u0437, \u0432\u043d\u0443\u0442\u0440\u0438 \u0444\u0443\u043d\u043a\u0446\u0438\u0438.<\/p>\n<p>\u041d\u0443, \u0432\u043e\u0442 \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u043e, \u0438 \u0432\u0441\u0451. \u041f\u043e\u043b\u044c\u0437\u0443\u0439\u0442\u0435\u0441\u044c \u0438 \u043d\u0430\u0441\u043b\u0430\u0436\u0434\u0430\u0439\u0442\u0435\u0441\u044c. \u0414\u043b\u044f \u0442\u0435\u0445, \u043a\u043e\u043c\u0443 \u043b\u0435\u043d\u044c \u0441\u043e\u0431\u0438\u0440\u0430\u0442\u044c \u043a\u043e\u043f\u0438\u043f\u0430\u0441\u0442\u043e\u0439 \u0432\u0441\u0451 \u0432 \u043a\u0443\u0447\u0443, \u043b\u043e\u0432\u0438\u0442\u0435 \u043f\u043e\u043b\u043d\u044b\u0439 \u0438\u0441\u0445\u043e\u0434\u043d\u0438\u043a:<\/p>\n<pre><code class=\"python\">import ast import math   def my_eval(expression):     tree = ast.parse(expression, mode='eval')     #dump(tree)     validate_ast(tree)     code = compile(tree, filename='', mode='eval')     context = {         '__builtins__': {},         'sin': math.sin,         'cos': math.cos     }     return eval(code, context)   _allowed_nodes = (     # \u0431\u0430\u0437\u043e\u0432\u044b\u0435 \u0443\u0437\u043b\u044b:     ast.BinOp, ast.UnaryOp, ast.Constant, ast.Call, ast.Name, ast.Load,      # \u043e\u0441\u043d\u043e\u0432\u043d\u044b\u0435 BinOp:     ast.Add, ast.Sub, ast.Mult, ast.Div, ast.FloorDiv, ast.Mod, ast.Pow,      # \u043e\u0441\u043d\u043e\u0432\u043d\u044b\u0435 UnaryOp:     ast.UAdd, ast.USub )  def validate_ast(tree):      # \u0432\u0430\u043b\u0438\u0434\u0438\u0440\u0443\u0435\u043c \u043a\u043e\u0440\u0435\u043d\u044c \u0434\u0435\u0440\u0435\u0432\u0430     if not isinstance(tree, ast.Expression):         raise Exception('\u041d\u0435\u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u043e\u0435 \u0432\u044b\u0440\u0430\u0436\u0435\u043d\u0438\u0435')      # \u0432\u0430\u043b\u0438\u0434\u0438\u0440\u0443\u0435\u043c \u0443\u0437\u043b\u044b     def validate_children(node):         for child in ast.iter_child_nodes(node):             if isinstance(child, list):                 for grandchild in child:                     validate_children(grandchild)             else:                 if not isinstance(child, _allowed_nodes):                     raise Exception('\u041d\u0435\u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u043e\u0435 \u0432\u044b\u0440\u0430\u0436\u0435\u043d\u0438\u0435')                 validate_children(child)      validate_children(tree.body)   def dump(node):     def _format(node, indent):         if isinstance(node, ast.AST):             print('%sAST %s' % (' ' * indent, node.__class__.__name__))             for a, b in ast.iter_fields(node):                 print('%s%s' % (' ' * indent, a))                 _format(b, indent + 4)         elif isinstance(node, list):             print('%sLIST %s' % (' ' * indent, node.__class__.__name__))             for x in node:                 _format(x, indent)         else:             print('%s%s' % (' ' * indent, repr(node)))     _format(node, 0)   print(my_eval(input()))<\/code><\/pre>\n<\/p>\n<\/div>\n<\/div>\n<\/div>\n<p> <!----> <!----><\/div>\n<p> <!----> <!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/749574\/\"> https:\/\/habr.com\/ru\/articles\/749574\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u0412\u00a0\u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u044f\u0445 \u043a\u00a0\u0441\u0442\u0430\u0442\u044c\u044f\u043c \u043f\u043e\u00a0\u0441\u0438\u043d\u0442\u0430\u043a\u0441\u0438\u0447\u0435\u0441\u043a\u043e\u043c\u0443 \u0430\u043d\u0430\u043b\u0438\u0437\u0443 \u044f \u0438\u043d\u043e\u0433\u0434\u0430 \u0432\u0438\u0436\u0443 \u0442\u0430\u043a\u0438\u0435:<\/p>\n<blockquote>\n<p>\u043d\u0430\u00a0\u043f\u0438\u0442\u043e\u043d\u0435 \u043a\u0430\u043b\u044c\u043a\u0443\u043b\u044f\u0442\u043e\u0440 \u043f\u0438\u0448\u0435\u0442\u0441\u044f \u043f\u0440\u043e\u0449\u0435 \u043f\u0440\u043e\u0441\u0442\u043e\u0433\u043e \u2014 <code>print(eval(input()))<\/code><\/p>\n<\/blockquote>\n<p>\u041d\u0443, \u0432\u043e\u0431\u0449\u0435\u043c\u2011\u0442\u043e\u00a0\u2014 \u0434\u0430, \u043d\u043e\u00a0\u0435\u0441\u043b\u0438, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0432\u044b \u043f\u0440\u0438\u043a\u0440\u0443\u0442\u0438\u0442\u0435 \u0442\u0430\u043a\u043e\u0439 \u043a\u0430\u043b\u044c\u043a\u0443\u043b\u044f\u0442\u043e\u0440 \u043a\u00a0\u0441\u0432\u043e\u0435\u043c\u0443 \u0441\u0430\u0439\u0442\u0443, \u0442\u043e \u043b\u044e\u0431\u043e\u0439\u00a0\u0436\u0435\u043b\u0430\u044e\u0449\u0438\u0439 \u0432\u043c\u0435\u0441\u0442\u043e <code>2+2*2<\/code> \u043c\u043e\u0436\u0435\u0442 \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c <code>exec(\"import os; os.removedirs('\/')\")<\/code>, \u043f\u0440\u0435\u0434\u0432\u0430\u0440\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u0438\u0437\u0443\u0447\u0438\u0432 \u0432\u0441\u0435 \u0432\u0430\u0448\u0438 \u0441\u0435\u043a\u0440\u0435\u0442\u043d\u044b\u0435 \u0444\u0430\u0439\u043b\u044b \u043f\u043e\u0434\u043e\u0431\u043d\u044b\u043c\u00a0\u0436\u0435 \u043e\u0431\u0440\u0430\u0437\u043e\u043c. \u0422\u0430\u043a\u0430\u044f \u043f\u0435\u0440\u0441\u043f\u0435\u043a\u0442\u0438\u0432\u0430 \u043d\u0435\u00a0\u043c\u043e\u0436\u0435\u0442 \u0440\u0430\u0434\u043e\u0432\u0430\u0442\u044c, \u043d\u043e\u00a0\u0438 \u043e\u0442\u043a\u0430\u0437\u044b\u0432\u0430\u0442\u044c\u0441\u044f \u043e\u0442 <code>eval()<\/code> \u0442\u043e\u0436\u0435 \u043d\u0435\u00a0\u0441\u0442\u043e\u0438\u0442.<\/p>\n<p>\u2014 \u0410\u00a0\u0447\u0442\u043e\u00a0\u0434\u0435\u043b\u0430\u0442\u044c\u2011\u0442\u043e?\u00a0\u2014 \u0441\u043f\u0440\u043e\u0441\u0438\u0442\u0435 \u0432\u044b. \u041e\u0442\u0432\u0435\u0442 \u043f\u0440\u043e\u0441\u0442\u043e\u0439: \u0432\u0430\u043b\u0438\u0434\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0432\u0445\u043e\u0434\u044f\u0449\u0438\u0435 \u0434\u0430\u043d\u043d\u044b\u0435, \u043a\u0430\u043a\u00a0\u0432\u044b \u044d\u0442\u043e \u0432\u0441\u0435\u0433\u0434\u0430 \u0434\u0435\u043b\u0430\u0435\u0442\u0435. \u0422\u043e\u043b\u044c\u043a\u043e \u043d\u0435\u00a0\u0442\u0435, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043f\u0435\u0440\u0435\u0434\u0430\u044e\u0442\u0441\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u0438 <code>eval()<\/code>\u00a0\u2014 \u0434\u043b\u044f\u00a0\u044d\u0442\u043e\u0433\u043e \u0432\u0430\u043c \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u043f\u0440\u0438\u0448\u043b\u043e\u0441\u044c \u0431\u044b \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0441\u0432\u043e\u0439 \u0441\u0438\u043d\u0442\u0430\u043a\u0441\u0438\u0447\u0435\u0441\u043a\u0438\u0439 \u0430\u043d\u0430\u043b\u0438\u0437\u0430\u0442\u043e\u0440, \u0430\u00a0\u0432\u043d\u0443\u0442\u0440\u0435\u043d\u043d\u0438\u0435 \u0434\u0430\u043d\u043d\u044b\u0435, \u043a\u043e\u0442\u043e\u0440\u044b\u043c\u0438 \u043e\u043f\u0435\u0440\u0438\u0440\u0443\u0435\u0442 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f <code>eval()<\/code>.<\/p>\n<p>\u2014 \u0410! \u042f \u0437\u043d\u0430\u044e\u00a0\u2014 \u0441\u043a\u0430\u0436\u0435\u0442\u0435 \u0432\u044b\u00a0\u2014 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c <code>compile()<\/code>, \u0432\u0430\u043b\u0438\u0434\u0438\u0440\u0443\u0435\u043c \u043a\u043e\u0434 \u0438 \u0442\u043e\u0433\u0434\u0430 \u0443\u0436\u0435 \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u043c <code>eval()<\/code>. \u0427\u0442\u043e-\u0436, \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e \u0438 \u0442\u0430\u043a. \u041d\u043e\u00a0\u044f \u043d\u0435\u00a0\u0437\u043d\u0430\u044e \u043f\u0440\u043e\u0441\u0442\u043e\u0433\u043e \u0441\u043f\u043e\u0441\u043e\u0431\u0430 \u0432\u0430\u043b\u0438\u0434\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0431\u0430\u0439\u0442\u2011\u043a\u043e\u0434. \u041d\u0435\u0442. \u041d\u0430\u043c \u043d\u0430\u0434\u043e \u0432\u0430\u043b\u0438\u0434\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u043f\u0438\u0442\u043e\u043d\u043e\u0432\u0441\u043a\u043e\u0433\u043e \u0441\u0438\u043d\u0442\u0430\u043a\u0441\u0438\u0447\u0435\u0441\u043a\u043e\u0433\u043e \u0430\u043d\u0430\u043b\u0438\u0437\u0430\u0442\u043e\u0440\u0430\u00a0\u2014 \u0430\u0431\u0441\u0442\u0440\u0430\u043a\u0442\u043d\u043e\u0435 \u0441\u0438\u043d\u0442\u0430\u043a\u0441\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u0434\u0435\u0440\u0435\u0432\u043e \u0438\u043b\u0438\u00a0AST. \u042d\u0442\u043e \u0433\u043e\u0440\u0430\u0437\u0434\u043e \u043f\u0440\u043e\u0449\u0435, \u0445\u043e\u0442\u044c \u0438 \u0437\u0432\u0443\u0447\u0438\u0442 \u043f\u0443\u0433\u0430\u044e\u0449\u0435.<\/p>\n<p>\u041d\u0430\u043f\u0438\u0448\u0435\u043c \u0441\u0432\u043e\u0439 <code>eval()<\/code> \u0442\u0430\u043a:<\/p>\n<pre><code class=\"python\">def my_eval(expression):     tree = ast.parse(expression, mode='eval')     code = compile(tree, filename='', mode='eval')     return eval(code)<\/code><\/pre>\n<p>\u041d\u0430\u043c, \u0435\u0441\u0442\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u043d\u043e, \u043f\u043e\u043d\u0430\u0434\u043e\u0431\u0438\u0442\u0441\u044f \u043c\u043e\u0434\u0443\u043b\u044c ast:<\/p>\n<pre><code>>>> import ast<\/code><\/pre>\n<p>\u041f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u043c &#8212; \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u043b\u0438?<\/p>\n<pre><code>>>> print(my_eval(input())) 2+2*2 6<\/code><\/pre>\n<p>\u0418\u0442\u0430\u043a, \u0443 \u043d\u0430\u0441 \u0435\u0441\u0442\u044c <code>tree<\/code>. \u0412\u0430\u043c, \u043a\u043e\u043d\u0435\u0447\u043d\u043e \u0436\u0435, \u043b\u044e\u0431\u043e\u043f\u044b\u0442\u043d\u043e, \u0447\u0442\u043e \u0442\u0430\u043c \u0432\u043d\u0443\u0442\u0440\u0438, \u0438 \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0442\u0435\u043b\u044c\u043d\u043e, \u043d\u0430\u0434\u043e \u0436\u0435 \u0437\u043d\u0430\u0442\u044c, \u0447\u0442\u043e \u043c\u044b \u0441\u043e\u0431\u0438\u0440\u0430\u0435\u043c\u0441\u044f \u0432\u0430\u043b\u0438\u0434\u0438\u0440\u043e\u0432\u0430\u0442\u044c. \u041d\u043e \u043f\u0440\u043e\u0441\u0442\u043e \u0442\u0430\u043a \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u043d\u0435 \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u0441\u044f \u2014 \u044d\u0442\u043e \u043e\u0431\u044a\u0435\u043a\u0442,  \u0438 <code>print()<\/code> \u0438\u043b\u0438 <code>pprint()<\/code>\u0432\u044b\u0434\u0430\u0434\u0443\u0442 \u0432\u0441\u0435\u0433\u043e \u043b\u0438\u0448\u044c <code>&lt;ast.Expression object at 0x7f6de7eb3c70><\/code> \u042f \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043b\u0441\u044f \u0442\u0430\u043a\u043e\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0435\u0439:<\/p>\n<pre><code class=\"python\">def dump(node):     def _format(node, indent):         if isinstance(node, ast.AST):             print('%sAST %s' % (' ' * indent, node.__class__.__name__))             for a, b in ast.iter_fields(node):                 print('%s%s' % (' ' * indent, a))                 _format(b, indent + 4)         elif isinstance(node, list):             print('%sLIST %s' % (' ' * indent, node.__class__.__name__))             for x in node:                 _format(x, indent)         else:             print('%s%s' % (' ' * indent, repr(node)))     _format(node, 0)<\/code><\/pre>\n<p>\u041c\u043e\u0436\u0435\u0442\u0435 \u0432\u0441\u0442\u0430\u0432\u0438\u0442\u044c <code>dump(tree)<\/code> \u0432 <code>my_eval(),<\/code> \u043f\u043e\u0438\u0433\u0440\u0430\u0442\u044c\u0441\u044f \u0438 \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c, \u043a\u0430\u043a\u043e\u0435 \u043e\u043d\u043e \u2014 \u044d\u0442\u043e \u0434\u0435\u0440\u0435\u0432\u043e. \u0422\u043e\u043b\u044c\u043a\u043e \u043d\u0435 \u043d\u0430\u0434\u043e \u043a\u043e\u043f\u0438\u043f\u0430\u0441\u0442\u0438\u0442\u044c \u043c\u043e\u0439 \u043f\u0440\u0438\u043c\u0435\u0440 \u0441 <code>removedirs()<\/code>.<\/p>\n<p>\u041a\u043e\u043c\u0443 \u0438\u0433\u0440\u0430\u0442\u044c\u0441\u044f \u043b\u0435\u043d\u0438\u0432\u043e \u2014 \u0432\u043e\u0442 \u043f\u0430\u0440\u0430 \u043f\u0440\u0438\u043c\u0435\u0440\u043e\u0432:<\/p>\n<pre><code>>>> print(my_eval(input())) 2+2*2 AST Expression body     AST BinOp     left         AST Constant         value             2         kind             None     op         AST Add     right         AST BinOp         left             AST Constant             value                 2             kind                 None         op             AST Mult         right             AST Constant             value                 2             kind                 None 6<\/code><\/pre>\n<pre><code>>>> print(my_eval(input())) print(globals()) AST Expression body     AST Call     func         AST Name         id             'print'         ctx             AST Load     args         LIST list         AST Call         func             AST Name             id                 'globals'             ctx                 AST Load         args             LIST list         keywords             LIST list     keywords         LIST list {'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': &lt;class '_frozen_importlib.BuiltinImporter'>, '__spec__': None, '__annotations__': {}, '__builtins__': &lt;module 'builtins' (built-in)>, 'my_eval': &lt;function my_eval at 0x7f6de9225480>, 'ast': &lt;module 'ast' from '\/usr\/lib\/python3.10\/ast.py'>, 'dump': &lt;function dump at 0x7f6de7efb490>} None<\/code><\/pre>\n<p>\u0412\u0438\u0434\u0438\u0442\u0435, \u0447\u0435\u043c \u043e\u0442\u043b\u0438\u0447\u0430\u044e\u0442\u0441\u044f \u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u044b\u0435 \u0432\u044b\u0440\u0430\u0436\u0435\u043d\u0438\u044f \u043e\u0442 \u043d\u0435\u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u044b\u0445? \u0411\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u044b\u0435 \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0442 \u0442\u043e\u043b\u044c\u043a\u043e <code>Constant<\/code> \u0438 <code>BinOp<\/code>, \u0430 \u043d\u0435\u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u044b\u0435 \u2014 \u0432\u0441\u044f\u043a\u0438\u0435 <code>Call<\/code> \u0438 <code>Name<\/code>. \u041f\u043e\u043b\u043d\u044b\u0439 \u0441\u043f\u0438\u0441\u043e\u043a \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439 \u0438 \u0434\u0440\u0443\u0433\u0438\u0445 \u0443\u0437\u043b\u043e\u0432 \u0434\u0435\u0440\u0435\u0432\u0430 \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u0432 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u0438 \u043a \u043c\u043e\u0434\u0443\u043b\u044e ast. \u042d\u0442\u043e \u043f\u043e\u043c\u043e\u0436\u0435\u0442 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c\u0441\u044f, \u0447\u0442\u043e \u0437\u0430\u043f\u0440\u0435\u0442\u0438\u0442\u044c, \u0430 \u0447\u0442\u043e \u0440\u0430\u0437\u0440\u0435\u0448\u0438\u0442\u044c. \u0414\u043b\u044f \u043f\u0440\u043e\u0441\u0442\u043e\u0433\u043e \u043a\u0430\u043b\u044c\u043a\u0443\u043b\u044f\u0442\u043e\u0440\u0430, \u044f \u0441\u0447\u0438\u0442\u0430\u044e, \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u0440\u0430\u0437\u0440\u0435\u0448\u0438\u0442\u044c \u043e\u0441\u043d\u043e\u0432\u043d\u044b\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 <code>BinOp<\/code> \u0438 <code>UnaryOp<\/code>. \u041f\u043b\u044e\u0441 <code>Constant<\/code>.<\/p>\n<p>\u041f\u043e\u0440\u0430 \u043f\u0438\u0441\u0430\u0442\u044c \u0432\u0430\u043b\u0438\u0434\u0430\u0442\u043e\u0440:<\/p>\n<pre><code class=\"python\">_allowed_nodes = (     # \u0431\u0430\u0437\u043e\u0432\u044b\u0435 \u0443\u0437\u043b\u044b:     ast.BinOp, ast.UnaryOp, ast.Constant,      # \u043e\u0441\u043d\u043e\u0432\u043d\u044b\u0435 BinOps:     ast.Add, ast.Sub, ast.Mult, ast.Div, ast.FloorDiv, ast.Mod, ast.Pow,      # \u043e\u0441\u043d\u043e\u0432\u043d\u044b\u0435 UnaryOps:     ast.UAdd, ast.USub )  def validate_ast(tree):      # \u0432\u0430\u043b\u0438\u0434\u0438\u0440\u0443\u0435\u043c \u043a\u043e\u0440\u0435\u043d\u044c \u0434\u0435\u0440\u0435\u0432\u0430     if not isinstance(tree, ast.Expression):         raise Exception('\u041d\u0435\u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u043e\u0435 \u0432\u044b\u0440\u0430\u0436\u0435\u043d\u0438\u0435')      # \u0432\u0430\u043b\u0438\u0434\u0438\u0440\u0443\u0435\u043c \u0443\u0437\u043b\u044b     def validate_children(node):         for child in ast.iter_child_nodes(node):             if not isinstance(child, _allowed_nodes):                 raise Exception('\u041d\u0435\u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u043e\u0435 \u0432\u044b\u0440\u0430\u0436\u0435\u043d\u0438\u0435')             validate_children(child)      validate_children(tree.body)<\/code><\/pre>\n<p>\u0412\u043e\u0442 \u0442\u0430\u043a \u0432\u0441\u0451 \u043f\u0440\u043e\u0441\u0442\u043e. \u0421\u043c\u0435\u043b\u043e \u0432\u044b\u0437\u044b\u0432\u0430\u0439\u0442\u0435 \u044d\u0442\u0443 \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u0438\u0437 <code>my_eval()<\/code> \u043f\u0435\u0440\u0435\u0434 <code>compile()<\/code>:<\/p>\n<pre><code class=\"python\">def my_eval(expression):     tree = ast.parse(expression, mode='eval')     validate_ast(tree)     code = compile(tree, filename='', mode='eval')     return eval(code)<\/code><\/pre>\n<p>\u0438 \u0431\u0443\u0434\u0435\u0442 \u0432\u0430\u043c \u0441\u0447\u0430\u0441\u0442\u044c\u0435. \u041d\u043e, \u0438\u043c\u0435\u0439\u0442\u0435 \u0432\u0432\u0438\u0434\u0443, \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u044f \u043a \u0444\u0443\u043d\u043a\u0446\u0438\u0438 <code>compile()<\/code> \u0441\u043e\u043e\u0431\u0449\u0430\u0435\u0442:<\/p>\n<blockquote>\n<p>Warning<\/p>\n<p>It is possible to crash the Python interpreter with a sufficiently large\/complex string when compiling to an AST object due to stack depth limitations in Python\u2019s AST compiler.<\/p>\n<\/blockquote>\n<p>\u0422\u0430\u043a \u0447\u0442\u043e \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0438\u0432\u0430\u0439\u0442\u0435 \u0434\u043b\u0438\u043d\u0443 \u0441\u0442\u0440\u043e\u043a\u0438, \u043f\u0435\u0440\u0435\u0434\u0430\u0432\u0430\u0435\u043c\u043e\u0439 <code>my_eval()<\/code>. \u0418 \u0442\u043e\u0433\u0434\u0430 \u0441\u0447\u0430\u0441\u0442\u044c\u0435 \u0431\u0443\u0434\u0435\u0442 \u043d\u0430\u0441\u0442\u043e\u044f\u0449\u0438\u043c.<\/p>\n<p>\u2014 \u041f\u043e\u0441\u0442\u043e\u0439\u0442\u0435! \u2014 \u0441\u043a\u0430\u0436\u0435\u0442\u0435 \u0432\u044b \u2014 \u041c\u043d\u0435 \u043d\u0435 \u043d\u0443\u0436\u0435\u043d \u043f\u0440\u043e\u0441\u0442\u043e\u0439 \u0431\u0443\u0445\u0433\u0430\u043b\u0442\u0435\u0440\u0441\u043a\u0438\u0439 \u043a\u0430\u043b\u044c\u043a\u0443\u043b\u044f\u0442\u043e\u0440! \u0413\u0434\u0435 \u0445\u043e\u0442\u044c \u043a\u0430\u043a\u043e\u0439-\u043d\u0438\u0431\u0443\u0434\u044c \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439 \u043d\u0430\u0431\u043e\u0440 \u043c\u0430\u0442\u0435\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u0445 \u0444\u0443\u043d\u043a\u0446\u0438\u0439? \u041d\u0435 \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0442\u044c \u0436\u0435 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u0432 \u043f\u0435\u0448\u0435\u0435 \u043f\u0443\u0442\u0435\u0448\u0435\u0441\u0442\u0432\u0438\u0435 \u0438\u0441\u043a\u0430\u0442\u044c \u043a\u043e\u0441\u0438\u043d\u0443\u0441 \u0444\u0438?<\/p>\n<p>\u041c-\u0434\u0430, \u043f\u043e\u0436\u0430\u043b\u0443\u0439, \u0441\u043e\u0433\u043b\u0430\u0448\u0443\u0441\u044c \u0441 \u0432\u0430\u043c\u0438. \u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u0440\u0430\u0437\u0440\u0435\u0448\u0438\u043c \u043a\u043e\u0435-\u0447\u0442\u043e \u0438\u0437 \u043c\u043e\u0434\u0443\u043b\u044f <code>math<\/code>. \u041d\u043e \u044d\u0442\u043e \u0431\u0443\u0434\u0443\u0442 \u0432\u044b\u0437\u043e\u0432\u044b \u0444\u0443\u043d\u043a\u0446\u0438\u0439, \u0430 \u0437\u043d\u0430\u0447\u0438\u0442 \u2014 \u043d\u0435\u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u044b\u0435 \u043e\u0431\u043b\u0430\u0441\u0442\u0438 \u0442\u044c\u043c\u044b. \u041d\u0430\u043c \u043f\u0440\u0438\u0434\u0451\u0442\u0441\u044f \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c <code>ast.Call<\/code>, <code>ast.Name<\/code> \u0438 <code>ast.Load<\/code> \u0432 <code>_allowed_nodes<\/code>, \u0438 \u044d\u0442\u043e \u043e\u0442\u043a\u0440\u044b\u0432\u0430\u0435\u0442 \u043f\u0443\u0442\u044c \u043a <code>exec()<\/code>. \u0427\u0442\u043e\u0431\u044b \u0438\u0437\u0431\u0435\u0436\u0430\u0442\u044c \u043d\u0435\u043f\u0440\u0438\u044f\u0442\u043d\u043e\u0441\u0442\u0435\u0439, \u043d\u0430\u0434\u043e \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0438\u0442\u044c \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u043c  \u0432\u0430\u0436\u043d\u043e \u043d\u0430\u043b\u0438\u0447\u0438\u0435 \u043f\u0443\u0441\u0442\u043e\u0433\u043e <code>__builtins__<\/code>:<\/p>\n<pre><code class=\"python\">def my_eval(expression):     tree = ast.parse(expression, mode='eval')     validate_ast(tree)     code = compile(tree, filename='', mode='eval')     context = {'__builtins__': {}}     return eval(code, context)<\/code><\/pre>\n<p>\u041f\u043b\u044e\u0441, \u043d\u0430\u0434\u043e \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u043a\u0443 \u0443\u0437\u043b\u043e\u0432-\u0441\u043f\u0438\u0441\u043a\u043e\u0432 \u0432 \u0432\u0430\u043b\u0438\u0434\u0430\u0442\u043e\u0440:<\/p>\n<pre><code class=\"python\">def validate_ast(tree):      # \u0432\u0430\u043b\u0438\u0434\u0438\u0440\u0443\u0435\u043c \u043a\u043e\u0440\u0435\u043d\u044c \u0434\u0435\u0440\u0435\u0432\u0430     if not isinstance(tree, ast.Expression):         raise Exception('\u041d\u0435\u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u043e\u0435 \u0432\u044b\u0440\u0430\u0436\u0435\u043d\u0438\u0435')      # \u0432\u0430\u043b\u0438\u0434\u0438\u0440\u0443\u0435\u043c \u0443\u0437\u043b\u044b     def validate_children(node):         for child in ast.iter_child_nodes(node):             if isinstance(child, list):                 for grandchild in child:                     validate_children(grandchild)             else:                 if not isinstance(child, _allowed_nodes):                     raise Exception('\u041d\u0435\u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u043e\u0435 \u0432\u044b\u0440\u0430\u0436\u0435\u043d\u0438\u0435')                 validate_children(child)      validate_children(tree.body)<\/code><\/pre>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c, \u0435\u0441\u043b\u0438 \u043c\u044b \u0432\u0432\u0435\u0434\u0451\u043c<\/p>\n<p><code>exec('print(42)')<\/code><\/p>\n<p>\u0442\u043e \u043f\u043e\u043b\u0443\u0447\u0438\u043c<\/p>\n<p><code>NameError: name 'exec' is not defined<\/code><\/p>\n<p>\u0423\u0440\u0430! \u041d\u0438\u0447\u0435\u0433\u043e \u043b\u0443\u0447\u0448\u0435\u0433\u043e \u044f, \u043a\u043e\u043d\u0435\u0447\u043d\u043e, \u043f\u0440\u0438\u0434\u0443\u043c\u0430\u0442\u044c \u043d\u0435 \u043c\u043e\u0433\u0443, \u043d\u043e \u043d\u0430\u0434\u0435\u044e\u0441\u044c \u0447\u0442\u043e\u00a0\u0442\u0435, \u043a\u0442\u043e \u0443\u043c\u043d\u0435\u0435 \u043c\u0435\u043d\u044f, \u043d\u0430\u043f\u0438\u0448\u0443\u0442 \u0432\u00a0\u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u044f\u0445 \u043a\u0430\u043a\u00a0\u044d\u0442\u043e \u0432\u0437\u043b\u043e\u043c\u0430\u0442\u044c \u0438, \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0435\u043d\u043d\u043e, \u0437\u0430\u0449\u0438\u0442\u0438\u0442\u044c\u0441\u044f.<\/p>\n<p>\u0418\u0442\u0430\u043a, \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0443 \u043d\u0430\u0441 \u0440\u0430\u0437\u0440\u0435\u0448\u0435\u043d\u044b, \u043d\u0430\u0434\u043e \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u043d\u0443\u0436\u043d\u044b\u0435 \u0432 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f. \u042f \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0443\u0441\u044c \u0441\u0438\u043d\u0443\u0441\u043e\u043c \u0438 \u043a\u043e\u0441\u0438\u043d\u0443\u0441\u043e\u043c, \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u043e\u0435 \u0432\u044b \u0438 \u0441\u0430\u043c\u0438 \u0441\u043c\u043e\u0436\u0435\u0442\u0435. \u041f\u043e \u0441\u0432\u043e\u0435\u043c\u0443 \u0432\u043a\u0443\u0441\u0443.<\/p>\n<pre><code class=\"python\">import math  def my_eval(expression):     tree = ast.parse(expression, mode='eval')     validate_ast(tree)     code = compile(tree, filename='', mode='eval')     context = {         '__builtins__': {},         'sin': math.sin,         'cos': math.cos     }     return eval(code, context)<\/code><\/pre>\n<p>\u041c\u043e\u0436\u0435\u0442 \u043f\u043e\u043a\u0430\u0437\u0430\u0442\u044c\u0441\u044f, \u0447\u0442\u043e \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442 \u0432\u0441\u0435\u0433\u043e \u043e\u0434\u0438\u043d \u0440\u0430\u0437, \u0432\u043d\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u0438, \u043a\u0430\u043a \u043c\u044b \u044d\u0442\u043e \u0441\u0434\u0435\u043b\u0430\u043b\u0438 \u0441 <code>_allowed_nodes<\/code> \u0432 \u0432\u0430\u043b\u0438\u0434\u0430\u0442\u043e\u0440\u0435, \u043d\u043e! \u2014 <code>_allowed_nodes<\/code> \u0443 \u043d\u0430\u0441 \u0432\u0441\u0451-\u0442\u0430\u043a\u0438 immutable, \u0430 \u0434\u043b\u044f \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0430 \u0435\u0441\u0442\u044c \u0440\u0438\u0441\u043a \u043c\u043e\u0434\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u0438, \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u043e\u0439 \u0438\u043b\u0438 \u043f\u0440\u0435\u0434\u043d\u0430\u043c\u0435\u0440\u0435\u043d\u043d\u043e\u0439. \u0418\u0437\u0432\u0435\u0441\u0442\u043d\u044b\u0435 \u0430\u043d\u0430\u043b\u043e\u0433\u0438\u0447\u043d\u044b\u0435 \u0433\u0440\u0430\u0431\u043b\u0438 \u2014 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 dict \u0438 list \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439 \u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e \u0434\u043b\u044f \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u043e\u0432. \u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u043b\u0443\u0447\u0448\u0435 \u0441\u043e\u0437\u0434\u0430\u0432\u0430\u0442\u044c \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442 \u043a\u0430\u0436\u0434\u044b\u0439 \u0440\u0430\u0437, \u0432\u043d\u0443\u0442\u0440\u0438 \u0444\u0443\u043d\u043a\u0446\u0438\u0438.<\/p>\n<p>\u041d\u0443, \u0432\u043e\u0442 \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u043e, \u0438 \u0432\u0441\u0451. \u041f\u043e\u043b\u044c\u0437\u0443\u0439\u0442\u0435\u0441\u044c \u0438 \u043d\u0430\u0441\u043b\u0430\u0436\u0434\u0430\u0439\u0442\u0435\u0441\u044c. \u0414\u043b\u044f \u0442\u0435\u0445, \u043a\u043e\u043c\u0443 \u043b\u0435\u043d\u044c \u0441\u043e\u0431\u0438\u0440\u0430\u0442\u044c \u043a\u043e\u043f\u0438\u043f\u0430\u0441\u0442\u043e\u0439 \u0432\u0441\u0451 \u0432 \u043a\u0443\u0447\u0443, \u043b\u043e\u0432\u0438\u0442\u0435 \u043f\u043e\u043b\u043d\u044b\u0439 \u0438\u0441\u0445\u043e\u0434\u043d\u0438\u043a:<\/p>\n<pre><code class=\"python\">import ast import math   def my_eval(expression):     tree = ast.parse(expression, mode='eval')     #dump(tree)     validate_ast(tree)     code = compile(tree, filename='', mode='eval')     context = {         '__builtins__': {},         'sin': math.sin,         'cos': math.cos     }     return eval(code, context)   _allowed_nodes = (     # \u0431\u0430\u0437\u043e\u0432\u044b\u0435 \u0443\u0437\u043b\u044b:     ast.BinOp, ast.UnaryOp, ast.Constant, ast.Call, ast.Name, ast.Load,      # \u043e\u0441\u043d\u043e\u0432\u043d\u044b\u0435 BinOp:     ast.Add, ast.Sub, ast.Mult, ast.Div, ast.FloorDiv, ast.Mod, ast.Pow,      # \u043e\u0441\u043d\u043e\u0432\u043d\u044b\u0435 UnaryOp:     ast.UAdd, ast.USub )  def validate_ast(tree):      # \u0432\u0430\u043b\u0438\u0434\u0438\u0440\u0443\u0435\u043c \u043a\u043e\u0440\u0435\u043d\u044c \u0434\u0435\u0440\u0435\u0432\u0430     if not isinstance(tree, ast.Expression):         raise Exception('\u041d\u0435\u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u043e\u0435 \u0432\u044b\u0440\u0430\u0436\u0435\u043d\u0438\u0435')      # \u0432\u0430\u043b\u0438\u0434\u0438\u0440\u0443\u0435\u043c \u0443\u0437\u043b\u044b     def validate_children(node):         for child in ast.iter_child_nodes(node):             if isinstance(child, list):                 for grandchild in child:                     validate_children(grandchild)             else:                 if not isinstance(child, _allowed_nodes):                     raise Exception('\u041d\u0435\u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u043e\u0435 \u0432\u044b\u0440\u0430\u0436\u0435\u043d\u0438\u0435')                 validate_children(child)      validate_children(tree.body)   def dump(node):     def _format(node, indent):         if isinstance(node, ast.AST):             print('%sAST %s' % (' ' * indent, node.__class__.__name__))             for a, b in ast.iter_fields(node):                 print('%s%s' % (' ' * indent, a))                 _format(b, indent + 4)         elif isinstance(node, list):             print('%sLIST %s' % (' ' * indent, node.__class__.__name__))             for x in node:                 _format(x, indent)         else:             print('%s%s' % (' ' * indent, repr(node)))     _format(node, 0)   print(my_eval(input()))<\/code><\/pre>\n<\/p>\n<\/div>\n<\/div>\n<\/div>\n<p> <!----> <!----><\/div>\n<p> <!----> <!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/749574\/\"> https:\/\/habr.com\/ru\/articles\/749574\/<\/a><br \/><\/br><\/br><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-350614","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/350614","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=350614"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/350614\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=350614"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=350614"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=350614"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}