Issue 27352: Bug in IMPORT_NAME (original) (raw)

Created on 2016-06-19 11:53 by serhiy.storchaka, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
import_name_level.patch serhiy.storchaka,2016-06-19 11:53
import_name_level_2.patch serhiy.storchaka,2016-06-26 19:51 review
Messages (11)
msg268849 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2016-06-19 11:53
Seems there is a bug in the implementation of the IMPORT_NAME opcode in ceval.c. If the level argument is -1, it is not passed to __import__ (should never happen, but looks correct). If it is an integer != -1 in C long range, it is passed to __import__ (this is correct). But if it is not integer (e.g. None) or can't be converted to C long, an exception is set and __import__ is called with level and not cleared error (this is wrong). In correct bytecode the level argument can be either integer or None. Default __import__ accepts only integers as the level argument and checks the range. Proposed patch makes the code always passing the level argument to __import__ unless it is None.
msg268931 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2016-06-20 20:57
My suspicion is the -1 aspect is a hold-over from that being the default level value in Python 2.7 that no one removed. The patch LGTM. Do we need a test for this for some reason?
msg268939 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2016-06-20 21:35
Thak you for the explanation Brett. Since current "import" without "from" is not broken, I suppose that the raised exception is cleared somewhere later. The patch doesn't have visible effect (unless may be using nonstandard loaders or __import__, don't know). It just cleans up the code, avoid raising unneeded exception (small performance boost) and may be fix some hidden non-reproducible bug.
msg268940 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2016-06-20 21:36
Then I say go ahead and commit it.
msg269306 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2016-06-26 19:51
Sorry, I was wrong. The level argument is always non-negative integer (if a bytecode is not corrupted). The second branch can be just removed. Following patch also fixes the validation of the ImportFrom AST node.
msg269385 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2016-06-27 16:33
LGTM
msg269394 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2016-06-27 18:39
New changeset e3164c9edb0b by Serhiy Storchaka in branch 'default': Issue #27352: Correct the validation of the ImportFrom AST node and simplify https://hg.python.org/cpython/rev/e3164c9edb0b
msg269395 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2016-06-27 18:40
Thanks Brett.
msg269401 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2016-06-27 20:33
You broke Python! Example: http://buildbot.python.org/all/builders/AMD64%20Snow%20Leop%203.x/builds/4853/steps/test/logs/stdio ====================================================================== FAIL: test_importfrom (test.test_ast.ASTValidatorTests) ---------------------------------------------------------------------- Traceback (most recent call last): File "/Users/buildbot/buildarea/3.x.murray-snowleopard/build/Lib/test/test_ast.py", line 757, in test_importfrom self.stmt(imp, "level less than -1") File "/Users/buildbot/buildarea/3.x.murray-snowleopard/build/Lib/test/test_ast.py", line 579, in stmt self.mod(mod, msg) File "/Users/buildbot/buildarea/3.x.murray-snowleopard/build/Lib/test/test_ast.py", line 571, in mod self.assertIn(msg, str(cm.exception)) AssertionError: 'level less than -1' not found in 'Negative ImportFrom level'
msg269402 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2016-06-27 20:41
New changeset e5063a82f490 by Serhiy Storchaka in branch 'default': Issue #27352: Fixed an error message in a test. https://hg.python.org/cpython/rev/e5063a82f490
msg269403 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2016-06-27 20:54
Thanks Victor!
History
Date User Action Args
2022-04-11 14:58:32 admin set github: 71539
2016-06-27 20:54:17 serhiy.storchaka set status: open -> closedresolution: fixedmessages: +
2016-06-27 20:41:03 python-dev set messages: +
2016-06-27 20:33:04 vstinner set status: closed -> opennosy: + vstinnermessages: + resolution: fixed -> (no value)
2016-06-27 18:40:51 serhiy.storchaka set status: open -> closedresolution: fixedmessages: + stage: commit review -> resolved
2016-06-27 18:39:38 python-dev set nosy: + python-devmessages: +
2016-06-27 16:33:17 brett.cannon set messages: + stage: patch review -> commit review
2016-06-26 19:51:42 serhiy.storchaka set files: + import_name_level_2.patchtype: behavior -> enhancementversions: - Python 2.7, Python 3.5nosy: + georg.brandl, benjamin.peterson, yselivanovmessages: + stage: commit review -> patch review
2016-06-20 21:36:30 brett.cannon set messages: +
2016-06-20 21:35:38 serhiy.storchaka set messages: +
2016-06-20 20:57:41 brett.cannon set assignee: serhiy.storchakamessages: + stage: patch review -> commit review
2016-06-19 11:53:12 serhiy.storchaka create