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

dict's pop() method fails to remove None-valued item #827

Open
phfaist opened this issue Sep 18, 2022 · 0 comments
Open

dict's pop() method fails to remove None-valued item #827

phfaist opened this issue Sep 18, 2022 · 0 comments

Comments

@phfaist
Copy link

phfaist commented Sep 18, 2022

There appears to be a bug in the runtime's implementation of the pop() method of dict objects. If the value in the dictionary associated with the given key happens to be None (null), then the value is not returned and it is not removed from the dictionary. I think the fix is very easy (see below).

Here's a minimal working example:

# bug.py
a = { 'hello': None }
value = a.pop('hello', '<DEFAULT>')
print('value = ', value, '; a = ', a)

The outputs I get with python, and with transcrypt/node, are:

# python bug.py
value =  None ; a =  {}

# transcrypt bug.py --nomin ; echo '{"type":"module"}' >__target__/package.json; node __target__/bug.js
value =  <DEFAULT> ; a =  {'hello': None}

It appears that the incorrect behavior is the result of a comparison of the value with undefined using the != operator in the runtime's implementation of __pop__(). Correct behavior is achieved if I modify the runtime's __pop__ function in __target__/org.transcrypt.__runtime__.js as follows:

function __pop__ (aKey, aDefault) {
    var result = this [aKey];
-    if (result != undefined) {
+    if (result !== undefined) {
        delete this [aKey];
        return result;
    } else {
        if ( aDefault === undefined ) {
            throw KeyError (aKey, new Error());
        }
    }
    return aDefault;
}

Then I obtain, correctly:

# node __target__/bug.js
value =  None ; a =  {}

From searching in transcrypt's code base, it would appear that the relevant line to update is this one.

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

1 participant