[Python] How can I make cPython and IronPython compatible script?

You know, like there is a tag in html5 that depending on the browser it executes different code to ensure compatibility.

<head>
  <!--[if lt IE 9]>
    <script src="/js/html5shiv.js"></script>
  <![endif]-->
</head>

Is there such check in cPython and IronPython?

Yes it’s in the platform module

1 Like

Thanks!

You’re welcome. Bear in mind that it’s generally recommended to test for features , not for version, in case you later switch interpreters/ Python version:

https://docs.python.org/2/howto/pyporting.html#use-feature-detection-instead-of-version-detection

Thanks.
In my case I have different behavior of IronPython than CPython. Such check was my first idea to solve issue. Unfortunately did’t help much. :frowning: .

Ahh - how do they differ?

I was trying to make a server-client app transfering sockets and I can pass strings from CPython but not from IronPython

zServer.py (915 Bytes)
zClient.py (576 Bytes)

You can see in there what kind of transformations I tried but didn’t work.

Ahh yes trying this in Ironpython I get an error `Message: failed to parse CPython sys.version: ‘2.7.0 ()’``

Also I think theres a problem with line 29 in zserver:

q = str("\"",input("Enter something to this client: "),"\"")

Did you mean:
q = "\""+str(input("Enter something to this client: "))+"\""
Which can be ‘simplified’ to
q = '"'+str(input("Enter something to this client: "))+'"'
to avoid escaping the "

Or something like this:

try:
    import rhinoscriptsyntax as rs
    pyimpl = 'IronPython'
except ImportError:
    pyimpl = 'CPython'

print pyimpl

:smiley: It was already 4a.m. this is just a build up from str(input()) , an evolution. I didn’t clean the code. I was gonna do it after I make it work on IronPython properly.

Ahh got you :slight_smile: Looks like you also need to work on the socket calls as there seem to have been changes to how they work from 2.7 through to modern Python.

What I don’t understand is why does it keep treating the text that’s entered as non string whereas the input() is inside a str(). Is this an issue with IronPython? It works properly with CPython.

Next, I wish to try on IronPython side to use .net libraries instead of Python modules. Exactly with try: except: as you suggested. That would require me translating csharp to python again :face_vomiting:

[quote=“ivelin.peychev, post:13, topic:79267, full:true”]
What I don’t understand is why does it keep treating the text that’s entered as non string whereas the input() is inside a str(). Is this an issue with IronPython? It works properly with CPython.
/quote]

In Cpython you are executing line 27, transforming the result of input() into a string. This is a null operation as input() already returns a string.
In Ironpython you are executing line 29, trying to run the str() function with 3 parameters separated by commas, but in python 2, the str function only takes a single parameter, so it wont like this.

yeah, well using only the input() didn’t work either

Hmmm.

I made it work after changing input() to raw_input() for CPython2 and IronPython :slight_smile: :partying_face:

1 Like