This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author vstinner
Recipients serhiy.storchaka, vstinner, yselivanov
Date 2016-01-26.22:01:33
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1453845693.38.0.0964972703029.issue26204@psf.upfronthosting.co.za>
In-reply-to
Content
Serhiy: "It looks to me that this optimization was added to avoid spending executing time for docstrings."

Hum, let me dig Mercurial history.
----
changeset:   39364:8ef3f8cf90e1
branch:      legacy-trunk
user:        Neal Norwitz <nnorwitz@gmail.com>
date:        Fri Aug 04 05:09:28 2006 +0000
files:       Lib/test/test_code.py Misc/NEWS Python/compile.c
description:
Bug #1333982: string/number constants were inappropriately stored
in the byte code and co_consts even if they were not used, ie
immediately popped off the stack.
---

https://bugs.python.org/issue1333982#msg26659:
"For reference, an optimization that got lost: (...) 'a' is the docstring, but the 'b' previously did not show up anywhere in the code object.  Now there is the LOAD_CONST/POP_TOP pair."

Ah, it was a regression introduced by the new AST compiler. But the change introduced a new optimization: numbers are now also ignored. In Python 2.4 (before AST), numbers were not ignored:
---
>>> def f():
...  "a"
...  1
...  "b"
... 

>>> import dis; dis.dis(f)
  3           0 LOAD_CONST               1 (1)
              3 POP_TOP             
              4 LOAD_CONST               2 (None)
              7 RETURN_VALUE        
---


If we continue to dig deeper, before AST, I found:
---
changeset:   4991:8276916e1ea8
branch:      legacy-trunk
user:        Guido van Rossum <guido@python.org>
date:        Fri Jan 17 21:04:03 1997 +0000
files:       Python/compile.c
description:
Add co_stacksize field to codeobject structure, and stacksize argument
to PyCode_New() argument list.  Move MAXBLOCKS constant to conpile.h.

Added accurate calculation of the actual stack size needed by the
generated code.

Also commented out all fprintf statements (except for a new one to
diagnose stack underflow, and one in #ifdef'ed out code), and added
some new TO DO suggestions (now that the stacksize is taken of the TO
DO list).
---

This patch added the following code to com_expr_stmt() in Python/compile.c:

+       /* Forget it if we have just a doc string here */
+       if (NCH(n) == 1 && get_rawdocstring(n) != NULL)
+               return;

I'm unable to find the exact part of the compiler which ignores strings in statement expressions.
History
Date User Action Args
2016-01-26 22:01:33vstinnersetrecipients: + vstinner, serhiy.storchaka, yselivanov
2016-01-26 22:01:33vstinnersetmessageid: <1453845693.38.0.0964972703029.issue26204@psf.upfronthosting.co.za>
2016-01-26 22:01:33vstinnerlinkissue26204 messages
2016-01-26 22:01:33vstinnercreate