Author terry.reedy
Recipients kbk, pitrou, ppperry, roger.serwy, terry.reedy
Date 2014-07-16.05:42:23
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <>
Code is really a code object, so the compile does not seem to be the problem. In 2.7, on Win7, I get exactly the same output after removing the possibly spurious space in the string posted.  (ppperry), what system (OS) are you using. (In the future, please report both system and Python version).

3.4 gives a mild variation

I ran both test_pickle and test_pickletools from both the command line 
>python -m test -v test_pickletools
and from an Idle editor with F5, with no errors and apparently identical output.

import pickle
code = compile("dummy_code", "this is a file name", "exec")
print('pickle: ', pickle.dumps(code))
>>> produces
<class 'code'>
pickle:  b'\x80\x03cidlelib.rpc ... \x13this is a file name\...'

which shows that the bytes come from picke, not from a garbled traceback.

Since the bytes look like an actual pickle, I tried unpickling and it works:

import pickle
code = compile("'a string'", "", "eval")
pick = pickle.dumps(code)
code2 = pickle.loads(pick)
print(eval(code), eval(code2))
a string a string

import pickle
code1 = compile("print('a string')", "", "exec")
pick = pickle.dumps(code1)
code2 = pickle.loads(pick)
a string
a string

I do not see any Idle bug here, so this might be closed unless you want to make this a pickle enhancement issue to work on code objects in the standard interpreter. The 3.4 interpreter traceback gives more info on why the pickle fails there.

>>> code0 = compile("'abc'", '', 'eval')
>>> pick = pickle.dumps(code0)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
_pickle.PicklingError: Can't pickle <class 'code'>: attribute lookup code on builtins failed

In Idle, __builtins__.code fails, just as in the interpreter, so pickle is doing something different, and more successful, in the slightly altered Idle user-process execution environment.
Date User Action Args
2014-07-16 05:42:25terry.reedysetrecipients: + terry.reedy, kbk, pitrou, roger.serwy, ppperry
2014-07-16 05:42:24terry.reedysetmessageid: <>
2014-07-16 05:42:24terry.reedylinkissue21986 messages
2014-07-16 05:42:23terry.reedycreate