Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_labeling_function_serialize(self) -> None:
lf = NLPLabelingFunction(name="my_lf", f=has_person_mention, pre=[combine_text])
lf_load = dill.loads(dill.dumps(lf))
self._run_lf(lf_load)
transitions = [
{'trigger': 'walk', 'source': 'A', 'dest': 'B'},
{'trigger': 'run', 'source': 'B', 'dest': 'C'},
{'trigger': 'sprint', 'source': 'C', 'dest': 'D'}
]
m = self.stuff.machine_cls(states=states, transitions=transitions, initial='A')
m.heavy_processing = heavy_processing
m.add_transition('forward', 'A', 'B', before='heavy_processing')
# # go to non initial state B
m.to_B()
# pickle Stuff model
dump = pickle.dumps(m)
self.assertIsNotNone(dump)
m2 = pickle.loads(dump)
self.assertTrue(m2.is_B())
m2.to_C_3_a()
m2.to_C_3_b()
# check if machines of stuff and stuff2 are truly separated
m2.to_A()
m.to_C()
self.assertTrue(m2.is_A())
thread = Thread(target=m2.forward)
thread.start()
# give thread some time to start
time.sleep(0.01)
# both objects should be in different states
# and also not share locks
begin = time.time()
# stuff should not be locked and execute fast
self.assertTrue(m.is_C())
def test_c2adder():
pc2adder = pickle.dumps(c2adder)
pc2add5 = pickle.loads(pc2adder)(x)
assert pc2add5(y) == x+y
def test_pickle_assessment_runner():
arunner = assessment.AAssessmentRunner()
brunner = dill.loads(dill.dumps(arunner))
assert type(arunner.parallel_factory) == type(brunner.parallel_factory)
del arunner.parallel_factory
del brunner.parallel_factory
assert arunner.__dict__ == brunner.__dict__
import dill as pickle
else:
import pickle
states = ['A', 'B', {'name': 'C', 'children': ['1', '2', {'name': '3', 'children': ['a', 'b', 'c']}]},
'D', 'E', 'F']
transitions = [
{'trigger': 'walk', 'source': 'A', 'dest': 'B'},
{'trigger': 'run', 'source': 'B', 'dest': 'C'},
{'trigger': 'sprint', 'source': 'C', 'dest': 'D'}
]
m = self.stuff.machine_cls(states=states, transitions=transitions, initial='A')
m.walk()
dump = pickle.dumps(m)
self.assertIsNotNone(dump)
m2 = pickle.loads(dump)
self.assertEqual(m.state, m2.state)
m2.run()
m2.to_C_3_a()
m2.to_C_3_b()
reload(module)
try:
dill.use_diff()
module.a = 1234
pik_mod = dill.dumps(module)
module.a = 0
# remove module
del sys.modules[module.__name__]
del module
module = dill.loads(pik_mod)
def test_diff_attributes():
assert hasattr(module, "a") and module.a == 1234
assert module.double_add(1, 2, 3) == 2 * module.fx
except AttributeError:
def test_diff_attributes():
pass
# clean up
import os
if os.path.exists(cached):
os.remove(cached)
pycache = os.path.join(os.path.dirname(module.__file__), "__pycache__")
if os.path.exists(pycache) and not os.listdir(pycache):
os.removedirs(pycache)
def _get_item(self, key, item_id, allow_empty=False):
'''Retrieve a specific item by its key type and id.'''
key = self._make_key(key, str(item_id))
value = self._store.get(key)
if not value:
if allow_empty:
return value
raise ValueError('Invalid key "{}" or missing data in store'.format(key))
return loads(value)
"""
if self.verbose:
print("Thread: %s connected to: %s" %
(threading.current_thread(), address))
try:
while True:
msg = self.receive_msg(client, 5)
msglen, serialization = struct.unpack('>Lc', msg)
if self.verbose:
print("Received message, length %d, serialization %r"
% (msglen, serialization))
msg = self.receive_msg(client, msglen)
if serialization != b'n':
try:
if serialization == b'd': # dill serialization
msg = dill.loads(msg)
elif serialization == b'j': # json serialization
msg = json.loads(msg.decode('utf-8'))
elif serialization == b'u': # utf-8 serialization
msg = msg.decode('utf-8')
else:
print("Unrecognized serialization type: %r"
% serialization)
continue
except (UnicodeDecodeError, ValueError) as e:
print("Error de-serializing message: %s \n %s" % (
msg, traceback.format_exc(e)))
continue
self.queue.put(msg)
except RuntimeError:
if self.verbose:
print("Client socket: " + str(address) + " closed")
def reraise(self):
t = dill.loads(self.e_type)
# the type is logged here before deserialising v and tb
# because occasionally there are problems deserialising the
# value (see #785, #548) and the fix is related to the
# specific exception type.
logger.debug("Reraising exception of type {}".format(t))
v = dill.loads(self.e_value)
tb = self.e_traceback.as_traceback()
reraise(t, v, tb)