IronLanguages / ironpython3

Implementation of Python 3.x for .NET Framework that is built on top of the Dynamic Language Runtime.
Apache License 2.0
2.49k stars 288 forks source link

Exception on using example in GITHUB encoding 850 #1569

Open borjaalonsoarbolus opened 2 years ago

borjaalonsoarbolus commented 2 years ago

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.

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

slozier commented 2 years ago

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.

BCSharp commented 2 years ago

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.

borjaalonsoarbolus commented 2 years ago

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.'

slozier commented 2 years ago

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>

4. Edit Program.cs to contain the following:
```cs
var engine = IronPython.Hosting.Python.CreateEngine();
engine.Execute("print('Hello World!')");
  1. dotnet run
  2. And you should see Hello World!.
borjaalonsoarbolus commented 2 years ago

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