Thrift
  1. Thrift
  2. THRIFT-153

Invalid code generated for string constants containing single quotes (')

    Details

    • Patch Info:
      Patch Available

      Description

      If I declare a constant in a Thrift iDL like this:

      const string HAMMER_TIME = "Can't touch this";

      The generated Python sources generate a constant like this:

      HAMMER_TIME = 'Can't touch this';

      This doesn't work because the apostrophe terminates the string constant.

      Since Python supports string constants using either ' or ", an easy fix is just to use " around Python constants instead:

      Index: compiler/cpp/src/generate/t_py_generator.cc
      ===================================================================
      — compiler/cpp/src/generate/t_py_generator.cc (revision 701711)
      +++ compiler/cpp/src/generate/t_py_generator.cc (working copy)
      @@ -363,7 +363,7 @@
      t_base_type::t_base tbase = ((t_base_type*)type)->get_base();
      switch (tbase) {
      case t_base_type::TYPE_STRING:

      • out << "'" << value->get_string() << "'";
        + out << '"' << value->get_string() << '"';
        break;
        case t_base_type::TYPE_BOOL:
        out << (value->get_integer() > 0 ? "True" : "False");

        Activity

        Dave Engberg created issue -
        Dave Engberg made changes -
        Field Original Value New Value
        Attachment t_py_generator.cc.diff [ 12391476 ]
        Alexander Shigin made changes -
        Comment [ It's better to use triple quote in this case, because your patch will break generated code if constant contains double quote.

        Here is my version of the patch.
        {{{
        Index: compiler/cpp/src/generate/t_py_generator.cc
        ===================================================================
        --- compiler/cpp/src/generate/t_py_generator.cc (revision 701711)
        +++ compiler/cpp/src/generate/t_py_generator.cc (working copy)
        @@ -363,7 +363,7 @@
             t_base_type::t_base tbase = ((t_base_type*)type)->get_base();
             switch (tbase) {
             case t_base_type::TYPE_STRING:
        - out << "'" << value->get_string() << "'";
        + out << "'''" << value->get_string() << "'''";
               break;
             case t_base_type::TYPE_BOOL:
               out << (value->get_integer() > 0 ? "True" : "False");
        }}} ]
        Alexander Shigin made changes -
        Attachment thrift-const-escaping.thrift [ 12403619 ]
        Alexander Shigin made changes -
        Component/s Compiler (C#) [ 12312361 ]
        Component/s Compiler (C++) [ 12312317 ]
        Component/s Compiler (Cocoa) [ 12312397 ]
        Component/s Compiler (Erlang) [ 12312389 ]
        Component/s Compiler (General) [ 12312311 ]
        Component/s Compiler (Haskell) [ 12312703 ]
        Component/s Compiler (Java) [ 12312318 ]
        Component/s Compiler (Perl) [ 12312396 ]
        Component/s Compiler (PHP) [ 12312432 ]
        Component/s Compiler (Ruby) [ 12312320 ]
        Alexander Shigin made changes -
        Attachment thrift-const-escaping-v2.thrift [ 12403703 ]
        David Reiss made changes -
        Spiros Eliopoulos made changes -
        David Reiss made changes -
        Status Open [ 1 ] Resolved [ 5 ]
        Assignee Alexander Shigin [ shigin ]
        Resolution Fixed [ 1 ]
        Jake Farrell made changes -
        Status Resolved [ 5 ] Closed [ 6 ]

          People

          • Assignee:
            Alexander Shigin
            Reporter:
            Dave Engberg
          • Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Development