nlocals: 3
flags: 3
consts: ("'hello'", "'world'")
+
+>>> class class_with_docstring:
+... '''This is a docstring for class'''
+... '''This line is not docstring'''
+... pass
+
+>>> print(class_with_docstring.__doc__)
+This is a docstring for class
+
+>>> class class_without_docstring:
+... pass
+
+>>> print(class_without_docstring.__doc__)
+None
"""
import copy
3 * [(42, 42, None, None)],
)
+ @cpython_only
+ def test_docstring_under_o2(self):
+ code = textwrap.dedent('''
+ def has_docstring(x, y):
+ """This is a first-line doc string"""
+ """This is a second-line doc string"""
+ a = x + y
+ b = x - y
+ return a, b
+
+
+ def no_docstring(x):
+ def g(y):
+ return x + y
+ return g
+
+
+ async def async_func():
+ """asynf function doc string"""
+ pass
+
+
+ for func in [has_docstring, no_docstring(4), async_func]:
+ assert(func.__doc__ is None)
+ ''')
+
+ rc, out, err = assert_python_ok('-OO', '-c', code)
if check_impl_detail(cpython=True) and ctypes is not None:
py = ctypes.pythonapi
l = lambda: "foo"
self.assertIsNone(l.__doc__)
+ def test_lambda_consts(self):
+ l = lambda: "this is the only const"
+ self.assertEqual(l.__code__.co_consts, ("this is the only const",))
+
def test_encoding(self):
code = b'# -*- coding: badencoding -*-\npass\n'
self.assertRaises(SyntaxError, compile, code, 'tmp', 'exec')
# Merge constants in tuple or frozenset
f1, f2 = lambda: "not a name", lambda: ("not a name",)
f3 = lambda x: x in {("not a name",)}
- self.assertIs(f1.__code__.co_consts[1],
- f2.__code__.co_consts[1][0])
- self.assertIs(next(iter(f3.__code__.co_consts[1])),
- f2.__code__.co_consts[1])
+ self.assertIs(f1.__code__.co_consts[0],
+ f2.__code__.co_consts[0][0])
+ self.assertIs(next(iter(f3.__code__.co_consts[0])),
+ f2.__code__.co_consts[0])
# {0} is converted to a constant frozenset({0}) by the peephole
# optimizer
def with_const_expression():
"also" + " not docstring"
+
+ def multiple_const_strings():
+ "not docstring " * 3
""")
for opt in [0, 1, 2]:
self.assertIsNone(ns['two_strings'].__doc__)
self.assertIsNone(ns['with_fstring'].__doc__)
self.assertIsNone(ns['with_const_expression'].__doc__)
+ self.assertIsNone(ns['multiple_const_strings'].__doc__)
@support.cpython_only
def test_docstring_interactive_mode(self):
codegen_enter_scope(c, name, COMPILE_SCOPE_ANNOTATIONS,
key, loc.lineno, NULL, &umd));
- // Insert None into consts to prevent an annotation
- // appearing to be a docstring
- _PyCompile_AddConst(c, Py_None);
+ assert(!SYMTABLE_ENTRY(c)->ste_has_docstring);
// if .format != 1: raise NotImplementedError
_Py_DECLARE_STR(format, ".format");
ADDOP_I(c, loc, LOAD_FAST, 0);
/* If from __future__ import annotations is active,
* every annotated class and module should have __annotations__.
* Else __annotate__ is created when necessary. */
- if ((FUTURE_FEATURES(c) & CO_FUTURE_ANNOTATIONS) && SYMTABLE_ENTRY(c)->ste_annotations_used) {
+ PySTEntryObject *ste = SYMTABLE_ENTRY(c);
+ if ((FUTURE_FEATURES(c) & CO_FUTURE_ANNOTATIONS) && ste->ste_annotations_used) {
ADDOP(c, loc, SETUP_ANNOTATIONS);
}
if (!asdl_seq_LEN(stmts)) {
}
Py_ssize_t first_instr = 0;
if (!is_interactive) { /* A string literal on REPL prompt is not a docstring */
- PyObject *docstring = _PyAST_GetDocString(stmts);
- if (docstring) {
+ if (ste->ste_has_docstring) {
+ PyObject *docstring = _PyAST_GetDocString(stmts);
+ assert(docstring);
first_instr = 1;
/* set docstring */
assert(OPTIMIZATION_LEVEL(c) < 2);
RETURN_IF_ERROR(
codegen_enter_scope(c, name, scope_type, (void *)s, firstlineno, NULL, &umd));
+ PySTEntryObject *ste = SYMTABLE_ENTRY(c);
Py_ssize_t first_instr = 0;
- PyObject *docstring = _PyAST_GetDocString(body);
- assert(OPTIMIZATION_LEVEL(c) < 2 || docstring == NULL);
- if (docstring) {
+ if (ste->ste_has_docstring) {
+ PyObject *docstring = _PyAST_GetDocString(body);
+ assert(docstring);
first_instr = 1;
docstring = _PyCompile_CleanDoc(docstring);
if (docstring == NULL) {
NEW_JUMP_TARGET_LABEL(c, start);
USE_LABEL(c, start);
- PySTEntryObject *ste = SYMTABLE_ENTRY(c);
bool add_stopiteration_handler = ste->ste_coroutine || ste->ste_generator;
if (add_stopiteration_handler) {
/* codegen_wrap_in_stopiteration_handler will push a block, so we need to account for that */
ADDOP_LOAD_CONST_NEW(c, loc, defaults);
RETURN_IF_ERROR(
codegen_setup_annotations_scope(c, LOC(s), s, name));
- /* Make None the first constant, so the evaluate function can't have a
- docstring. */
- RETURN_IF_ERROR(_PyCompile_AddConst(c, Py_None));
+
+ assert(!SYMTABLE_ENTRY(c)->ste_has_docstring);
VISIT_IN_SCOPE(c, expr, s->v.TypeAlias.value);
ADDOP_IN_SCOPE(c, loc, RETURN_VALUE);
PyCodeObject *co = _PyCompile_OptimizeAndAssemble(c, 0);
codegen_enter_scope(c, &_Py_STR(anon_lambda), COMPILE_SCOPE_LAMBDA,
(void *)e, e->lineno, NULL, &umd));
- /* Make None the first constant, so the lambda can't have a
- docstring. */
- RETURN_IF_ERROR(_PyCompile_AddConst(c, Py_None));
+ assert(!SYMTABLE_ENTRY(c)->ste_has_docstring);
VISIT_IN_SCOPE(c, expr, e->v.Lambda.body);
if (SYMTABLE_ENTRY(c)->ste_generator) {