Parse the source into an AST node. Equivalent to compile(source, filename, mode, PyCF_ONLY_AST). Pass type_comments=True to get back type comments where the syntax allows.
(source, filename='<unknown>', mode='exec', *,
type_comments=False, feature_version=None, optimize=-1, module=None)
| 24 | |
| 25 | |
| 26 | def parse(source, filename='<unknown>', mode='exec', *, |
| 27 | type_comments=False, feature_version=None, optimize=-1, module=None): |
| 28 | """ |
| 29 | Parse the source into an AST node. |
| 30 | Equivalent to compile(source, filename, mode, PyCF_ONLY_AST). |
| 31 | Pass type_comments=True to get back type comments where the syntax allows. |
| 32 | """ |
| 33 | flags = PyCF_ONLY_AST |
| 34 | if optimize > 0: |
| 35 | flags |= PyCF_OPTIMIZED_AST |
| 36 | if type_comments: |
| 37 | flags |= PyCF_TYPE_COMMENTS |
| 38 | if feature_version is None: |
| 39 | feature_version = -1 |
| 40 | elif isinstance(feature_version, tuple): |
| 41 | major, minor = feature_version # Should be a 2-tuple. |
| 42 | if major != 3: |
| 43 | raise ValueError(f"Unsupported major version: {major}") |
| 44 | feature_version = minor |
| 45 | # Else it should be an int giving the minor version for 3.x. |
| 46 | return compile(source, filename, mode, flags, |
| 47 | _feature_version=feature_version, optimize=optimize, |
| 48 | module=module) |
| 49 | |
| 50 | |
| 51 | def literal_eval(node_or_string): |
no test coverage detected
searching dependent graphs…