Commit Graph

273 Commits

Author SHA1 Message Date
Serhiy Storchaka bac3fcba5b
gh-108512: Add and use new replacements for PySys_GetObject() (GH-111035)
Add functions PySys_GetAttr(), PySys_GetAttrString(),
PySys_GetOptionalAttr() and PySys_GetOptionalAttrString().
2025-05-28 20:11:09 +03:00
Bénédikt Tran 30dde1eeb3
gh-133579: consistently report C curses function failures (#134327)
Some curses module-level functions and window methods now raise
a `curses.error` when a call to a C curses function fails:

- Module-level functions: assume_default_colors, baudrate, cbreak,
  echo, longname, initscr, nl, raw, termattrs, termname, and unctrl.
- Window methods: addch, addnstr, addstr, border, box, chgat,
  getbkgd, inch, insstr, and insnstr.

In addition, `curses.window.refresh` and `curses.window.noutrefresh`
now raise a `TypeError` instead of a `curses.error` when called with an
incorrect number of arguments for pads.

See also ee36db5500 for similar
changes.
2025-05-27 10:15:16 +00:00
Bénédikt Tran 604f83550b
gh-134210: refactor signal handling in `_curses.window.{get_wch,getkey}` (#134646) 2025-05-27 10:57:41 +02:00
Bénédikt Tran 51762b6cad
gh-134210: handle signals in `_curses.window.getch` (#134326) 2025-05-27 10:51:05 +02:00
Bénédikt Tran 29e8115964
gh-134208: remove dead AC directives for `_curses.window.{chgat,getstr,instr}` (#134325) 2025-05-26 10:52:19 +02:00
tigerding aadda87b3d
gh-134209: use heap-allocated memory in `_curses.window.{instr,getstr}` (GH-134283)
* made curses buffer heap allocated instead of stack
* change docs to explicitly mention the max buffer size
* changing GetStr() function to behave similarly too
* Update Doc/library/curses.rst
* Update instr with proper return error handling
* Update Modules/_cursesmodule.c
* change to strlen and better memory safety
* change from const int to Py_ssize_t
* add mem allocation guard
* update versionchanged to mention it was an increase.
* explicitly use versionchanged 3.14 as that is its own branch now.

TESTED: `python -m test -u curses test_curses`

---------

Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com>
Co-authored-by: Gregory P. Smith <greg@krypto.org>
Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
2025-05-20 20:36:04 +00:00
Bénédikt Tran e007e62ba7
gh-125843: fix `test_curses.test_attributes` on x86-64 macOS (#134252)
While some `libcurses` functions are meant to return OK on success,
this is not always the case for all implementations. As such, we relax
the checks on the return values and allow any non-ERR value to be
considered equivalent to OK.
2025-05-20 09:15:39 +00:00
Bénédikt Tran ee36db5500
gh-125843: indicate which C function caused a `curses.error` (#125844)
- Rename error helpers with a `curses_set_error_*` prefix instead of `PyCurses*`.
- Cleanly report both NULL and ERR cases.
- Raise `curses.error` in `is_linetouched` instead of a `TypeError`.
2025-05-19 15:53:39 +02:00
Michael Forney 0af61fe2f4
bpo-44172: Keep reference to original window in curses subwindow objects (GH-26226)
The X/Open curses specification[0] and ncurses documentation[1]
both state that subwindows must be deleted before the main window.

Deleting the windows in the wrong order causes a double-free with
NetBSD's curses implementation.

To fix this, keep track of the original window object in the subwindow
object, and keep a reference to the original for the lifetime of
the subwindow.

[0] https://pubs.opengroup.org/onlinepubs/7908799/xcurses/delwin.html
[1] https://invisible-island.net/ncurses/man/curs_window.3x.html

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
2025-05-04 13:29:44 +03:00
Serhiy Storchaka 7363e8d24d
gh-133139: Add curses.assume_default_colors() (GH-133145)
This is a refinement of the curses.use_default_colors() function which
allows to change the color pair 0.
2025-05-03 23:33:22 +03:00
Bénédikt Tran edbf7fb129
gh-111178: remove redundant casts for functions with correct signatures (#131673) 2025-04-01 17:18:11 +02:00
Mark Shannon a1aeec61c4
GH-131238: Core header refactor (GH-131250)
* Moves most structs in pycore_ header files into pycore_structs.h and pycore_runtime_structs.h

* Removes many cross-header dependencies
2025-03-17 09:19:04 +00:00
Victor Stinner 9d759b63d8
gh-111178: Change Argument Clinic signature for METH_O (#130682)
Use "PyObject*" for METH_O functions to fix an undefined behavior.
2025-03-11 16:33:36 +01:00
Serhiy Storchaka 0ef4ffeefd
gh-130163: Fix crashes related to PySys_GetObject() (GH-130503)
The use of PySys_GetObject() and _PySys_GetAttr(), which return a borrowed
reference, has been replaced by using one of the following functions, which
return a strong reference and distinguish a missing attribute from an error:
_PySys_GetOptionalAttr(), _PySys_GetOptionalAttrString(),
_PySys_GetRequiredAttr(), and _PySys_GetRequiredAttrString().
2025-02-25 23:04:27 +02:00
Victor Stinner 3bebe46d34
gh-128911: Add PyImport_ImportModuleAttr() function (#128912)
Add PyImport_ImportModuleAttr() and
PyImport_ImportModuleAttrString() functions.

* Add unit tests.
* Replace _PyImport_GetModuleAttr()
  with PyImport_ImportModuleAttr().
* Replace _PyImport_GetModuleAttrString()
  with PyImport_ImportModuleAttrString().
* Remove "pycore_import.h" includes, no longer needed.
2025-01-30 11:17:29 +00:00
Bénédikt Tran 621d4ff35e
gh-111178: fix UBSan failures in `Modules/curses*.c` (GH-128244)
* fix UBSan failures in `_cursesmodule.c`
* fix UBSan failures in `_curses_panel.c`
* suppress an unused return value
2025-01-03 15:12:40 +01:00
Serhiy Storchaka 8d16919a06
gh-123925: Fix building curses on platforms without libncursesw (GH-128405) 2025-01-02 13:38:21 +02:00
Bénédikt Tran 19d9358437
gh-126313: Fix a crash in curses.napms() due to incorrect error handling (GH-126351) 2024-11-03 15:08:34 +00:00
Bénédikt Tran e4292c0410
gh-123961: Convert _curses to a multi-phase init module (PEP-489) (#124965) 2024-10-08 13:42:44 +02:00
Bénédikt Tran f66d785861
gh-123961: Convert `curses.window` static type into a heap type (#124934) 2024-10-04 11:58:34 +02:00
Bénédikt Tran e6dd71da3a
gh-123961: Clean up curses C API/capsule (#124907) 2024-10-03 10:48:50 +00:00
Peter Bierma c2ba931318
gh-120378: Fix crash caused by integer overflow in `curses` (#124555)
This is actually an upstream problem in curses, and has been reported
to them already:
https://lists.gnu.org/archive/html/bug-ncurses/2024-09/msg00101.html

This is a nice workaround in the meantime to prevent the segfault.

Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
2024-10-02 14:31:23 +00:00
Bénédikt Tran 4d8e7c40a0
gh-123961: Add a global state to _curses (#124729) 2024-09-29 15:17:20 +00:00
Bénédikt Tran 12417a984c
gh-123290: Fix decref in _curses update_lines_cols() (#124767) 2024-09-29 15:02:43 +00:00
Bénédikt Tran acb3f875fb
gh-123961: Add `curses` prefix to global variables in `_cursesmodule.c` (#124047)
Use the `const char*` type instead of a `const *` for the encoding name.
2024-09-13 12:43:06 +00:00
Bénédikt Tran e49d1b44d3
gh-124044: protect macros expansions in `_cursesmodules.c` using `do { ... } while (0)` (#124045) 2024-09-13 11:26:19 +00:00
Bénédikt Tran 403f3ddedc
gh-123961: Remove global variable `ModDict` in `_cursesmodule.c` (#123962) 2024-09-13 10:33:13 +00:00
Bénédikt Tran 2938c3dec9
gh-123290: fix reference leaks in the error-branches of `_cursesmodule.c` (#123953) 2024-09-11 15:49:33 +00:00
Erlend E. Aasland f80376b129
gh-113565: Improve and harden detection of curses dependencies (#119816)
1. Use pkg-config to check for ncursesw/panelw. If that fails, use 
   pkg-config to check for ncurses/panel.
2. Regardless of pkg-config output, search for curses/panel headers, so
   we're sure we have all defines in pyconfig.h.
3. Regardless of pkg-config output, check if libncurses or libncursesw
   contains the 'initscr' symbol; if it does _and_ pkg-config failed
   earlier, add the resulting -llib linker option to CURSES_LIBS.
   Ditto for 'update_panels' and PANEL_LIBS.
4. Wrap the rest of the checks with WITH_SAVE_ENV and make sure we're 
   using updated LIBS and CPPFLAGS for those.

Add the PY_CHECK_CURSES convenience macro.
2024-07-01 08:10:03 +00:00
Serhiy Storchaka b313cc68d5
gh-117557: Improve error messages when a string, bytes or bytearray of length 1 are expected (GH-117631) 2024-05-28 12:01:37 +03:00
Petr Viktorin 417dd3aca7
gh-116322: Rename PyModule_ExperimentalSetGIL to PyUnstable_Module_SetGIL (GH-118645) 2024-05-06 18:59:36 +02:00
Davide Rizzo 08d169f14a
gh-109617: fix ncurses incompatibility on macOS with Xcode 15 (#111258)
Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com>
Co-authored-by: Łukasz Langa <lukasz@langa.pl>
2024-05-04 23:41:47 +02:00
Brett Simmers c2627d6eea
gh-116322: Add Py_mod_gil module slot (#116882)
This PR adds the ability to enable the GIL if it was disabled at
interpreter startup, and modifies the multi-phase module initialization
path to enable the GIL when loading a module, unless that module's spec
includes a slot indicating it can run safely without the GIL.

PEP 703 called the constant for the slot `Py_mod_gil_not_used`; I went
with `Py_MOD_GIL_NOT_USED` for consistency with gh-104148.

A warning will be issued up to once per interpreter for the first
GIL-using module that is loaded. If `-v` is given, a shorter message
will be printed to stderr every time a GIL-using module is loaded
(including the first one that issues a warning).
2024-05-03 11:30:55 -04:00
Serhiy Storchaka 59ea0f523e
gh-110093: Replace trivial Py_BuildValue() with direct C API call (GH-110094) 2023-10-20 18:08:41 +03:00
Serhiy Storchaka 2b15536fa9
gh-107913: Fix possible losses of OSError error codes (GH-107930)
Functions like PyErr_SetFromErrno() and SetFromWindowsErr() should be
called immediately after using the C API which sets errno or the Windows
error code.
2023-08-27 00:35:06 +03:00
Inada Naoki d5bd32fb48
gh-104922: remove PY_SSIZE_T_CLEAN (#106315) 2023-07-02 15:07:46 +09:00
Serhiy Storchaka a87c46eab3
bpo-15999: Accept arbitrary values for boolean parameters. (#15609)
builtins and extension module functions and methods that expect boolean values for parameters now accept any Python object rather than just a bool or int type. This is more consistent with how native Python code itself behaves.
2022-12-03 11:52:21 -08:00
Victor Stinner 3817607b8a
gh-99300: Use Py_NewRef() in Modules/ directory (#99466)
Replace Py_INCREF() and Py_XINCREF() with Py_NewRef() and
Py_XNewRef() in test C files of the Modules/ directory.
2022-11-14 13:08:15 +01:00
Kumar Aditya 0a806f2684
GH-90699: Remove `_Py_IDENTIFIER` usage from `_curses` module (GH-98957) 2022-11-02 08:41:11 -07:00
Kumar Aditya 277f55cb04
GH-94644: fix test_curses ref leak (GH-94647) 2022-07-07 03:57:45 -07:00
Eric Snow 81c72044a1
bpo-46541: Replace core use of _Py_IDENTIFIER() with statically initialized global objects. (gh-30928)
We're no longer using _Py_IDENTIFIER() (or _Py_static_string()) in any core CPython code.  It is still used in a number of non-builtin stdlib modules.

The replacement is: PyUnicodeObject (not pointer) fields under _PyRuntimeState, statically initialized as part of _PyRuntime.  A new _Py_GET_GLOBAL_IDENTIFIER() macro facilitates lookup of the fields (along with _Py_GET_GLOBAL_STRING() for non-identifier strings).

https://bugs.python.org/issue46541#msg411799 explains the rationale for this change.

The core of the change is in:

* (new) Include/internal/pycore_global_strings.h - the declarations for the global strings, along with the macros
* Include/internal/pycore_runtime_init.h - added the static initializers for the global strings
* Include/internal/pycore_global_objects.h - where the struct in pycore_global_strings.h is hooked into _PyRuntimeState
* Tools/scripts/generate_global_objects.py - added generation of the global string declarations and static initializers

I've also added a --check flag to generate_global_objects.py (along with make check-global-objects) to check for unused global strings.  That check is added to the PR CI config.

The remainder of this change updates the core code to use _Py_GET_GLOBAL_IDENTIFIER() instead of _Py_IDENTIFIER() and the related _Py*Id functions (likewise for _Py_GET_GLOBAL_STRING() instead of _Py_static_string()).  This includes adding a few functions where there wasn't already an alternative to _Py*Id(), replacing the _Py_Identifier * parameter with PyObject *.

The following are not changed (yet):

* stop using _Py_IDENTIFIER() in the stdlib modules
* (maybe) get rid of _Py_IDENTIFIER(), etc. entirely -- this may not be doable as at least one package on PyPI using this (private) API
* (maybe) intern the strings during runtime init

https://bugs.python.org/issue46541
2022-02-08 13:39:07 -07:00
Victor Stinner 1781d55eb3
bpo-46417: _curses uses PyStructSequence_NewType() (GH-30736)
The _curses module now creates its ncurses_version type as a heap
type using PyStructSequence_NewType(), rather than using a static
type.

* Move _PyStructSequence_FiniType() definition to pycore_structseq.h.
* test.pythoninfo: log curses.ncurses_version.
2022-01-21 03:30:20 +01:00
Kumar Aditya 41026c3155
bpo-45855: Replaced deprecated `PyImport_ImportModuleNoBlock` with PyImport_ImportModule (GH-30046) 2021-12-12 10:45:20 +02:00
Thomas Klausner 2fb797e93c
bpo-46000: Improve NetBSD curses compatibility (GH-29947) 2021-12-10 11:41:47 +02:00
Christian Heimes 03e9f5dc75
bpo-43974: Move Py_BUILD_CORE_MODULE into module code (GH-29157)
setup.py no longer defines Py_BUILD_CORE_MODULE. Instead every
module defines the macro before #include "Python.h" unless
Py_BUILD_CORE_BUILTIN is already defined.

Py_BUILD_CORE_BUILTIN is defined for every module that is built by
Modules/Setup.

The PR also simplifies Modules/Setup. Makefile and makesetup
already define Py_BUILD_CORE_BUILTIN and include Modules/internal
for us.

Signed-off-by: Christian Heimes <christian@python.org>
2021-10-22 15:36:28 +02:00
Senthil Kumaran 794430700d
bpo-45067 - Verify the version of ncurses for extended color support feature usage. (GH-28260)
* issue45067 - Fix _curses compilation in CentOS 7. Verify the version of ncurses
for extended color support feature usage.

The function extended_color_content was introduced in 2017.  The ncurses-devel
package in CentOS 7 had a older version ncurses resulted in compilation error.
For compiling ncurses with extended color support, we verify the version of the
ncurses library.

Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com>
Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
2021-09-09 11:32:11 -07:00
Noah Kantrowitz be42c06bb0
Update URLs in comments and metadata to use HTTPS (GH-27458) 2021-07-30 15:54:46 +02:00
Pablo Galindo c2931d31f8
bpo-43916: Move the _PyStructSequence_InitType function to the internal API (GH-25854) 2021-05-03 15:50:24 +01:00
Victor Stinner 3bb09947ec
bpo-43916: Add Py_TPFLAGS_DISALLOW_INSTANTIATION type flag (GH-25721)
Add a new Py_TPFLAGS_DISALLOW_INSTANTIATION type flag to disallow
creating type instances: set tp_new to NULL and don't create the
"__new__" key in the type dictionary.

The flag is set automatically on static types if tp_base is NULL or
&PyBaseObject_Type and tp_new is NULL.

Use the flag on the following types:

* _curses.ncurses_version type
* _curses_panel.panel
* _tkinter.Tcl_Obj
* _tkinter.tkapp
* _tkinter.tktimertoken
* _xxsubinterpretersmodule.ChannelID
* sys.flags type
* sys.getwindowsversion() type
* sys.version_info type

Update MyStr example in the C API documentation to use
Py_TPFLAGS_DISALLOW_INSTANTIATION.

Add _PyStructSequence_InitType() function to create a structseq type
with the Py_TPFLAGS_DISALLOW_INSTANTIATION flag set.

type_new() calls _PyType_CheckConsistency() at exit.
2021-04-30 12:46:15 +02:00
Serhiy Storchaka b1dc1aacf8
bpo-43084: Return bool instead of int from curses.window.enclose() (GH-24398) 2021-04-05 16:50:24 +03:00