Skip to content

Commit 6291458

Browse files
committed
[lldb/Python] Make use of PythonObject and PythonFormat in callbacks (NFC)
This patch extends the template specialization of PythonFormat structs and makes use of the pre-existing PythonObject class to format arguments and pass them to the right method, before calling it. This is a preparatory patch to merge PythonFormat with SWIGPythonBridge's GetPythonValueFormatString methods. Differential Revision: https://reviews.llvm.org/D138248 Signed-off-by: Med Ismail Bennani <[email protected]>
1 parent 27fddf0 commit 6291458

File tree

2 files changed

+85
-227
lines changed

2 files changed

+85
-227
lines changed

lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.h

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -185,22 +185,34 @@ inline const char *py2_const_cast(const char *s) { return s; }
185185

186186
enum class PyInitialValue { Invalid, Empty };
187187

188+
// DOC: https://docs.python.org/3/c-api/arg.html#building-values
188189
template <typename T, typename Enable = void> struct PythonFormat;
189190

190-
template <> struct PythonFormat<unsigned long long> {
191-
static constexpr char format = 'K';
192-
static auto get(unsigned long long value) { return value; }
191+
template <typename T, char F> struct PassthroughFormat {
192+
static constexpr char format = F;
193+
static constexpr T get(T t) { return t; }
193194
};
194195

195-
template <> struct PythonFormat<long long> {
196-
static constexpr char format = 'L';
197-
static auto get(long long value) { return value; }
198-
};
199-
200-
template <> struct PythonFormat<PyObject *> {
201-
static constexpr char format = 'O';
202-
static auto get(PyObject *value) { return value; }
203-
};
196+
template <> struct PythonFormat<char *> : PassthroughFormat<char *, 's'> {};
197+
template <> struct PythonFormat<char> : PassthroughFormat<char, 'b'> {};
198+
template <>
199+
struct PythonFormat<unsigned char> : PassthroughFormat<unsigned char, 'B'> {};
200+
template <> struct PythonFormat<short> : PassthroughFormat<short, 'h'> {};
201+
template <>
202+
struct PythonFormat<unsigned short> : PassthroughFormat<unsigned short, 'H'> {};
203+
template <> struct PythonFormat<int> : PassthroughFormat<int, 'i'> {};
204+
template <>
205+
struct PythonFormat<unsigned int> : PassthroughFormat<unsigned int, 'I'> {};
206+
template <> struct PythonFormat<long> : PassthroughFormat<long, 'l'> {};
207+
template <>
208+
struct PythonFormat<unsigned long> : PassthroughFormat<unsigned long, 'k'> {};
209+
template <>
210+
struct PythonFormat<long long> : PassthroughFormat<long long, 'L'> {};
211+
template <>
212+
struct PythonFormat<unsigned long long>
213+
: PassthroughFormat<unsigned long long, 'K'> {};
214+
template <>
215+
struct PythonFormat<PyObject *> : PassthroughFormat<PyObject *, 'O'> {};
204216

205217
template <typename T>
206218
struct PythonFormat<

0 commit comments

Comments
 (0)