Ask Your Question
1

ValueError: Cannot pickle code objects from closures

asked 2021-09-16 02:48:15 +0100

Max Alekseyev gravatar image

updated 2021-09-16 02:50:16 +0100

The following code produces a strange error ValueError: Cannot pickle code objects from closures in Sage 9.4. It does not happen at SageCell, and also disappears if I remove if set(v)&S condition. What's wrong?

def mytestproc(n,G,S):
    V = [v for v in G.vertices() if set(v)&S]
    return n*len(V)

def test(n):
  G = DiGraph()
  return sum( val for _,val in sage.parallel.multiprocessing_sage.parallel_iter(2, mytestproc, [((n,G,S),{}) for S in Subsets(1..n)]) )

test(2)
edit retag flag offensive close merge delete

Comments

A difference with SageMathCell might be due to different Python versions; compare the output of import sys; print(sys.version).

rburing gravatar imagerburing ( 2021-09-16 12:56:33 +0100 )edit

Python version 3.9.5 here and there. So, it does not explain the different behavior.

Max Alekseyev gravatar imageMax Alekseyev ( 2021-09-16 15:47:48 +0100 )edit
1

You are right. Instead, the difference can be seen by running

import types
from copyreg import dispatch_table
reduce_code = dispatch_table[types.CodeType]
reduce_code??

On my local machine this shows sage.misc.fpickle.reduce_code, whereas on SageMathCell it shows ipykernel.codeutil.reduce_code (which does not include the check for objects from closures). Seems fishy.

rburing gravatar imagerburing ( 2021-09-16 16:44:33 +0100 )edit

1 Answer

Sort by ยป oldest newest most voted
0

answered 2021-09-16 12:43:02 +0100

rburing gravatar image

The condition in the list comprehension is like a lambda, and it doesn't like that you want to capture S in it.

You can avoid lambdas that capture other objects by using partial from functools:

def mytestproc(n,G,S):
    def f(S, v):
        return set(v) & S
    from functools import partial
    V = list(filter(partial(f, S), G.vertices()))
    return n*len(V)
edit flag offensive delete link more

Comments

Why it does not like capturing S? And why SageCell does not have this issue?

Max Alekseyev gravatar imageMax Alekseyev ( 2021-09-16 15:47:42 +0100 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2021-09-16 02:48:15 +0100

Seen: 77 times

Last updated: Sep 16