• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2020 The TensorFlow Authors. All Rights Reserved.
2 
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6 
7     http://www.apache.org/licenses/LICENSE-2.0
8 
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15 
16 #include <Python.h>
17 
18 #include <vector>
19 
20 #include "absl/types/span.h"
21 #include "pybind11/pybind11.h"
22 #include "pybind11/pytypes.h"
23 #include "pybind11/stl.h"
24 #include "tensorflow/python/util/function_parameter_canonicalizer.h"
25 
26 namespace py = pybind11;
27 
28 class FunctionParameterCanonicalizerWrapper {
29  public:
FunctionParameterCanonicalizerWrapper(absl::Span<const char * > arg_names,absl::Span<PyObject * > defaults)30   FunctionParameterCanonicalizerWrapper(absl::Span<const char*> arg_names,
31                                         absl::Span<PyObject*> defaults)
32       : function_parameter_canonicalizer_(arg_names, defaults) {}
33 
34   tensorflow::FunctionParameterCanonicalizer function_parameter_canonicalizer_;
35 };
36 
PYBIND11_MODULE(_function_parameter_canonicalizer_binding_for_test,m)37 PYBIND11_MODULE(_function_parameter_canonicalizer_binding_for_test, m) {
38   py::class_<FunctionParameterCanonicalizerWrapper>(
39       m, "FunctionParameterCanonicalizer")
40       .def(py::init([](std::vector<std::string> arg_names, py::tuple defaults) {
41         std::vector<const char*> arg_names_c_str;
42         for (const std::string& name : arg_names)
43           arg_names_c_str.emplace_back(name.c_str());
44 
45         tensorflow::Safe_PyObjectPtr defaults_fast(
46             PySequence_Fast(defaults.ptr(), "Expected tuple"));
47         if (!defaults) throw py::error_already_set();
48         PyObject** default_items = PySequence_Fast_ITEMS(defaults_fast.get());
49         return new FunctionParameterCanonicalizerWrapper(
50             absl::MakeSpan(arg_names_c_str),
51             absl::MakeSpan(default_items,
52                            PySequence_Fast_GET_SIZE(defaults_fast.get())));
53       }))
54       .def("canonicalize", [](FunctionParameterCanonicalizerWrapper& self,
55                               py::args args, py::kwargs kwargs) {
56         std::vector<PyObject*> result_raw(
57             self.function_parameter_canonicalizer_.GetArgSize());
58 
59         bool is_suceeded = self.function_parameter_canonicalizer_.Canonicalize(
60             args.ptr(), kwargs.ptr(), absl::MakeSpan(result_raw));
61 
62         if (!is_suceeded) {
63           CHECK(PyErr_Occurred());
64           throw py::error_already_set();
65         }
66 
67         py::list result;
68         for (PyObject* obj : result_raw) result.append(obj);
69         return result;
70       });
71 }
72