5555import com .oracle .graal .python .builtins .objects .PNone ;
5656import com .oracle .graal .python .builtins .objects .function .PBuiltinFunction ;
5757import com .oracle .graal .python .builtins .objects .function .PFunction ;
58+ import com .oracle .graal .python .builtins .objects .method .PBuiltinMethod ;
59+ import com .oracle .graal .python .builtins .objects .method .PMethod ;
60+ import com .oracle .graal .python .builtins .objects .module .PythonModule ;
5861import com .oracle .graal .python .nodes .SpecialAttributeNames ;
62+ import com .oracle .graal .python .nodes .attributes .GetAttributeNode ;
5963import com .oracle .graal .python .nodes .function .PythonBuiltinNode ;
64+ import com .oracle .graal .python .nodes .util .CastToStringNode ;
6065import com .oracle .graal .python .runtime .PythonContext ;
6166import com .oracle .graal .python .runtime .PythonCore ;
6267import com .oracle .graal .python .runtime .PythonOptions ;
6368import com .oracle .graal .python .runtime .exception .PythonErrorType ;
6469import com .oracle .truffle .api .CompilerDirectives .TruffleBoundary ;
70+ import com .oracle .truffle .api .CompilerDirectives ;
6571import com .oracle .truffle .api .TruffleFile ;
6672import com .oracle .truffle .api .TruffleLanguage .Env ;
6773import com .oracle .truffle .api .dsl .Cached ;
@@ -215,6 +221,9 @@ protected boolean isMimeType(String lang) {
215221 @ Builtin (name = "export_value" , minNumOfPositionalArgs = 1 , keywordArguments = {"name" })
216222 @ GenerateNodeFactory
217223 public abstract static class ExportSymbolNode extends PythonBuiltinNode {
224+ @ Child private GetAttributeNode getNameAttributeNode ;
225+ @ Child private CastToStringNode castToStringNode ;
226+
218227 @ Specialization
219228 @ TruffleBoundary
220229 public Object exportSymbol (Object value , String name ) {
@@ -235,6 +244,36 @@ public Object exportSymbol(PBuiltinFunction fun, @SuppressWarnings("unused") PNo
235244 getContext ().getEnv ().exportSymbol (fun .getName (), fun );
236245 return fun ;
237246 }
247+
248+ @ Specialization (guards = "isModule(fun.getSelf())" )
249+ @ TruffleBoundary
250+ public Object exportSymbol (PMethod fun , @ SuppressWarnings ("unused" ) PNone name ) {
251+ getContext ().getEnv ().exportSymbol (getMethodName (fun ), fun );
252+ return fun ;
253+ }
254+
255+ @ Specialization (guards = "isModule(fun.getSelf())" )
256+ @ TruffleBoundary
257+ public Object exportSymbol (PBuiltinMethod fun , @ SuppressWarnings ("unused" ) PNone name ) {
258+ getContext ().getEnv ().exportSymbol (getMethodName (fun ), fun );
259+ return fun ;
260+ }
261+
262+ private String getMethodName (Object o ) {
263+ if (getNameAttributeNode == null ) {
264+ CompilerDirectives .transferToInterpreterAndInvalidate ();
265+ getNameAttributeNode = insert (GetAttributeNode .create (SpecialAttributeNames .__NAME__ , null ));
266+ }
267+ if (castToStringNode == null ) {
268+ CompilerDirectives .transferToInterpreterAndInvalidate ();
269+ castToStringNode = insert (CastToStringNode .create ());
270+ }
271+ return castToStringNode .execute (getNameAttributeNode .executeObject (o ));
272+ }
273+
274+ protected static boolean isModule (Object o ) {
275+ return o instanceof PythonModule ;
276+ }
238277 }
239278
240279 @ Builtin (name = "__read__" , fixedNumOfPositionalArgs = 2 )
0 commit comments