]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
gdb/python: add type traits check for all PyObject sub-classes
authorAndrew Burgess <aburgess@redhat.com>
Thu, 14 May 2026 08:39:57 +0000 (09:39 +0100)
committerAndrew Burgess <aburgess@redhat.com>
Sat, 16 May 2026 11:44:37 +0000 (12:44 +0100)
All of our custom Python types are created as structs, like this:

  struct some_new_type : public PyObject
  {
    ... various fields ...
  };

Then instances of this struct are created by calling PyObject_New,
either directly within GDB's C++ code, or within Python when a user's
Python script creates an instance of that class.

The problem is that Python is written in C, and PyObject_New doesn't
call any constructors for `some_new_type`, nor for any of the fields
within `some_new_type`.

If `some_new_type` is Plain Old Data (POD), then this is fine.  Or, to
be more C++ specific, if `some_new_type` is trivially default
constructable, then we're fine.

But if a field within `some_new_type` has a non-trivial constructor,
then we're in trouble as that constructor will never be run.

An example of a problematic field type is frame_info_ptr.  The
constructor for this type registers the new object with a central
management object, recording the `this` pointer, using this type within
`some_new_type` will not work as expected; frame invalidation will not
show up within the frame_info_ptr as you might expect.

And so, this type trait exists.  Whenever a struct is created to define
a new Python type we should add a line like:

  static_assert (gdb::is_python_allocatable_v<some_new_type>);

This will fail if any field of `some_new_type` are unsuitable for this
use.

We don't actually check is_trivially_default_constructible here.  Some
types, e.g. ui_file_style::color, have non-trivial (or no default)
constructors, but are still safe to use within `some_new_type` because
their constructors just initialise data fields; there's nothing
"special" that the constructor does that cannot be achieved by
assigning the fields after creation with PyObject_New.

What actually matters is that the type is trivially destructible
(Python won't call C++ destructors, so destructors with side effects,
like deregistering from a list, would be skipped) and trivially
copyable (Python may copy objects with memcpy).  Types like
frame_info_ptr, whose constructors and destructors have side effects
such as registering with a central management object, will be caught
because they are neither trivially destructible nor trivially copyable.
Simple POD types like ui_file_style are trivially destructible and
copyable, so pass this trait.

This commit adds the new type trait, and makes use of it in all cases
but one, pending_frame_object in python/py-unwind.c, has a field of
type frame_info_ptr, which is currently broken.  This will be fixed,
and the static_assert added, in the next commit.

Approved-By: Tom Tromey <tom@tromey.com>
31 files changed:
gdb/python/py-arch.c
gdb/python/py-block.c
gdb/python/py-breakpoint.c
gdb/python/py-cmd.c
gdb/python/py-color.c
gdb/python/py-connection.c
gdb/python/py-corefile.c
gdb/python/py-disasm.c
gdb/python/py-events.h
gdb/python/py-frame.c
gdb/python/py-instruction.c
gdb/python/py-lazy-string.c
gdb/python/py-linetable.c
gdb/python/py-membuf.c
gdb/python/py-micmd.c
gdb/python/py-param.c
gdb/python/py-prettyprint.c
gdb/python/py-record-btrace.c
gdb/python/py-record.c
gdb/python/py-record.h
gdb/python/py-ref.h
gdb/python/py-registers.c
gdb/python/py-style.c
gdb/python/py-symbol.c
gdb/python/py-symtab.c
gdb/python/py-tui.c
gdb/python/py-type.c
gdb/python/py-unwind.c
gdb/python/py-value.c
gdb/python/python-internal.h
gdb/python/python-traits.h [new file with mode: 0644]

index ac0c40dcf45c7075dcde1f20043e55688ab8e134..7a0cb0a2a59802b48e09e93d80af182c81088c21 100644 (file)
@@ -27,6 +27,8 @@ struct arch_object : public PyObject
   struct gdbarch *gdbarch;
 };
 
+static_assert (gdb::is_python_allocatable_v<arch_object>);
+
 static const registry<gdbarch>::key<PyObject, gdb::noop_deleter<PyObject>>
      arch_object_data;
 
index 6fe18e455640eccf2505ee7746fc63a58f7f8bfc..903f800683a06944352470cdec4f9c70ecaa659c 100644 (file)
@@ -33,6 +33,8 @@ struct block_object : public PyObject
   struct objfile *objfile;
 };
 
