@@ -588,6 +588,165 @@ valpy_string (PyObject *self, PyObject *args, PyObject *kw)
588
588
encoding , errors );
589
589
}
590
590
591
+ /* Given a Python object, copy its truth value to a C int (the value
592
+ pointed by dest).
593
+ If src_obj is NULL, then *dest is not modified.
594
+
595
+ Return true in case of success (including src_obj being NULL), false
596
+ in case of error. */
597
+
598
+ static bool
599
+ copy_py_bool_obj (int * dest , PyObject * src_obj )
600
+ {
601
+ if (src_obj )
602
+ {
603
+ int cmp = PyObject_IsTrue (src_obj );
604
+ if (cmp < 0 )
605
+ return false;
606
+ * dest = cmp ;
607
+ }
608
+
609
+ return true;
610
+ }
611
+
612
+ /* Implementation of gdb.Value.format_string (...) -> string.
613
+ Return Unicode string with value contents formatted using the
614
+ keyword-only arguments. */
615
+
616
+ static PyObject *
617
+ valpy_format_string (PyObject * self , PyObject * args , PyObject * kw )
618
+ {
619
+ static const char * keywords [] =
620
+ {
621
+ /* Basic C/C++ options. */
622
+ "raw" , /* See the /r option to print. */
623
+ "pretty_arrays" , /* See set print array on|off. */
624
+ "pretty_structs" , /* See set print pretty on|off. */
625
+ "array_indexes" , /* See set print array-indexes on|off. */
626
+ "symbols" , /* See set print symbol on|off. */
627
+ "unions" , /* See set print union on|off. */
628
+ /* C++ options. */
629
+ "deref_refs" , /* No corresponding setting. */
630
+ "actual_objects" , /* See set print object on|off. */
631
+ "static_members" , /* See set print static-members on|off. */
632
+ /* C non-bool options. */
633
+ "max_elements" , /* See set print elements N. */
634
+ "repeat_threshold" , /* See set print repeats. */
635
+ "format" , /* The format passed to the print command. */
636
+ NULL
637
+ };
638
+
639
+ /* This function has too many arguments to be useful as positionals, so
640
+ the user should specify them all as keyword arguments.
641
+ Python 3.3 and later have a way to specify it (both in C and Python
642
+ itself), but we could be compiled with older versions, so we just
643
+ check that the args tuple is empty. */
644
+ Py_ssize_t positional_count = PyObject_Length (args );
645
+ if (positional_count < 0 )
646
+ return NULL ;
647
+ else if (positional_count > 0 )
648
+ {
649
+ /* This matches the error message that Python 3.3 raises when
650
+ passing positionals to functions expecting keyword-only
651
+ arguments. */
652
+ PyErr_Format (PyExc_TypeError ,
653
+ "format_string() takes 0 positional arguments but %zu were given" ,
654
+ positional_count );
655
+ return NULL ;
656
+ }
657
+
658
+ struct value_print_options opts ;
659
+ get_user_print_options (& opts );
660
+ opts .deref_ref = 0 ;
661
+
662
+ /* We need objects for booleans as the "p" flag for bools is new in
663
+ Python 3.3. */
664
+ PyObject * raw_obj = NULL ;
665
+ PyObject * pretty_arrays_obj = NULL ;
666
+ PyObject * pretty_structs_obj = NULL ;
667
+ PyObject * array_indexes_obj = NULL ;
668
+ PyObject * symbols_obj = NULL ;
669
+ PyObject * unions_obj = NULL ;
670
+ PyObject * deref_refs_obj = NULL ;
671
+ PyObject * actual_objects_obj = NULL ;
672
+ PyObject * static_members_obj = NULL ;
673
+ char * format = NULL ;
674
+ if (!gdb_PyArg_ParseTupleAndKeywords (args ,
675
+ kw ,
676
+ "|O!O!O!O!O!O!O!O!O!IIs" ,
677
+ keywords ,
678
+ & PyBool_Type , & raw_obj ,
679
+ & PyBool_Type , & pretty_arrays_obj ,
680
+ & PyBool_Type , & pretty_structs_obj ,
681
+ & PyBool_Type , & array_indexes_obj ,
682
+ & PyBool_Type , & symbols_obj ,
683
+ & PyBool_Type , & unions_obj ,
684
+ & PyBool_Type , & deref_refs_obj ,
685
+ & PyBool_Type , & actual_objects_obj ,
686
+ & PyBool_Type , & static_members_obj ,
687
+ & opts .print_max ,
688
+ & opts .repeat_count_threshold ,
689
+ & format ))
690
+ return NULL ;
691
+
692
+ /* Set boolean arguments. */
693
+ if (!copy_py_bool_obj (& opts .raw , raw_obj ))
694
+ return NULL ;
695
+ if (!copy_py_bool_obj (& opts .prettyformat_arrays , pretty_arrays_obj ))
696
+ return NULL ;
697
+ if (!copy_py_bool_obj (& opts .prettyformat_structs , pretty_structs_obj ))
698
+ return NULL ;
699
+ if (!copy_py_bool_obj (& opts .print_array_indexes , array_indexes_obj ))
700
+ return NULL ;
701
+ if (!copy_py_bool_obj (& opts .symbol_print , symbols_obj ))
702
+ return NULL ;
703
+ if (!copy_py_bool_obj (& opts .unionprint , unions_obj ))
704
+ return NULL ;
705
+ if (!copy_py_bool_obj (& opts .deref_ref , deref_refs_obj ))
706
+ return NULL ;
707
+ if (!copy_py_bool_obj (& opts .objectprint , actual_objects_obj ))
708
+ return NULL ;
709
+ if (!copy_py_bool_obj (& opts .static_field_print , static_members_obj ))
710
+ return NULL ;
711
+
712
+ /* Numeric arguments for which 0 means unlimited (which we represent as
713
+ UINT_MAX). */
714
+ if (opts .print_max == 0 )
715
+ opts .print_max = UINT_MAX ;
716
+ if (opts .repeat_count_threshold == 0 )
717
+ opts .repeat_count_threshold = UINT_MAX ;
718
+
719
+ /* Other arguments. */
720
+ if (format != NULL )
721
+ {
722
+ if (strlen (format ) == 1 )
723
+ opts .format = format [0 ];
724
+ else
725
+ {
726
+ /* Mimic the message on standard Python ones for similar
727
+ errors. */
728
+ PyErr_SetString (PyExc_ValueError ,
729
+ "a single character is required" );
730
+ return NULL ;
731
+ }
732
+ }
733
+
734
+ string_file stb ;
735
+
736
+ TRY
737
+ {
738
+ common_val_print (((value_object * ) self )-> value , & stb , 0 ,
739
+ & opts , python_language );
740
+ }
741
+ CATCH (except , RETURN_MASK_ALL )
742
+ {
743
+ GDB_PY_HANDLE_EXCEPTION (except );
744
+ }
745
+ END_CATCH
746
+
747
+ return PyUnicode_Decode (stb .c_str (), stb .size (), host_charset (), NULL );
748
+ }
749
+
591
750
/* A helper function that implements the various cast operators. */
592
751
593
752
static PyObject *
@@ -1944,6 +2103,11 @@ Return a lazy string representation of the value." },
1944
2103
Return Unicode string representation of the value." },
1945
2104
{ "fetch_lazy" , valpy_fetch_lazy , METH_NOARGS ,
1946
2105
"Fetches the value from the inferior, if it was lazy." },
2106
+ { "format_string" , (PyCFunction ) valpy_format_string ,
2107
+ METH_VARARGS | METH_KEYWORDS ,
2108
+ "format_string (...) -> string\n\
2109
+ Return a string representation of the value using the specified\n\
2110
+ formatting options" },
1947
2111
{NULL } /* Sentinel */
1948
2112
};
1949
2113
0 commit comments