Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support conversion of JS Boolean variables to Python bool #22

Open
mattaustin opened this issue Jun 2, 2014 · 6 comments
Open

Support conversion of JS Boolean variables to Python bool #22

mattaustin opened this issue Jun 2, 2014 · 6 comments

Comments

@mattaustin
Copy link

I have a problem where a JavaScript variable with a boolean value is not being passed to the Python function correctly, and I end up with an empty Python dict.

qmlexperiment.py contains:

def pyfoo(bar):
    print('python - bar is: {0}'.format(bar))

qml contains:

import QtQuick 2.0
import io.thp.pyotherside 1.2

Python {

    onError: {
        console.log('python error: ' + traceback);
    }

    function foo() {
      importModule('qmlexperiment', function() {
            var bar = new Boolean(true);
            print('qml - bar is: ' + bar);
            call('qmlexperiment.pyfoo', [bar], function(result) {});
        });
    }

}

output:

qml - bar is: true
python - bar is: {}

Am I doing something wrong, or should the function be being passed a Python boolean?

If bar is replaced with true in the call args, then the Python function is correctly passed in 'True':

qml - bar is: true
python - bar is: True
@zyga
Copy link

zyga commented Jun 2, 2014

The translator doesn't handle Boolean objects, just the true/false constants.

BTW: why would you use Boolean(true)?

@mattaustin
Copy link
Author

Just simplifying with Boolean(true) for the example (my actual code is generating the boolean from another var, and wanted to make sure I had a pure boolean object for the translator). The docs say the data type mapping should work both ways: http://pyotherside.readthedocs.org/en/latest/#data-type-mapping

@zyga
Copy link

zyga commented Jun 2, 2014

It does but apparently the javascript engine doesn't treat new Boolean() as a boolean

@zyga
Copy link

zyga commented Jun 2, 2014

Specifically it seems that instead of QVariant with bool you get some other (I don't know javascript engine types to be sure) type.

@mattaustin
Copy link
Author

Indeed - thank you for your help!

I've replaced:

var bar = new Boolean(baz);

with:

var bar = baz ? true : false;

and all is well!

@thp
Copy link
Owner

thp commented Jun 2, 2014

While in general, you should use true and false, it might make sense that we also support Boolean objects and convert them to a boolean value on the Python side:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean

Reopening. Maybe we can get around to implement that at some point.

@thp thp reopened this Jun 2, 2014
@thp thp changed the title Problem passing javascript boolean to python function Support conversion of JS Boolean variables to Python bool Jun 2, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants