Where is the error ??

Apr 27, 2010 at 8:05 PM

I am using VS2010 + .Net 4.0 and I had compiled all sources from this download (CodePlex_DLR-1.0-Src.zip).

In a WPF project I created a box to receive a expression (like "2 + 3") and then, after create the object, I make a call to the function Calculate() but I had received a error... Why?

Can someone ilustrate where I was wrong, please?

Imports Microsoft.Scripting
Imports Microsoft.Scripting.Hosting
Imports Microsoft.Scripting.Runtime
Imports IronPython
Imports IronPython.Modules

Imports System.Collections.Generic

Public Class MyEngine

    Private _engine As ScriptEngine
    Private _scope As ScriptScope

    Public Sub New()
        MyBase.new()

        Dim options As Dictionary(Of String, Object) = New Dictionary(Of String, Object) ' CreateObject("Scripting.Dictionary")
        options("DivisionOptions") = PythonDivisionOptions.New

        _engine = IronPython.Hosting.Python.CreateEngine(options)
        _scope = _engine.CreateScope()
    End Sub

    Public Function Calculate(ByVal code As String) As String
        Dim src As ScriptSource = _engine.CreateScriptSourceFromString(code, SourceCodeKind.Expression)
        Dim ret As Object = src.Execute(_scope)
        Return ret.ToString
    End Function
End Class
Apr 27, 2010 at 8:19 PM

Hi.  Can you elaborate on the exact error you’re seeing?  Your code works fine for me when used as…

Module Module1

    Sub Main()

        Dim eng = New MyEngine()

        Dim res = eng.Calculate("2+3")

    End Sub

End Module

res is the string “5”

From: Ildefonso [mailto:notifications@codeplex.com]
Sent: Tuesday, April 27, 2010 1:05 PM
To: Ryan Dawson
Subject: Where is the error ?? [dlr:210752]

From: Ildefonso

I am using VS2010 + .Net 4.0 and I had compiled all sources from this download (CodePlex_DLR-1.0-Src.zip).

In a WPF project I created a box to receive a expression (like "2 + 3") and then, after create the object, I make a call to the function Calculate() but I had received a error... Why?

Can someone ilustrate where I was wrong, please?

Imports Microsoft.Scripting
Imports Microsoft.Scripting.Hosting
Imports Microsoft.Scripting.Runtime
Imports IronPython
Imports IronPython.Modules
 
Imports System.Collections.Generic
 
Public Class MyEngine
 
    Private _engine As ScriptEngine
    Private _scope As ScriptScope
 
    Public Sub New()
        MyBase.new()
 
        Dim options As Dictionary(Of String, Object) = New Dictionary(Of String, Object) ' CreateObject("Scripting.Dictionary")
        options("DivisionOptions") = PythonDivisionOptions.New
 
        _engine = IronPython.Hosting.Python.CreateEngine(options)
        _scope = _engine.CreateScope()
    End Sub
 
    Public Function Calculate(ByVal code As String) As String
        Dim src As ScriptSource = _engine.CreateScriptSourceFromString(code, SourceCodeKind.Expression)
        Dim ret As Object = src.Execute(_scope)
        Return ret.ToString
    End Function
End Class

Read the full discussion online.

To add a post to this discussion, reply to this email (dlr@discussions.codeplex.com)

To start a new discussion for this project, email dlr@discussions.codeplex.com

You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe or change your settings on codePlex.com.

Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at codeplex.com

Apr 27, 2010 at 8:59 PM

Hi, Ryan. Thanks, again.

I will try get the exact erro line from yours code. It gone deap into sources calls.

But I just observed that the same program in a WinForm project didn't fire erros.

