# Buggy code
def end_parallel_block(self, code):
    """
    To ensure all OpenMP threads have thread states, we ensure the GIL
    in each thread (which creates a thread state if it doesn't exist),
    after which we release the GIL.
    On exit, reacquire the GIL and release the thread state.

    If compiled without OpenMP support (at the C level), then we still have
    to acquire the GIL to decref any object temporaries.
    """
    if self.error_label_used:
        begin_code = self.begin_of_parallel_block
        end_code = code

        begin_code.putln("#ifdef _OPENMP")
        begin_code.put_ensure_gil(declare_gilstate=True)
        begin_code.putln("Py_BEGIN_ALLOW_THREADS")
        begin_code.putln("#endif /* _OPENMP */")

        end_code.putln("#ifdef _OPENMP")
        end_code.putln("Py_END_ALLOW_THREADS")
        end_code.putln("#else")
        end_code.put_safe("{\n")
        end_code.put_ensure_gil()
        end_code.putln("#endif /* _OPENMP */")
        self.cleanup_temps(end_code)
        end_code.put_release_ensured_gil()
        end_code.putln("#ifndef _OPENMP")
        end_code.put_safe("}\n")
        end_code.putln("#endif /* _OPENMP */")


# Traceback (TypeError)
%%cython
from contextlib import contextmanager

@contextmanager
def tag(name):
print("<%s>" % name)
yield
print("</%s>" % name)

from cython.parallel cimport prange
from libc.stdio cimport printf

def func():
cdef int i

with tag('aaa'):
for i in prange(5, nogil=True):   # using "xrange" or "with nogil" works well
printf("%d", i)

/Users/tema/Projects/miniconda2/lib/python2.7/site-packages/Cython/Compiler/Main.py:367: FutureWarning: Cython directive 'language_level' not set, using 2 for now (Py2). This will change in a later release! File: /Users/tema/.ipython/cython/_cython_magic_52369e6689303208367674016d90c298.pyx
tree = Parsing.p_module(s, pxd, full_module_name)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-11-72854e963c38> in <module>()
----> 1 get_ipython().run_cell_magic(u'cython', u'', u'from contextlib import contextmanager\n\n@contextmanager\ndef tag(name):\n    print("<%s>" % name)\n    yield\n    print("</%s>" % name)\n\n\nfrom cython.parallel cimport prange\nfrom libc.stdio cimport printf\n\n\ndef func():\n    cdef int i\n\n    with tag(\'aaa\'):\n        for i in prange(5, nogil=True):\n            printf("%d", i)')

/Users/tema/Projects/miniconda2/lib/python2.7/site-packages/IPython/core/interactiveshell.pyc in run_cell_magic(self, magic_name, line, cell)
2115             magic_arg_s = self.var_expand(line, stack_depth)
2116             with self.builtin_trap:
-> 2117                 result = fn(magic_arg_s, cell)
2118             return result
2119

<decorator-gen-118> in cython(self, line, cell)

/Users/tema/Projects/miniconda2/lib/python2.7/site-packages/IPython/core/magic.pyc in <lambda>(f, *a, **k)
186     # but it's overkill for just that one bit of state.
187     def magic_deco(arg):
--> 188         call = lambda f, *a, **k: f(*a, **k)
189
190         if callable(arg):

/Users/tema/Projects/miniconda2/lib/python2.7/site-packages/Cython/Build/IpythonMagic.pyc in cython(self, line, cell)
322         extension = None
323         if need_cythonize:
--> 324             extensions = self._cythonize(module_name, code, lib_dir, args, quiet=args.quiet)
325             assert len(extensions) == 1
326             extension = extensions[0]

/Users/tema/Projects/miniconda2/lib/python2.7/site-packages/Cython/Build/IpythonMagic.pyc in _cythonize(self, module_name, code, lib_dir, args, quiet)
430             elif sys.version_info[0] >= 3:
431                 opts['language_level'] = 3
--> 432             return cythonize([extension], **opts)
433         except CompileError:
434             return None

