1

I am trying to use IronPython with monodroid (using a Android 4 phone) so that I can execute a python script inside my project. I'm basing my code off of the example here: Instantiating a python class in C#

I have included the dlls in the Android platform directory (using IronPython-2.7.2rc1) and have a python script in the project directory (right now a test script called Calculator from that example linked)

The following is my C# code:

using System;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using IronPython;
using IronPython.Hosting;
using Microsoft.Scripting;
using Microsoft.Scripting.Hosting; 
namespace PythonTest
{
    [Activity (Label = "PythonTest", MainLauncher = true)]
    public class Activity1 : Activity
    {
        int count = 1;
        protected override void OnCreate (Bundle bundle)
        {
            base.OnCreate (bundle);

            // Set our view from the "main" layout resource
            SetContentView (Resource.Layout.Main);

            ScriptEngine engine = Python.CreateEngine();
            ScriptSource source = engine.CreateScriptSourceFromFile("Calculator.py");
            ScriptScope scope = engine.CreateScope();
            ObjectOperations op = engine.Operations;
            source.Execute(scope); // class object created
            object klaz = scope.GetVariable("Calculator"); // get the class object
            object instance = op.Invoke(klaz); // create the instance
            object method = op.GetMember(instance, "add"); // get a method
            int result = (int)op.Invoke(method, 4, 5); // call method and get result (9)


            var aLabel = new TextView (this);
            //aLabel.Text = result.ToString();

            // Get our button from the layout resource,
            // and attach an event to it
            Button button = FindViewById<Button> (Resource.Id.myButton);

            button.Click += delegate {
                button.Text = string.Format ("{0} clicks!", count++); };
        }
    }
}

It builds with no errors. When I try to run this on my phone, I get these errors and the program crashes (phone goes back to home screen)

I/monodroid-gc( 2110): environment supports jni NewWeakGlobalRef

I/MonoDroid( 2110): UNHANDLED EXCEPTION: System.NotImplementedException: The requested feature is not implemented.

I/MonoDroid( 2110): at Microsoft.Scripting.PlatformAdaptationLayer.OpenInputFileStream (string) <0x0001c>

I/MonoDroid( 2110): at Microsoft.Scripting.FileStreamContentProvider/PALHolder.GetStream (string) <0x0002b>

I/MonoDroid( 2110): at Microsoft.Scripting.FileStreamContentProvider.GetStream () <0x00023>

I/MonoDroid( 2110): at Microsoft.Scripting.Runtime.LanguageBoundTextContentProvider.GetReader () <0x00027>

I/MonoDroid( 2110): at Microsoft.Scripting.SourceUnit.GetReader () <0x00023>

I/MonoDroid( 2110): at IronPython.Compiler.Parser.CreateParserWorker (Microsoft.Scripting.Runtime.CompilerContext,IronPython.PythonOptions,bool) <0x000bf>

I/MonoDroid( 2110): at IronPython.Compiler.Parser.CreateParser (Microsoft.Scripting.Runtime.CompilerContext,IronPython.PythonOptions) <0x0001f>

I/MonoDroid( 2110): at IronPython.Runtime.PythonContext.ParseAndBindAst (Microsoft.Scripting.Runtime.CompilerContext) <0x0005f>

I/MonoDroid( 2110): at IronPython.Runtime.PythonContext.CompilePythonCode (Microsoft.Scripting.SourceUnit,Microsoft.Scripting.CompilerOptions,Microsoft.Scripting.ErrorSink) <0x0008f>

I/MonoDroid( 2110): at IronPython.Runtime.PythonContext.CompileSourceCode (Microsoft.Scripting.SourceUnit,Microsoft.Scripting.CompilerOptions,Microsoft.Scripting.ErrorSink) <0x0002b>

I/MonoDroid( 2110): at Microsoft.Scripting.SourceUnit.Compile (Microsoft.Scripting.CompilerOptions,Microsoft.Scripting.ErrorSink) <0x0005b>

I/MonoDroid( 2110): at Microsoft.Scripting.SourceUnit.Execute (Microsoft.Scripting.Runtime.Scope,Microsoft.Scripting.ErrorSink) <0x00057>

I/MonoDroid( 2110): at Microsoft.Scripting.SourceUnit.Execute (Microsoft.Scripting.Runtime.Scope) <0x00027>

I/MonoDroid( 2110): at Microsoft.Scripting.Hosting.ScriptSource.Execute (Microsoft.Scripting.Hosting.ScriptScope) <0x00043>

I/MonoDroid( 2110): at PythonTest.Activity1.OnCreate (Android.OS.Bundle) <0x000c3>

I/MonoDroid( 2110): at Android.App.Activity.n_OnCreate_Landroid_os_Bundle_ (intptr,intptr,intptr) <0x00057>

I/MonoDroid( 2110): at (wrapper dynamic-method) object.5a9608db-a03f-4bd2-831d-6cb21e250354 (intptr,intptr,intptr) <0x00033>

E/mono    ( 2110): 

E/mono    ( 2110): Unhandled Exception: System.NotImplementedException: The requested feature is not implemented.

E/mono    ( 2110):   at Microsoft.Scripting.PlatformAdaptationLayer.OpenInputFileStream (System.String path) [0x00000] in <filename unknown>:0 

E/mono    ( 2110):   at Microsoft.Scripting.FileStreamContentProvider+PALHolder.GetStream (System.String path) [0x00000] in <filename unknown>:0 

E/mono    ( 2110):   at Microsoft.Scripting.FileStreamContentProvider.GetStream () [0x00000] in <filename unknown>:0 

E/mono    ( 2110):   at Microsoft.Scripting.Runtime.LanguageBoundTextContentProvider.GetReader () [0x00000] in <filename unknown>:0 

E/mono    ( 2110):   at Microsoft.Scripting.SourceUnit.GetReader () [0x00000] in <filename unknown>:0 

E/mono    ( 2110):   at IronPython.Compiler.Parser.CreateParserWorker (Microsoft.Scripting.Runtime.CompilerContext context, IronPython.PythonOptions options, Boolean verbatim) [0x00000] in <filename unknown>:0 

E/mono    ( 2110):   at IronPython.Compiler.Parser.CreateParser (Microsoft.Scripting.Runtime.CompilerContext context, IronPython.PythonOptions options) [0x00000] in <filename unknown>:0 

E/mono    ( 2110):   at IronPython.Ru

Does anyone know how to fix this? Thank you.

Community
  • 1
  • 1
d10sfan
  • 105
  • 1
  • 5

1 Answers1

0

Looks like a straight-up bug (the PAL for android is incomplete), and it looks like you already opened an issue (thank you!).

IronPython's support is very early, so the more people testing it and reporting issues, the better.

Jeff Hardy
  • 7,632
  • 24
  • 24