Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrong bytecode generated for 'in' operation #62408

Closed
phmc mannequin opened this issue Jun 14, 2013 · 3 comments
Closed

Wrong bytecode generated for 'in' operation #62408

phmc mannequin opened this issue Jun 14, 2013 · 3 comments
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs)

Comments

@phmc
Copy link
Mannequin

phmc mannequin commented Jun 14, 2013

BPO 18208
Nosy @brettcannon, @birkenfeld, @amauryfa, @ncoghlan, @benjaminp, @phmc

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = <Date 2013-06-14.14:59:31.484>
created_at = <Date 2013-06-14.08:42:34.558>
labels = ['interpreter-core', 'invalid']
title = "Wrong bytecode generated for 'in' operation"
updated_at = <Date 2013-06-14.14:59:31.483>
user = 'https://github.com/phmc'

bugs.python.org fields:

activity = <Date 2013-06-14.14:59:31.483>
actor = 'pconnell'
assignee = 'none'
closed = True
closed_date = <Date 2013-06-14.14:59:31.484>
closer = 'pconnell'
components = ['Interpreter Core']
creation = <Date 2013-06-14.08:42:34.558>
creator = 'pconnell'
dependencies = []
files = []
hgrepos = []
issue_num = 18208
keywords = []
message_count = 3.0
messages = ['191108', '191112', '191134']
nosy_count = 7.0
nosy_names = ['brett.cannon', 'georg.brandl', 'amaury.forgeotdarc', 'ncoghlan', 'benjamin.peterson', 'pconnell', 'isoschiz']
pr_nums = []
priority = 'normal'
resolution = 'not a bug'
stage = None
status = 'closed'
superseder = None
type = None
url = 'https://bugs.python.org/issue18208'
versions = []

@phmc
Copy link
Mannequin Author

phmc mannequin commented Jun 14, 2013

The following two expressions should have the same value:

Python 3.4.0a0 (default:fae92309c3be, Jun 14 2013, 09:29:54) 
[GCC 4.8.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 1 in [2] == False
False
>>> (1 in [2]) == False
True

It looks like this is a compiler issue - there shouldn't be a jump if the 'in' expression is false:

>>> dis.dis("1 in [2] == False")
  1           0 LOAD_CONST               0 (1)
              3 LOAD_CONST               1 (2)
              6 BUILD_LIST               1
              9 DUP_TOP             
             10 ROT_THREE           
             11 COMPARE_OP               6 (in)
             14 JUMP_IF_FALSE_OR_POP    24
             17 LOAD_CONST               2 (False)
             20 COMPARE_OP               2 (==)
             23 RETURN_VALUE        
        >>   24 ROT_TWO             
             25 POP_TOP             
             26 RETURN_VALUE        
>>>

@phmc phmc mannequin added the interpreter-core (Objects, Python, Grammar, and Parser dirs) label Jun 14, 2013
@amauryfa
Copy link
Member

This is one case of chained comparisons:
http://docs.python.org/3/reference/expressions.html#not-in

"x <= y <= z" is equivalent to "(x <= y) and (y <= z)"
"x in y == z" is equivalent to "(x in y) and (y == z)"

There is a jump if the 'in' expression is false, because 'and' should short-circuit the second comparison.

@phmc
Copy link
Mannequin Author

phmc mannequin commented Jun 14, 2013

Thanks Amaury. That's quite surprising, but I wouldn't advocate changing such long standing behaviour.

I'm closing the issue.

@phmc phmc mannequin closed this as completed Jun 14, 2013
@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs)
Projects
None yet
Development

No branches or pull requests

1 participant