Is there the possibility that has problems with WPF and Silverlight (like I just post at http://dlr.codeplex.com/Thread/View.aspx?ThreadId=210348) ??

Please, try using WPF if possible. The xaml code of form is...

<Window x:Class="MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Interpretador Python" mc:Ignorable="d" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" Height="440" Width="660">
    <Grid>
        <Button Content="Processar" Height="23" HorizontalAlignment="Left" Margin="35,51,0,0" Name="ProcessarButton" VerticalAlignment="Top" Width="89" />
        <TextBox Height="23" Margin="12,22,0,0" Name="ArgumentosTextBox" VerticalAlignment="Top" HorizontalAlignment="Left" Width="120" AllowDrop="True" />
        <TextBlock Height="23" HorizontalAlignment="Left" Margin="12,95,0,0" Name="ResultadosTextBlock" Text="Resultados..." VerticalAlignment="Top" Width="236" />
        <Label Content="Argumentos" Height="28" HorizontalAlignment="Left" Margin="12,0,0,0" Name="Label1" VerticalAlignment="Top" />
        <TextBox AllowDrop="True" Margin="266,95,12,12" Name="CodeTextBox" AcceptsTab="True" AcceptsReturn="True" AutoWordSelection="True" FontFamily="Courier New" FontSize="11" HorizontalScrollBarVisibility="Visible" VerticalScrollBarVisibility="Visible" Text=" 2+3" />
        <Label Content="Programação" Height="28" HorizontalAlignment="Left" Margin="266,73,0,0" Name="Label2" VerticalAlignment="Top" />
    </Grid>
</Window>

Apr 27, 2010 at 9:53 PM

Hi.  One thing about your XAML, your default text is actually “ 2+3” (note the initial space).  Of course white space is significant in Python, so this will not parse as a valid expression.  It causes a SyntaxErrorException instead.

Remove the initial space and everything works.  For reference, this is the backing code I used to test your WPF…

Class MainWindow

    Private Sub ProcessarButton_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles ProcessarButton.Click

        Dim _eng = New MyEngine()

        Me.ResultadosTextBlock.Text = _eng.Calculate(Me.CodeTextBox.Text)

    End Sub

End Class

Cheers,

                -Ryan

From: Ildefonso [mailto:notifications@codeplex.com]
Sent: Tuesday, April 27, 2010 2:00 PM
To: Ryan Dawson
Subject: Re: Where is the error ?? [dlr:210752]

From: Ildefonso

Hi, Ryan. Thanks, again.

I will try get the exact erro line from yours code. It gone deap into sources calls.

But I just observed that the same program in a WinForm project didn't fire erros.

Is there the possibility that has problems with WPF and Silverlight (like I just post at http://dlr.codeplex.com/Thread/View.aspx?ThreadId=210348) ??

Please, try using WPF if possible. The xaml code of form is...

<Window x:Class="MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Interpretador Python" mc:Ignorable="d" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" Height="440" Width="660">
    <Grid>
        <Button Content="Processar" Height="23" HorizontalAlignment="Left" Margin="35,51,0,0" Name="ProcessarButton" VerticalAlignment="Top" Width="89" />
        <TextBox Height="23" Margin="12,22,0,0" Name="ArgumentosTextBox" VerticalAlignment="Top" HorizontalAlignment="Left" Width="120" AllowDrop="True" />
        <TextBlock Height="23" HorizontalAlignment="Left" Margin="12,95,0,0" Name="ResultadosTextBlock" Text="Resultados..." VerticalAlignment="Top" Width="236" />
        <Label Content="Argumentos" Height="28" HorizontalAlignment="Left" Margin="12,0,0,0" Name="Label1" VerticalAlignment="Top" />
        <TextBox AllowDrop="True" Margin="266,95,12,12" Name="CodeTextBox" AcceptsTab="True" AcceptsReturn="True" AutoWordSelection="True" FontFamily="Courier New" FontSize="11" HorizontalScrollBarVisibility="Visible" VerticalScrollBarVisibility="Visible" Text=" 2+3" />
        <Label Content="Programação" Height="28" HorizontalAlignment="Left" Margin="266,73,0,0" Name="Label2" VerticalAlignment="Top" />
    </Grid>
</Window>
 

Read the full discussion online.

To add a post to this discussion, reply to this email (dlr@discussions.codeplex.com)

To start a new discussion for this project, email dlr@discussions.codeplex.com

You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe or change your settings on codePlex.com.

Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at codeplex.com

Apr 27, 2010 at 9:59 PM

If you specify SourceCodeKind.Expression to ScriptSource factory method IronPython is strict about whitespace. You can specify SourceCodeKind.AutoDetect to relax the syntax. Or in your case you can just call _engine.Execute(code, _scope) instead of constructing a ScriptSource object.

Apr 27, 2010 at 10:02 PM

Oops, it seems that the relaxation is not enough and the leading space will still cause a syntax error. Maybe we should consider allowing leading whitespace in this mode.

Apr 27, 2010 at 10:09 PM

>>> Oops, it seems that the relaxation is not enough and the leading space will still cause a syntax error. Maybe we should consider allowing leading whitespace in this mode.

We discussed this once before, but never went anywhere with it. This would be useful for the DLR Silverlight integration, as it contains code to remove the whitespace margin created by using <script> tags on the HTML page.

Coordinator
Apr 27, 2010 at 10:18 PM

I agree for simple expressions we should trim the string – in fact we do this when a user calls eval(‘ 2+2’) (as does CPython) but for whatever reason the compiler doesn’t do this.

You’re wanting a bigger feature here that I’d personally rather see be a helper rather than built-in to the compiler.

From: jimmysch [mailto:notifications@codeplex.com]
Sent: Tuesday, April 27, 2010 3:10 PM
To: Dino Viehland
Subject: Re: Where is the error ?? [dlr:210752]

From: jimmysch

>>> Oops, it seems that the relaxation is not enough and the leading space will still cause a syntax error. Maybe we should consider allowing leading whitespace in this mode.

We discussed this once before, but never went anywhere with it. This would be useful for the DLR Silverlight integration, as it contains code to remove the whitespace margin created by using <script> tags on the HTML page.

Read the full discussion online.

To add a post to this discussion, reply to this email (dlr@discussions.codeplex.com)

To start a new discussion for this project, email dlr@discussions.codeplex.com

You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe or change your settings on codePlex.com.

Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at codeplex.com

Apr 27, 2010 at 10:58 PM

>>> You’re wanting a bigger feature here that I’d personally rather see be a helper rather than built-in to the compiler.

Yes, I’m asking for something more substantial, and we already have a helper: Microsoft.Scripting.Silverlight.DynamicScriptTags.RemoveMargin (though not optimized at all and needs to be rewritten … but it works =P). This is only needed for Python, so I feel like this helper should at least be part of Python, giving people a way of being sure they’re not messing up the code by “unindenting” it. But other white-space sensitive languages would need it too, so it could just be a helper in the DLR.

Also, not only do we need to remove margins for inline HTML script-tags, but we need to make sure the line numbers are accurate, so for each script-tag we prepend enough newlines to make sure the line numbers line up with the original HTML file. Not exactly the same problem, but definitely the same class of problems: right now we leave it entirely to the host to transform any inline code into something valid for the DLR. Regardless of being prone to issues, it’s hard to get right, and hosts will appreciate it (I’m sure jdeville could use it in his MSBuild host). I’m also not sure if prepending newlines is the best solution, but I was sure it would work. =)

Basically this all boils down to, “Are we OK with some hosts needing to manipulate the characters in script code before using the DLR to execute it?” If so, OK, and these things should be helpers in the DLR. If not, then it still doesn’t need to be part of the compiler, but there should be a SourceCodeKind which safely manipulates the whitespace of script code so the host doesn’t have to.

~Jimmy