Ironpython 2.0 Beta 5

Source: Internet
Author: User

Ironpython 2.0 Beta 5 has been released: http://www.codeplex.com/IronPython/Release/ProjectReleases.aspx? Releaseid = 15625. Ironpython 2.0 Beta 5 is the last beta version of the 2.0 series. The next version is the RC version. That is to say, all the APIs in the next version will be solidified. Now it's time to learn ironpython 2. From the release of beta 4, the MSI Installation File is available, andPython standard libraryThe standard library license is released using the python Software Foundation license, which means it is easy to shift the value of cpython application.

It is worth noting that the stable version of the DLR hosting spec has been introduced and you can download the word version http://compilerlab.members.winisp.net/dlr-spec-hosting.doc for the DLR hosting spec from here

Another major change in this version is that the namespace has made a major change, moving all DLR types from system to Microsoft, the reason is that many people in http://lists.ironpython.com/pipermail/users-ironpython.com/2008-August/thread.html#8036 embed ironpython into C #(.. Net 3.5) project.

Another point is that the DLR host API does not have a default configuration for python. Now we should use ironpython. Hosting. Python to create a script engine, which makes it easier to host ironpython in applications. Ironpython. Hosting. Python has several helper methods to create a scriptruntime or scriptengine, and adds some Python-specific extension methods for scriptruntime and scriptengine.

Using ironpython. Hosting;

Scriptengine engine = python. createengine ();

Scriptscope sys = engine. getsysmodule ();
VaR platform = SYS. getvariable ("Platform ");
Console. writeline (Platform );

Scriptscope builtins = engine. getbuiltinmodule ();
VaR POW = builtins. getvariable <func <Double, double, double> ("pow ");
Console. writeline (POW (2, 3 ));

Scriptscope CLR = engine. getclrmodule ();
VaR getpythontype = CLR. getvariable <func <type, pythontype> ("getpythontype ");
Console. writeline (pythontype. Get _ name _ (getpythontype (typeof (string ))));

Reference: http://blogs.msdn.com/srivatsn/archive/2008/09/16/hosting-ironpython-made-easier.aspx

How to call dynamic languages in managed languages.

First, we need to initialize the environment configuration of the Dynamic Language, obtain the list of all available dynamic languages, and then obtain the running engine of the corresponding dynamic language.

Scriptruntimesetup setup = new scriptruntimesetup (true); // true indicates loading environment configurations for all supported dynamic languages
Scriptruntime runtime = scriptruntime. Create (Setup); // create a dynamic Language Runtime Environment

Foreach (languageprovidersetup langsetup in setup. languageproviders) // traverses the environment configuration of all dynamic languages
{
Try
{
Scriptengine engine = NULL;
If (runtime. trygetengine (langsetup. Names [0], out engine) // try to obtain the Dynamic Language running engine
{
// Engine is the engine we need
}
}
Catch (missingtypeexception) // an exception may be thrown when an unsupported Dynamic Language is created.
{
}
}

After obtaining the running engine, we can execute the dynamic language code.

Public class myerrorsink: errorsink // compile error handling
{
Ilist <string> m_errormsg = new list <string> ();
Public ilist <string> errormsg
{
Get {return m_errormsg ;}
}

Public myerrorsink ()
{
}
Public Virtual void add (sourceunit source, string message, sourcespan span, int errorcode, severity)
{
If (severity = severity. Error | severity = severity. fatalError)
{
M_errormsg.add (Message );
}
}
}

Languagecontext langcontext = hostinghelpers. getlanguagecontext (engine );
Sourceunit = langcontext. createsourceunit (New sourcestringcontentprovider ("1/4 + 3"), null, sourcecodekind. expression); // create a code sequence: 1/4 + 3

Myerrorsink errorsink = new myerrorsink ();
Try
{
Scope scope = new scope ();
Object ret = sourceunit. Execute (scope, errorsink); // executes the dynamic language code. RET is the return value of the execution result.

If (errorsink. errormsg. Count> 0) // check the compilation Error
{
//
}
}
Catch (exception)
{
}

Reference: http://blogs.msdn.com/silverlightshanghai/archive/2008/09/18/dlr-in-silverlight.aspx

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.