+static_assert (gdb::is_python_allocatable_v<block_object>);
+
 struct block_syms_iterator_object : public PyObject
 {
   /* The block.  */
@@ -47,6 +49,8 @@ struct block_syms_iterator_object : public PyObject
   block_object *source;
 };
 
+static_assert (gdb::is_python_allocatable_v<block_syms_iterator_object>);
+
 /* Require a valid block.  All access to block_object->block should be
    gated by this call.  */
 #define BLPY_REQUIRE_VALID(block_obj, block)           \
index 68d4f77c608357f6a5a8a1147cef70800f883e27..d1fec6a770062537d75908a8997455a612cbdab0 100644 (file)
@@ -46,6 +46,8 @@ struct gdbpy_breakpoint_location_object : public PyObject
   gdbpy_breakpoint_object *owner;
 };
 
+static_assert (gdb::is_python_allocatable_v<gdbpy_breakpoint_location_object>);
+
 /* Require that BREAKPOINT and LOCATION->OWNER are the same; throw a Python
    exception if they are not.  */
 #define BPLOCPY_REQUIRE_VALID(Breakpoint, Location)                         \
index c515446dad8036953a777537905511c3d446ace5..24938fcda15f44d70373010aab018ce878ef9e70 100644 (file)
@@ -63,6 +63,8 @@ struct cmdpy_object : public PyObject
   struct cmd_list_element *sub_list;
 };
 
+static_assert (gdb::is_python_allocatable_v<cmdpy_object>);
+
 extern PyTypeObject cmdpy_object_type;
 
 /* Constants used by this module.  */
index 971209958cf7da0eff5c4c4f8564f9a043bc06b6..29a42977adc963f54a4c6247586323b05fc98fc7 100644 (file)
@@ -43,6 +43,8 @@ struct colorpy_object : public PyObject
   ui_file_style::color color;
 };
 
+static_assert (gdb::is_python_allocatable_v<colorpy_object>);
+
 extern PyTypeObject colorpy_object_type;
 
 /* See py-color.h.  */
index 43e03ad81f79314b5f220a82c181bbbce21da873..02330b2806818f90ed109eb5ffa5c3738a1fd9a9 100644 (file)
@@ -42,6 +42,8 @@ struct connection_object : public PyObject
   struct process_stratum_target *target;
 };
 
+static_assert (gdb::is_python_allocatable_v<connection_object>);
+
 extern PyTypeObject connection_object_type;
 
 extern PyTypeObject remote_connection_object_type;
index 5acfd98d32620947a6554340538b01efa2525b65..fbefbd267f48030657d804c079c06f8fb6f9940b 100644 (file)
@@ -65,6 +65,8 @@ struct corefile_mapped_file_object : public PyObject
   bool is_main_exec_p;
 };
 
+static_assert (gdb::is_python_allocatable_v<corefile_mapped_file_object>);
+
 extern PyTypeObject corefile_mapped_file_object_type;
 
 /* A gdb.CorefileMappedFileRegion object.  */
@@ -80,6 +82,8 @@ struct corefile_mapped_file_region_object : public PyObject
   ULONGEST file_offset;
 };
 
+static_assert (gdb::is_python_allocatable_v<corefile_mapped_file_region_object>);
+
 extern PyTypeObject corefile_mapped_file_region_object_type;
 
 /* Clear the inferior pointer in a Corefile object OBJ when an inferior is
index d4d50e90035c4b7918ab2436e34f2ad5b751fe36..e5d7174b7d6bf8c0111945faa9596ac187048e62 100644 (file)
@@ -50,6 +50,8 @@ struct disasm_info_object : public PyObject
   struct disasm_info_object *next;
 };
 
+static_assert (gdb::is_python_allocatable_v<disasm_info_object>);
+
 extern PyTypeObject disasm_info_object_type;
 
 /* Implement gdb.disassembler.DisassembleAddressPart type.  An object of
@@ -69,6 +71,8 @@ struct disasm_addr_part_object : public PyObject
   struct gdbarch *gdbarch;
 };
 
+static_assert (gdb::is_python_allocatable_v<disasm_addr_part_object>);
+
 extern PyTypeObject disasm_addr_part_object_type;
 
 /* Implement gdb.disassembler.DisassembleTextPart type.  An object of
@@ -84,6 +88,8 @@ struct disasm_text_part_object : public PyObject
   enum disassembler_style style;
 };
 
+static_assert (gdb::is_python_allocatable_v<disasm_text_part_object>);
+
 extern PyTypeObject disasm_text_part_object_type;
 
 extern PyTypeObject disasm_part_object_type;
@@ -103,6 +109,8 @@ struct disasm_result_object : public PyObject
   std::vector<gdbpy_ref<>> *parts;
 };
 
+static_assert (gdb::is_python_allocatable_v<disasm_result_object>);
+
 extern PyTypeObject disasm_result_object_type;
 
 /* When this is false we fast path out of gdbpy_print_insn, which should
index e44b4b4a7616427dd057482038385cf88e9f1c29..169a5f4a4dbeb2042cd0d5433e2312b3f8e5eeb3 100644 (file)
@@ -32,6 +32,8 @@ struct eventregistry_object : public PyObject
   PyObject *callbacks;
 };
 
+static_assert (gdb::is_python_allocatable_v<eventregistry_object>);
+
 /* Struct holding references to event registries both in python and c.
    This is meant to be a singleton.  */
 
