Skip to content

Commit 3cf2165

Browse files
committed
GR-37243: fix __module__ attribute for functions when not found in globals
1 parent 9b2f722 commit 3cf2165

File tree

4 files changed

+33
-9
lines changed

4 files changed

+33
-9
lines changed

graalpython/com.oracle.graal.python.cext/posix/posix.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2020, 2021, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2020, 2022, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* The Universal Permissive License (UPL), Version 1.0

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

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright (c) 2018, 2021, Oracle and/or its affiliates. All rights reserved.
1+
# Copyright (c) 2018, 2022, Oracle and/or its affiliates. All rights reserved.
22
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
33
#
44
# The Universal Permissive License (UPL), Version 1.0
@@ -249,3 +249,21 @@ def test_function_text_signature_writable():
249249
def foo(): pass
250250
foo.__text_signature__ = 'foo()'
251251
assert foo.__text_signature__ == 'foo()'
252+
253+
254+
def test_function_no_module():
255+
def wrapper_2(func):
256+
ns = {}
257+
exec('def fail_func(): raise ValueError("fail")', ns)
258+
return ns['fail_func']
259+
260+
def wrapper_1(func):
261+
assert hasattr(func, '__name__')
262+
assert hasattr(func, '__module__')
263+
assert func.__module__ is None
264+
return func
265+
266+
@wrapper_1
267+
@wrapper_2
268+
def test_func():
269+
pass

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/function/AbstractFunctionBuiltins.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2017, 2021, Oracle and/or its affiliates.
2+
* Copyright (c) 2017, 2022, Oracle and/or its affiliates.
33
* Copyright (c) 2014, Regents of the University of California
44
*
55
* All rights reserved.
@@ -67,6 +67,7 @@
6767
import com.oracle.graal.python.nodes.object.GetOrCreateDictNode;
6868
import com.oracle.graal.python.nodes.object.SetDictNode;
6969
import com.oracle.graal.python.nodes.subscript.GetItemNode;
70+
import com.oracle.graal.python.runtime.exception.PException;
7071
import com.oracle.truffle.api.CompilerDirectives;
7172
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
7273
import com.oracle.truffle.api.dsl.Cached;
@@ -181,10 +182,15 @@ static Object getModule(VirtualFrame frame, PFunction self, @SuppressWarnings("u
181182
if (module == PNone.NO_VALUE) {
182183
CompilerDirectives.transferToInterpreter();
183184
PythonObject globals = self.getGlobals();
184-
if (globals instanceof PythonModule) {
185-
module = globals.getAttribute(__NAME__);
186-
} else {
187-
module = getItem.execute(frame, globals, __NAME__);
185+
// __module__: If module name is in globals, use it. Otherwise, use None.
186+
try {
187+
if (globals instanceof PythonModule) {
188+
module = globals.getAttribute(__NAME__);
189+
} else {
190+
module = getItem.execute(frame, globals, __NAME__);
191+
}
192+
} catch (PException pe) {
193+
module = PNone.NONE;
188194
}
189195
writeObject.execute(self, __MODULE__, module);
190196
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/lib/PyConvertOptionalToSizeNode.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@
5252
import com.oracle.truffle.api.frame.VirtualFrame;
5353

5454
/**
55-
* Equivalent of CPython's {@code _Py_convert_optional_to_ssize_t} function.
56-
* If value is {@code None}, {@code defaultValue} is returned.
55+
* Equivalent of CPython's {@code _Py_convert_optional_to_ssize_t} function. If value is
56+
* {@code None}, {@code defaultValue} is returned.
5757
*/
5858
@GenerateUncached
5959
public abstract class PyConvertOptionalToSizeNode extends PNodeWithContext {

0 commit comments

Comments
 (0)