/Users/tema/Projects/miniconda2/lib/python2.7/site-packages/Cython/Build/Dependencies.pyc in cythonize(module_list, exclude, nthreads, aliases, quiet, force, language, exclude_failures, **options)
1084     if not nthreads:
1085         for args in to_compile:
-> 1086             cythonize_one(*args)
1087
1088     if exclude_failures:

/Users/tema/Projects/miniconda2/lib/python2.7/site-packages/Cython/Build/Dependencies.pyc in cythonize_one(pyx_file, c_file, fingerprint, quiet, options, raise_on_failure, embedded_metadata, full_module_name, progress)
1190     any_failures = 0
1191     try:
-> 1192         result = compile_single(pyx_file, options, full_module_name=full_module_name)
1193         if result.num_errors > 0:
1194             any_failures = 1

/Users/tema/Projects/miniconda2/lib/python2.7/site-packages/Cython/Compiler/Main.pyc in compile_single(source, options, full_module_name)
723     recursion.
724     """
--> 725     return run_pipeline(source, options, full_module_name)
726
727

/Users/tema/Projects/miniconda2/lib/python2.7/site-packages/Cython/Compiler/Main.pyc in run_pipeline(source, options, full_module_name, context)
511
512     context.setup_errors(options, result)
--> 513     err, enddata = Pipeline.run_pipeline(pipeline, source)
514     context.teardown_errors(err, options, result)
515     return result

/Users/tema/Projects/miniconda2/lib/python2.7/site-packages/Cython/Compiler/Pipeline.pyc in run_pipeline(pipeline, source, printtree)
353                             exec("def %s(phase, data): return phase(data)" % phase_name, exec_ns)
354                             run = _pipeline_entry_points[phase_name] = exec_ns[phase_name]
--> 355                     data = run(phase, data)
356                     if DebugFlags.debug_verbose_pipeline:
357                         print("    %.3f seconds" % (time() - t))

/Users/tema/Projects/miniconda2/lib/python2.7/site-packages/Cython/Compiler/Pipeline.pyc in run(phase, data)
333
334     def run(phase, data):
--> 335         return phase(data)
336
337     error = None

/Users/tema/Projects/miniconda2/lib/python2.7/site-packages/Cython/Compiler/Pipeline.pyc in generate_pyx_code_stage(module_node)
50 def generate_pyx_code_stage_factory(options, result):
51     def generate_pyx_code_stage(module_node):
---> 52         module_node.process_implementation(options, result)
53         result.compilation_source = module_node.compilation_source
54         return result

/Users/tema/Projects/miniconda2/lib/python2.7/site-packages/Cython/Compiler/ModuleNode.pyc in process_implementation(self, options, result)
141         self.find_referenced_modules(env, self.referenced_modules, {})
142         self.sort_cdef_classes(env)
--> 143         self.generate_c_code(env, options, result)
144         self.generate_h_code(env, options, result)
145         self.generate_api_code(env, options, result)

/Users/tema/Projects/miniconda2/lib/python2.7/site-packages/Cython/Compiler/ModuleNode.pyc in generate_c_code(self, env, options, result)
377         self.generate_variable_definitions(env, code)
378
--> 379         self.body.generate_function_definitions(env, code)
380
381         code.mark_pos(None)

/Users/tema/Projects/miniconda2/lib/python2.7/site-packages/Cython/Compiler/Nodes.pyc in generate_function_definitions(self, env, code)
440         #print "StatListNode.generate_function_definitions" ###
441         for stat in self.stats:
--> 442             stat.generate_function_definitions(env, code)
443
444     def generate_execution_code(self, code):

/Users/tema/Projects/miniconda2/lib/python2.7/site-packages/Cython/Compiler/Nodes.pyc in generate_function_definitions(self, env, code)
3171             self.py_wrapper.func_cname = self.entry.func_cname
3172             self.py_wrapper.generate_function_definitions(env, code)
-> 3173         FuncDefNode.generate_function_definitions(self, env, code)
3174
3175     def generate_function_header(self, code, with_pymethdef, proto_only=0):

/Users/tema/Projects/miniconda2/lib/python2.7/site-packages/Cython/Compiler/Nodes.pyc in generate_function_definitions(self, env, code)
1981         # ----- Function body -----
1982         # -------------------------
-> 1983         self.generate_function_body(env, code)
1984
1985         code.mark_pos(self.pos, trace=False)

/Users/tema/Projects/miniconda2/lib/python2.7/site-packages/Cython/Compiler/Nodes.pyc in generate_function_body(self, env, code)
1743
1744     def generate_function_body(self, env, code):
-> 1745         self.body.generate_execution_code(code)
1746
1747     def generate_function_definitions(self, env, code):

/Users/tema/Projects/miniconda2/lib/python2.7/site-packages/Cython/Compiler/Nodes.pyc in generate_execution_code(self, code)
446         for stat in self.stats:
447             code.mark_pos(stat.pos)
--> 448             stat.generate_execution_code(code)
449
450     def annotate(self, code):

/Users/tema/Projects/miniconda2/lib/python2.7/site-packages/Cython/Compiler/Nodes.pyc in generate_execution_code(self, code)
7090
7091         code.error_label = old_error_label
-> 7092         self.body.generate_execution_code(code)
7093
7094         if code.label_used(intermediate_error_label):

/Users/tema/Projects/miniconda2/lib/python2.7/site-packages/Cython/Compiler/Nodes.pyc in generate_execution_code(self, code)
7559         if not self.body.is_terminator:
7560             code.putln('/*normal exit:*/{')
-> 7561             fresh_finally_clause().generate_execution_code(code)
7562             if not self.finally_clause.is_terminator:
7563                 code.put_goto(catch_label)

/Users/tema/Projects/miniconda2/lib/python2.7/site-packages/Cython/Compiler/Nodes.pyc in fresh_finally_clause(_next)
7547             # generate the original subtree once and always keep a fresh copy
7548             node = _next[0]
-> 7549             node_copy = copy.deepcopy(node)
7550             if node is self.finally_clause:
7551                 _next[0] = node_copy

/Users/tema/Projects/miniconda2/lib/python2.7/copy.pyc in deepcopy(x, memo, _nil)
188                             raise Error(
189                                 "un(deep)copyable object of type %s" % cls)
--> 190                 y = _reconstruct(x, rv, 1, memo)
191
192     memo[d] = y

/Users/tema/Projects/miniconda2/lib/python2.7/copy.pyc in _reconstruct(x, info, deep, memo)
332     if state is not None:
333         if deep:
--> 334             state = deepcopy(state, memo)
335         if hasattr(y, '__setstate__'):
336             y.__setstate__(state)

/Users/tema/Projects/miniconda2/lib/python2.7/copy.pyc in deepcopy(x, memo, _nil)
161     copier = _deepcopy_dispatch.get(cls)
162     if copier:
--> 163         y = copier(x, memo)
164     else:
165         try:

/Users/tema/Projects/miniconda2/lib/python2.7/copy.pyc in _deepcopy_dict(x, memo)
255     memo[id(x)] = y
256     for key, value in x.iteritems():
--> 257         y[deepcopy(key, memo)] = deepcopy(value, memo)
258     return y
259 d[dict] = _deepcopy_dict

/Users/tema/Projects/miniconda2/lib/python2.7/copy.pyc in deepcopy(x, memo, _nil)
188                             raise Error(
189                                 "un(deep)copyable object of type %s" % cls)
--> 190                 y = _reconstruct(x, rv, 1, memo)
191
192     memo[d] = y

/Users/tema/Projects/miniconda2/lib/python2.7/copy.pyc in _reconstruct(x, info, deep, memo)
332     if state is not None:
333         if deep:
--> 334             state = deepcopy(state, memo)
335         if hasattr(y, '__setstate__'):
336             y.__setstate__(state)

/Users/tema/Projects/miniconda2/lib/python2.7/copy.pyc in deepcopy(x, memo, _nil)
161     copier = _deepcopy_dispatch.get(cls)
162     if copier:
--> 163         y = copier(x, memo)
164     else:
165         try:

/Users/tema/Projects/miniconda2/lib/python2.7/copy.pyc in _deepcopy_dict(x, memo)
255     memo[id(x)] = y
256     for key, value in x.iteritems():
--> 257         y[deepcopy(key, memo)] = deepcopy(value, memo)
258     return y
259 d[dict] = _deepcopy_dict

/Users/tema/Projects/miniconda2/lib/python2.7/copy.pyc in deepcopy(x, memo, _nil)
188                             raise Error(
189                                 "un(deep)copyable object of type %s" % cls)
--> 190                 y = _reconstruct(x, rv, 1, memo)
191
192     memo[d] = y

/Users/tema/Projects/miniconda2/lib/python2.7/copy.pyc in _reconstruct(x, info, deep, memo)
332     if state is not None:
333         if deep:
--> 334             state = deepcopy(state, memo)
335         if hasattr(y, '__setstate__'):
336             y.__setstate__(state)

/Users/tema/Projects/miniconda2/lib/python2.7/copy.pyc in deepcopy(x, memo, _nil)
161     copier = _deepcopy_dispatch.get(cls)
162     if copier:
--> 163         y = copier(x, memo)
164     else:
165         try:

/Users/tema/Projects/miniconda2/lib/python2.7/copy.pyc in _deepcopy_dict(x, memo)
255     memo[id(x)] = y
256     for key, value in x.iteritems():
--> 257         y[deepcopy(key, memo)] = deepcopy(value, memo)
258     return y
259 d[dict] = _deepcopy_dict

/Users/tema/Projects/miniconda2/lib/python2.7/copy.pyc in deepcopy(x, memo, _nil)
188                             raise Error(
189                                 "un(deep)copyable object of type %s" % cls)
--> 190                 y = _reconstruct(x, rv, 1, memo)
191
192     memo[d] = y

/Users/tema/Projects/miniconda2/lib/python2.7/copy.pyc in _reconstruct(x, info, deep, memo)
332     if state is not None:
333         if deep:
--> 334             state = deepcopy(state, memo)
335         if hasattr(y, '__setstate__'):
336             y.__setstate__(state)

/Users/tema/Projects/miniconda2/lib/python2.7/copy.pyc in deepcopy(x, memo, _nil)
161     copier = _deepcopy_dispatch.get(cls)
162     if copier:
--> 163         y = copier(x, memo)
164     else:
165         try:

/Users/tema/Projects/miniconda2/lib/python2.7/copy.pyc in _deepcopy_dict(x, memo)
255     memo[id(x)] = y
256     for key, value in x.iteritems():
--> 257         y[deepcopy(key, memo)] = deepcopy(value, memo)
258     return y
259 d[dict] = _deepcopy_dict

/Users/tema/Projects/miniconda2/lib/python2.7/copy.pyc in deepcopy(x, memo, _nil)
188                             raise Error(
189                                 "un(deep)copyable object of type %s" % cls)
--> 190                 y = _reconstruct(x, rv, 1, memo)
191
192     memo[d] = y

/Users/tema/Projects/miniconda2/lib/python2.7/copy.pyc in _reconstruct(x, info, deep, memo)
332     if state is not None:
333         if deep:
--> 334             state = deepcopy(state, memo)
335         if hasattr(y, '__setstate__'):
336             y.__setstate__(state)

/Users/tema/Projects/miniconda2/lib/python2.7/copy.pyc in deepcopy(x, memo, _nil)
161     copier = _deepcopy_dispatch.get(cls)
162     if copier:
--> 163         y = copier(x, memo)
164     else:
165         try:

/Users/tema/Projects/miniconda2/lib/python2.7/copy.pyc in _deepcopy_dict(x, memo)
255     memo[id(x)] = y
256     for key, value in x.iteritems():
--> 257         y[deepcopy(key, memo)] = deepcopy(value, memo)
258     return y
259 d[dict] = _deepcopy_dict

/Users/tema/Projects/miniconda2/lib/python2.7/copy.pyc in deepcopy(x, memo, _nil)
188                             raise Error(
189                                 "un(deep)copyable object of type %s" % cls)
--> 190                 y = _reconstruct(x, rv, 1, memo)
191
192     memo[d] = y

/Users/tema/Projects/miniconda2/lib/python2.7/copy.pyc in _reconstruct(x, info, deep, memo)
332     if state is not None:
333         if deep:
--> 334             state = deepcopy(state, memo)
335         if hasattr(y, '__setstate__'):
336             y.__setstate__(state)

/Users/tema/Projects/miniconda2/lib/python2.7/copy.pyc in deepcopy(x, memo, _nil)
161     copier = _deepcopy_dispatch.get(cls)
162     if copier:
--> 163         y = copier(x, memo)
164     else:
165         try:

/Users/tema/Projects/miniconda2/lib/python2.7/copy.pyc in _deepcopy_dict(x, memo)
255     memo[id(x)] = y
256     for key, value in x.iteritems():
--> 257         y[deepcopy(key, memo)] = deepcopy(value, memo)
258     return y
259 d[dict] = _deepcopy_dict

/Users/tema/Projects/miniconda2/lib/python2.7/copy.pyc in deepcopy(x, memo, _nil)
161     copier = _deepcopy_dispatch.get(cls)
162     if copier:
--> 163         y = copier(x, memo)
164     else:
165         try:

/Users/tema/Projects/miniconda2/lib/python2.7/copy.pyc in _deepcopy_list(x, memo)
228     memo[id(x)] = y
229     for a in x:
--> 230         y.append(deepcopy(a, memo))
231     return y
232 d[list] = _deepcopy_list

/Users/tema/Projects/miniconda2/lib/python2.7/copy.pyc in deepcopy(x, memo, _nil)
188                             raise Error(
189                                 "un(deep)copyable object of type %s" % cls)
--> 190                 y = _reconstruct(x, rv, 1, memo)
191
192     memo[d] = y

/Users/tema/Projects/miniconda2/lib/python2.7/copy.pyc in _reconstruct(x, info, deep, memo)
332     if state is not None:
333         if deep:
--> 334             state = deepcopy(state, memo)
335         if hasattr(y, '__setstate__'):
336             y.__setstate__(state)

/Users/tema/Projects/miniconda2/lib/python2.7/copy.pyc in deepcopy(x, memo, _nil)
161     copier = _deepcopy_dispatch.get(cls)
162     if copier:
--> 163         y = copier(x, memo)
164     else:
165         try:

/Users/tema/Projects/miniconda2/lib/python2.7/copy.pyc in _deepcopy_dict(x, memo)
255     memo[id(x)] = y
256     for key, value in x.iteritems():
--> 257         y[deepcopy(key, memo)] = deepcopy(value, memo)
258     return y
259 d[dict] = _deepcopy_dict

/Users/tema/Projects/miniconda2/lib/python2.7/copy.pyc in deepcopy(x, memo, _nil)
188                             raise Error(
189                                 "un(deep)copyable object of type %s" % cls)
--> 190                 y = _reconstruct(x, rv, 1, memo)
191
192     memo[d] = y

/Users/tema/Projects/miniconda2/lib/python2.7/copy.pyc in _reconstruct(x, info, deep, memo)
332     if state is not None:
333         if deep:
--> 334             state = deepcopy(state, memo)
335         if hasattr(y, '__setstate__'):
336             y.__setstate__(state)

/Users/tema/Projects/miniconda2/lib/python2.7/copy.pyc in deepcopy(x, memo, _nil)
161     copier = _deepcopy_dispatch.get(cls)
162     if copier:
--> 163         y = copier(x, memo)
164     else:
165         try:

/Users/tema/Projects/miniconda2/lib/python2.7/copy.pyc in _deepcopy_dict(x, memo)
255     memo[id(x)] = y
256     for key, value in x.iteritems():
--> 257         y[deepcopy(key, memo)] = deepcopy(value, memo)
258     return y
259 d[dict] = _deepcopy_dict

/Users/tema/Projects/miniconda2/lib/python2.7/copy.pyc in deepcopy(x, memo, _nil)
188                             raise Error(
189                                 "un(deep)copyable object of type %s" % cls)
--> 190                 y = _reconstruct(x, rv, 1, memo)
191
192     memo[d] = y

/Users/tema/Projects/miniconda2/lib/python2.7/copy.pyc in _reconstruct(x, info, deep, memo)
332     if state is not None:
333         if deep:
--> 334             state = deepcopy(state, memo)
335         if hasattr(y, '__setstate__'):
336             y.__setstate__(state)

/Users/tema/Projects/miniconda2/lib/python2.7/copy.pyc in deepcopy(x, memo, _nil)
161     copier = _deepcopy_dispatch.get(cls)
162     if copier:
--> 163         y = copier(x, memo)
164     else:
165         try:

/Users/tema/Projects/miniconda2/lib/python2.7/copy.pyc in _deepcopy_dict(x, memo)
255     memo[id(x)] = y
256     for key, value in x.iteritems():
--> 257         y[deepcopy(key, memo)] = deepcopy(value, memo)
258     return y
259 d[dict] = _deepcopy_dict

/Users/tema/Projects/miniconda2/lib/python2.7/copy.pyc in deepcopy(x, memo, _nil)
188                             raise Error(
189                                 "un(deep)copyable object of type %s" % cls)
--> 190                 y = _reconstruct(x, rv, 1, memo)
191
192     memo[d] = y

/Users/tema/Projects/miniconda2/lib/python2.7/copy.pyc in _reconstruct(x, info, deep, memo)
332     if state is not None:
333         if deep:
--> 334             state = deepcopy(state, memo)
335         if hasattr(y, '__setstate__'):
336             y.__setstate__(state)

/Users/tema/Projects/miniconda2/lib/python2.7/copy.pyc in deepcopy(x, memo, _nil)
161     copier = _deepcopy_dispatch.get(cls)
162     if copier:
--> 163         y = copier(x, memo)
164     else:
165         try:

/Users/tema/Projects/miniconda2/lib/python2.7/copy.pyc in _deepcopy_dict(x, memo)
255     memo[id(x)] = y
256     for key, value in x.iteritems():
--> 257         y[deepcopy(key, memo)] = deepcopy(value, memo)
258     return y
259 d[dict] = _deepcopy_dict

/Users/tema/Projects/miniconda2/lib/python2.7/copy.pyc in deepcopy(x, memo, _nil)
188                             raise Error(
189                                 "un(deep)copyable object of type %s" % cls)
--> 190                 y = _reconstruct(x, rv, 1, memo)
191
192     memo[d] = y

/Users/tema/Projects/miniconda2/lib/python2.7/copy.pyc in _reconstruct(x, info, deep, memo)
332     if state is not None:
333         if deep:
--> 334             state = deepcopy(state, memo)
335         if hasattr(y, '__setstate__'):
336             y.__setstate__(state)

/Users/tema/Projects/miniconda2/lib/python2.7/copy.pyc in deepcopy(x, memo, _nil)
161     copier = _deepcopy_dispatch.get(cls)
162     if copier:
--> 163         y = copier(x, memo)
164     else:
165         try:

/Users/tema/Projects/miniconda2/lib/python2.7/copy.pyc in _deepcopy_dict(x, memo)
255     memo[id(x)] = y
256     for key, value in x.iteritems():
--> 257         y[deepcopy(key, memo)] = deepcopy(value, memo)
258     return y
259 d[dict] = _deepcopy_dict

/Users/tema/Projects/miniconda2/lib/python2.7/copy.pyc in deepcopy(x, memo, _nil)
188                             raise Error(
189                                 "un(deep)copyable object of type %s" % cls)
--> 190                 y = _reconstruct(x, rv, 1, memo)
191
192     memo[d] = y

/Users/tema/Projects/miniconda2/lib/python2.7/copy.pyc in _reconstruct(x, info, deep, memo)
332     if state is not None:
333         if deep:
--> 334             state = deepcopy(state, memo)
335         if hasattr(y, '__setstate__'):
336             y.__setstate__(state)

/Users/tema/Projects/miniconda2/lib/python2.7/copy.pyc in deepcopy(x, memo, _nil)
161     copier = _deepcopy_dispatch.get(cls)
162     if copier:
--> 163         y = copier(x, memo)
164     else:
165         try:

/Users/tema/Projects/miniconda2/lib/python2.7/copy.pyc in _deepcopy_dict(x, memo)
255     memo[id(x)] = y
256     for key, value in x.iteritems():
--> 257         y[deepcopy(key, memo)] = deepcopy(value, memo)
258     return y
259 d[dict] = _deepcopy_dict

/Users/tema/Projects/miniconda2/lib/python2.7/copy.pyc in deepcopy(x, memo, _nil)
161     copier = _deepcopy_dispatch.get(cls)
162     if copier:
--> 163         y = copier(x, memo)
164     else:
165         try:

/Users/tema/Projects/miniconda2/lib/python2.7/copy.pyc in _deepcopy_list(x, memo)
228     memo[id(x)] = y
229     for a in x:
--> 230         y.append(deepcopy(a, memo))
231     return y
232 d[list] = _deepcopy_list

/Users/tema/Projects/miniconda2/lib/python2.7/copy.pyc in deepcopy(x, memo, _nil)
188                             raise Error(
189                                 "un(deep)copyable object of type %s" % cls)
--> 190                 y = _reconstruct(x, rv, 1, memo)
191
192     memo[d] = y

/Users/tema/Projects/miniconda2/lib/python2.7/copy.pyc in _reconstruct(x, info, deep, memo)
332     if state is not None:
333         if deep:
--> 334             state = deepcopy(state, memo)
335         if hasattr(y, '__setstate__'):
336             y.__setstate__(state)

/Users/tema/Projects/miniconda2/lib/python2.7/copy.pyc in deepcopy(x, memo, _nil)
161     copier = _deepcopy_dispatch.get(cls)
162     if copier:
--> 163         y = copier(x, memo)
164     else:
165         try:

/Users/tema/Projects/miniconda2/lib/python2.7/copy.pyc in _deepcopy_dict(x, memo)
255     memo[id(x)] = y
256     for key, value in x.iteritems():
--> 257         y[deepcopy(key, memo)] = deepcopy(value, memo)
258     return y
259 d[dict] = _deepcopy_dict

/Users/tema/Projects/miniconda2/lib/python2.7/copy.pyc in deepcopy(x, memo, _nil)
180                     reductor = getattr(x, "__reduce_ex__", None)
181                     if reductor:
--> 182                         rv = reductor(2)
183                     else:
184                         reductor = getattr(x, "__reduce__", None)

TypeError: can't pickle cStringIO.StringO objects

# Fixed code
def end_parallel_block(self, code):
    """
    To ensure all OpenMP threads have thread states, we ensure the GIL
    in each thread (which creates a thread state if it doesn't exist),
    after which we release the GIL.
    On exit, reacquire the GIL and release the thread state.

    If compiled without OpenMP support (at the C level), then we still have
    to acquire the GIL to decref any object temporaries.
    """
    begin_code = self.begin_of_parallel_block
    self.begin_of_parallel_block = None

    if self.error_label_used:
        end_code = code

        begin_code.putln("#ifdef _OPENMP")
        begin_code.put_ensure_gil(declare_gilstate=True)
        begin_code.putln("Py_BEGIN_ALLOW_THREADS")
        begin_code.putln("#endif /* _OPENMP */")

        end_code.putln("#ifdef _OPENMP")
        end_code.putln("Py_END_ALLOW_THREADS")
        end_code.putln("#else")
        end_code.put_safe("{\n")
        end_code.put_ensure_gil()
        end_code.putln("#endif /* _OPENMP */")
        self.cleanup_temps(end_code)
        end_code.put_release_ensured_gil()
        end_code.putln("#ifndef _OPENMP")
        end_code.put_safe("}\n")
        end_code.putln("#endif /* _OPENMP */")