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

Error with join method #51

Closed
ed2050 opened this issue Dec 24, 2020 · 7 comments
Closed

Error with join method #51

ed2050 opened this issue Dec 24, 2020 · 7 comments

Comments

@ed2050
Copy link

ed2050 commented Dec 24, 2020

Found what seems to be a pscript bug. My code has the following convenience function:

def css (rules, important = false) :
    'turn a dict of { property : value } pairs into css styles'
    decs = []
    imp = important and '!important' or ''
    for k,v in rules.items () :
        decs += '%s: %s %s; ' % (k, v, imp)
    return ''.join (decs)

Running this code gives the following error:

myapp.js:199 Uncaught TypeError: x.join is not a function
    at String._pymeth_join (myapp.js:199)
    at flx_css (myapp.js:369)

Which traces to this bit of transpiled code in myapp.js (line nums shown):

197: var _pymeth_join = function (x) { // nargs: 1
198:     if (this.constructor !== String) return this.join.apply(this, arguments);
199:     return x.join(this);  // call join on the list instead of the string.
200: };
...
369:     return _pymeth_join.call("", decs);

Not sure what the problem is. What I do notice:

  1. My code calls join on a string with a list as argument.
  2. The pymeth_join code seems to do the same. Line 369 would indicate "this" is a string and x is a list (array) when pymeth_join is called.
  3. Line 199 should thus be calling array.join (string). Which is valid js and should work. Not sure why the error says "x.join is not a function".

My python code is valid and works in cpython. It should work in pscript. Not sure why it doesn't.

I did find a workaround for pscript. Changing the inner loop from += to append fixes the problem with pscript. Which is better practice anyway, since list += string adds each character as a separate list item (this is very old code, prob >10 years). So I did "solve" it. But the original code should still work, even if suboptimal. Seems like a bug in pscript.

@almarklein
Copy link
Member

I think you meant: decs += -> decs.append(...)

@almarklein
Copy link
Member

To expand a bit: in JS you can add a list and a string (sigh) and the result will be a string. The _pymeth_join comes from PScript, and it performs the join using the JS Array.join, but this fails because the argument is not an array, but a string.

@ed2050
Copy link
Author

ed2050 commented Jan 2, 2021

Yes that code would have been better. Both work in python though.

Thanks for the explanation. It still seems like an issue with pscript to me. Regardless of funky js conversions, the behavior is different from python and will surprise people.

How about mentioning it in the docs under Caveats? To avoid adding list + str because it =str in pscript instead of =list in python.
Thanks for your help.

@almarklein
Copy link
Member

Well in Python you cannot add a list or a string: you'd get error. So it's not surprising that it actually works, which I think is the JavaScript shining through section.

@ed2050
Copy link
Author

ed2050 commented Jan 2, 2021 via email

@almarklein
Copy link
Member

I think I was not really aware that you could do some_list += some_iterable ! I'm not sure whether I like this feature much though :P the asymmetry with + is indeed surprising.

In that case, yes, we should document this.

almarklein added a commit that referenced this issue Jan 4, 2021
@almarklein almarklein mentioned this issue Jan 4, 2021
almarklein added a commit that referenced this issue Jan 4, 2021
@almarklein
Copy link
Member

done

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants