ironpython3 icon indicating copy to clipboard operation
ironpython3 copied to clipboard

Exception on using example in GITHUB encoding 850

Open borjaalonsoarbolus opened this issue 3 years ago • 6 comments
trafficstars

Prerequisites

The issue tracker is used to report bugs and request new features, NOT to ask questions.

Questions should be posted to the users mailing list which can be accessed at https://ironpython.groups.io/g/users.

  • [ X] Are you running the latest version?
  • [X] Are you reporting to the correct repository?
  • [X ] Did you perform a cursory search?

Description

System.Reflection.TargetInvocationException: 'Failed to load language 'IronPython 3.4.0b1': No data is available for encoding 850. For information on defining a custom encoding, see the documentation for the Encoding.RegisterProvider method.'

Steps to Reproduce

  1. Console app either NET6 or NET CORE 3.1

  2. System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance); //UNSUCCESSFUL TRY adding this.

  3. The code: public class Program { static void Main(string[] args) {

         var eng = IronPython.Hosting.Python.CreateEngine();
         var scope = eng.CreateScope();
         eng.Execute(@"
             def greetings(name):
                 return 'Hello ' + name.title() + '!'
             ", scope);
         dynamic greetings = scope.GetVariable("greetings");
         System.Console.WriteLine(greetings("world"));
     }
    

    }

Expected behavior:

Shot the greeting in console

Actual behavior:

TargetInvocationException

Versions

IronPython 3.4.0b1

borjaalonsoarbolus avatar Sep 21 '22 20:09 borjaalonsoarbolus

Sounds like you are trying to use the .NET Framework version of the assemblies on .NET 6 (or .NET Core 3.1). This will not work. How are you linking in the IronPython assemblies? You need to use the net6.0 (or netcoreapp3.1) version of the assemblies.

slozier avatar Sep 22 '22 13:09 slozier

You have also reformatted the example by adding some whitespace at the beginning of the lines. After you resolve the linking problem, the example will still fail. In Python, there are strict rules on indentation of code. The simplest way to fix your test code is by moving def to the beginning of the line.

BCSharp avatar Sep 22 '22 19:09 BCSharp

I referenced the NET6 dll with add reference in project, its a .NET 6 console app with nothing else. Thanks!

UPDATE: I tried in another machine, now the error is other encoding(same project)

'Failed to load language 'IronPython 3.4.0b1': No data is available for encoding 437. For information on defining a custom encoding, see the documentation for the Encoding.RegisterProvider method.'

borjaalonsoarbolus avatar Sep 23 '22 06:09 borjaalonsoarbolus

This is exactly the issue you would get when trying to use the .NET Framework version of the assemblies. Can you try changing your program to:

Console.WriteLine(IronPython.Runtime.ClrModule.TargetFramework);

What does this output? You should see .NETCoreApp,Version=v6.0 if you are using the correct assemblies.

In any case, I would suggest using the NuGet package instead of referencing the assemblies directly. Assuming you're using the .NET 6 SDK:

  1. dotnet new console
  2. dotnet add package IronPython -v 3.4.0-beta1
  3. Your project file now looks like this:
<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net6.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="IronPython" Version="3.4.0-beta1" />
  </ItemGroup>

</Project>
  1. Edit Program.cs to contain the following:
var engine = IronPython.Hosting.Python.CreateEngine();
engine.Execute("print('Hello World!')");
  1. dotnet run
  2. And you should see Hello World!.

slozier avatar Sep 23 '22 13:09 slozier

Thanks all i will try and tell you if it worked, sorry , its beeing a busy week.

borjaalonsoarbolus avatar Sep 29 '22 12:09 borjaalonsoarbolus