]> git.proxmox.com Git - ceph.git/blob - ceph/src/arrow/python/pyarrow/tests/bound_function_visit_strings.pyx
import quincy 17.2.0
[ceph.git] / ceph / src / arrow / python / pyarrow / tests / bound_function_visit_strings.pyx
1 # Licensed to the Apache Software Foundation (ASF) under one
2 # or more contributor license agreements. See the NOTICE file
3 # distributed with this work for additional information
4 # regarding copyright ownership. The ASF licenses this file
5 # to you under the Apache License, Version 2.0 (the
6 # "License"); you may not use this file except in compliance
7 # with the License. You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing,
12 # software distributed under the License is distributed on an
13 # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 # KIND, either express or implied. See the License for the
15 # specific language governing permissions and limitations
16 # under the License.
17
18 # distutils: language=c++
19 # cython: language_level = 3
20
21 import pyarrow as pa
22 from pyarrow.lib cimport *
23 from pyarrow.lib import frombytes, tobytes
24
25 # basic test to roundtrip through a BoundFunction
26
27 ctypedef CStatus visit_string_cb(const c_string&)
28
29 cdef extern from * namespace "arrow::py" nogil:
30 """
31 #include <functional>
32 #include <string>
33 #include <vector>
34
35 #include "arrow/status.h"
36
37 namespace arrow {
38 namespace py {
39
40 Status VisitStrings(const std::vector<std::string>& strs,
41 std::function<Status(const std::string&)> cb) {
42 for (const std::string& str : strs) {
43 RETURN_NOT_OK(cb(str));
44 }
45 return Status::OK();
46 }
47
48 } // namespace py
49 } // namespace arrow
50 """
51 cdef CStatus CVisitStrings" arrow::py::VisitStrings"(
52 vector[c_string], function[visit_string_cb])
53
54
55 cdef void _visit_strings_impl(py_cb, const c_string& s) except *:
56 py_cb(frombytes(s))
57
58
59 def _visit_strings(strings, cb):
60 cdef:
61 function[visit_string_cb] c_cb
62 vector[c_string] c_strings
63
64 c_cb = BindFunction[visit_string_cb](&_visit_strings_impl, cb)
65 for s in strings:
66 c_strings.push_back(tobytes(s))
67
68 check_status(CVisitStrings(c_strings, c_cb))