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
Prev Previous commit
Next Next commit
Add a test for EXTENDED_ARG
  • Loading branch information
pitrou committed Nov 25, 2016
commit e26a76fda93315e6874dda3293dbb475698cd9c0
27 changes: 27 additions & 0 deletions tests/cloudpickle_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import pytest
import pickle
import sys
import random
import functools
import itertools
import platform
Expand Down Expand Up @@ -333,6 +334,32 @@ def g(y):
res = loop.run_sync(functools.partial(g2, 5))
self.assertEqual(res, 7)

def test_extended_arg(self):
Copy link
Member

Choose a reason for hiding this comment

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

Awesome!

# Functions with more than 65535 global vars prefix some global
# variable references with the EXTENDED_ARG opcode.
nvars = 65537 + 258
names = ['g%d' % i for i in range(1, nvars)]
r = random.Random(42)
d = dict((name, r.randrange(100)) for name in names)
# def f(x):
# x = g1, g2, ...
# return zlib.crc32(bytes(bytearray(x)))
code = """
import zlib

def f():
x = {tup}
return zlib.crc32(bytes(bytearray(x)))
""".format(tup=', '.join(names))
exec(textwrap.dedent(code), d)
f = d['f']
res = f()
data = cloudpickle.dumps([f, f])
d = f = None
f2, f3 = pickle.loads(data)
self.assertTrue(f2 is f3)
self.assertEqual(f2(), res)


if __name__ == '__main__':
unittest.main()