index 374b934a0d9c8f69528e2c0eabe75717195fc676..0a1a0dffbf9059ad69e7e4aaf3d0442ae26ce561 100644 (file)
@@ -44,6 +44,8 @@ struct frame_object : public PyObject
   int frame_id_is_next;
 };
 
+static_assert (gdb::is_python_allocatable_v<frame_object>);
+
 /* Require a valid frame.  This must be called inside a TRY_CATCH, or
    another context in which a gdb exception is allowed.  */
 #define FRAPY_REQUIRE_VALID(frame_obj, frame)          \
index df64d85988fecdc8f4f9e4498d0d80ee0aaf964a..d8dad094ee9acf98691004cc2615a16e8082fe4b 100644 (file)
@@ -32,6 +32,8 @@ PyTypeObject py_insn_type = {
 struct py_insn_obj: public PyObject
 {};
 
+static_assert (gdb::is_python_allocatable_v<py_insn_obj>);
+
 /* Getter function for gdb.Instruction attributes.  */
 
 static PyObject *
index ba3920e07c32705382cbfc25528a10f7c11ec1d3..c9d06338b0a86a92050443555cd2f209974217e1 100644 (file)
@@ -51,6 +51,8 @@ struct lazy_string_object : public PyObject
   PyObject *type;
 };
 
+static_assert (gdb::is_python_allocatable_v<lazy_string_object>);
+
 extern PyTypeObject lazy_string_object_type;
 
 static PyObject *
index d2d85026a321a4b3fbe3586bb0e03a059dccd9c5..1db13af4c12c88077e5e168c757151e535a8973b 100644 (file)
@@ -27,6 +27,8 @@ struct linetable_entry_object : public PyObject
   CORE_ADDR pc;
 };
 
+static_assert (gdb::is_python_allocatable_v<linetable_entry_object>);
+
 extern PyTypeObject linetable_entry_object_type;
 
 struct linetable_object : public PyObject
@@ -37,6 +39,8 @@ struct linetable_object : public PyObject
   PyObject *symtab;
 };
 
+static_assert (gdb::is_python_allocatable_v<linetable_object>);
+
 extern PyTypeObject linetable_object_type;
 
 struct ltpy_iterator_object : public PyObject
@@ -49,6 +53,8 @@ struct ltpy_iterator_object : public PyObject
   PyObject *source;
 };
 
+static_assert (gdb::is_python_allocatable_v<ltpy_iterator_object>);
+
 extern PyTypeObject ltpy_iterator_object_type;
 
 /* Internal helper function to extract gdb.Symtab from a gdb.LineTable
index e3bf5e2ceabdf5235885824282da7ba94dd57779..fa5156b885e514b0452911a6eb20eb607355b9fc 100644 (file)
@@ -31,6 +31,8 @@ struct membuf_object : public PyObject
   CORE_ADDR length;
 };
 
+static_assert (gdb::is_python_allocatable_v<membuf_object>);
+
 extern PyTypeObject membuf_object_type;
 
 /* Wrap BUFFER, ADDRESS, and LENGTH into a gdb.Membuf object.  ADDRESS is
index 73f5297bd70d122dda31c8922151731fa325f7cd..9ae7fc51bee4488dbdcf829af618aabefb5d4ccd 100644 (file)
@@ -74,6 +74,8 @@ struct micmdpy_object : public PyObject
   char *mi_command_name;
 };
 
+static_assert (gdb::is_python_allocatable_v<micmdpy_object>);
+
 /* The MI command implemented in Python.  */
 
 struct mi_command_py : public mi_command
