equal
deleted
inserted
replaced
7 # This software may be used and distributed according to the terms of the |
7 # This software may be used and distributed according to the terms of the |
8 # GNU General Public License version 2 or any later version. |
8 # GNU General Public License version 2 or any later version. |
9 |
9 |
10 from __future__ import absolute_import |
10 from __future__ import absolute_import |
11 |
11 |
|
12 import __future__ |
12 import codecs |
13 import codecs |
13 import re as remod |
14 import re as remod |
14 import textwrap |
15 import textwrap |
15 |
16 |
16 from ..i18n import _ |
17 from ..i18n import _ |
495 """Parse s into a boolean. |
496 """Parse s into a boolean. |
496 |
497 |
497 If s is not a valid boolean, returns None. |
498 If s is not a valid boolean, returns None. |
498 """ |
499 """ |
499 return _booleans.get(s.lower(), None) |
500 return _booleans.get(s.lower(), None) |
|
501 |
|
502 def evalpython(s): |
|
503 """Evaluate a string containing a Python expression. |
|
504 |
|
505 THIS FUNCTION IS NOT SAFE TO USE ON UNTRUSTED INPUT. IT'S USE SHOULD BE |
|
506 LIMITED TO DEVELOPER-FACING FUNCTIONALITY. |
|
507 """ |
|
508 globs = { |
|
509 r'__builtins__': { |
|
510 r'None': None, |
|
511 r'False': False, |
|
512 r'True': True, |
|
513 r'int': int, |
|
514 r'set': set, |
|
515 r'tuple': tuple, |
|
516 # Don't need to expose dict and list because we can use |
|
517 # literals. |
|
518 }, |
|
519 } |
|
520 |
|
521 # We can't use eval() directly because it inherits compiler |
|
522 # flags from this module and we need unicode literals for Python 3 |
|
523 # compatibility. |
|
524 code = compile(s, r'<string>', r'eval', |
|
525 __future__.unicode_literals.compiler_flag, True) |
|
526 return eval(code, globs, {}) |