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

jsonpickle.decode exhibits different behavior in AREPL than in Pycharm #10

Closed
Almenon opened this issue Aug 14, 2017 · 4 comments
Closed
Assignees
Labels

Comments

@Almenon
Copy link
Owner

Almenon commented Aug 14, 2017

In AREPL json.decode fails to decode a simple pickled enum. In Pycharm the decoding happens without any error. The reason behind this is that AREPL uses exec to execute the user's code. In the example below you can see the exact same code that works outside of exec() fails inside exec().

from enum import Enum
from jsonpickle import encode, decode


class C(Enum):
    VALUE = 1

my_list_encode = encode(C.VALUE)
decode(my_list_encode) # no error

execCode = """ # exact same code as above follows!
from enum import Enum
from jsonpickle import encode, decode


class C(Enum):
    VALUE = 1

my_list_encode = encode(C.VALUE)
decode(my_list_encode)
"""

execLocals = {}
exec(execCode,execLocals) # error!

Traceback (most recent call last):
File "C:/dev/random python scripts/misc.py", line 24, in
exec(execCode,execLocals)
File "", line 10, in
File "C:\dev\AREPL\src\python\jsonpickle_init_.py", line 152, in decode
return unpickler.decode(string, backend=backend, keys=keys)
File "C:\dev\AREPL\src\python\jsonpickle\unpickler.py", line 27, in decode
return context.restore(backend.decode(string), reset=reset)
File "C:\dev\AREPL\src\python\jsonpickle\unpickler.py", line 120, in restore
value = self._restore(obj)
File "C:\dev\AREPL\src\python\jsonpickle\unpickler.py", line 162, in _restore
return restore(obj)
File "C:\dev\AREPL\src\python\jsonpickle\unpickler.py", line 183, in _restore_reduce
if f == tags.NEWOBJ or f.__name__ == '__newobj__':
AttributeError: 'dict' object has no attribute '__name__'
"""

@Almenon Almenon added the bug label Aug 14, 2017
@Almenon Almenon self-assigned this Aug 14, 2017
@Almenon
Copy link
Owner Author

Almenon commented Aug 14, 2017

the problem is fixed when you replace execLocals = {} with execLocals = locals(). It must be something missing in locals that causes the error.

@Almenon
Copy link
Owner Author

Almenon commented Aug 14, 2017

Found it! execLocals was missing the __name__ property. Inserting the following code after execLocals fixes the issue:

execLocals['__doc__'] = locals()['__doc__']
execLocals['__file__'] = locals()['__file__']
execLocals['__loader__'] = locals()['__loader__']
execLocals['__name__'] = locals()['__name__']
execLocals['__package__'] = locals()['__package__']
execLocals['__spec__'] = locals()['__spec__']

@Almenon Almenon closed this as completed Aug 14, 2017
@Almenon Almenon added this to the v1.0.0-alpha.2 milestone Aug 14, 2017
@Almenon Almenon reopened this Aug 14, 2017
@Almenon Almenon removed this from the v1.0.0-alpha.2 milestone Aug 14, 2017
@Almenon
Copy link
Owner Author

Almenon commented Aug 14, 2017

When I tried applying the fix to the pythonEvaluator the fix did not work. I reinvestigated and found out that when you remove the block of code in the beginning:

from enum import Enum
from jsonpickle import encode, decode

class C(Enum):
    VALUE = 1

my_list_encode = encode(C.VALUE)
decode(my_list_encode) # no error

then the fix does not work anymore! I have no idea why the lack of this code would cause an error in execd code - they don't share the same locals. But I have already spent too much time on this - I decided to just remove the v1.0.0-alpha.2 milestone and commit what I had so far. Adding the special vars (__doc__, etc...) is a good idea that should've been done already, even if it didn't fix this particular problem.

Also, I found out that the code block above does not work in the console. This means something funky is going on in jsonpickle - possibly if I raise an issue with them the fix would also fix the error in my case.

I'll look into this issue again if I have time to investigate it further or someone comments. For now I have more important stuff to do.

@Almenon
Copy link
Owner Author

Almenon commented Dec 24, 2017

I moved this over to AREPL-backend

@Almenon Almenon closed this as completed Dec 24, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant