Python Forum
Anaconda Spyder erratically reset by distutils C extension
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Anaconda Spyder erratically reset by distutils C extension
#1
I am using Anaconda Spyder 3.3.6 (Python 3.7) on CentOS 7. I would like to pass 3D numpy arrays to a C function that processes the input and returns a numpy array as output. The C code is in a module, surfaceModules, which has the following form.

#include <stdio.h>
#include <stdlib.h>
#include "/home/peter/anaconda3/include/python3.7m/Python.h"
#include "numpy/arrayobject.h"

#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION

static PyObject *  squeezeSurfaces(PyObject* self, PyObject* args)
{
    PyArrayObject *matin, *matout;
    double ***cin, ***cout, dfactor;
    int i,j,k,n,m,l, dims[3], ifactor;

    // Parse tuples separately since args will differ between C fcns
    if (!PyArg_ParseTuple(args, "O!id",
        &PyArray_Type, &matin, &ifactor, &dfactor))  return NULL;
    if (NULL == matin)  return NULL;

    return PyArray_Return(matin);
}

static PyMethodDef surfaceMethods[] = {
    {"squeezeSurfaces", squeezeSurfaces, METH_VARARGS, "Squeezes surfaces along normal     vectors"},
    {NULL, NULL, 0, NULL}
};

static struct PyModuleDef surfaceModules = {
    PyModuleDef_HEAD_INIT,
    "surfaceModules",
    "Surface function Module",
    -1,
    surfaceMethods
};

PyMODINIT_FUNC PyInit_surfaceModules(void){
    import_array();
    return PyModule_Create(&surfaceModules);
}
The following code

import surfaceModules as sm
import numpy as NP

x=NP.zeros(27)
for i in range(0,27): x[i]=i 
x=x.reshape(3,3,3)
jfac=2
xfac=1.5
y=sm.squeezeSurfaces(x, jfac, xfac)
sometimes produces this

In [1]: import surfaceModules as sm
import numpy as NP

x=NP.zeros(27)
for i in range(0,27): x[i]=i 

x=x.reshape(3,3,3)
jfac=2
xfac=1.5
y=sm.squeezeSurfaces(x, jfac, xfac)

In [1]: y
Out[2]: 
array([[[ 0.,  1.,  2.],
    [ 3.,  4.,  5.],
    [ 6.,  7.,  8.]],

   [[ 9., 10., 11.],
    [12., 13., 14.],
    [15., 16., 17.]],

   [[18., 19., 20.],
    [21., 22., 23.],
    [24., 25., 26.]]])
which is what I want. However, sometimes, it produces this.

In [4]: import surfaceModules as sm
import numpy as NP

x=NP.zeros(27)
for i in range(0,27): x[i]=i 

x=x.reshape(3,3,3)
jfac=2
xfac=1.5
y=sm.squeezeSurfaces(x, jfac, xfac)

In [1]: y
Traceback (most recent call last):

  File "<ipython-input-1-9063a9f0e032>", line 1, in <module>
    y

NameError: name 'y' is not defined
Note In [1]: y instead of In [5]: y. Seems to reset the Spyder kernel. But seems erratic and unpredictable. I would not want this happening when it is part of larger code.

The same erratic behavior also happens with this code.

import surfaceModules as sm
import numpy as NP

x=NP.zeros(27)
jfac=2
xfac=1.5
y=sm.squeezeSurfaces(x, jfac, xfac)
Reply
#2
I tried running the following code.

import surfaceModules as sm
import numpy as NP

x=NP.zeros(27)
for i in range(0,27): x[i]=i 
x=x.reshape(3,3,3)
jfac=2
xfac=1.5
y=sm.squeezeSurfaces(x, jfac, xfac)
y
in Jupyter Notebook. Sometimes, I got the desired result, which is a 3D numpy array. Other times I got '_execution_count_validate'. Again, the result appeared to be unpredictable and random.
Reply
#3
I think the issue is with your use of PyArg_ParseTuple. The command O! returns a borrowed reference via matin. You are returning the borrowed reference without incrementing it.

Untested...

static PyObject *  squeezeSurfaces(PyObject* self, PyObject* args)
{
    PyArrayObject *matin, *matout;
    double ***cin, ***cout, dfactor;
    int i,j,k,n,m,l, dims[3], ifactor;
 
    // Parse tuples separately since args will differ between C fcns
    if (!PyArg_ParseTuple(args, "O!id",
        &PyArray_Type, &matin, &ifactor, &dfactor))  return NULL;
    if (NULL == matin)  return NULL;
 
    Py_INCREF(matin);
    return PyArray_Return(matin);
}
casevh
Reply
#4
@casevh. That appears to have worked. Thanks very much
Reply
#5
x=NP.zeros(27)
for i in range(0,27): x[i]=i 
x=x.reshape(3,3,3)
This code makes my brain bleeding.
1) NO ONE imports numpy as NP, instead it´s worldwide common use to import numpy as np
2) make it a oneliner
x = np.arange(27, dtype=np.float).reshape(3,3,3)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Use different Anaconda environments on Linux Mint and Spyder StaLLoNe_CoBRa 0 1,883 Jan-20-2021, 03:12 AM
Last Post: StaLLoNe_CoBRa
  time.sleep works erratically, a bug in Python? stipcevic 2 3,890 Jan-21-2020, 09:38 PM
Last Post: Marbelous
  Element misalignment passing numpy array with distutils OTAGOHARBOUR 1 2,058 Dec-06-2019, 01:47 AM
Last Post: OTAGOHARBOUR
  Anaconda / Spyder / VPython mwatson87 3 88,339 May-24-2018, 01:51 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020