Issue39216
Created on 2020-01-05 01:32 by Anthony Sottile, last changed 2020-01-05 17:05 by pablogsal. This issue is now closed.
| Pull Requests | |||
|---|---|---|---|
| URL | Status | Linked | Edit |
| PR 17837 | merged | Anthony Sottile, 2020-01-05 14:56 | |
| PR 17841 | merged | miss-islington, 2020-01-05 17:04 | |
| Messages (4) | |||
|---|---|---|---|
| msg359317 - (view) | Author: Anthony Sottile (Anthony Sottile) * | Date: 2020-01-05 01:32 | |
while fixing bpo-39215, I noticed that there seems to be a place here where posonlyargs was missed: https://github.com/python/cpython/blob/7dc72b8d4f2c9d1eed20f314fd6425eab66cbc89/Python/ast_opt.c#L617-L627 not sure if this is intentional or not -- happy to make a patch which adds a line there if someone can help me with the test |
|||
| msg359326 - (view) | Author: Pablo Galindo Salgado (pablogsal) * | Date: 2020-01-05 10:08 | |
Hi Anthony,
> not sure if this is intentional or not
Thanks for the catch! Is not intentional. Could you made a PR adding the line?
> Happy to make a patch which adds a line there if someone can help me with the test
We do not have explicit tests for constant folding in the ast level an, in this case, is going to be folded anyway via another code path. For example:
def g():
def f(x : 3 in {1,2,3}, /): ...
return f
print(g.__code__.co_consts[2])
frozenset({1, 2, 3})
|
|||
| msg359327 - (view) | Author: Pablo Galindo Salgado (pablogsal) * | Date: 2020-01-05 10:18 | |
If you want to add a test, you could check the dis output for this example:
def make_func(x):
def f(x : not (x is x), /): ...
In this case the compiler emits:
3 0 LOAD_GLOBAL 0 (x)
2 LOAD_GLOBAL 0 (x)
4 COMPARE_OP 8 (is)
6 UNARY_NOT
8 LOAD_CONST 1 (('x',))
10 BUILD_CONST_KEY_MAP 1
12 LOAD_CONST 2 (<code object f at 0x7f3754f71040, file "/home/pablogsal/github/python/master/lel.py", line 3>)
14 LOAD_CONST 3 ('code.<locals>.f')
16 MAKE_FUNCTION 4 (annotations)
18 STORE_FAST 0 (f)
while for
def make_func(x):
def f(x : not (x is x)): .
emits more efficient code (using 'not is' instead of is and UNARY_NOT:
4 20 LOAD_GLOBAL 0 (x)
22 LOAD_GLOBAL 0 (x)
24 COMPARE_OP 9 (is not)
26 LOAD_CONST 1 (('x',))
28 BUILD_CONST_KEY_MAP 1
30 LOAD_CONST 4 (<code object g at 0x7f3754f135f0, file "/home/pablogsal/github/python/master/lel.py", line 4>)
32 LOAD_CONST 5 ('code.<locals>.g')
34 MAKE_FUNCTION 4 (annotations)
36 STORE_FAST 1 (g)
38 LOAD_CONST 0 (None)
40 RETURN_VALUE
Adding the missing line makes them do the same.
|
|||
| msg359354 - (view) | Author: Pablo Galindo Salgado (pablogsal) * | Date: 2020-01-05 17:05 | |
Thanks again for the fix and the investigation! |
|||
| History | |||
|---|---|---|---|
| Date | User | Action | Args |
| 2020-01-05 17:05:50 | pablogsal | set | status: open -> closed resolution: fixed messages: + msg359354 stage: patch review -> resolved |
| 2020-01-05 17:04:52 | miss-islington | set | pull_requests: + pull_request17268 |
| 2020-01-05 14:56:50 | Anthony Sottile | set | keywords:
+ patch stage: patch review pull_requests: + pull_request17264 |
| 2020-01-05 10:18:08 | pablogsal | set | messages: + msg359327 |
| 2020-01-05 10:08:08 | pablogsal | set | messages: + msg359326 |
| 2020-01-05 02:35:48 | xtreak | set | nosy:
+ pablogsal |
| 2020-01-05 01:32:20 | Anthony Sottile | create | |