index bc028f1746c6f3fff8307759984e66837f2f844c..35f55c3dbd067a8d099751262270954719e10eab 100644 (file)
@@ -141,6 +141,8 @@ struct parmpy_object : public PyObject
   const char **enumeration;
 };
 
+static_assert (gdb::is_python_allocatable_v<parmpy_object>);
+
 /* Wraps a setting around an existing parmpy_object.  This abstraction
    is used to manipulate the value in S->VALUE in a type safe manner using
    the setting interface.  */
index 72ad5efcf0773604ec3940fc05c37c1e50108dba..d3faf861fcfba7d7cc5789f781a8c220d39c455c 100644 (file)
@@ -784,6 +784,8 @@ gdbpy_get_print_options (value_print_options *opts)
 struct printer_object : public PyObject
 {};
 
+static_assert (gdb::is_python_allocatable_v<printer_object>);
+
 /* The ValuePrinter type object.  */
 PyTypeObject printer_object_type =
 {
index f46b434313e7dad7d273d9410e21d8d736da3f87..e60bc44e81fece3fd1da6b5a088ffb00b9e485dc 100644 (file)
@@ -48,6 +48,8 @@ struct btpy_list_object : public PyObject
   PyTypeObject* element_type;
 };
 
+static_assert (gdb::is_python_allocatable_v<btpy_list_object>);
+
 /* Python type for btrace lists.  */
 
 static PyTypeObject btpy_list_type = {
index 3ceedecc16565cf5dc30085c0f454220d1282420..15e1b9f33161cc2ebbeeb8cde75fef7ac47e2cde 100644 (file)
@@ -67,6 +67,8 @@ struct recpy_gap_object : public PyObject
   Py_ssize_t number;
 };
 
+static_assert (gdb::is_python_allocatable_v<recpy_gap_object>);
+
 /* Implementation of record.method.  */
 
 static PyObject *
index 35ab54cf6abf35ebe5925a5e2c1d64005a2946d1..f62fcc2a7e9f8d8ce9682947b00baad2dba4f5cf 100644 (file)
@@ -34,6 +34,8 @@ struct recpy_record_object : public PyObject
   enum record_method method;
 };
 
+static_assert (gdb::is_python_allocatable_v<recpy_record_object>);
+
 /* Python recorded element object.  This is generic enough to represent
    recorded instructions as well as recorded function call segments, hence the
    generic name.  */
@@ -49,6 +51,8 @@ struct recpy_element_object : public PyObject
   Py_ssize_t number;
 };
 
+static_assert (gdb::is_python_allocatable_v<recpy_element_object>);
+
 /* Python RecordInstruction type.  */
 extern PyTypeObject recpy_insn_type;
 
index dc0b14814afb3be951af59718ce9c4bb43c08e51..3d2906fd7f5a0b41435b158b0d259e339a030e45 100644 (file)
@@ -21,6 +21,7 @@
 #define GDB_PYTHON_PY_REF_H
 
 #include "gdbsupport/gdb_ref_ptr.h"
+#include "python-traits.h"
 
 /* A policy class for gdb::ref_ptr for Python reference counting.  */
 struct gdbpy_ref_policy
@@ -102,4 +103,6 @@ struct gdbpy_dict_wrapper : public PyObject
   }
 };
 
+static_assert (gdb::is_python_allocatable_v<gdbpy_dict_wrapper>);
+
 #endif /* GDB_PYTHON_PY_REF_H */
index 14cf19a6e66f5ac5df2c4886dc9c2d2d933056e3..cc7b09079da9ac017dfb2c47587cc9a9c5495eb6 100644 (file)
@@ -45,6 +45,8 @@ struct register_descriptor_iterator_object : public PyObject
   struct gdbarch *gdbarch;
 };
 
+static_assert (gdb::is_python_allocatable_v<register_descriptor_iterator_object>);
+
 extern PyTypeObject register_descriptor_iterator_object_type;
 
 /* A register descriptor.  */
@@ -57,6 +59,8 @@ struct register_descriptor_object : public PyObject
   struct gdbarch *gdbarch;
 };
 
+static_assert (gdb::is_python_allocatable_v<register_descriptor_object>);
+
 extern PyTypeObject register_descriptor_object_type;
 
 /* Structure for iterator over register groups.  */
@@ -69,6 +73,8 @@ struct reggroup_iterator_object : public PyObject
   struct gdbarch *gdbarch;
 };
 
+static_assert (gdb::is_python_allocatable_v<reggroup_iterator_object>);
+
 extern PyTypeObject reggroup_iterator_object_type;
 
 /* A register group object.  */
@@ -78,6 +84,8 @@ struct reggroup_object : public PyObject
   const struct reggroup *reggroup;
 };
 
+static_assert (gdb::is_python_allocatable_v<reggroup_object>);
+
 extern PyTypeObject reggroup_object_type;
 
 /* Map from GDB's internal reggroup objects to the Python
index 60a9ea4792ffaef67d52929496b60e4ead425a4d..c45eba60867e71306e9b8c2be12e009d07bb2534 100644 (file)
@@ -46,6 +46,8 @@ struct style_object : public PyObject
   char *style_name;
 };
 
+static_assert (gdb::is_python_allocatable_v<style_object>);
+
 extern PyTypeObject style_object_type;
 
 /* Initialize the 'style' module.  */
index 76255f53f852303538ee30c14318b1276aa72c79..5b1d843a36f6f4c95ff9e244d128f00ccdade5f6 100644 (file)
@@ -31,6 +31,8 @@ struct symbol_object : public PyObject
   struct symbol *symbol;
 };
 
+static_assert (gdb::is_python_allocatable_v<symbol_object>);
+
 /* Require a valid symbol.  All access to symbol_object->symbol should be
    gated by this call.  */
 #define SYMPY_REQUIRE_VALID(symbol_obj, symbol)                \
index f1fbfa58d652588f1abc43080b41067823fc92e8..9a343167624c40391eebf91f88c7db3316c4c353 100644 (file)
@@ -32,6 +32,8 @@ struct symtab_object : public PyObject
   struct symtab *symtab;
 };
 
+static_assert (gdb::is_python_allocatable_v<symtab_object>);
+
 extern PyTypeObject symtab_object_type;
 static const gdbpy_registry<gdbpy_memoizing_registry_storage<symtab_object,
   symtab, &symtab_object::symtab>> stpy_registry;
@@ -61,6 +63,8 @@ struct sal_object : public PyObject
   sal_object *next;
 };
 
+static_assert (gdb::is_python_allocatable_v<sal_object>);
+
 /* This is called when an objfile is about to be freed.  Invalidate
    the sal object as further actions on the sal would result in bad
    data.  All access to obj->sal should be gated by
index 111e0f4c9e3a7516a45c5c4a38287f1de3b16de6..ca0fb89ea4986b5b4158c0729fb3be42667e77c6 100644 (file)
@@ -53,6 +53,8 @@ struct gdbpy_tui_window: public PyObject
   bool is_valid () const;
 };
 
+static_assert (gdb::is_python_allocatable_v<gdbpy_tui_window>);
+
 extern PyTypeObject gdbpy_tui_window_object_type;
 
 /* A TUI window written in Python.  */
index cbb984309eb45cbb16d5cbb3a034f217b9993801..c002d97d0c6487aa180c7fae82b2e7c3c172a313 100644 (file)
@@ -33,6 +33,8 @@ struct type_object : public PyObject
   struct type *type;
 };
 
+static_assert (gdb::is_python_allocatable_v<type_object>);
+
 extern PyTypeObject type_object_type;
 
 /* A Field object.  */
@@ -52,6 +54,8 @@ struct typy_iterator_object : public PyObject
   type_object *source;
 };
 
+static_assert (gdb::is_python_allocatable_v<typy_iterator_object>);
+
 extern PyTypeObject type_iterator_object_type;
 
 /* This is used to initialize various gdb.TYPE_ constants.  */
index 6bff66849dfffb3eb034340dcee390d11c44857f..eef926c6dfa3ae8dae8fb24cb60549348b33a166 100644 (file)
@@ -104,6 +104,8 @@ struct unwind_info_object : public PyObject
   std::vector<saved_reg> *saved_regs;
 };
 
+static_assert (gdb::is_python_allocatable_v<unwind_info_object>);
+
 /* The data we keep for a frame we can unwind: frame ID and an array of
    (register_number, register_value) pairs.  */
 
