You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@thrift.apache.org by je...@apache.org on 2022/03/05 08:58:37 UTC

[thrift] branch master updated (5a44db8 -> b8920b0)

This is an automated email from the ASF dual-hosted git repository.

jensg pushed a change to branch master
in repository https://gitbox.apache.org/repos/asf/thrift.git.


    from 5a44db8  THRIFT-5525 use "reuse_objects" instead of "reuse-objects" as a consistent param casing Client: java Patch: Jiayu Liu
     new 7ce2216  Support for Python and enums
     new a0ccc7e  init gen_enum_ to false
     new b89f079  clean up formatting
     new 411df3a  fix broken syntax for old style enum
     new c4e5dba  match existing style for setting default value of gen_enum_
     new 8d6694d  don't output empty import line if old style enums are used
     new d0e49f0  add tests
     new b8920b0  update makefile

The 8 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 compiler/cpp/src/thrift/generate/t_py_generator.cc | 64 +++++++++++++++++-----
 test/py/Makefile.am                                | 10 +++-
 test/py/RunClientServer.py                         |  2 +-
 test/py/SerializationTest.py                       | 14 ++++-
 test/py/generate.cmake                             |  4 ++
 5 files changed, 78 insertions(+), 16 deletions(-)

[thrift] 05/08: match existing style for setting default value of gen_enum_

Posted by je...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

jensg pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/thrift.git

commit c4e5dbac2ad1c0020a58dae4968d0ac7dcc188f1
Author: Kevin Wojniak <ka...@users.noreply.github.com>
AuthorDate: Sat Jan 22 20:09:37 2022 -0800

    match existing style for setting default value of gen_enum_
---
 compiler/cpp/src/thrift/generate/t_py_generator.cc | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/compiler/cpp/src/thrift/generate/t_py_generator.cc b/compiler/cpp/src/thrift/generate/t_py_generator.cc
index 38792fd..f330b50 100644
--- a/compiler/cpp/src/thrift/generate/t_py_generator.cc
+++ b/compiler/cpp/src/thrift/generate/t_py_generator.cc
@@ -64,6 +64,7 @@ public:
     gen_zope_interface_ = false;
     gen_twisted_ = false;
     gen_dynamic_ = false;
+    gen_enum_ = false;
     coding_ = "";
     gen_dynbaseclass_ = "";
     gen_dynbaseclass_exc_ = "";
@@ -297,7 +298,7 @@ private:
    * True if we should generate new-style classes.
    */
   bool gen_newstyle_;
-  bool gen_enum_{false};
+  bool gen_enum_;
 
   /**
   * True if we should generate dynamic style classes.

[thrift] 04/08: fix broken syntax for old style enum

Posted by je...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

jensg pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/thrift.git

commit 411df3aacb4e472f1ddeeada501cadadf4f5c9c2
Author: Kevin Wojniak <ka...@users.noreply.github.com>
AuthorDate: Sat Jan 22 20:06:24 2022 -0800

    fix broken syntax for old style enum
---
 compiler/cpp/src/thrift/generate/t_py_generator.cc | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/compiler/cpp/src/thrift/generate/t_py_generator.cc b/compiler/cpp/src/thrift/generate/t_py_generator.cc
index 34c7bd3..38792fd 100644
--- a/compiler/cpp/src/thrift/generate/t_py_generator.cc
+++ b/compiler/cpp/src/thrift/generate/t_py_generator.cc
@@ -1008,7 +1008,7 @@ void t_py_generator::generate_py_struct_reader(ostream& out, t_struct* tstruct)
   indent_down();
 
   indent(out) << "iprot.readStructBegin()" << endl;
-  
+
   if (is_immutable(tstruct)) {
     for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
       t_field* tfield = *f_iter;
@@ -2287,8 +2287,8 @@ void t_py_generator::generate_deserialize_field(ostream& out,
     if (gen_enum_) {
       indent(out) << name << " = " << type_name(type) << "(iprot.readI32()).name";
     } else {
-      out << "readI32()";
-    }      
+      indent(out) << name << " = iprot.readI32()";
+    }
     out << endl;
   } else {
     printf("DO NOT KNOW HOW TO DESERIALIZE FIELD '%s' TYPE '%s'\n",

[thrift] 02/08: init gen_enum_ to false

Posted by je...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

jensg pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/thrift.git

commit a0ccc7e5fae124d6fb43f77bf1f0e7bb12c176f9
Author: Kevin Wojniak <ka...@users.noreply.github.com>
AuthorDate: Sun Dec 12 13:12:18 2021 -0800

    init gen_enum_ to false
---
 compiler/cpp/src/thrift/generate/t_py_generator.cc | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/compiler/cpp/src/thrift/generate/t_py_generator.cc b/compiler/cpp/src/thrift/generate/t_py_generator.cc
index af5821b..164365b 100644
--- a/compiler/cpp/src/thrift/generate/t_py_generator.cc
+++ b/compiler/cpp/src/thrift/generate/t_py_generator.cc
@@ -297,7 +297,7 @@ private:
    * True if we should generate new-style classes.
    */
   bool gen_newstyle_;
