Skip to content

[cppyy] Add converters and low-level views for fixed width integers #18492

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions bindings/pyroot/cppyy/CPyCppyy/src/CallContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@ struct Parameter {
union Value {
bool fBool;
int8_t fInt8;
int16_t fInt16;
int32_t fInt32;
uint8_t fUInt8;
uint16_t fUInt16;
uint32_t fUInt32;
short fShort;
unsigned short fUShort;
int fInt;
Expand Down
44 changes: 43 additions & 1 deletion bindings/pyroot/cppyy/CPyCppyy/src/Converters.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,9 @@ struct CPyCppyy_tagPyCArgObject { // not public (but stable; note that olde
#define ct_c_fcomplex 21
#define ct_c_complex 22
#define ct_c_pointer 23
#define NTYPES 24
#define ct_c_int16 24
#define ct_c_int32 25
#define NTYPES 26

static std::array<const char*, NTYPES> gCTypesNames = {
"c_bool", "c_char", "c_wchar", "c_byte", "c_ubyte", "c_short", "c_ushort", "c_uint16",
Expand Down Expand Up @@ -387,6 +389,10 @@ static inline type CPyCppyy_PyLong_As##name(PyObject* pyobject) \

CPPYY_PYLONG_AS_TYPE(UInt8, uint8_t, 0, UCHAR_MAX)
CPPYY_PYLONG_AS_TYPE(Int8, int8_t, SCHAR_MIN, SCHAR_MAX)
CPPYY_PYLONG_AS_TYPE(UInt16, uint16_t, 0, UINT16_MAX)
CPPYY_PYLONG_AS_TYPE(Int16, int16_t, INT16_MIN, INT16_MAX)
CPPYY_PYLONG_AS_TYPE(UInt32, uint32_t, 0, UINT32_MAX)
CPPYY_PYLONG_AS_TYPE(Int32, int32_t, INT32_MIN, INT32_MAX)
CPPYY_PYLONG_AS_TYPE(UShort, unsigned short, 0, USHRT_MAX)
CPPYY_PYLONG_AS_TYPE(Short, short, SHRT_MIN, SHRT_MAX)
CPPYY_PYLONG_AS_TYPE(StrictInt, int, INT_MIN, INT_MAX)
Expand Down Expand Up @@ -782,6 +788,10 @@ CPPYY_IMPL_BASIC_CONST_CHAR_REFCONVERTER(UChar, unsigned char, c_uchar, 0
CPPYY_IMPL_BASIC_CONST_REFCONVERTER(Bool, bool, c_bool, CPyCppyy_PyLong_AsBool)
CPPYY_IMPL_BASIC_CONST_REFCONVERTER(Int8, int8_t, c_int8, CPyCppyy_PyLong_AsInt8)
CPPYY_IMPL_BASIC_CONST_REFCONVERTER(UInt8, uint8_t, c_uint8, CPyCppyy_PyLong_AsUInt8)
CPPYY_IMPL_BASIC_CONST_REFCONVERTER(Int16, int16_t, c_int16, CPyCppyy_PyLong_AsInt16)
CPPYY_IMPL_BASIC_CONST_REFCONVERTER(UInt16, uint16_t, c_uint16, CPyCppyy_PyLong_AsUInt16)
CPPYY_IMPL_BASIC_CONST_REFCONVERTER(Int32, int32_t, c_int32, CPyCppyy_PyLong_AsInt32)
CPPYY_IMPL_BASIC_CONST_REFCONVERTER(UInt32, uint32_t, c_uint32, CPyCppyy_PyLong_AsUInt32)
CPPYY_IMPL_BASIC_CONST_REFCONVERTER(Short, short, c_short, CPyCppyy_PyLong_AsShort)
CPPYY_IMPL_BASIC_CONST_REFCONVERTER(UShort, unsigned short, c_ushort, CPyCppyy_PyLong_AsUShort)
CPPYY_IMPL_BASIC_CONST_REFCONVERTER(Int, int, c_int, CPyCppyy_PyLong_AsStrictInt)
Expand Down Expand Up @@ -857,6 +867,10 @@ CPPYY_IMPL_REFCONVERTER(SChar, c_byte, signed char, 'b');
CPPYY_IMPL_REFCONVERTER(UChar, c_ubyte, unsigned char, 'B');
CPPYY_IMPL_REFCONVERTER(Int8, c_int8, int8_t, 'b');
CPPYY_IMPL_REFCONVERTER(UInt8, c_uint8, uint8_t, 'B');
CPPYY_IMPL_REFCONVERTER(Int16, c_int16, int16_t, 'h');
CPPYY_IMPL_REFCONVERTER(UInt16, c_uint16, uint16_t, 'H');
CPPYY_IMPL_REFCONVERTER(Int32, c_int32, int32_t, 'i');
CPPYY_IMPL_REFCONVERTER(UInt32, c_uint32, uint32_t, 'I');
CPPYY_IMPL_REFCONVERTER(Short, c_short, short, 'h');
CPPYY_IMPL_REFCONVERTER(UShort, c_ushort, unsigned short, 'H');
CPPYY_IMPL_REFCONVERTER_FROM_MEMORY(Int, c_int);
Expand Down Expand Up @@ -1015,6 +1029,14 @@ CPPYY_IMPL_BASIC_CONVERTER_IB(
Int8, int8_t, long, c_int8, PyInt_FromLong, CPyCppyy_PyLong_AsInt8, 'l')
CPPYY_IMPL_BASIC_CONVERTER_IB(
UInt8, uint8_t, long, c_uint8, PyInt_FromLong, CPyCppyy_PyLong_AsUInt8, 'l')
CPPYY_IMPL_BASIC_CONVERTER_IB(
Int16, int16_t, long, c_int16, PyInt_FromLong, CPyCppyy_PyLong_AsInt16, 'l')
CPPYY_IMPL_BASIC_CONVERTER_IB(
UInt16, uint16_t, long, c_uint16, PyInt_FromLong, CPyCppyy_PyLong_AsUInt16, 'l')
CPPYY_IMPL_BASIC_CONVERTER_IB(
Int32, int32_t, long, c_int32, PyInt_FromLong, CPyCppyy_PyLong_AsInt32, 'l')
CPPYY_IMPL_BASIC_CONVERTER_IB(
UInt32, uint32_t, long, c_uint32, PyInt_FromLong, CPyCppyy_PyLong_AsUInt32, 'l')
CPPYY_IMPL_BASIC_CONVERTER_IB(
Short, short, long, c_short, PyInt_FromLong, CPyCppyy_PyLong_AsShort, 'l')
CPPYY_IMPL_BASIC_CONVERTER_IB(
Expand Down Expand Up @@ -1750,7 +1772,11 @@ CPPYY_IMPL_ARRAY_CONVERTER(UChar, c_ubyte, unsigned char, 'B', )
CPPYY_IMPL_ARRAY_CONVERTER(Byte, c_ubyte, std::byte, 'B', )
#endif
CPPYY_IMPL_ARRAY_CONVERTER(Int8, c_byte, int8_t, 'b', _i8)
CPPYY_IMPL_ARRAY_CONVERTER(Int16, c_int16, int16_t, 'h', _i16)
CPPYY_IMPL_ARRAY_CONVERTER(Int32, c_int32, int32_t, 'i', _i32)
CPPYY_IMPL_ARRAY_CONVERTER(UInt8, c_ubyte, uint8_t, 'B', _i8)
CPPYY_IMPL_ARRAY_CONVERTER(UInt16, c_uint16, uint16_t, 'H', _i16)
CPPYY_IMPL_ARRAY_CONVERTER(UInt32, c_uint32, uint32_t, 'I', _i32)
CPPYY_IMPL_ARRAY_CONVERTER(Short, c_short, short, 'h', )
CPPYY_IMPL_ARRAY_CONVERTER(UShort, c_ushort, unsigned short, 'H', )
CPPYY_IMPL_ARRAY_CONVERTER(Int, c_int, int, 'i', )
Expand Down Expand Up @@ -3466,9 +3492,21 @@ static struct InitConvFactories_t {
gf["int8_t"] = (cf_t)+[](cdims_t) { static Int8Converter c{}; return &c; };
gf["const int8_t&"] = (cf_t)+[](cdims_t) { static ConstInt8RefConverter c{}; return &c; };
gf["int8_t&"] = (cf_t)+[](cdims_t) { static Int8RefConverter c{}; return &c; };
gf["int16_t"] = (cf_t)+[](cdims_t) { static Int16Converter c{}; return &c; };
gf["const int16_t&"] = (cf_t)+[](cdims_t) { static ConstInt16RefConverter c{}; return &c; };
gf["int16_t&"] = (cf_t)+[](cdims_t) { static Int16RefConverter c{}; return &c; };
gf["int32_t"] = (cf_t)+[](cdims_t) { static Int32Converter c{}; return &c; };
gf["const int32_t&"] = (cf_t)+[](cdims_t) { static ConstInt32RefConverter c{}; return &c; };
gf["int32_t&"] = (cf_t)+[](cdims_t) { static Int32RefConverter c{}; return &c; };
gf["uint8_t"] = (cf_t)+[](cdims_t) { static UInt8Converter c{}; return &c; };
gf["const uint8_t&"] = (cf_t)+[](cdims_t) { static ConstUInt8RefConverter c{}; return &c; };
gf["uint8_t&"] = (cf_t)+[](cdims_t) { static UInt8RefConverter c{}; return &c; };
gf["uint16_t"] = (cf_t)+[](cdims_t) { static UInt16Converter c{}; return &c; };
gf["const uint16_t&"] = (cf_t)+[](cdims_t) { static ConstUInt16RefConverter c{}; return &c; };
gf["uint16_t&"] = (cf_t)+[](cdims_t) { static UInt16RefConverter c{}; return &c; };
gf["uint32_t"] = (cf_t)+[](cdims_t) { static UInt32Converter c{}; return &c; };
gf["const uint32_t&"] = (cf_t)+[](cdims_t) { static ConstUInt32RefConverter c{}; return &c; };
gf["uint32_t&"] = (cf_t)+[](cdims_t) { static UInt32RefConverter c{}; return &c; };
gf["short"] = (cf_t)+[](cdims_t) { static ShortConverter c{}; return &c; };
gf["const short&"] = (cf_t)+[](cdims_t) { static ConstShortRefConverter c{}; return &c; };
gf["short&"] = (cf_t)+[](cdims_t) { static ShortRefConverter c{}; return &c; };
Expand Down Expand Up @@ -3521,7 +3559,11 @@ static struct InitConvFactories_t {
gf["std::byte ptr"] = (cf_t)+[](cdims_t d) { return new ByteArrayConverter{d}; };
#endif
gf["int8_t ptr"] = (cf_t)+[](cdims_t d) { return new Int8ArrayConverter{d}; };
gf["int16_t ptr"] = (cf_t)+[](cdims_t d) { return new Int16ArrayConverter{d}; };
gf["int32_t ptr"] = (cf_t)+[](cdims_t d) { return new Int32ArrayConverter{d}; };
gf["uint8_t ptr"] = (cf_t)+[](cdims_t d) { return new UInt8ArrayConverter{d}; };
gf["uint16_t ptr"] = (cf_t)+[](cdims_t d) { return new UInt16ArrayConverter{d}; };
gf["uint32_t ptr"] = (cf_t)+[](cdims_t d) { return new UInt32ArrayConverter{d}; };
gf["short ptr"] = (cf_t)+[](cdims_t d) { return new ShortArrayConverter{d}; };
gf["unsigned short ptr"] = (cf_t)+[](cdims_t d) { return new UShortArrayConverter{d}; };
gf["int ptr"] = (cf_t)+[](cdims_t d) { return new IntArrayConverter{d}; };
Expand Down
12 changes: 12 additions & 0 deletions bindings/pyroot/cppyy/CPyCppyy/src/DeclareConverters.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,11 @@ CPPYY_DECLARE_BASIC_CONVERTER(WChar);
CPPYY_DECLARE_BASIC_CONVERTER(Char16);
CPPYY_DECLARE_BASIC_CONVERTER(Char32);
CPPYY_DECLARE_BASIC_CONVERTER(Int8);
CPPYY_DECLARE_BASIC_CONVERTER(Int16);
CPPYY_DECLARE_BASIC_CONVERTER(Int32);
CPPYY_DECLARE_BASIC_CONVERTER(UInt8);
CPPYY_DECLARE_BASIC_CONVERTER(UInt16);
CPPYY_DECLARE_BASIC_CONVERTER(UInt32);
CPPYY_DECLARE_BASIC_CONVERTER(Short);
CPPYY_DECLARE_BASIC_CONVERTER(UShort);
CPPYY_DECLARE_BASIC_CONVERTER(Int);
Expand All @@ -107,7 +111,11 @@ CPPYY_DECLARE_REFCONVERTER(Char32);
CPPYY_DECLARE_REFCONVERTER(SChar);
CPPYY_DECLARE_REFCONVERTER(UChar);
CPPYY_DECLARE_REFCONVERTER(Int8);
CPPYY_DECLARE_REFCONVERTER(Int16);
CPPYY_DECLARE_REFCONVERTER(Int32);
CPPYY_DECLARE_REFCONVERTER(UInt8);
CPPYY_DECLARE_REFCONVERTER(UInt16);
CPPYY_DECLARE_REFCONVERTER(UInt32);
CPPYY_DECLARE_REFCONVERTER(Short);
CPPYY_DECLARE_REFCONVERTER(UShort);
CPPYY_DECLARE_REFCONVERTER(UInt);
Expand Down Expand Up @@ -214,7 +222,11 @@ CPPYY_DECLARE_ARRAY_CONVERTER(UChar);
CPPYY_DECLARE_ARRAY_CONVERTER(Byte);
#endif
CPPYY_DECLARE_ARRAY_CONVERTER(Int8);
CPPYY_DECLARE_ARRAY_CONVERTER(Int16);
CPPYY_DECLARE_ARRAY_CONVERTER(Int32);
CPPYY_DECLARE_ARRAY_CONVERTER(UInt8);
CPPYY_DECLARE_ARRAY_CONVERTER(UInt16);
CPPYY_DECLARE_ARRAY_CONVERTER(UInt32);
CPPYY_DECLARE_ARRAY_CONVERTER(Short);
CPPYY_DECLARE_ARRAY_CONVERTER(UShort);
CPPYY_DECLARE_ARRAY_CONVERTER(Int);
Expand Down
40 changes: 40 additions & 0 deletions bindings/pyroot/cppyy/CPyCppyy/src/LowLevelViews.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -1200,3 +1200,43 @@ PyObject* CPyCppyy::CreateLowLevelView_i8(uint8_t** address, cdims_t shape) {
LowLevelView* ll = CreateLowLevelViewT<uint8_t>(address, shape, "B", "uint8_t");
CPPYY_RET_W_CREATOR(uint8_t**, CreateLowLevelView_i8);
}

PyObject* CPyCppyy::CreateLowLevelView_i16(int16_t* address, cdims_t shape) {
LowLevelView* ll = CreateLowLevelViewT<int16_t>(address, shape, "h", "int16_t");
CPPYY_RET_W_CREATOR(int16_t*, CreateLowLevelView_i16);
}

PyObject* CPyCppyy::CreateLowLevelView_i16(int16_t** address, cdims_t shape) {
LowLevelView* ll = CreateLowLevelViewT<int16_t>(address, shape, "h", "int16_t");
CPPYY_RET_W_CREATOR(int16_t**, CreateLowLevelView_i16);
}

PyObject* CPyCppyy::CreateLowLevelView_i16(uint16_t* address, cdims_t shape) {
LowLevelView* ll = CreateLowLevelViewT<uint16_t>(address, shape, "H", "uint16_t");
CPPYY_RET_W_CREATOR(uint16_t*, CreateLowLevelView_i16);
}

PyObject* CPyCppyy::CreateLowLevelView_i16(uint16_t** address, cdims_t shape) {
LowLevelView* ll = CreateLowLevelViewT<uint16_t>(address, shape, "H", "uint16_t");
CPPYY_RET_W_CREATOR(uint16_t**, CreateLowLevelView_i16);
}

PyObject* CPyCppyy::CreateLowLevelView_i32(int32_t* address, cdims_t shape) {
LowLevelView* ll = CreateLowLevelViewT<int32_t>(address, shape, "i", "int32_t");
CPPYY_RET_W_CREATOR(int32_t*, CreateLowLevelView_i32);
}

PyObject* CPyCppyy::CreateLowLevelView_i32(int32_t** address, cdims_t shape) {
LowLevelView* ll = CreateLowLevelViewT<int32_t>(address, shape, "i", "int32_t");
CPPYY_RET_W_CREATOR(int32_t**, CreateLowLevelView_i32);
}

PyObject* CPyCppyy::CreateLowLevelView_i32(uint32_t* address, cdims_t shape) {
LowLevelView* ll = CreateLowLevelViewT<uint32_t>(address, shape, "I", "uint32_t");
CPPYY_RET_W_CREATOR(uint32_t*, CreateLowLevelView_i32);
}

PyObject* CPyCppyy::CreateLowLevelView_i32(uint32_t** address, cdims_t shape) {
LowLevelView* ll = CreateLowLevelViewT<uint32_t>(address, shape, "I", "uint32_t");
CPPYY_RET_W_CREATOR(uint32_t**, CreateLowLevelView_i32);
}
9 changes: 9 additions & 0 deletions bindings/pyroot/cppyy/CPyCppyy/src/LowLevelViews.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,15 @@ PyObject* CreateLowLevelView_i8(int8_t*, cdims_t shape);
PyObject* CreateLowLevelView_i8(int8_t**, cdims_t shape);
PyObject* CreateLowLevelView_i8(uint8_t*, cdims_t shape);
PyObject* CreateLowLevelView_i8(uint8_t**, cdims_t shape);
PyObject* CreateLowLevelView_i16(int16_t*, cdims_t shape);
PyObject* CreateLowLevelView_i16(int16_t**, cdims_t shape);
PyObject* CreateLowLevelView_i16(uint16_t*, cdims_t shape);
PyObject* CreateLowLevelView_i16(uint16_t**, cdims_t shape);
PyObject* CreateLowLevelView_i32(int32_t*, cdims_t shape);
PyObject* CreateLowLevelView_i32(int32_t**, cdims_t shape);
PyObject* CreateLowLevelView_i32(uint32_t*, cdims_t shape);
PyObject* CreateLowLevelView_i32(uint32_t**, cdims_t shape);

CPPYY_DECL_VIEW_CREATOR(short);
CPPYY_DECL_VIEW_CREATOR(unsigned short);
CPPYY_DECL_VIEW_CREATOR(int);
Expand Down
Loading
Loading