Permalink
Browse files

threads.py module; DefinedFunction has a context; FunctionWrapper.Cre…

…ateThreadStart with tests
  • Loading branch information...
1 parent 6d84d93 commit 570f06bbe94a6958c55d16081fb0c18693aad61c @ajlopez committed Nov 17, 2012
@@ -0,0 +1,12 @@
+"Native threads module"
+
+from System.Threading import Thread
+from PythonSharp.Language import FunctionWrapper
+
+def start(function):
+ wrapper = FunctionWrapper(function, locals())
+ ts = wrapper.CreateThreadStart()
+ thread = Thread(ts)
+ thread.Start()
+ return thread
+
@@ -71,6 +71,9 @@
<Content Include="examples\httpserver.py">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
+ <None Include="Lib\threads.py">
+ <CopyToOutputDirectory>Always</CopyToOutputDirectory>
+ </None>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
@@ -80,7 +80,7 @@ public void EvaluateSimpleRangeWithStep()
public void EvaluateMethodCall()
{
DefinedClass klass = new DefinedClass("Spam");
- DefinedFunction function = new DefinedFunction("get", new Parameter[] { new Parameter("self", null, false), new Parameter("a", null, false) }, new ReturnCommand(new NameExpression("a")));
+ DefinedFunction function = new DefinedFunction("get", new Parameter[] { new Parameter("self", null, false), new Parameter("a", null, false) }, new ReturnCommand(new NameExpression("a")), null);
klass.SetMethod(function.Name, function);
DynamicObject foo = (DynamicObject)klass.Apply(this.machine.Environment, null, null);
this.machine.Environment.SetValue("foo", foo);
@@ -209,6 +209,43 @@ public void ExecuteSimpleSetDictionaryKey()
Assert.AreEqual("bar", this.Evaluate("a['foo']"));
}
+ [TestMethod]
+ [DeploymentItem("Examples/defif.py")]
+ public void ImportDefIfAndEvaluatePositive()
+ {
+ this.Execute("import defif");
+ Assert.AreEqual(true, this.Evaluate("defif.positive(1)"));
+ Assert.AreEqual(false, this.Evaluate("defif.positive(-12)"));
+ }
+
+ [TestMethod]
+ public void ImportFunctionWrapperFromPythonSharp()
+ {
+ this.Execute("from PythonSharp.Language import FunctionWrapper");
+ Assert.IsNotNull(this.Evaluate("FunctionWrapper"));
+ }
+
+ [TestMethod]
+ public void ImportFunctionWrapper()
+ {
+ this.Execute("import PythonSharp.Language");
+ Assert.IsNotNull(this.Evaluate("PythonSharp.Language.FunctionWrapper"));
+ }
+
+ [TestMethod]
+ public void CreateFunctionWrapper()
+ {
+ this.Execute("import PythonSharp.Language");
+ Assert.IsNotNull(this.Evaluate("PythonSharp.Language.FunctionWrapper(None, None)"));
+ }
+
+ [TestMethod]
+ public void CreateFileInfoAndEvaluateExists()
+ {
+ this.Execute("import System.IO");
+ Assert.AreEqual(false, this.Evaluate("System.IO.FileInfo('unknown.py').Exists()"));
+ }
+
private string ExecuteAndPrint(string text)
{
this.machine.Output = new StringWriter();
@@ -184,7 +184,7 @@ public void CreateInstanceWithConstructor()
{
DefinedClass klass = new DefinedClass("Spam");
ICommand body = new SetAttributeCommand(new NameExpression("self"), "name", new NameExpression("name"));
- DefinedFunction constructor = new DefinedFunction("__init__", new Parameter[] { new Parameter("self", null, false), new Parameter("name", null, false) }, body);
+ DefinedFunction constructor = new DefinedFunction("__init__", new Parameter[] { new Parameter("self", null, false), new Parameter("name", null, false) }, body, null);
klass.SetMethod(constructor.Name, constructor);
var result = klass.Apply(null, new object[] { "Adam" }, null);
@@ -21,7 +21,7 @@ public void CreateSimpleDefinedFunction()
{
IList<Parameter> parameters = new Parameter[] { new Parameter("a", null, false), new Parameter("b", null, false) };
ICommand body = new SetCommand("c", new NameExpression("a"));
- DefinedFunction func = new DefinedFunction("foo", parameters, body);
+ DefinedFunction func = new DefinedFunction("foo", parameters, body, null);
Assert.AreEqual(parameters, func.Parameters);
Assert.AreEqual(body, func.Body);
@@ -39,7 +39,7 @@ public void ExecuteFunctionWithPrint()
StringWriter writer = new StringWriter();
machine.Output = writer;
- DefinedFunction func = new DefinedFunction("foo", parameters, body);
+ DefinedFunction func = new DefinedFunction("foo", parameters, body, machine.Environment);
func.Apply(machine.Environment, new object[] { 1, 2 }, null);
Assert.AreEqual("1\r\n2\r\n", writer.ToString());
@@ -56,7 +56,7 @@ public void ExecuteFunctionWithReturn()
StringWriter writer = new StringWriter();
machine.Output = writer;
- DefinedFunction func = new DefinedFunction("foo", parameters, body);
+ DefinedFunction func = new DefinedFunction("foo", parameters, body, null);
var result = func.Apply(machine.Environment, new object[] { 1, 2 }, null);
@@ -74,7 +74,7 @@ public void RaiseWhenFewParametersProvided()
StringWriter writer = new StringWriter();
machine.Output = writer;
- DefinedFunction func = new DefinedFunction("foo", parameters, body);
+ DefinedFunction func = new DefinedFunction("foo", parameters, body, null);
try
{
@@ -98,7 +98,7 @@ public void RaiseWhenFewerThanExpectedParametersProvided()
StringWriter writer = new StringWriter();
machine.Output = writer;
- DefinedFunction func = new DefinedFunction("foo", parameters, body);
+ DefinedFunction func = new DefinedFunction("foo", parameters, body, null);
try
{
@@ -120,7 +120,7 @@ public void RaiseWhenOneParameterExpectedAndNoneIsProvided()
Machine machine = new Machine();
- DefinedFunction func = new DefinedFunction("foo", parameters, body);
+ DefinedFunction func = new DefinedFunction("foo", parameters, body, null);
try
{
@@ -140,7 +140,7 @@ public void EvaluateUsingDefaultValuesForArguments()
IList<Parameter> parameters = new Parameter[] { new Parameter("a", 1, false), new Parameter("b", 2, false) };
ICommand body = new ReturnCommand(new BinaryOperatorExpression(new NameExpression("a"), new NameExpression("b"), BinaryOperator.Add));
- DefinedFunction func = new DefinedFunction("foo", parameters, body);
+ DefinedFunction func = new DefinedFunction("foo", parameters, body, null);
Assert.AreEqual(3, func.Apply(new BindingEnvironment(), null, null));
}
@@ -151,7 +151,7 @@ public void EvaluateUsingListArgument()
IList<Parameter> parameters = new Parameter[] { new Parameter("a", null, false), new Parameter("b", null, true) };
ICommand body = new ReturnCommand(new NameExpression("b"));
- DefinedFunction func = new DefinedFunction("foo", parameters, body);
+ DefinedFunction func = new DefinedFunction("foo", parameters, body, null);
var result = func.Apply(new BindingEnvironment(), new object[] { 1, 2, 3 }, null);
@@ -171,7 +171,7 @@ public void EvaluateUsingEmptyListArgument()
IList<Parameter> parameters = new Parameter[] { new Parameter("a", null, false), new Parameter("b", null, true) };
ICommand body = new ReturnCommand(new NameExpression("b"));
- DefinedFunction func = new DefinedFunction("foo", parameters, body);
+ DefinedFunction func = new DefinedFunction("foo", parameters, body, null);
var result = func.Apply(new BindingEnvironment(), new object[] { 1 }, null);
@@ -189,7 +189,7 @@ public void EvaluateUsingEmptyListArgumentWithDefaultValue()
IList<Parameter> parameters = new Parameter[] { new Parameter("a", null, false), new Parameter("b", new object[] { 1, 2 }, true) };
ICommand body = new ReturnCommand(new NameExpression("b"));
- DefinedFunction func = new DefinedFunction("foo", parameters, body);
+ DefinedFunction func = new DefinedFunction("foo", parameters, body, null);
var result = func.Apply(new BindingEnvironment(), new object[] { 1 }, null);
@@ -209,7 +209,7 @@ public void EvaluateUsingNamedArgument()
IList<Parameter> parameters = new Parameter[] { new Parameter("a", 1, false), new Parameter("b", 2, false) };
ICommand body = new ReturnCommand(new BinaryOperatorExpression(new NameExpression("a"), new NameExpression("b"), BinaryOperator.Add));
- DefinedFunction func = new DefinedFunction("foo", parameters, body);
+ DefinedFunction func = new DefinedFunction("foo", parameters, body, null);
var result = func.Apply(new BindingEnvironment(), null, new Dictionary<string, object> { { "a", 2 } });
@@ -223,7 +223,7 @@ public void EvaluateUsingTwoNamedArguments()
IList<Parameter> parameters = new Parameter[] { new Parameter("a", 1, false), new Parameter("b", 2, false) };
ICommand body = new ReturnCommand(new BinaryOperatorExpression(new NameExpression("a"), new NameExpression("b"), BinaryOperator.Add));
- DefinedFunction func = new DefinedFunction("foo", parameters, body);
+ DefinedFunction func = new DefinedFunction("foo", parameters, body, null);
var result = func.Apply(new BindingEnvironment(), null, new Dictionary<string, object> { { "a", 2 }, { "b", 3 } });
@@ -237,7 +237,7 @@ public void RaiseWhenMultipleValuesForKeywordArgument()
IList<Parameter> parameters = new Parameter[] { new Parameter("a", 1, false), new Parameter("b", 2, false) };
ICommand body = new ReturnCommand(new BinaryOperatorExpression(new NameExpression("a"), new NameExpression("b"), BinaryOperator.Add));
- DefinedFunction func = new DefinedFunction("foo", parameters, body);
+ DefinedFunction func = new DefinedFunction("foo", parameters, body, null);
try
{
@@ -256,7 +256,7 @@ public void ValuesInFunction()
{
ICommand body = new PassCommand();
- DefinedFunction func = new DefinedFunction("foo", null, body);
+ DefinedFunction func = new DefinedFunction("foo", null, body, null);
func.SetValue("__doc__", "foo function");
@@ -270,7 +270,7 @@ public void GetAndUseDelegate()
{
ICommand body = new PassCommand();
- DefinedFunction func = new DefinedFunction("foo", null, body);
+ DefinedFunction func = new DefinedFunction("foo", null, body, null);
var type = typeof(ThreadStart);
//Activator.CreateInstance(typeof(ThreadStart), func.DoFunction);
}
@@ -6,9 +6,12 @@
using System.Reflection;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;
+ using PythonSharp.Commands;
+ using PythonSharp.Expressions;
using PythonSharp.Language;
using PythonSharp.Tests.Classes;
using PythonSharp.Utilities;
+ using System.Threading;
[TestClass]
public class FunctionWrapperTests
@@ -86,6 +89,29 @@ public void AddFunctionAsPersonEventHandler()
Assert.AreEqual("Adam", listener.Name);
}
+ [TestMethod]
+ public void CreateThreadStart()
+ {
+ BindingEnvironment environment = new BindingEnvironment();
+ Runner function = new Runner();
+ FunctionWrapper wrapper = new FunctionWrapper(function, environment);
+ Thread th = new Thread(wrapper.CreateThreadStart());
+ th.Start();
+ th.Join();
+ Assert.IsTrue(function.WasInvoked);
+ }
+
+ internal class Runner : IFunction
+ {
+ public bool WasInvoked { get; set; }
+
+ public object Apply(IContext context, IList<object> arguments, IDictionary<string, object> namedArguments)
+ {
+ this.WasInvoked = true;
+ return null;
+ }
+ }
+
internal class Adder : IFunction
{
public object Apply(IContext context, IList<object> arguments, IDictionary<string, object> namedArguments)
@@ -34,5 +34,18 @@ public void CreateModuleWithGlobalContext()
Assert.IsNotNull(module.GlobalContext);
Assert.AreEqual(global, module.GlobalContext);
}
+
+ [TestMethod]
+ public void GetValueFromGlobalContext()
+ {
+ BindingEnvironment global = new BindingEnvironment();
+ global.SetValue("one", 1);
+ Module module = new Module(global);
+
+ var result = module.GetValue("one");
+ Assert.IsNotNull(result);
+ Assert.AreEqual(1, result);
+ Assert.IsTrue(module.HasValue("one"));
+ }
}
}
@@ -19,7 +19,7 @@ public void TypeName()
Assert.AreEqual("int", Types.GetTypeName(123));
Assert.AreEqual("str", Types.GetTypeName("spam"));
Assert.AreEqual("float", Types.GetTypeName(1.2));
- Assert.AreEqual("function", Types.GetTypeName(new DefinedFunction("spam", null, null)));
+ Assert.AreEqual("function", Types.GetTypeName(new DefinedFunction("spam", null, null, null)));
Assert.AreEqual("list", Types.GetTypeName(new object[] { 1, 2 }));
Assert.AreEqual("TypeError", Types.GetTypeName(new TypeError(string.Empty)));
}
@@ -90,7 +90,7 @@ public void GetIndexedValuesFromDynamicObject()
{
DynamicObject obj = new DynamicObject(null);
obj.SetValue("name", "Adam");
- obj.SetValue("get_age", new DefinedFunction("get_age", null, null));
+ obj.SetValue("get_age", new DefinedFunction("get_age", null, null, null));
Assert.AreEqual("Adam", ObjectUtilities.GetIndexedValue(obj, new object[] { "name" }));
@@ -106,7 +106,7 @@ public void ClassAsString()
[TestMethod]
public void FunctionAsString()
{
- DefinedFunction function = new DefinedFunction("foo", null, null);
+ DefinedFunction function = new DefinedFunction("foo", null, null, null);
Assert.AreEqual("<function foo>", ValueUtilities.AsString(function));
Assert.AreEqual("<function foo>", ValueUtilities.AsPrintString(function));
}
@@ -52,7 +52,7 @@ public void Execute(IContext context)
parameters.Add((Parameter)parexpr.Evaluate(context));
}
- DefinedFunction function = new DefinedFunction(this.name, parameters, this.body);
+ DefinedFunction function = new DefinedFunction(this.name, parameters, this.body, context);
function.SetValue("__doc__", this.doc);
@@ -26,19 +26,24 @@ public AttributeExpression(IExpression expression, string name)
public object Evaluate(IContext context)
{
var result = this.expression.Evaluate(context);
- IValues values = result as IValues;
+ return this.GetValue(result);
+ }
+
+ public object GetValue(object obj)
+ {
+ IValues values = obj as IValues;
if (values == null)
{
- IType type = Types.GetType(result);
+ IType type = Types.GetType(obj);
if (type != null)
return type.GetMethod(this.name);
- if (result is Type)
- return TypeUtilities.GetValue((Type)result, this.name);
+ if (obj is Type)
+ return TypeUtilities.GetValue((Type)obj, this.name);
- return ObjectUtilities.GetValue(result, this.name);
+ return ObjectUtilities.GetValue(obj, this.name);
}
object value = values.GetValue(this.name);
Oops, something went wrong.

0 comments on commit 570f06b

Please sign in to comment.