-  bool gen_enum_;
+  bool gen_enum_{false};
 
   /**
   * True if we should generate dynamic style classes.

[thrift] 07/08: add tests

Posted by je...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

jensg pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/thrift.git

commit d0e49f01ca40154348d2deaf229541e2f9ee2988
Author: Kevin Wojniak <ka...@users.noreply.github.com>
AuthorDate: Sat Jan 22 21:04:30 2022 -0800

    add tests
---
 test/py/RunClientServer.py   |  2 +-
 test/py/SerializationTest.py | 14 +++++++++++++-
 test/py/generate.cmake       |  4 ++++
 3 files changed, 18 insertions(+), 2 deletions(-)

diff --git a/test/py/RunClientServer.py b/test/py/RunClientServer.py
index 56a408e..cb0decf 100755
--- a/test/py/RunClientServer.py
+++ b/test/py/RunClientServer.py
@@ -259,7 +259,7 @@ def main():
     parser = OptionParser()
     parser.add_option('--all', action="store_true", dest='all')
     parser.add_option('--genpydirs', type='string', dest='genpydirs',
-                      default='default,slots,oldstyle,no_utf8strings,dynamic,dynamicslots',
+                      default='default,slots,oldstyle,no_utf8strings,dynamic,dynamicslots,enum',
                       help='directory extensions for generated code, used as suffixes for \"gen-py-*\" added sys.path for individual tests')
     parser.add_option("--port", type="int", dest="port", default=9090,
                       help="port number for server to listen on")
diff --git a/test/py/SerializationTest.py b/test/py/SerializationTest.py
index ef79835..f47c3d4 100755
--- a/test/py/SerializationTest.py
+++ b/test/py/SerializationTest.py
@@ -432,7 +432,19 @@ class SerializersTest(unittest.TestCase):
         self.assertEquals(obj, objcopy)
 
         # test enums
-        for num, name in Numberz._VALUES_TO_NAMES.items():
+        def _enumerate_enum(enum_class):
+            if hasattr(enum_class, '_VALUES_TO_NAMES'):
+                # old-style enums
+                for num, name in enum_class._VALUES_TO_NAMES.items():
+                    yield (num, name)
+            else:
+                # assume Python 3.4+ IntEnum-based
+                from enum import IntEnum
+                self.assertTrue((issubclass(enum_class, IntEnum)))
+                for num in enum_class:
+                    yield (num.value, num.name)
+
+        for num, name in _enumerate_enum(Numberz):
             obj = Bonk(message='enum Numberz value %d is string %s' % (num, name), type=num)
             objcopy = Bonk()
             deserialize(objcopy, serialize(obj))
diff --git a/test/py/generate.cmake b/test/py/generate.cmake
index 4ed14cc..e6a6735 100644
--- a/test/py/generate.cmake
+++ b/test/py/generate.cmake
@@ -13,6 +13,7 @@ generate(${MY_PROJECT_DIR}/test/ThriftTest.thrift py:old_style gen-py-oldstyle)
 generate(${MY_PROJECT_DIR}/test/ThriftTest.thrift py:no_utf8strings gen-py-no_utf8strings)
 generate(${MY_PROJECT_DIR}/test/ThriftTest.thrift py:dynamic gen-py-dynamic)
 generate(${MY_PROJECT_DIR}/test/ThriftTest.thrift py:dynamic,slots gen-py-dynamicslots)
+generate(${MY_PROJECT_DIR}/test/ThriftTest.thrift py:enum gen-py-enum)
 
 generate(${MY_PROJECT_DIR}/test/DebugProtoTest.thrift py gen-py-default)
 generate(${MY_PROJECT_DIR}/test/DebugProtoTest.thrift py:slots gen-py-slots)
@@ -20,6 +21,7 @@ generate(${MY_PROJECT_DIR}/test/DebugProtoTest.thrift py:old_style gen-py-oldsty
 generate(${MY_PROJECT_DIR}/test/DebugProtoTest.thrift py:no_utf8strings gen-py-no_utf8strings)
 generate(${MY_PROJECT_DIR}/test/DebugProtoTest.thrift py:dynamic gen-py-dynamic)
 generate(${MY_PROJECT_DIR}/test/DebugProtoTest.thrift py:dynamic,slots gen-py-dynamicslots)
+generate(${MY_PROJECT_DIR}/test/DebugProtoTest.thrift py:enum gen-py-enum)
 
 generate(${MY_PROJECT_DIR}/test/DoubleConstantsTest.thrift py gen-py-default)
 generate(${MY_PROJECT_DIR}/test/DoubleConstantsTest.thrift py:slots gen-py-slots)
@@ -27,6 +29,7 @@ generate(${MY_PROJECT_DIR}/test/DoubleConstantsTest.thrift py:old_style gen-py-o
 generate(${MY_PROJECT_DIR}/test/DoubleConstantsTest.thrift py:no_utf8strings gen-py-no_utf8strings)
 generate(${MY_PROJECT_DIR}/test/DoubleConstantsTest.thrift py:dynamic gen-py-dynamic)
 generate(${MY_PROJECT_DIR}/test/DoubleConstantsTest.thrift py:dynamic,slots gen-py-dynamicslots)
+generate(${MY_PROJECT_DIR}/test/DoubleConstantsTest.thrift py:enum gen-py-enum)
 
 generate(${MY_PROJECT_DIR}/test/Recursive.thrift py gen-py-default)
 generate(${MY_PROJECT_DIR}/test/Recursive.thrift py:slots gen-py-slots)
@@ -34,3 +37,4 @@ generate(${MY_PROJECT_DIR}/test/Recursive.thrift py:old_style gen-py-oldstyle)
 generate(${MY_PROJECT_DIR}/test/Recursive.thrift py:no_utf8strings gen-py-no_utf8strings)
 generate(${MY_PROJECT_DIR}/test/Recursive.thrift py:dynamic gen-py-dynamic)
 generate(${MY_PROJECT_DIR}/test/Recursive.thrift py:dynamic,slots gen-py-dynamicslots)
+generate(${MY_PROJECT_DIR}/test/Recursive.thrift py:enum gen-py-enum)

[thrift] 01/08: Support for Python and enums

Posted by je...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

jensg pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/thrift.git

commit 7ce2216e3601252c46ae4a617e0630c06dc5b083
Author: Constantinos Dimitriou <ko...@gmail.com>
AuthorDate: Tue Apr 23 23:36:11 2019 -0700

    Support for Python and enums
    
    In the spirit and steps of https://grokbase.com/t/thrift/user/13614a6xd1/introspection-of-thrift-enums-in-python
    
    (cherry picked from commit 4a8beb65f82a9525c5835cf93b664b7ac04ff076)
---
 compiler/cpp/src/thrift/generate/t_py_generator.cc | 58 ++++++++++++++++++----
 1 file changed, 47 insertions(+), 11 deletions(-)

diff --git a/compiler/cpp/src/thrift/generate/t_py_generator.cc b/compiler/cpp/src/thrift/generate/t_py_generator.cc
index fe40fc2..af5821b 100644
--- a/compiler/cpp/src/thrift/generate/t_py_generator.cc
+++ b/compiler/cpp/src/thrift/generate/t_py_generator.cc
@@ -72,7 +72,9 @@ public:
     import_dynbase_ = "";
     package_prefix_ = "";
     for( iter = parsed_options.begin(); iter != parsed_options.end(); ++iter) {
-      if( iter->first.compare("new_style") == 0) {
+      if( iter->first.compare("enum") == 0) {
+        gen_enum_ = true;
+      } else if( iter->first.compare("new_style") == 0) {
         pwarning(0, "new_style is enabled by default, so the option will be removed in the near future.\n");
       } else if( iter->first.compare("old_style") == 0) {
         gen_newstyle_ = false;
@@ -295,6 +297,7 @@ private:
    * True if we should generate new-style classes.
    */
   bool gen_newstyle_;
