bpo-31680: Add curses.ncurses_version. (GH-4217) · python/cpython@b232df9

@@ -4289,6 +4289,59 @@ _curses_use_default_colors_impl(PyObject *module)

42894289

}

42904290

#endif /* STRICT_SYSV_CURSES */

429142914292+4293+

#ifdef NCURSES_VERSION

4294+4295+

PyDoc_STRVAR(ncurses_version__doc__,

4296+

"curses.ncurses_version\n\

4297+

\n\

4298+

Ncurses version information as a named tuple.");

4299+4300+

static PyTypeObject NcursesVersionType;

4301+4302+

static PyStructSequence_Field ncurses_version_fields[] = {

4303+

{"major", "Major release number"},

4304+

{"minor", "Minor release number"},

4305+

{"patch", "Patch release number"},

4306+

{0}

4307+

};

4308+4309+

static PyStructSequence_Desc ncurses_version_desc = {

4310+

"curses.ncurses_version", /* name */

4311+

ncurses_version__doc__, /* doc */

4312+

ncurses_version_fields, /* fields */

4313+

3

4314+

};

4315+4316+

static PyObject *

4317+

make_ncurses_version(void)

4318+

{

4319+

PyObject *ncurses_version;

4320+

int pos = 0;

4321+4322+

ncurses_version = PyStructSequence_New(&NcursesVersionType);

4323+

if (ncurses_version == NULL) {

4324+

return NULL;

4325+

}

4326+4327+

#define SetIntItem(flag) \

4328+

PyStructSequence_SET_ITEM(ncurses_version, pos++, PyLong_FromLong(flag)); \

4329+

if (PyErr_Occurred()) { \

4330+

Py_CLEAR(ncurses_version); \

4331+

return NULL; \

4332+

}

4333+4334+

SetIntItem(NCURSES_VERSION_MAJOR)

4335+

SetIntItem(NCURSES_VERSION_MINOR)

4336+

SetIntItem(NCURSES_VERSION_PATCH)

4337+

#undef SetIntItem

4338+4339+

return ncurses_version;

4340+

}

4341+4342+

#endif /* NCURSES_VERSION */

4343+4344+42924345

/* List of functions defined in the module */

4293434642944347

static PyMethodDef PyCurses_methods[] = {

@@ -4426,6 +4479,30 @@ PyInit__curses(void)

44264479

PyDict_SetItemString(d, "__version__", v);

44274480

Py_DECREF(v);

442844814482+

#ifdef NCURSES_VERSION

4483+

/* ncurses_version */

4484+

if (NcursesVersionType.tp_name == NULL) {

4485+

if (PyStructSequence_InitType2(&NcursesVersionType,

4486+

&ncurses_version_desc) < 0)

4487+

return NULL;

4488+

}

4489+

v = make_ncurses_version();

4490+

if (v == NULL) {

4491+

return NULL;

4492+

}

4493+

PyDict_SetItemString(d, "ncurses_version", v);

4494+

Py_DECREF(v);

4495+4496+

/* prevent user from creating new instances */

4497+

NcursesVersionType.tp_init = NULL;

4498+

NcursesVersionType.tp_new = NULL;

4499+

if (PyDict_DelItemString(NcursesVersionType.tp_dict, "__new__") < 0 &&

4500+

PyErr_ExceptionMatches(PyExc_KeyError))

4501+

{

4502+

PyErr_Clear();

4503+

}

4504+

#endif /* NCURSES_VERSION */

4505+44294506

SetDictInt("ERR", ERR);

44304507

SetDictInt("OK", OK);

44314508