]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-152332: Add the curses.term_attrs() function (GH-152333)
authorSerhiy Storchaka <storchaka@gmail.com>
Sat, 27 Jun 2026 20:23:55 +0000 (23:23 +0300)
committerGitHub <noreply@github.com>
Sat, 27 Jun 2026 20:23:55 +0000 (23:23 +0300)
term_attrs() returns the video attributes supported by the terminal as
WA_* values, the counterpart of termattrs() for the A_* values.

Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
Doc/library/curses.rst
Doc/whatsnew/3.16.rst
Lib/test/test_curses.py
Misc/NEWS.d/next/Library/2026-06-26-22-15-00.gh-issue-152332.Lp3WqN.rst [new file with mode: 0644]
Modules/_cursesmodule.c
Modules/clinic/_cursesmodule.c.h

index 60c60a9edeec49c19398ebd167c7d106e9319289..71eadfd5ca2ad70fc57eeee5307afedca6159c13 100644 (file)
@@ -841,6 +841,14 @@ The module :mod:`!curses` defines the following functions:
    appearance of the screen.
 
 
+.. function:: term_attrs()
+
+   Like :func:`termattrs`, but return the attributes as :ref:`WA_*
+   <curses-wa-constants>` values rather than ``A_*`` values.
+
+   .. versionadded:: next
+
+
 .. function:: termname()
 
    Return the value of the environment variable :envvar:`TERM`, as a bytes object,
index 7841fa56cc5952c6937fa284b584f139ad55adca..90ab3ee630b165e79069f0ef904eee197ec35512 100644 (file)
@@ -165,6 +165,11 @@ curses
   :func:`~curses.scr_set`, which dump the whole screen to a file and restore it.
   (Contributed by Serhiy Storchaka in :gh:`152260`.)
 
+* Add the :func:`curses.term_attrs` function, which returns the supported
+  video attributes as :ref:`WA_* <curses-wa-constants>` values, the
+  counterpart of :func:`curses.termattrs`.
+  (Contributed by Serhiy Storchaka in :gh:`152332`.)
+
 * Add the :mod:`curses` key-management functions :func:`~curses.define_key`,
   :func:`~curses.key_defined` and :func:`~curses.keyok`, available when built
   against an ncurses with ``NCURSES_EXT_FUNCS``.
index d9e9cf394fa86fbaf9e44945b8f0fd0c1213b36d..5f01ac6546a0ba0ab7db90755856bf74d2423d13 100644 (file)
@@ -1512,13 +1512,13 @@ class TestCurses(unittest.TestCase):
         curses.newpad(50, 50)
 
     def test_env_queries(self):
-        # TODO: term_attrs()
         self.assertIsInstance(curses.termname(), bytes)
         self.assertIsInstance(curses.longname(), bytes)
         self.assertIsInstance(curses.baudrate(), int)
         self.assertIsInstance(curses.has_ic(), bool)
         self.assertIsInstance(curses.has_il(), bool)
         self.assertIsInstance(curses.termattrs(), int)
+        self.assertIsInstance(curses.term_attrs(), int)
 
         c = curses.killchar()
         self.assertIsInstance(c, bytes)
diff --git a/Misc/NEWS.d/next/Library/2026-06-26-22-15-00.gh-issue-152332.Lp3WqN.rst b/Misc/NEWS.d/next/Library/2026-06-26-22-15-00.gh-issue-152332.Lp3WqN.rst
new file mode 100644 (file)
index 0000000..32875cd
--- /dev/null
@@ -0,0 +1,2 @@
+Add the :func:`curses.term_attrs` function, the counterpart of
+:func:`curses.termattrs` for the ``WA_*`` attributes.
index 76e93784b94d46615f12608f237cfef412bc6830..5e50f9b1e0b2384be2e44b5d15544948fd8a57d1 100644 (file)
@@ -7347,6 +7347,24 @@ _curses_termattrs_impl(PyObject *module)
 /*[clinic end generated code: output=b06f437fce1b6fc4 input=0559882a04f84d1d]*/
 NoArgReturnIntFunctionBody(termattrs)
 
+/*[clinic input]
+_curses.term_attrs
+
+Return a logical OR of all video attributes supported by the terminal.
+
+The attributes are WA_* values, the extended-attribute counterparts of
+the A_* values returned by termattrs().
+[clinic start generated code]*/
+
+static PyObject *
+_curses_term_attrs_impl(PyObject *module)
+/*[clinic end generated code: output=c559daa1370948d6 input=963136fd17ab797a]*/
+{
+    PyCursesStatefulInitialised(module);
+
+    return PyLong_FromUnsignedLong(term_attrs());
+}
+
 /*[clinic input]
 @permit_long_summary
 _curses.termname
@@ -7905,6 +7923,7 @@ static PyMethodDef cursesmodule_methods[] = {
     _CURSES_SETUPTERM_METHODDEF
     _CURSES_START_COLOR_METHODDEF
     _CURSES_TERMATTRS_METHODDEF
+    _CURSES_TERM_ATTRS_METHODDEF
     _CURSES_TERMNAME_METHODDEF
     _CURSES_TIGETFLAG_METHODDEF
     _CURSES_TIGETNUM_METHODDEF
index a4a7791fa6adcd6b694a52eb39b51ca2e1e8e989..1c05a125fd1cc6cf7dfcee95c5a1c7c6e20642f4 100644 (file)
@@ -5073,6 +5073,27 @@ _curses_termattrs(PyObject *module, PyObject *Py_UNUSED(ignored))
     return _curses_termattrs_impl(module);
 }
 
+PyDoc_STRVAR(_curses_term_attrs__doc__,
+"term_attrs($module, /)\n"
+"--\n"
+"\n"
+"Return a logical OR of all video attributes supported by the terminal.\n"
+"\n"
+"The attributes are WA_* values, the extended-attribute counterparts of\n"
+"the A_* values returned by termattrs().");
+
+#define _CURSES_TERM_ATTRS_METHODDEF    \
+    {"term_attrs", (PyCFunction)_curses_term_attrs, METH_NOARGS, _curses_term_attrs__doc__},
+
+static PyObject *
+_curses_term_attrs_impl(PyObject *module);
+
+static PyObject *
+_curses_term_attrs(PyObject *module, PyObject *Py_UNUSED(ignored))
+{
+    return _curses_term_attrs_impl(module);
+}
+
 PyDoc_STRVAR(_curses_termname__doc__,
 "termname($module, /)\n"
 "--\n"
@@ -5645,4 +5666,4 @@ _curses_has_extended_color_support(PyObject *module, PyObject *Py_UNUSED(ignored
 #ifndef _CURSES_ASSUME_DEFAULT_COLORS_METHODDEF
     #define _CURSES_ASSUME_DEFAULT_COLORS_METHODDEF
 #endif /* !defined(_CURSES_ASSUME_DEFAULT_COLORS_METHODDEF) */
-/*[clinic end generated code: output=01cb1ecb396881c9 input=a9049054013a1b77]*/
+/*[clinic end generated code: output=976a6629bfe58a3d input=a9049054013a1b77]*/