Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improving the error message thrown when attempting to set the value of a constant member #212

Open
michalwyszynski93 opened this issue Jun 19, 2024 · 0 comments

Comments

@michalwyszynski93
Copy link

The error message is generic and therefore not very useful/actionable for the user who encounters it.
In setattrbehavior.cpp

int
constant_handler( Member* member, CAtom* atom, PyObject* value )
{
    cppy::type_error( "cannot set the value of a constant member" );
    return -1;
}

could be replaced with something like

int
constant_handler( Member* member, CAtom* atom, PyObject* value )
{
    const char* member_str = PyUnicode_AsUTF8(member->name);
    PyObject* object_repr = PyObject_Repr(value);
    const PyObject object_base = atom->ob_base;
    const PyTypeObject* object_type = object_base.ob_type;
    char error_msg[500];
    if((object_repr == NULL) || (object_type == NULL)) {
        snprintf(error_msg, 500, "cannot set the value of a constant member %s", member_str);
        cppy::type_error(error_msg);
        if(object_repr != NULL) {
            Py_DECREF(object_repr);
        }
        return -1;
    }
    const char* value_str = PyUnicode_AsUTF8(object_repr);
    const char* type_str = object_type->tp_name;
    snprintf(error_msg, 500, "cannot set the value %s of a constant member %s of an instance of %s", value_str, member_str, type_str);
    cppy::type_error(error_msg);
    Py_DECREF(object_repr);
    return -1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant