Comments (6)
Here is a an approach where there is apparently no conversion malfunction between C# and IronPython for C# bool variable type.
It will be interesting if you can share the differences between the two approaches.
Filename: test_ipy1.py
def test_func1(string_list, var_bool):
if var_bool is True:
return string_list[0][0] + ";" + string_list[0][1]
elif var_bool is False:
return string_list[1][0] + "!" + string_list[1][1]
else:
return None
if __name__ == '__main__':
def main():
string_list = [["string1", "string2"], ["string3", "string4"]]
print test_func1(string_list, False)
main()
The C# code accessing the above mentioned Python code is as follows:
using System;
using IronPython.Hosting;
using IronPython.Runtime;
using Microsoft.Scripting.Hosting;
using System.Collections.Generic;
namespace TestAppIPy1
{
class Program
{
static void Main(string[] args)
{
List<string> sublist1 = new List<string>();
sublist1.Add("string1");
sublist1.Add("string2");
List<string> sublist2 = new List<string>();
sublist2.Add("string3");
sublist2.Add("string4");
List<List<string>> list1 = new List<List<string>>();
list1.Add(sublist1);
list1.Add(sublist2);
ScriptRuntime runtime = Python.CreateRuntime();
ScriptEngine engine = runtime.GetEngine("IronPython");
var paths = engine.GetSearchPaths();
var ironpythonpath = Environment.GetEnvironmentVariable("IRONPYTHONPATH", EnvironmentVariableTarget.Process);
var path_elements = ironpythonpath.Split(';');
foreach (string path_element in path_elements)
{
paths.Add(path_element);
}
paths.Add(Environment.CurrentDirectory);
engine.SetSearchPaths(paths);
dynamic scope = runtime.UseFile("test_ipy1.py");
bool var_bool = false;
var str1 = scope.test_func1(list1, var_bool);
Console.WriteLine(str1);
}
}
}
The environment variable ironpythonpath is defined as follows:
SET IRONPYTHONPATH=C:\Program Files\IronPython 2.7\Lib;C:\Program Files\IronPython 2.7\DLLs;C:\Progam Files\IronPython 2.7;C:\Program Files\IronPython 2.7\lib\site-packages
from main.
Your approach is calling a method in script. Whereas I am executing the script in a scope.
When I execute script I set variables with their vales in scope and then just execute it.
The reason I could think of is if ip_bool is set to None in the context of script.
ip_bool = None
print(ip_bool is not True)
print(ip_bool is not False)
Value of None in python is neither True nor False.
But that is not the case also. If it is None then the __class__.__name__
should have been NoneType
Contrary in this case ip_bool is recognized as bool
So it is malfunctioning.
from main.
is compares identity. None is not identical to True or False. So "ip_bool is not True" will evaluate to True and "ip_bool is not False" will evaluate to True. the standard Python function bool evaluates a Boolean expression.
[Reference: http://stackoverflow.com/questions/5119709/python-true-false]
So bool value conversion does not malfunction in IronPython.
I suggest that you close the issue.
from main.
If ip_bool is none then I would expect ip_bool.__class__.__name__
not to return the type as class :bool
from main.
This has to do with the fact that True/False are actually objects in IronPython, so when you do an is comparison of a variable with the object True, they are not the same object (when the variable is set from C#). When you set the value from Python, then the True object is assigned to the Python object and thus the instance comparison is True. I'm not sure there is a good fix for this.
from main.
In @bhadra's example, the call site binder likely converts C# System.Boolean
into the appropriate Python True/False object, while in @sriharshav's original example ScriptEngine.SetVariable
has no such conversion.
I see two solutions and a cop-out: adding code in SetVariable
to convert booleans on the way in (but should we do that to Long as well? Decimal? DateTime? Nullable types?), or adding a special case to is
handling for Boolean (and possible Nullable types/None, I don't know what it does).
The cop-out is to simply document it, and recommend against using is
checks on True/False, which is already strongly discouraged by PEP8 (search for "is greeting") anyway.
I don't like special cases in the other code paths so I think we'll leave it as-is.
from main.
Related Issues (20)
- Python 3 HOT 2
- test_math_cpy fails in 64-bit
- Upgrade pip to 9.0.1 HOT 3
- Links in you site HOT 1
- Drop support for Microsoft.Scripting.Core HOT 1
- Drop support for Silverlight HOT 1
- Switch to using IronLanguages/dlr HOT 1
- Move IronLanguages/main to IronLanguages/ironpython2? HOT 1
- Application does not run on mono 5 runtime. HOT 2
- Crash in WeakRefTracker finalizer with mono 5 HOT 1
- json module raise error when run with -X:Python30 argument. HOT 1
- traceback.format_exc() not working HOT 4
- Misleading ArgumentTypeException message when calling C# method HOT 6
- IronPython 2.7.7 is not compatible with dotnet core 1.1 HOT 1
- Crash when using unquote_plus HOT 1
- Strange behavior of symbolic group name on regular expression module. HOT 1
- PARSINGCONTEXT can be used as ASTCONTEXT in the Expression nodes of IRony.Interpreter??Can anyone suggest please? HOT 1
- Looking to save file into AWS S3 bucket using IronPython? HOT 1
- Does not build with current Mono HOT 1
- weakref.ref object sometimes returns None in a mulithreaded scenario HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from main.