|
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
|
@@ -879,6 +880,82 @@ def test_synopsis(self):
|
879 | 880 | synopsis = pydoc.synopsis(TESTFN, {})
|
880 | 881 | self.assertEqual(synopsis, 'line 1: h\xe9')
|
881 | 882 |
|
| 883 | + def test_source_synopsis(self): |
| 884 | + def check(source, expected, encoding=None): |
| 885 | + if isinstance(source, str): |
| 886 | + source_file = StringIO(source) |
| 887 | + else: |
| 888 | + source_file = io.TextIOWrapper(io.BytesIO(source), encoding=encoding) |
| 889 | + with source_file: |
| 890 | + result = pydoc.source_synopsis(source_file) |
| 891 | + self.assertEqual(result, expected) |
| 892 | + |
| 893 | + check('"""Single line docstring."""', |
| 894 | + 'Single line docstring.') |
| 895 | + check('"""First line of docstring.\nSecond line.\nThird line."""', |
| 896 | + 'First line of docstring.') |
| 897 | + check('"""First line of docstring.\\nSecond line.\\nThird line."""', |
| 898 | + 'First line of docstring.') |
| 899 | + check('""" Whitespace around docstring. """', |
| 900 | + 'Whitespace around docstring.') |
| 901 | + check('import sys\n"""No docstring"""', |
| 902 | + None) |
| 903 | + check(' \n"""Docstring after empty line."""', |
| 904 | + 'Docstring after empty line.') |
| 905 | + check('# Comment\n"""Docstring after comment."""', |
| 906 | + 'Docstring after comment.') |
| 907 | + check(' # Indented comment\n"""Docstring after comment."""', |
| 908 | + 'Docstring after comment.') |
| 909 | + check('""""""', # Empty docstring |
| 910 | + '') |
| 911 | + check('', # Empty file |
| 912 | + None) |
| 913 | + check('"""Embedded\0null byte"""', |
| 914 | + None) |
| 915 | + check('"""Embedded null byte"""\0', |
| 916 | + None) |
| 917 | + check('"""Café and résumé."""', |
| 918 | + 'Café and résumé.') |
| 919 | + check("'''Triple single quotes'''", |
| 920 | + 'Triple single quotes') |
| 921 | + check('"Single double quotes"', |
| 922 | + 'Single double quotes') |
| 923 | + check("'Single single quotes'", |
| 924 | + 'Single single quotes') |
| 925 | + check('"""split\\\nline"""', |
| 926 | + 'splitline') |
| 927 | + check('"""Unrecognized escape \\sequence"""', |
| 928 | + 'Unrecognized escape \\sequence') |
| 929 | + check('"""Invalid escape seq\\uence"""', |
| 930 | + None) |
| 931 | + check('r"""Raw \\stri\\ng"""', |
| 932 | + 'Raw \\stri\\ng') |
| 933 | + check('b"""Bytes literal"""', |
| 934 | + None) |
| 935 | + check('f"""f-string"""', |
| 936 | + None) |
| 937 | + check('"""Concatenated""" \\\n"string" \'literals\'', |
| 938 | + 'Concatenatedstringliterals') |
| 939 | + check('"""String""" + """expression"""', |
| 940 | + None) |
| 941 | + check('("""In parentheses""")', |
| 942 | + 'In parentheses') |
| 943 | + check('("""Multiple lines """\n"""in parentheses""")', |
| 944 | + 'Multiple lines in parentheses') |
| 945 | + check('()', # tuple |
| 946 | + None) |
| 947 | + check(b'# coding: iso-8859-15\n"""\xa4uro sign"""', |
| 948 | + '€uro sign', encoding='iso-8859-15') |
| 949 | + check(b'"""\xa4"""', # Decoding error |
| 950 | + None, encoding='utf-8') |
| 951 | + |
| 952 | + with tempfile.NamedTemporaryFile(mode='w+', encoding='utf-8') as temp_file: |
| 953 | + temp_file.write('"""Real file test."""\n') |
| 954 | + temp_file.flush() |
| 955 | + temp_file.seek(0) |
| 956 | + result = pydoc.source_synopsis(temp_file) |
| 957 | + self.assertEqual(result, "Real file test.") |
| 958 | + |
882 | 959 | @requires_docstrings
|
883 | 960 | def test_synopsis_sourceless(self):
|
884 | 961 | os = import_helper.import_fresh_module('os')
|
|
0 commit comments