Python .3.7.13 - Anaconda / macOS

I have Anaconda installed in my Macbook Air computer (macOS Catalina). The version is:

          conda version : 4.12.0
    conda-build version : 3.20.5
         python version : 3.7.13.final.0
       virtual packages : __osx=10.15.7=0
                          __unix=0=0
                          __archspec=1=x86_64

I am trying to build in this environment Python C API extensions, like the following (myextension.c):

#include "Python.h"

struct module_state {
    PyObject *error;
};

#if PY_MAJOR_VERSION >= 3
#define GETSTATE(m) ((struct module_state*)PyModule_GetState(m))
#else
#define GETSTATE(m) (&_state)
static struct module_state _state;
#endif

static PyObject *
error_out(PyObject *m) {
    struct module_state *st = GETSTATE(m);
    PyErr_SetString(st->error, "something bad happened");
    return NULL;
}

static PyMethodDef myextension_methods[] = {
    {"error_out", (PyCFunction)error_out, METH_NOARGS, NULL},
    {NULL, NULL}
};

#if PY_MAJOR_VERSION >= 3

static int myextension_traverse(PyObject *m, visitproc visit, void *arg) {
    Py_VISIT(GETSTATE(m)->error);
    return 0;
}

static int myextension_clear(PyObject *m) {
    Py_CLEAR(GETSTATE(m)->error);
    return 0;
}


static struct PyModuleDef moduledef = {
        PyModuleDef_HEAD_INIT,
        "myextension",
        NULL,
        sizeof(struct module_state),
        myextension_methods,
        NULL,
        myextension_traverse,
        myextension_clear,
        NULL
};

#define INITERROR return NULL

PyMODINIT_FUNC
PyInit_myextension(void)

#else
#define INITERROR return

void
initmyextension(void)
#endif
{
#if PY_MAJOR_VERSION >= 3
    PyObject *module = PyModule_Create(&moduledef);
#else
    PyObject *module = Py_InitModule("myextension", myextension_methods);
#endif

    if (module == NULL)
        INITERROR;
    struct module_state *st = GETSTATE(module);

    st->error = PyErr_NewException("myextension.Error", NULL, NULL);
    if (st->error == NULL) {
        Py_DECREF(module);
        INITERROR;
    }

#if PY_MAJOR_VERSION >= 3
    return module;
#endif
}

Using the commands:

gcc -DNDEBUG -g -O3 -Wall -fPIC -I/Users/Joao/anaconda3/include/python3.7m/ -c myextension.c
gcc -shared myextension.o -L/Users/Joao/anaconda3/lib/ -lpython3.7m -o myextension.so

However, after building the extension (myextension.so) crashes on import (import myextension), with Segmentation Fault. I have tried to build this extension in other computers with success, however, on my Macbook Air, I don’t manage to import the extension to python as described above.

On debug:

 $ lldb python
(lldb) target create "python"
Current executable set to 'python' (x86_64).
(lldb) run
Process 11715 launched: '/Users/Joao/anaconda3/bin/python' (x86_64)
Python 3.7.13 (default, Mar 28 2022, 07:24:34)
[Clang 12.0.0 ] :: Anaconda, Inc. on darwin
Type "help", "copyright", "credits" or "license" for more information.
>> import myextension
Process 11715 stopped

thread

    Support "bpo-" in Misc/NEWS #1, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=1, address=0x10)
    frame #0: 0x0000000103077d9b libpython3.7m.dylibPyModule_Create2 + 11 libpython3.7m.dylibPyModule_Create2:
    -> 0x103077d9b <+11>: movq 0x10(%rax), %rax
    0x103077d9f <+15>: cmpq $0x0, 0x28(%rax)
    0x103077da4 <+20>: je 0x103077dac ; <+28>
    0x103077da6 <+22>: popq %rbp
    Target 0: (python) stopped.
    (lldb)
    (lldb) reg read rax
    rax = 0x0000000000000000
    (lldb) disass
    libpython3.7m.dylib`PyModule_Create2:
    0x103077d90 <+0>: pushq %rbp
    0x103077d91 <+1>: movq %rsp, %rbp
    0x103077d94 <+4>: movq 0x2576fd(%rip), %rax ; _PyRuntime + 1528
    -> 0x103077d9b <+11>: movq 0x10(%rax), %rax