|
4 | 4 | import contextlib
|
5 | 5 | import importlib.util
|
6 | 6 | import inspect
|
| 7 | +import io |
7 | 8 | import pydoc
|
8 | 9 | import py_compile
|
9 | 10 | import keyword
|
@@ -899,6 +900,82 @@ def test_synopsis(self):
|
899 | 900 | synopsis = pydoc.synopsis(TESTFN, {})
|
900 | 901 | self.assertEqual(synopsis, 'line 1: h\xe9')
|
901 | 902 |
|
| 903 | + def test_source_synopsis(self): |
| 904 | + def check(source, expected, encoding=None): |
| 905 | + if isinstance(source, str): |
| 906 | + source_file = StringIO(source) |
| 907 | + else: |
| 908 | + source_file = io.TextIOWrapper(io.BytesIO(source), encoding=encoding) |
| 909 | + with source_file: |
| 910 | + result = pydoc.source_synopsis(source_file) |
| 911 | + self.assertEqual(result, expected) |
| 912 | + |
| 913 | + check('"""Single line docstring."""', |
| 914 | + 'Single line docstring.') |
| 915 | + check('"""First line of docstring.\nSecond line.\nThird line."""', |
| 916 | + 'First line of docstring.') |
| 917 | + check('"""First line of docstring.\\nSecond line.\\nThird line."""', |
| 918 | + 'First line of docstring.') |
| 919 | + check('""" Whitespace around docstring. """', |
| 920 | + 'Whitespace around docstring.') |
| 921 | + check('import sys\n"""No docstring"""', |
| 922 | + None) |
| 923 | + check(' \n"""Docstring after empty line."""', |
| 924 | + 'Docstring after empty line.') |
| 925 | + check('# Comment\n"""Docstring after comment."""', |
| 926 | + 'Docstring after comment.') |
| 927 | + check(' # Indented comment\n"""Docstring after comment."""', |
| 928 | + 'Docstring after comment.') |
| 929 | + check('""""""', # Empty docstring |
| 930 | + '') |
| 931 | + check('', # Empty file |
| 932 | + None) |
| 933 | + check('"""Embedded\0null byte"""', |
| 934 | + None) |
| 935 | + check('"""Embedded null byte"""\0', |
| 936 | + None) |
| 937 | + check('"""Café and résumé."""', |
| 938 | + 'Café and résumé.') |
| 939 | + check("'''Triple single quotes'''", |
| 940 | + 'Triple single quotes') |
| 941 | + check('"Single double quotes"', |
| 942 | + 'Single double quotes') |
| 943 | + check("'Single single quotes'", |
| 944 | + 'Single single quotes') |
| 945 | + check('"""split\\\nline"""', |
| 946 | + 'splitline') |
| 947 | + check('"""Unrecognized escape \\sequence"""', |
| 948 | + 'Unrecognized escape \\sequence') |
| 949 | + check('"""Invalid escape seq\\uence"""', |
| 950 | + None) |
| 951 | + check('r"""Raw \\stri\\ng"""', |
| 952 | + 'Raw \\stri\\ng') |
| 953 | + check('b"""Bytes literal"""', |
| 954 | + None) |
| 955 | + check('f"""f-string"""', |
| 956 | + None) |
| 957 | + check('"""Concatenated""" \\\n"string" \'literals\'', |
| 958 | + 'Concatenatedstringliterals') |
| 959 | + check('"""String""" + """expression"""', |
| 960 | + None) |
| 961 | + check('("""In parentheses""")', |
| 962 | + 'In parentheses') |
| 963 | + check('("""Multiple lines """\n"""in parentheses""")', |
| 964 | + 'Multiple lines in parentheses') |
| 965 | + check('()', # tuple |
| 966 | + None) |
| 967 | + check(b'# coding: iso-8859-15\n"""\xa4uro sign"""', |
| 968 | + '€uro sign', encoding='iso-8859-15') |
| 969 | + check(b'"""\xa4"""', # Decoding error |
| 970 | + None, encoding='utf-8') |
| 971 | + |
| 972 | + with tempfile.NamedTemporaryFile(mode='w+', encoding='utf-8') as temp_file: |
| 973 | + temp_file.write('"""Real file test."""\n') |
| 974 | + temp_file.flush() |
| 975 | + temp_file.seek(0) |
| 976 | + result = pydoc.source_synopsis(temp_file) |
| 977 | + self.assertEqual(result, "Real file test.") |
| 978 | + |
902 | 979 | @requires_docstrings
|
903 | 980 | def test_synopsis_sourceless(self):
|
904 | 981 | os = import_helper.import_fresh_module('os')
|
|
0 commit comments