Skip to content

numpy.set_string_function is unsafe when using multiple embedded sub interpreters #3961

Closed
@ndjensen

Description

@ndjensen

Our application uses multiple embedded python interpreters. When starting or stopping new interpreters, everything with numpy seems to work fine except for the printing of arrays. After starting a second interpreter that uses numpy and then printing an array in the first interpreter, we get an error:

<type 'exceptions.TypeError'>: 'NoneType' object is not callable >>>   File "<string>", line 1, in <module>
  File "/pythonInstall/python/lib/python2.7/site-packages/numpy/core/numeric.py", line 1471, in array_str
    return array2string(a, max_line_width, precision, suppress_small, ' ', "", str)

I've traced it down somewhat. Another embedded interpreter does not share sys.modules but any static variables used in CPython extensions are shared across interpreters. It seems to be that when the second interpreter imports numpy it imports numeric.py which has these two lines:

set_string_function(array_str, 0)
set_string_function(array_repr, 1)

That changes the static variables PyArray_StrFunction and PyArrayReprFunction in arrayobject.c that are shared across interpreters, and the first interpreter somehow still references the original PyArray_StrFunction and therefore fails on a print. Note that if the first interpreter directly uses numpy.core.arrayprint.array2string or numpy.core.numeric.array_str those still work fine, it only seems to be the str(array) that is broken.

Tested with python 2.7.1 and numpy 1.7.1 and numpy 1.5.0 on CentOS 5.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions