@@ -8374,35 +8374,155 @@ _curses_tigetstr_impl(PyObject *module, const char *capname)
83748374 return PyBytes_FromString ( capname );
83758375}
83768376
8377+ #ifndef NCURSES_TPARM_ARG
8378+ # define NCURSES_TPARM_ARG long
8379+ #endif
8380+
8381+ /* Return a bit mask of the parameters of a terminfo string that curses takes
8382+ as strings: those that %s or %l follows directly after %p, as in ncurses'
8383+ _nc_tparm_analyze(). The others are numbers. */
8384+ static unsigned int
8385+ curses_tparm_string_params (const char * str )
8386+ {
8387+ unsigned int mask = 0 ;
8388+ int last = 0 ; /* the parameter pushed last, or 0 */
8389+
8390+ while (* str ) {
8391+ if (* str ++ != '%' ) {
8392+ continue ;
8393+ }
8394+ /* Skip the flags, width and precision of a conversion. */
8395+ int allowminus = 0 ;
8396+ while (* str ) {
8397+ if (* str == ':' ) {
8398+ allowminus = 1 ;
8399+ }
8400+ else if (!(* str == '#' || * str == ' ' || * str == '.'
8401+ || Py_ISDIGIT (* str ) || (* str == '-' && allowminus )))
8402+ {
8403+ break ;
8404+ }
8405+ str ++ ;
8406+ }
8407+ switch (* str ) {
8408+ case 'p' :
8409+ if (str [1 ] >= '1' && str [1 ] <= '9' ) {
8410+ last = str [1 ] - '0' ;
8411+ }
8412+ str ++ ;
8413+ break ;
8414+ case 's' :
8415+ case 'l' :
8416+ if (last > 0 ) {
8417+ mask |= 1u << (last - 1 );
8418+ }
8419+ break ;
8420+ case 'd' : case 'o' : case 'x' : case 'X' : case 'c' :
8421+ case '+' : case '-' : case '*' : case '/' : case 'm' :
8422+ case '&' : case '|' : case '^' : case '=' : case '<' : case '>' :
8423+ case 'A' : case 'O' : case '!' : case '~' :
8424+ last = 0 ;
8425+ break ;
8426+ case '\'' :
8427+ str += 2 ;
8428+ last = 0 ;
8429+ break ;
8430+ case '\0' :
8431+ return mask ;
8432+ default :
8433+ /* %P, %g, %{, %i, %?, %t, %e, %; and %% keep it. */
8434+ break ;
8435+ }
8436+ str ++ ;
8437+ }
8438+ return mask ;
8439+ }
8440+
8441+ /* Convert a tparm() parameter: a string for those that the terminfo string
8442+ takes as strings (passing a number there would crash curses), otherwise a
8443+ number. An omitted one is 0, or NULL, which curses takes as "". */
8444+ static int
8445+ curses_tparm_param (PyObject * arg , int argnum , int is_string ,
8446+ NCURSES_TPARM_ARG * result )
8447+ {
8448+ if (arg == NULL ) {
8449+ * result = 0 ;
8450+ return 0 ;
8451+ }
8452+ if (is_string ) {
8453+ if (!PyBytes_Check (arg )) {
8454+ PyErr_Format (PyExc_TypeError ,
8455+ "tparm() argument %d must be bytes, not %T" ,
8456+ argnum , arg );
8457+ return -1 ;
8458+ }
8459+ const char * value = PyBytes_AS_STRING (arg );
8460+ if (strlen (value ) != (size_t )PyBytes_GET_SIZE (arg )) {
8461+ PyErr_SetString (PyExc_ValueError , "embedded null byte" );
8462+ return -1 ;
8463+ }
8464+ * result = (NCURSES_TPARM_ARG )(intptr_t )value ;
8465+ return 0 ;
8466+ }
8467+ if (PyBytes_Check (arg )) {
8468+ PyErr_Format (PyExc_TypeError ,
8469+ "tparm() argument %d must be int, not bytes" , argnum );
8470+ return -1 ;
8471+ }
8472+ int value = PyLong_AsInt (arg );
8473+ if (value == -1 && PyErr_Occurred ()) {
8474+ return -1 ;
8475+ }
8476+ * result = value ;
8477+ return 0 ;
8478+ }
8479+
83778480/*[clinic input]
83788481_curses.tparm
83798482
8380- str: str(accept={robuffer})
8483+ str as string: object
83818484 Parameterized byte string obtained from the terminfo database.
8382- i1: int = 0
8383- i2: int = 0
8384- i3: int = 0
8385- i4: int = 0
8386- i5: int = 0
8387- i6: int = 0
8388- i7: int = 0
8389- i8: int = 0
8390- i9: int = 0
83918485 /
8486+ *params: tuple
8487+ Up to nine parameters.
83928488
83938489Instantiate the specified byte string with the supplied parameters.
8490+
8491+ The parameters are integers, except in a few cases where they must be
8492+ bytes objects.
83948493[clinic start generated code]*/
83958494
83968495static PyObject *
8397- _curses_tparm_impl (PyObject * module , const char * str , int i1 , int i2 , int i3 ,
8398- int i4 , int i5 , int i6 , int i7 , int i8 , int i9 )
8399- /*[clinic end generated code: output=599f62b615c667ff input=5e30b15786f032aa]*/
8496+ _curses_tparm_impl (PyObject * module , PyObject * string , PyObject * params )
8497+ /*[clinic end generated code: output=3439bb1b37a13d5b input=4889c0e3a5eab5b5]*/
84008498{
84018499 char * result = NULL ;
8500+ NCURSES_TPARM_ARG values [9 ];
8501+ Py_ssize_t count = PyTuple_GET_SIZE (params );
8502+ const char * str ;
84028503
8504+ if (!PyArg_Parse (string , "y:tparm" , & str )) {
8505+ return NULL ;
8506+ }
8507+
8508+ if (count > 9 ) {
8509+ PyErr_Format (PyExc_TypeError ,
8510+ "tparm() takes at most 9 parameters (%zd given)" , count );
8511+ return NULL ;
8512+ }
84038513 PyCursesStatefulSetupTermCalled (module );
84048514
8405- result = tparm ((char * )str ,i1 ,i2 ,i3 ,i4 ,i5 ,i6 ,i7 ,i8 ,i9 );
8515+ unsigned int strings = curses_tparm_string_params (str );
8516+ for (int i = 0 ; i < 9 ; i ++ ) {
8517+ PyObject * arg = i < count ? PyTuple_GET_ITEM (params , i ) : NULL ;
8518+ if (curses_tparm_param (arg , i + 2 , strings & (1u << i ),
8519+ & values [i ]) < 0 )
8520+ {
8521+ return NULL ;
8522+ }
8523+ }
8524+ result = tparm ((char * )str , values [0 ], values [1 ], values [2 ], values [3 ],
8525+ values [4 ], values [5 ], values [6 ], values [7 ], values [8 ]);
84068526 if (!result ) {
84078527 curses_set_null_error (module , "tparm" , NULL );
84088528 return NULL ;
0 commit comments