title: PyImport_Import segmentation fault after reading in TSV with C++
body: <p>I am using C++ as a wrapper around a Python module. First, I read in a TSV file, cast it as a numpy array, import my Python module, and then pass the numpy array to Python for further analysis. When I first wrote the program, I was testing everything using a randomly generated array, and it worked well. However, once I replaced the randomly generated array with the imported TSV array, I got a segmentation fault when I tried to import the Python module. Here is some of my code:</p>
  <pre><code>#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
  #define PY_SSIZE_T_CLEAN
  
  #include &lt;python3.8/Python.h&gt;
  #include &quot;./venv/lib/python3.8/site-packages/numpy/core/include/numpy/arrayobject.h&quot;
  
  #include &lt;stdio.h&gt;
  #include &lt;iostream&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;random&gt;
  #include &lt;fstream&gt;
  #include &lt;sstream&gt;
  
  int main(int argc, char* argv[]) {
  
      setenv(&quot;PYTHONPATH&quot;, &quot;.&quot;, 0);
  
      Py_Initialize();
      import_array();
  
      static const int numberRows = 1000;
      static const int numberColumns = 500;
  
      npy_intp dims[2]{ numberRows, numberColumns };
  
      static const int numberDims = 2;
  
      double(*c_arr)[numberColumns]{ new double[numberRows][numberColumns] };
  
      // *********************************************************** 
      // THIS PART OF THE CODE GENERATES A RANDOM ARRAY AND WORKS WITH THE REST OF THE CODE
      // // initialize random number generation
      // typedef std::mt19937 MyRNG;
      // std::random_device r;
      // MyRNG rng{r()};
      // std::lognormal_distribution&lt;double&gt; lognormalDistribution(1.6, 0.25);
  
      // //populate array
      // for (int i=0; i &lt; numberRows; i++) {
      //     for (int j=0; j &lt; numberColumns; j++) {
      //         c_arr[i][j] = lognormalDistribution(rng);
      //     }
      // }
      // ***********************************************************
  
      // *********************************************************** 
      // THIS PART OF THE CODE INGESTS AN ARRAY FROM TSV AND CAUSES CODE TO FAIL AT PyImport_Import
      std::ifstream data(&quot;data.mat&quot;);
      std::string line;
      int row = 0;
      int column = 0;
      while (std::getline(data, line)) {
          std::stringstream lineStream(line);
          std::string cell;
          while (std::getline(lineStream, cell, '\t')) {
              c_arr[row][column] = std::stod(cell);
              column++;
          }
          row++;
          column = 0;
          if (row &gt; numberRows) {
              break;
          }
      }
      // *********************************************************** 
  
      PyArrayObject *npArray = reinterpret_cast&lt;PyArrayObject*&gt;(
          PyArray_SimpleNewFromData(numberDims, dims, NPY_DOUBLE, reinterpret_cast&lt;void*&gt;(c_arr))
          );
  
      const char *moduleName = &quot;cpp_test&quot;;
      PyObject *pname = PyUnicode_FromString(moduleName);
  
      // ***********************************************************
      // CODE FAILS HERE - SEGMENTATION FAULT
      PyObject *pyModule = PyImport_Import(pname);
  
  
      // .......
      // THERE IS MORE CODE BELOW NOT INCLUDED HERE
  }
  </code></pre>
  <p>So, I'm not sure why the code fails when ingest data from a TSV file, but not when I use randomly generated data.</p>
  <p>EDIT: (very stupid mistake incoming) I used the conditional <code>row &gt; numberRows</code> for the stopping condition in the <code>while</code> loop and so this affected the row number used for the final line in the array. Once I changed that conditional to <code>row == numberRows</code>, everything worked. Who knew being specific about rows when building an array was so important? I'll leave this up as a testament to stupid programming mistakes and maybe someone will learn a little something from it.</p>