Skip to content

Commit b3f330b

Browse files
committed
add unittest for generator function with nested inner function and non local vars
- GeneratorFunctionRootNode: fix (c) header
1 parent 3c7782e commit b3f330b

File tree

2 files changed

+65
-19
lines changed

2 files changed

+65
-19
lines changed

graalpython/com.oracle.graal.python.test/src/tests/test_scope.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -852,6 +852,39 @@ def register():
852852
assert MyClass.a_counter == 24
853853

854854

855+
def test_generator_func_with_nested_nonlocals():
856+
def b_func():
857+
exec_gen = False
858+
859+
def _inner_func():
860+
def doit():
861+
nonlocal exec_gen
862+
exec_gen = True
863+
return [1]
864+
865+
assert set(doit.__code__.co_cellvars) == set()
866+
assert set(doit.__code__.co_freevars) == {'exec_gen'}
867+
for A in doit():
868+
for C in Y:
869+
yield A
870+
871+
assert set(_inner_func.__code__.co_cellvars) == set()
872+
assert set(_inner_func.__code__.co_freevars) == {'Y', 'exec_gen'}
873+
874+
gen = _inner_func()
875+
assert not exec_gen
876+
877+
Y = [1, 2]
878+
879+
list(gen)
880+
assert exec_gen
881+
return gen
882+
883+
assert set(b_func.__code__.co_cellvars) == {'Y', 'exec_gen'}
884+
assert set(b_func.__code__.co_freevars) == set()
885+
b_func()
886+
887+
855888
def test_generator_scope():
856889
my_obj = [1, 2, 3, 4]
857890
my_obj = (i for i in my_obj for j in y)

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/generator/GeneratorFunctionRootNode.java

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,40 @@
11
/*
2-
* Copyright (c) 2017, 2018, Oracle and/or its affiliates.
3-
* Copyright (c) 2013, Regents of the University of California
2+
* Copyright (c) 2018, Oracle and/or its affiliates.
43
*
5-
* All rights reserved.
4+
* The Universal Permissive License (UPL), Version 1.0
65
*
7-
* Redistribution and use in source and binary forms, with or without modification, are
8-
* permitted provided that the following conditions are met:
6+
* Subject to the condition set forth below, permission is hereby granted to any
7+
* person obtaining a copy of this software, associated documentation and/or data
8+
* (collectively the "Software"), free of charge and under any and all copyright
9+
* rights in the Software, and any and all patent rights owned or freely
10+
* licensable by each licensor hereunder covering either (i) the unmodified
11+
* Software as contributed to or provided by such licensor, or (ii) the Larger
12+
* Works (as defined below), to deal in both
913
*
10-
* 1. Redistributions of source code must retain the above copyright notice, this list of
11-
* conditions and the following disclaimer.
12-
* 2. Redistributions in binary form must reproduce the above copyright notice, this list of
13-
* conditions and the following disclaimer in the documentation and/or other materials provided
14-
* with the distribution.
14+
* (a) the Software, and
15+
* (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
16+
* one is included with the Software (each a "Larger Work" to which the
17+
* Software is contributed by such licensors),
1518
*
16-
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
17-
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18-
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19-
* COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20-
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
21-
* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
22-
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
23-
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
24-
* OF THE POSSIBILITY OF SUCH DAMAGE.
19+
* without restriction, including without limitation the rights to copy, create
20+
* derivative works of, display, perform, and distribute the Software and make,
21+
* use, sell, offer for sale, import, export, have made, and have sold the
22+
* Software and the Larger Work(s), and to sublicense the foregoing rights on
23+
* either these or other terms.
24+
*
25+
* This license is subject to the following condition:
26+
*
27+
* The above copyright notice and either this complete permission notice or at a
28+
* minimum a reference to the UPL must be included in all copies or substantial
29+
* portions of the Software.
30+
*
31+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
32+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
33+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
34+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
35+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
36+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
37+
* SOFTWARE.
2538
*/
2639
package com.oracle.graal.python.nodes.generator;
2740

0 commit comments

Comments
 (0)