remove unused ifdef SWIG macro
authorTaku Kudo <taku@google.com>
Wed, 3 Aug 2022 06:45:09 +0000 (15:45 +0900)
committerKentaro Hayashi <kenhys@xdump.org>
Mon, 21 Nov 2022 13:43:46 +0000 (13:43 +0000)
Signed-off-by: Kentaro Hayashi <kenhys@gmail.com>
Gbp-Pq: Name 0015-remove-unused-ifdef-SWIG-macro.patch

python/src/sentencepiece/sentencepiece.i
src/sentencepiece_processor.h

index f3a4f3044cc5a9017053dcc503b7c54f54df33e3..75f62c80c73bdabc3505a7bae6557bbe04feff0d 100644 (file)
@@ -326,6 +326,8 @@ inline void InitNumThreads(const std::vector<T> &ins, int *num_threads) {
 %ignore sentencepiece::SentencePieceProcessor::model_proto;
 %ignore sentencepiece::SentencePieceProcessor::Load;
 %ignore sentencepiece::SentencePieceProcessor::LoadOrDie;
+%ignore sentencepiece::SentencePieceProcessor::SetModel;
+%ignore sentencepiece::SentencePieceProcessor::SetNormalizer;
 %ignore sentencepiece::pretokenizer::PretokenizerForTrainingInterface;
 %ignore sentencepiece::SentenceIterator;
 %ignore sentencepiece::ConvertToUnicodeSpans;
@@ -339,6 +341,9 @@ inline void InitNumThreads(const std::vector<T> &ins, int *num_threads) {
 %ignore sentencepiece::SentencePieceTrainer::SetPretokenizerForTraining;
 %ignore sentencepiece::SentencePieceTrainer::GetPretokenizerForTraining;
 
+%ignore sentencepiece::io::LoadModelProto;
+%ignore sentencepiece::io::SaveModelProto;
+
 %extend sentencepiece::SentencePieceProcessor {
   sentencepiece::util::Status LoadFromFile(absl::string_view arg) {
     return $self->Load(arg);
index d107a2a4d8b515c69da0a151a36322c3b419b291..be9449e5faa5f227741d91b941db5d13cc43a246 100644 (file)
@@ -26,7 +26,7 @@
 namespace absl {
 using std::string_view;
 }  // namespace absl
-#endif
+#endif  // SWIG
 
 namespace sentencepiece {
 namespace util {
@@ -420,36 +420,46 @@ class SentencePieceProcessor {
   virtual util::Status Decode(const std::vector<int> &ids,
                               SentencePieceText *spt) const;
 
-#ifdef SWIGPYTHON
-#define CONVERT_TO_UNICODE_SPAN output.ConvertToUnicodeSpans();
-#define SPP_SWIG_CHECK_AND_THROW \
-  if (!status.ok()) throw status;
+#ifndef SWIGPYTHON
+
+#define DEFINE_SPP_DIRECT_FUNC_IMPL(FuncName, OutType, ...) \
+  OutType output;                                           \
+  const auto status = FuncName(__VA_ARGS__, &output);       \
+  return output;
+
+#define DEFINE_SPP_SERIALIZED_PROTO_IMPL(FuncName, OutType, ...)     \
+  OutType output;                                                    \
+  const auto status = FuncName(__VA_ARGS__, output.mutable_proto()); \
+  return output.SerializeAsString();
+
+#define DEFINE_SPP_IMMUTABLE_PROTO_IMPL(FuncName, OutType, ...)      \
+  OutType output;                                                    \
+  const auto status = FuncName(__VA_ARGS__, output.mutable_proto()); \
+  return output;
+
 #else
-#define CONVERT_TO_UNICODE_SPAN
-#define SPP_SWIG_CHECK_AND_THROW \
-  if (!status.ok()) {            \
-  }
-#endif  // SWIGPYTHON
 
 #define DEFINE_SPP_DIRECT_FUNC_IMPL(FuncName, OutType, ...) \
   OutType output;                                           \
   const auto status = FuncName(__VA_ARGS__, &output);       \
-  SPP_SWIG_CHECK_AND_THROW;                                 \
+  if (!status.ok()) throw status;                           \
   return output;
 
 #define DEFINE_SPP_SERIALIZED_PROTO_IMPL(FuncName, OutType, ...)     \
   OutType output;                                                    \
   const auto status = FuncName(__VA_ARGS__, output.mutable_proto()); \
-  SPP_SWIG_CHECK_AND_THROW;                                          \
+  if (!status.ok()) throw status;                                    \
   return output.SerializeAsString();
 
 #define DEFINE_SPP_IMMUTABLE_PROTO_IMPL(FuncName, OutType, ...)      \
   OutType output;                                                    \
   const auto status = FuncName(__VA_ARGS__, output.mutable_proto()); \
-  CONVERT_TO_UNICODE_SPAN;                                           \
-  SPP_SWIG_CHECK_AND_THROW;                                          \
+  if (!status.ok()) throw status;                                    \
+  output.ConvertToUnicodeSpans();                                    \
   return output;
 
+#endif  // SWIGPYTHON
+
   //////////////////////////////////////////////////////////////
   // Handy methods that return the result directly.
   // These functions ignore internal errors.
@@ -664,7 +674,6 @@ class SentencePieceProcessor {
   // Returns PAD (<pad>) id.
   virtual int pad_id() const;
 
-#ifndef SWIG
   //////////////////////////////////////////////////////////////
   // Model management.
   //
@@ -673,7 +682,6 @@ class SentencePieceProcessor {
 
   // Allows injection of a normalizer instance. `normalizer` is moved.
   void SetNormalizer(std::unique_ptr<normalizer::Normalizer> &&normalizer);
-#endif  // SWIG
 
   // Returns immutable model proto. Useful to obtain extended
   // or experimental parameters encoded in model_proto.
@@ -715,7 +723,6 @@ class SentencePieceProcessor {
 // std::random_device.
 void SetRandomGeneratorSeed(unsigned int seed);
 
-#ifndef SWIG
 // IO related functions to absorb model formats.
 namespace io {
 // Loads `model_proto` from `filename`.
@@ -730,6 +737,5 @@ util::Status LoadModelProto(absl::string_view, ModelProto *model_proto);
 // Saves `model_proto` as `filename`.
 util::Status SaveModelProto(absl::string_view, const ModelProto &model_proto);
 }  // namespace io
-#endif  // SWIG
 }  // namespace sentencepiece
 #endif  // SENTENCEPIECE_PROCESSOR_H_