Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Fix #71: make cloudpickle Python 3.6 compatible
Also add a cache to the function which extracts global-referencing opcodes,
speeding up repetitive pickling of non-tiny functions.
  • Loading branch information
pitrou committed Nov 25, 2016
commit 4945361c2db92095f934b92a6c00316243caf3cc
91 changes: 57 additions & 34 deletions cloudpickle/cloudpickle.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,19 @@
"""
from __future__ import print_function

import operator
import io
import dis
from functools import partial
import imp
import io
import itertools
import opcode
import operator
import pickle
import struct
import sys
import types
from functools import partial
import itertools
import dis
import traceback
import types
import weakref

if sys.version < '3':
from pickle import Pickler
Expand All @@ -68,10 +70,10 @@
PY3 = True

#relevant opcodes
STORE_GLOBAL = dis.opname.index('STORE_GLOBAL')
DELETE_GLOBAL = dis.opname.index('DELETE_GLOBAL')
LOAD_GLOBAL = dis.opname.index('LOAD_GLOBAL')
GLOBAL_OPS = [STORE_GLOBAL, DELETE_GLOBAL, LOAD_GLOBAL]
STORE_GLOBAL = opcode.opmap['STORE_GLOBAL']
DELETE_GLOBAL = opcode.opmap['DELETE_GLOBAL']
LOAD_GLOBAL = opcode.opmap['LOAD_GLOBAL']
GLOBAL_OPS = (STORE_GLOBAL, DELETE_GLOBAL, LOAD_GLOBAL)
HAVE_ARGUMENT = dis.HAVE_ARGUMENT
EXTENDED_ARG = dis.EXTENDED_ARG

Expand All @@ -90,6 +92,46 @@ def _builtin_type(name):
return getattr(types, name)


if sys.version_info < (3, 4):
def _walk_global_ops_real(code):
code = getattr(code, 'co_code', b'')
if not PY3:
code = map(ord, code)

n = len(code)
i = 0
extended_arg = 0
while i < n:
op = code[i]
i += 1
if op >= HAVE_ARGUMENT:
oparg = code[i] + code[i + 1] * 256 + extended_arg
extended_arg = 0
i += 2
if op == EXTENDED_ARG:
extended_arg = oparg * 65536
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you think of a good test that would cover this branch?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not really. You would need a function using a huge number of constants.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, we can generate that function.

if op in GLOBAL_OPS:
yield op, oparg

else:
def _walk_global_ops_real(code):
for instr in dis.get_instructions(code):
op = instr.opcode
if op in GLOBAL_OPS:
yield op, instr.arg


def _walk_global_ops(code, _cache=weakref.WeakKeyDictionary()):
"""
Return a list of (opcode, argument number) tuples for all
global-referencing instructions in *code*.
"""
res = _cache.get(code)
if res is None:
res = _cache[code] = list(_walk_global_ops_real(code))
return res


class CloudPickler(Pickler):

dispatch = Pickler.dispatch.copy()
Expand Down Expand Up @@ -281,41 +323,22 @@ def save_function_tuple(self, func):
write(pickle.TUPLE)
write(pickle.REDUCE) # applies _fill_function on the tuple

@staticmethod
def extract_code_globals(co):
@classmethod
def extract_code_globals(cls, co):
"""
Find all globals names read or written to by codeblock co
"""

code = getattr(co, 'co_code', None)
if code is None:
return set()
if not PY3:
code = [ord(c) for c in code]
names = co.co_names
out_names = set()

n = len(code)
i = 0
extended_arg = 0
while i < n:
op = code[i]

i += 1
if op >= HAVE_ARGUMENT:
oparg = code[i] + code[i+1] * 256 + extended_arg
extended_arg = 0
i += 2
if op == EXTENDED_ARG:
extended_arg = oparg*65536
if op in GLOBAL_OPS:
out_names.add(names[oparg])
for op, oparg in _walk_global_ops(co):
out_names.add(names[oparg])

# see if nested function have any global refs
if co.co_consts:
for const in co.co_consts:
if type(const) is types.CodeType:
out_names |= CloudPickler.extract_code_globals(const)
out_names |= cls.extract_code_globals(const)

return out_names

Expand Down