@@ -79,32 +79,32 @@ const char * ConvertPyUnicodeToUtf8(const void * input, int kind, size_t codepoi
79
79
return output_buffer;
80
80
}
81
81
82
- const char * GetPyUtf8StrData (const py::handle & obj, size_t & buf_len)
82
+ const char * GetPyUtf8StrData (PyObject * obj, size_t & buf_len)
83
83
{
84
84
// See: https://github.com/python/cpython/blob/3.9/Include/cpython/unicodeobject.h#L81
85
- if (PyUnicode_IS_COMPACT_ASCII (obj. ptr () ))
85
+ if (PyUnicode_IS_COMPACT_ASCII (obj))
86
86
{
87
- const char * data = reinterpret_cast <const char *>(PyUnicode_1BYTE_DATA (obj. ptr () ));
88
- buf_len = PyUnicode_GET_LENGTH (obj. ptr () );
87
+ const char * data = reinterpret_cast <const char *>(PyUnicode_1BYTE_DATA (obj));
88
+ buf_len = PyUnicode_GET_LENGTH (obj);
89
89
return data;
90
90
}
91
91
else
92
92
{
93
- PyCompactUnicodeObject * unicode = reinterpret_cast <PyCompactUnicodeObject *>(obj. ptr () );
93
+ PyCompactUnicodeObject * unicode = reinterpret_cast <PyCompactUnicodeObject *>(obj);
94
94
if (unicode->utf8 != nullptr )
95
95
{
96
96
// It's utf8 string, treat it like ASCII
97
97
const char * data = reinterpret_cast <const char *>(unicode->utf8 );
98
98
buf_len = unicode->utf8_length ;
99
99
return data;
100
100
}
101
- else if (PyUnicode_IS_COMPACT (obj. ptr () ))
101
+ else if (PyUnicode_IS_COMPACT (obj))
102
102
{
103
- auto kind = PyUnicode_KIND (obj. ptr () );
103
+ auto kind = PyUnicode_KIND (obj);
104
104
// if (kind == PyUnicode_1BYTE_KIND || kind == PyUnicode_2BYTE_KIND || kind == PyUnicode_4BYTE_KIND)
105
105
// {
106
106
// // always convert it to utf8
107
- // const char * data = PyUnicode_AsUTF8AndSize(obj.ptr() , &unicode->utf8_length);
107
+ // const char * data = PyUnicode_AsUTF8AndSize(obj, &unicode->utf8_length);
108
108
// buf_len = unicode->utf8_length;
109
109
// // set the utf8 buffer back
110
110
// unicode->utf8 = const_cast<char *>(data);
@@ -114,16 +114,16 @@ const char * GetPyUtf8StrData(const py::handle & obj, size_t & buf_len)
114
114
size_t codepoint_cnt;
115
115
116
116
if (kind == PyUnicode_1BYTE_KIND)
117
- data = reinterpret_cast <const char *>(PyUnicode_1BYTE_DATA (obj. ptr () ));
117
+ data = reinterpret_cast <const char *>(PyUnicode_1BYTE_DATA (obj));
118
118
else if (kind == PyUnicode_2BYTE_KIND)
119
- data = reinterpret_cast <const char *>(PyUnicode_2BYTE_DATA (obj. ptr () ));
119
+ data = reinterpret_cast <const char *>(PyUnicode_2BYTE_DATA (obj));
120
120
else if (kind == PyUnicode_4BYTE_KIND)
121
- data = reinterpret_cast <const char *>(PyUnicode_4BYTE_DATA (obj. ptr () ));
121
+ data = reinterpret_cast <const char *>(PyUnicode_4BYTE_DATA (obj));
122
122
else
123
123
throw Exception (ErrorCodes::NOT_IMPLEMENTED, " Unsupported unicode kind {}" , kind);
124
124
// always convert it to utf8, and we can't use as function provided by CPython because it requires GIL
125
125
// holded by the caller. So we have to do it manually with libicu
126
- codepoint_cnt = PyUnicode_GET_LENGTH (obj. ptr () );
126
+ codepoint_cnt = PyUnicode_GET_LENGTH (obj);
127
127
data = ConvertPyUnicodeToUtf8 (data, kind, codepoint_cnt, buf_len);
128
128
unicode->utf8 = const_cast <char *>(data);
129
129
unicode->utf8_length = buf_len;
@@ -133,7 +133,7 @@ const char * GetPyUtf8StrData(const py::handle & obj, size_t & buf_len)
133
133
{
134
134
// always convert it to utf8, but this case is rare, here goes the slow path
135
135
py::gil_scoped_acquire acquire;
136
- const char * data = PyUnicode_AsUTF8AndSize (obj. ptr () , &unicode->utf8_length );
136
+ const char * data = PyUnicode_AsUTF8AndSize (obj, &unicode->utf8_length );
137
137
buf_len = unicode->utf8_length ;
138
138
// set the utf8 buffer back
139
139
unicode->utf8 = const_cast <char *>(data);
@@ -167,8 +167,9 @@ const void * tryGetPyArray(const py::object & obj, py::handle & result, std::str
167
167
{
168
168
// Return the handle of py::array directly
169
169
row_count = py::len (obj);
170
- result = obj;
171
- return obj.cast <py::array>().data ();
170
+ py::array array = obj.cast <py::array>();
171
+ result = array;
172
+ return array.data ();
172
173
}
173
174
else if (type_name == " Series" )
174
175
{
@@ -187,6 +188,8 @@ const void * tryGetPyArray(const py::object & obj, py::handle & result, std::str
187
188
return array.data ();
188
189
}
189
190
191
+ // chdb todo: maybe convert list to py::array?
192
+
190
193
return nullptr ;
191
194
}
192
195
}
0 commit comments