+  bool gen_enum_;
 
   /**
   * True if we should generate dynamic style classes.
@@ -463,6 +466,8 @@ string t_py_generator::py_imports() {
      << "from thrift.protocol.TProtocol import TProtocolException"
      << endl
      << "from thrift.TRecursive import fix_spec"
+     << endl
+     << (gen_enum_ ? "from enum import IntEnum" : "")
      << endl;
 
   if (gen_utf8strings_) {
@@ -502,9 +507,22 @@ void t_py_generator::generate_typedef(t_typedef* ttypedef) {
  */
 void t_py_generator::generate_enum(t_enum* tenum) {
   std::ostringstream to_string_mapping, from_string_mapping;
+  std::string base_class;
+
+  if (gen_enum_) {
+    base_class = "IntEnum";
+  } else if (gen_newstyle_) {
+    base_class = "object";
+  } else if (gen_dynamic_) {
+    base_class = gen_dynbaseclass_;
+  }
 
-  f_types_ << endl << endl << "class " << tenum->get_name() << (gen_newstyle_ ? "(object)" : "")
-           << (gen_dynamic_ ? "(" + gen_dynbaseclass_ + ")" : "") << ":" << endl;
+  f_types_ << endl
+           << endl
+           << "class " << tenum->get_name()
+           << (base_class.empty() ? "" : "(" + base_class + ")")
+           << ":"
+           << endl;
   indent_up();
   generate_python_docstring(f_types_, tenum);
 
@@ -528,7 +546,9 @@ void t_py_generator::generate_enum(t_enum* tenum) {
 
   indent_down();
   f_types_ << endl;
-  f_types_ << to_string_mapping.str() << endl << from_string_mapping.str();
+  if (!gen_enum_) {
+    f_types_ << to_string_mapping.str() << endl << from_string_mapping.str();
+  }
 }
 
 /**
@@ -581,7 +601,14 @@ string t_py_generator::render_const_value(t_type* type, t_const_value* value) {
       throw "compiler error: no const of base type " + t_base_type::t_base_name(tbase);
     }
   } else if (type->is_enum()) {
-    out << value->get_integer();
+    out << indent();
+    int int_val = value->get_integer();
+    if (gen_enum_) {
+      t_enum_value* enum_val = ((t_enum*)type)->get_constant_by_value(int_val);
+      out << type->get_name() << "." << enum_val->get_name();
+    } else {
+      out << int_val;
+    }
   } else if (type->is_struct() || type->is_xception()) {
     out << type_name(type) << "(**{" << endl;
     indent_up();
@@ -2216,7 +2243,7 @@ void t_py_generator::generate_deserialize_field(ostream& out,
     generate_deserialize_struct(out, (t_struct*)type, name);
   } else if (type->is_container()) {
     generate_deserialize_container(out, type, name);
-  } else if (type->is_base_type() || type->is_enum()) {
+  } else if (type->is_base_type()) {
     indent(out) << name << " = iprot.";
 
     if (type->is_base_type()) {
@@ -2254,11 +2281,15 @@ void t_py_generator::generate_deserialize_field(ostream& out,
       default:
         throw "compiler error: no Python name for base type " + t_base_type::t_base_name(tbase);
       }
-    } else if (type->is_enum()) {
-      out << "readI32()";
     }
     out << endl;
-
+  } else if (type->is_enum()) {
+    if (gen_enum_) {
+      indent(out) << name << " = " << type_name(type) << "(iprot.readI32()).name";
+    } else {
+      out << "readI32()";
+    }      
+    out << endl;
   } else {
     printf("DO NOT KNOW HOW TO DESERIALIZE FIELD '%s' TYPE '%s'\n",
            tfield->get_name().c_str(),
@@ -2443,7 +2474,11 @@ void t_py_generator::generate_serialize_field(ostream& out, t_field* tfield, str
         throw "compiler error: no Python name for base type " + t_base_type::t_base_name(tbase);
       }
     } else if (type->is_enum()) {
-      out << "writeI32(" << name << ")";
+      if (gen_enum_){
+        out << "writeI32(" << type_name(type) << "[" << name << "].value)";
+      } else {
+        out << "writeI32(" << name << ")";
+      }
     }
     out << endl;
   } else {
@@ -2801,4 +2836,5 @@ THRIFT_REGISTER_GENERATOR(
     "                     Add an import line to generated code to find the dynbase class.\n"
     "    package_prefix='top.package.'\n"
     "                     Package prefix for generated files.\n"
-    "    old_style:       Deprecated. Generate old-style classes.\n")
+    "    old_style:       Deprecated. Generate old-style classes.\n"
+    "    enum:            Generates Python's IntEnum, connects thrift to python enums . Python 3.4 and higher.\n")

[thrift] 06/08: don't output empty import line if old style enums are used

Posted by je...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

jensg pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/thrift.git

commit 8d6694d60787e2c5e16378e3738a4b31382b6834
Author: Kevin Wojniak <ka...@users.noreply.github.com>
AuthorDate: Sat Jan 22 20:10:26 2022 -0800

    don't output empty import line if old style enums are used
---
 compiler/cpp/src/thrift/generate/t_py_generator.cc | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/compiler/cpp/src/thrift/generate/t_py_generator.cc b/compiler/cpp/src/thrift/generate/t_py_generator.cc
index f330b50..4457871 100644
--- a/compiler/cpp/src/thrift/generate/t_py_generator.cc
+++ b/compiler/cpp/src/thrift/generate/t_py_generator.cc
@@ -467,10 +467,10 @@ string t_py_generator::py_imports() {
      << "from thrift.protocol.TProtocol import TProtocolException"
      << endl
      << "from thrift.TRecursive import fix_spec"
-     << endl
-     << (gen_enum_ ? "from enum import IntEnum" : "")
      << endl;
-
+  if (gen_enum_) {
+    ss << "from enum import IntEnum" << endl;
+  }
   if (gen_utf8strings_) {
     ss << endl << "import sys";
   }

[thrift] 08/08: update makefile

Posted by je...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

jensg pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/thrift.git

commit b8920b01cb72af93a716bb203fcd8a1202936b97
Author: Kevin Wojniak <ka...@users.noreply.github.com>
AuthorDate: Thu Mar 3 20:09:53 2022 -0800

    update makefile
---
 test/py/Makefile.am | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)

diff --git a/test/py/Makefile.am b/test/py/Makefile.am
index 9433e59..57529d7 100644
--- a/test/py/Makefile.am
+++ b/test/py/Makefile.am
@@ -48,7 +48,11 @@ thrift_gen =                                    \
         gen-py-dynamicslots/ThriftTest/__init__.py           \
         gen-py-dynamicslots/DebugProtoTest/__init__.py \
         gen-py-dynamicslots/DoubleConstantsTest/__init__.py \
-        gen-py-dynamicslots/Recursive/__init__.py
+        gen-py-dynamicslots/Recursive/__init__.py \
+        gen-py-enum/ThriftTest/__init__.py \
+        gen-py-enum/DebugProtoTest/__init__.py \
+        gen-py-enum/DoubleConstantsTest/__init__.py \
+        gen-py-enum/Recursive/__init__.py
 
 
 precross: $(thrift_gen)
@@ -93,6 +97,10 @@ gen-py-dynamicslots/%/__init__.py: ../%.thrift $(THRIFT)
 	test -d gen-py-dynamicslots || $(MKDIR_P) gen-py-dynamicslots
 	$(THRIFT) --gen py:dynamic,slots -out gen-py-dynamicslots $<
 
+gen-py-enum/%/__init__.py: ../%.thrift $(THRIFT)
+	test -d gen-py-enum || $(MKDIR_P) gen-py-enum
+	$(THRIFT) --gen py:enum -out gen-py-enum $<
+
 clean-local:
 	$(RM) -r build
 	find . -type f \( -iname "*.pyc" \) | xargs rm -f

[thrift] 03/08: clean up formatting

Posted by je...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

jensg pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/thrift.git

commit b89f07977a2f2007fdac493ce1e3e342ce9c4f3f
Author: Kevin Wojniak <ka...@users.noreply.github.com>
AuthorDate: Sat Jan 22 19:45:52 2022 -0800

    clean up formatting
---
 compiler/cpp/src/thrift/generate/t_py_generator.cc | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/compiler/cpp/src/thrift/generate/t_py_generator.cc b/compiler/cpp/src/thrift/generate/t_py_generator.cc
index 164365b..34c7bd3 100644
--- a/compiler/cpp/src/thrift/generate/t_py_generator.cc
+++ b/compiler/cpp/src/thrift/generate/t_py_generator.cc
@@ -2837,4 +2837,5 @@ THRIFT_REGISTER_GENERATOR(
     "    package_prefix='top.package.'\n"
     "                     Package prefix for generated files.\n"
     "    old_style:       Deprecated. Generate old-style classes.\n"
-    "    enum:            Generates Python's IntEnum, connects thrift to python enums . Python 3.4 and higher.\n")
+    "    enum:            Generates Python's IntEnum, connects thrift to python enums. Python 3.4 and higher.\n"
+)