index 04b34758b90f4d34348f037db916b56df5d87cf3..5b38110396e464a979d0e48d1bef58d08b6ce62d 100644 (file)
@@ -64,6 +64,8 @@ struct value_object : public PyObject
   PyObject *content_bytes;
 };
 
+static_assert (gdb::is_python_allocatable_v<value_object>);
+
 /* List of all values which are currently exposed to Python. It is
    maintained so that when an objfile is discarded, preserve_values
    can copy the values' types if needed.  */
index 693829e920d575ac601a76f0270ad67d9d308fc0..82f3262ae07927ad5c73e8290fa0ee12b3f6d4e1 100644 (file)
@@ -367,6 +367,8 @@ struct gdbpy_breakpoint_object : public PyObject
   int is_finish_bp;
 };
 
+static_assert (gdb::is_python_allocatable_v<gdbpy_breakpoint_object>);
+
 /* Require that BREAKPOINT be a valid breakpoint ID; throw a Python
    exception if it is invalid.  */
 #define BPPY_REQUIRE_VALID(Breakpoint)                                  \
diff --git a/gdb/python/python-traits.h b/gdb/python/python-traits.h
new file mode 100644 (file)
index 0000000..d1d3053
--- /dev/null
@@ -0,0 +1,93 @@
+/* Type traits relating to GDB's Python integration.
+
+   Copyright (C) 2008-2026 Free Software Foundation, Inc.
+
+   This file is part of GDB.
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
+
+#ifndef GDB_PYTHON_PYTHON_TRAITS_H
+#define GDB_PYTHON_PYTHON_TRAITS_H
+
+namespace gdb
+{
+/* All of our custom Python types are created as structs, like this:
+
+   struct some_new_type : public PyObject
+   {
+     ... various fields ...
+   };
+
+   Then instances of this struct are created by calling PyObject_New,
+   either directly within GDB's C++ code, or within Python when a user's
+   Python script creates an instance of that class.
+
+   The problem is that Python is written in C, and PyObject_New doesn't
+   call any constructors for `some_new_type`, nor for any of the fields
+   within `some_new_type`.
+
+   If `some_new_type` is Plain Old Data (POD), then this is fine.  Or, to
+   be more C++ specific, if `some_new_type` is trivially default
+   constructable, then we're fine.
+
+   But if a field within `some_new_type` has a non-trivial constructor,
+   then we're in trouble as that constructor will never be run.
+
+   An example of a problematic field type is frame_info_ptr.  The
+   constructor for this type registers the new object with a central
+   management object, recording the `this` pointer, using this type within
+   `some_new_type` will not work as expected; frame invalidation will not
+   show up within the frame_info_ptr as you might expect.
+
+   And so, this type trait exists.  Whenever a struct is created to define
+   a new Python type we should add a line like:
+
+     static_assert (gdb::is_python_allocatable_v<some_new_type>);
+
+   This will fail if any field of `some_new_type` is unsuitable for this
+   use.
+
+   We don't actually check is_trivially_default_constructible here.  Some
+   types, e.g. ui_file_style::color, have non-trivial (or no default)
+   constructors, but are still safe to use within `some_new_type` because
+   their constructors just initialise data fields; there's nothing
+   "special" that the constructor does that cannot be achieved by
+   assigning the fields after creation with PyObject_New.
+
+   What actually matters is that the type is trivially destructible
+   (Python won't call C++ destructors, so destructors with side effects,
+   like deregistering from a list, would be skipped) and trivially
+   copyable (Python may copy objects with memcpy).  Types like
+   frame_info_ptr, whose constructors and destructors have side effects
+   such as registering with a central management object, will be caught
+   because they are neither trivially destructible nor trivially copyable.
+   Types like ui_file_style are trivially destructible and copyable, so
+   pass this trait.  */
+
+template<typename T>
+struct is_python_allocatable
+{
+  static constexpr bool value =
+    std::is_trivially_destructible_v<T>
+    && std::is_trivially_copyable_v<T>;
+};
+
+/* Helper for the above trait to make it more usable.  */
+
+template<typename T>
+inline constexpr bool is_python_allocatable_v
+  = is_python_allocatable<T>::value;
+}
+
+#endif /* GDB_PYTHON_PYTHON_TRAITS_H */