1 // Copyright (c) 2006- Facebook
2 // Distributed under the Thrift Software License
4 // See accompanying file LICENSE or visit the Thrift site at:
5 // http://developers.facebook.com/thrift/
12 #include "t_py_generator.h"
16 * Prepares for file generation by opening up the necessary file output
19 * @param tprogram The program to generate
21 void t_py_generator::init_generator() {
22 // Make output directory
23 string module
= get_real_py_module(program_
);
24 package_dir_
= get_out_dir();
26 // TODO: Do better error checking here.
27 mkdir(package_dir_
.c_str(), S_IRWXU
| S_IRWXG
| S_IRWXO
);
28 std::ofstream
init_py((package_dir_
+"/__init__.py").c_str());
33 string::size_type pos
= module
.find('.');
34 if (pos
== string::npos
) {
36 package_dir_
+= module
;
40 package_dir_
+= module
.substr(0, pos
);
41 module
.erase(0, pos
+1);
46 string f_types_name
= package_dir_
+"/"+"ttypes.py";
47 f_types_
.open(f_types_name
.c_str());
49 string f_consts_name
= package_dir_
+"/"+"constants.py";
50 f_consts_
.open(f_consts_name
.c_str());
52 string f_init_name
= package_dir_
+"/__init__.py";
54 f_init
.open(f_init_name
.c_str());
56 "__all__ = ['ttypes', 'constants'";
57 vector
<t_service
*> services
= program_
->get_services();
58 vector
<t_service
*>::iterator sv_iter
;
59 for (sv_iter
= services
.begin(); sv_iter
!= services
.end(); ++sv_iter
) {
60 f_init
<< ", '" << (*sv_iter
)->get_name() << "'";
62 f_init
<< "]" << endl
;
67 py_autogen_comment() << endl
<<
68 py_imports() << endl
<<
69 render_includes() << endl
<<
70 render_fastbinary_includes() <<
74 py_autogen_comment() << endl
<<
75 py_imports() << endl
<<
76 "from ttypes import *" << endl
<<
81 * Renders all the imports necessary for including another Thrift program
83 string
t_py_generator::render_includes() {
84 const vector
<t_program
*>& includes
= program_
->get_includes();
86 for (size_t i
= 0; i
< includes
.size(); ++i
) {
87 result
+= "import " + get_real_py_module(includes
[i
]) + ".ttypes\n";
89 if (includes
.size() > 0) {
96 * Renders all the imports necessary to use the accelerated TBinaryProtocol
98 string
t_py_generator::render_fastbinary_includes() {
100 "from thrift.transport import TTransport\n"
101 "from thrift.protocol import TBinaryProtocol\n"
103 " from thrift.protocol import fastbinary\n"
105 " fastbinary = None\n";
111 string
t_py_generator::py_autogen_comment() {
114 "# Autogenerated by Thrift\n" +
116 "# DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING\n" +
121 * Prints standard thrift imports
123 string
t_py_generator::py_imports() {
125 string("from thrift.Thrift import *");
129 * Closes the type files
131 void t_py_generator::close_generator() {
138 * Generates a typedef. This is not done in Python, types are all implicit.
140 * @param ttypedef The type definition
142 void t_py_generator::generate_typedef(t_typedef
* ttypedef
) {}
145 * Generates code for an enumerated type. Done using a class to scope
148 * @param tenum The enumeration
150 void t_py_generator::generate_enum(t_enum
* tenum
) {
152 "class " << tenum
->get_name() <<
153 (gen_newstyle_
? "(object)" : "") <<
157 vector
<t_enum_value
*> constants
= tenum
->get_constants();
158 vector
<t_enum_value
*>::iterator c_iter
;
160 for (c_iter
= constants
.begin(); c_iter
!= constants
.end(); ++c_iter
) {
161 if ((*c_iter
)->has_value()) {
162 value
= (*c_iter
)->get_value();
168 indent() << (*c_iter
)->get_name() << " = " << value
<< endl
;
176 * Generate a constant value
178 void t_py_generator::generate_const(t_const
* tconst
) {
179 t_type
* type
= tconst
->get_type();
180 string name
= tconst
->get_name();
181 t_const_value
* value
= tconst
->get_value();
183 indent(f_consts_
) << name
<< " = " << render_const_value(type
, value
);
184 f_consts_
<< endl
<< endl
;
188 * Prints the value of a constant with the given type. Note that type checking
189 * is NOT performed in this function as it is always run beforehand using the
190 * validate_types method in main.cc
192 string
t_py_generator::render_const_value(t_type
* type
, t_const_value
* value
) {
193 std::ostringstream out
;
195 if (type
->is_base_type()) {
196 t_base_type::t_base tbase
= ((t_base_type
*)type
)->get_base();
198 case t_base_type::TYPE_STRING
:
199 out
<< "'" << value
->get_string() << "'";
201 case t_base_type::TYPE_BOOL
:
202 out
<< (value
->get_integer() > 0 ? "True" : "False");
204 case t_base_type::TYPE_BYTE
:
205 case t_base_type::TYPE_I16
:
206 case t_base_type::TYPE_I32
:
207 case t_base_type::TYPE_I64
:
208 out
<< value
->get_integer();
210 case t_base_type::TYPE_DOUBLE
:
211 if (value
->get_type() == t_const_value::CV_INTEGER
) {
212 out
<< value
->get_integer();
214 out
<< value
->get_double();
218 throw "compiler error: no const of base type " + t_base_type::t_base_name(tbase
);
220 } else if (type
->is_enum()) {
221 indent(out
) << value
->get_integer();
222 } else if (type
->is_struct() || type
->is_xception()) {
223 out
<< type
->get_name() << "({" << endl
;
225 const vector
<t_field
*>& fields
= ((t_struct
*)type
)->get_members();
226 vector
<t_field
*>::const_iterator f_iter
;
227 const map
<t_const_value
*, t_const_value
*>& val
= value
->get_map();
228 map
<t_const_value
*, t_const_value
*>::const_iterator v_iter
;
229 for (v_iter
= val
.begin(); v_iter
!= val
.end(); ++v_iter
) {
230 t_type
* field_type
= NULL
;
231 for (f_iter
= fields
.begin(); f_iter
!= fields
.end(); ++f_iter
) {
232 if ((*f_iter
)->get_name() == v_iter
->first
->get_string()) {
233 field_type
= (*f_iter
)->get_type();
236 if (field_type
== NULL
) {
237 throw "type error: " + type
->get_name() + " has no field " + v_iter
->first
->get_string();
240 out
<< render_const_value(g_type_string
, v_iter
->first
);
242 out
<< render_const_value(field_type
, v_iter
->second
);
247 } else if (type
->is_map()) {
248 t_type
* ktype
= ((t_map
*)type
)->get_key_type();
249 t_type
* vtype
= ((t_map
*)type
)->get_val_type();
252 const map
<t_const_value
*, t_const_value
*>& val
= value
->get_map();
253 map
<t_const_value
*, t_const_value
*>::const_iterator v_iter
;
254 for (v_iter
= val
.begin(); v_iter
!= val
.end(); ++v_iter
) {
256 out
<< render_const_value(ktype
, v_iter
->first
);
258 out
<< render_const_value(vtype
, v_iter
->second
);
263 } else if (type
->is_list() || type
->is_set()) {
265 if (type
->is_list()) {
266 etype
= ((t_list
*)type
)->get_elem_type();
268 etype
= ((t_set
*)type
)->get_elem_type();
270 if (type
->is_set()) {
275 const vector
<t_const_value
*>& val
= value
->get_list();
276 vector
<t_const_value
*>::const_iterator v_iter
;
277 for (v_iter
= val
.begin(); v_iter
!= val
.end(); ++v_iter
) {
279 out
<< render_const_value(etype
, *v_iter
);
284 if (type
->is_set()) {
293 * Generates a python struct
295 void t_py_generator::generate_struct(t_struct
* tstruct
) {
296 generate_py_struct(tstruct
, false);
300 * Generates a struct definition for a thrift exception. Basically the same
301 * as a struct but extends the Exception class.
303 * @param txception The struct definition
305 void t_py_generator::generate_xception(t_struct
* txception
) {
306 generate_py_struct(txception
, true);
310 * Generates a python struct
312 void t_py_generator::generate_py_struct(t_struct
* tstruct
,
314 generate_py_struct_definition(f_types_
, tstruct
, is_exception
);
318 * Comparator to sort fields in ascending order by key.
319 * Make this a functor instead of a function to help GCC inline it.
320 * The arguments are (const) references to const pointers to const t_fields.
321 * Unfortunately, we cannot declare it within the function. Boo!
322 * http://www.open-std.org/jtc1/sc22/open/n2356/ (paragraph 9).
324 struct FieldKeyCompare
{
325 bool operator()(t_field
const * const & a
, t_field
const * const & b
) {
326 return a
->get_key() < b
->get_key();
331 * Generates a struct definition for a thrift data type.
333 * @param tstruct The struct definition
335 void t_py_generator::generate_py_struct_definition(ofstream
& out
,
340 const vector
<t_field
*>& members
= tstruct
->get_members();
341 vector
<t_field
*>::const_iterator m_iter
;
342 vector
<t_field
*> sorted_members(members
);
343 std::sort(sorted_members
.begin(), sorted_members
.end(), FieldKeyCompare());
346 "class " << tstruct
->get_name();
348 out
<< "(Exception)";
349 } else if (gen_newstyle_
) {
359 Here we generate the structure specification for the fastbinary codec.
360 These specifications have the following structure:
361 thrift_spec -> tuple of item_spec
362 item_spec -> None | (tag, type_enum, name, spec_args, default)
364 type_enum -> TType.I32 | TType.STRING | TType.STRUCT | ...
365 name -> string_literal
366 default -> None # Handled by __init__
367 spec_args -> None # For simple types
368 | (type_enum, spec_args) # Value type for list/set
369 | (type_enum, spec_args, type_enum, spec_args)
370 # Key and value for map
371 | (class_name, spec_args_ptr) # For struct/exception
372 class_name -> identifier # Basically a pointer to the class
373 spec_args_ptr -> expression # just class_name.spec_args
375 TODO(dreiss): Consider making this work for structs with negative tags.
378 // TODO(dreiss): Look into generating an empty tuple instead of None
379 // for structures with no members.
380 // TODO(dreiss): Test encoding of structs where some inner structs
381 // don't have thrift_spec.
382 if (sorted_members
.empty() || (sorted_members
[0]->get_key() >= 0)) {
383 indent(out
) << "thrift_spec = (" << endl
;
386 int sorted_keys_pos
= 0;
387 for (m_iter
= sorted_members
.begin(); m_iter
!= sorted_members
.end(); ++m_iter
) {
389 for (; sorted_keys_pos
!= (*m_iter
)->get_key(); sorted_keys_pos
++) {
390 indent(out
) << "None, # " << sorted_keys_pos
<< endl
;
393 indent(out
) << "(" << (*m_iter
)->get_key() << ", "
394 << type_to_enum((*m_iter
)->get_type()) << ", "
395 << "'" << (*m_iter
)->get_name() << "'" << ", "
396 << type_to_spec_args((*m_iter
)->get_type()) << ", "
399 << " # " << sorted_keys_pos
406 indent(out
) << ")" << endl
<< endl
;
408 indent(out
) << "thrift_spec = None" << endl
;
413 indent() << "def __init__(self, d=None):" << endl
;
416 if (members
.size() == 0) {
420 for (m_iter
= members
.begin(); m_iter
!= members
.end(); ++m_iter
) {
421 // This fills in default values, as opposed to nulls
423 declare_field(*m_iter
) << endl
;
427 "if isinstance(d, dict):" << endl
;
429 for (m_iter
= members
.begin(); m_iter
!= members
.end(); ++m_iter
) {
431 indent() << "if '" << (*m_iter
)->get_name() << "' in d:" << endl
<<
432 indent() << " self." << (*m_iter
)->get_name() << " = d['" << (*m_iter
)->get_name() << "']" << endl
;
442 generate_py_struct_reader(out
, tstruct
);
443 generate_py_struct_writer(out
, tstruct
);
445 // Printing utilities so that on the command line thrift
446 // structs look pretty like dictionaries
448 indent() << "def __str__(self): " << endl
<<
449 indent() << " return str(self.__dict__)" << endl
<<
451 indent() << "def __repr__(self): " << endl
<<
452 indent() << " return repr(self.__dict__)" << endl
<<
455 // Equality and inequality methods that compare by value
457 indent() << "def __eq__(self, other):" << endl
;
460 indent() << "return isinstance(other, self.__class__) and "
461 "self.__dict__ == other.__dict__" << endl
;
466 indent() << "def __ne__(self, other):" << endl
;
469 indent() << "return not (self == other)" << endl
;
477 * Generates the read method for a struct
479 void t_py_generator::generate_py_struct_reader(ofstream
& out
,
481 const vector
<t_field
*>& fields
= tstruct
->get_members();
482 vector
<t_field
*>::const_iterator f_iter
;
485 "def read(self, iprot):" << endl
;
489 "if iprot.__class__ == TBinaryProtocol.TBinaryProtocolAccelerated "
490 "and isinstance(iprot.trans, TTransport.CReadableTransport) "
491 "and self.thrift_spec is not None "
492 "and fastbinary is not None:" << endl
;
496 "fastbinary.decode_binary(self, iprot.trans, (self.__class__, self.thrift_spec))" << endl
;
502 "iprot.readStructBegin()" << endl
;
504 // Loop over reading in fields
506 "while True:" << endl
;
509 // Read beginning field marker
511 "(fname, ftype, fid) = iprot.readFieldBegin()" << endl
;
513 // Check for field STOP marker and break
515 "if ftype == TType.STOP:" << endl
;
521 // Switch statement on the field we are reading
524 // Generate deserialization code for known cases
525 for (f_iter
= fields
.begin(); f_iter
!= fields
.end(); ++f_iter
) {
534 out
<< "fid == " << (*f_iter
)->get_key() << ":" << endl
;
536 indent(out
) << "if ftype == " << type_to_enum((*f_iter
)->get_type()) << ":" << endl
;
538 generate_deserialize_field(out
, *f_iter
, "self.");
541 indent() << "else:" << endl
<<
542 indent() << " iprot.skip(ftype)" << endl
;
546 // In the default case we skip the field
548 indent() << "else:" << endl
<<
549 indent() << " iprot.skip(ftype)" << endl
;
551 // Read field end marker
553 "iprot.readFieldEnd()" << endl
;
558 "iprot.readStructEnd()" << endl
;
564 void t_py_generator::generate_py_struct_writer(ofstream
& out
,
566 string name
= tstruct
->get_name();
567 const vector
<t_field
*>& fields
= tstruct
->get_members();
568 vector
<t_field
*>::const_iterator f_iter
;
571 "def write(self, oprot):" << endl
;
575 "if oprot.__class__ == TBinaryProtocol.TBinaryProtocolAccelerated "
576 "and self.thrift_spec is not None "
577 "and fastbinary is not None:" << endl
;
581 "oprot.trans.write(fastbinary.encode_binary(self, (self.__class__, self.thrift_spec)))" << endl
;
587 "oprot.writeStructBegin('" << name
<< "')" << endl
;
589 for (f_iter
= fields
.begin(); f_iter
!= fields
.end(); ++f_iter
) {
590 // Write field header
592 "if self." << (*f_iter
)->get_name() << " != None:" << endl
;
595 "oprot.writeFieldBegin(" <<
596 "'" << (*f_iter
)->get_name() << "', " <<
597 type_to_enum((*f_iter
)->get_type()) << ", " <<
598 (*f_iter
)->get_key() << ")" << endl
;
600 // Write field contents
601 generate_serialize_field(out
, *f_iter
, "self.");
603 // Write field closer
605 "oprot.writeFieldEnd()" << endl
;
610 // Write the struct map
612 indent() << "oprot.writeFieldStop()" << endl
<<
613 indent() << "oprot.writeStructEnd()" << endl
;
621 * Generates a thrift service.
623 * @param tservice The service definition
625 void t_py_generator::generate_service(t_service
* tservice
) {
626 string f_service_name
= package_dir_
+"/"+service_name_
+".py";
627 f_service_
.open(f_service_name
.c_str());
630 py_autogen_comment() << endl
<<
631 py_imports() << endl
;
633 if (tservice
->get_extends() != NULL
) {
635 "import " << get_real_py_module(tservice
->get_extends()->get_program()) <<
636 "." << tservice
->get_extends()->get_name() << endl
;
640 "from ttypes import *" << endl
<<
641 "from thrift.Thrift import TProcessor" << endl
<<
642 render_fastbinary_includes() <<
645 // Generate the three main parts of the service (well, two for now in PHP)
646 generate_service_interface(tservice
);
647 generate_service_client(tservice
);
648 generate_service_server(tservice
);
649 generate_service_helpers(tservice
);
650 generate_service_remote(tservice
);
652 // Close service file
658 * Generates helper functions for a service.
660 * @param tservice The service to generate a header definition for
662 void t_py_generator::generate_service_helpers(t_service
* tservice
) {
663 vector
<t_function
*> functions
= tservice
->get_functions();
664 vector
<t_function
*>::iterator f_iter
;
667 "# HELPER FUNCTIONS AND STRUCTURES" << endl
<< endl
;
669 for (f_iter
= functions
.begin(); f_iter
!= functions
.end(); ++f_iter
) {
670 t_struct
* ts
= (*f_iter
)->get_arglist();
671 generate_py_struct_definition(f_service_
, ts
, false);
672 generate_py_function_helpers(*f_iter
);
677 * Generates a struct and helpers for a function.
679 * @param tfunction The function
681 void t_py_generator::generate_py_function_helpers(t_function
* tfunction
) {
682 t_struct
result(program_
, tfunction
->get_name() + "_result");
683 t_field
success(tfunction
->get_returntype(), "success", 0);
684 if (!tfunction
->get_returntype()->is_void()) {
685 result
.append(&success
);
688 t_struct
* xs
= tfunction
->get_xceptions();
689 const vector
<t_field
*>& fields
= xs
->get_members();
690 vector
<t_field
*>::const_iterator f_iter
;
691 for (f_iter
= fields
.begin(); f_iter
!= fields
.end(); ++f_iter
) {
692 result
.append(*f_iter
);
694 generate_py_struct_definition(f_service_
, &result
, false, true);
698 * Generates a service interface definition.
700 * @param tservice The service to generate a header definition for
702 void t_py_generator::generate_service_interface(t_service
* tservice
) {
704 string extends_if
= "";
705 if (tservice
->get_extends() != NULL
) {
706 extends
= type_name(tservice
->get_extends());
707 extends_if
= "(" + extends
+ ".Iface)";
711 "class Iface" << extends_if
<< ":" << endl
;
713 vector
<t_function
*> functions
= tservice
->get_functions();
714 vector
<t_function
*>::iterator f_iter
;
715 for (f_iter
= functions
.begin(); f_iter
!= functions
.end(); ++f_iter
) {
717 indent() << "def " << function_signature(*f_iter
) << ":" << endl
<<
718 indent() << " pass" << endl
<< endl
;
726 * Generates a service client definition.
728 * @param tservice The service to generate a server for.
730 void t_py_generator::generate_service_client(t_service
* tservice
) {
732 string extends_client
= "";
733 if (tservice
->get_extends() != NULL
) {
734 extends
= type_name(tservice
->get_extends());
735 extends_client
= extends
+ ".Client, ";
739 "class Client(" << extends_client
<< "Iface):" << endl
;
742 // Constructor function
744 indent() << "def __init__(self, iprot, oprot=None):" << endl
;
745 if (extends
.empty()) {
747 indent() << " self._iprot = self._oprot = iprot" << endl
<<
748 indent() << " if oprot != None:" << endl
<<
749 indent() << " self._oprot = oprot" << endl
<<
750 indent() << " self._seqid = 0" << endl
<<
754 indent() << " " << extends
<< ".Client.__init__(self, iprot, oprot)" << endl
<<
758 // Generate client method implementations
759 vector
<t_function
*> functions
= tservice
->get_functions();
760 vector
<t_function
*>::const_iterator f_iter
;
761 for (f_iter
= functions
.begin(); f_iter
!= functions
.end(); ++f_iter
) {
762 t_struct
* arg_struct
= (*f_iter
)->get_arglist();
763 const vector
<t_field
*>& fields
= arg_struct
->get_members();
764 vector
<t_field
*>::const_iterator fld_iter
;
765 string funname
= (*f_iter
)->get_name();
768 indent(f_service_
) <<
769 "def " << function_signature(*f_iter
) << ":" << endl
;
771 indent(f_service_
) <<
772 "self.send_" << funname
<< "(";
775 for (fld_iter
= fields
.begin(); fld_iter
!= fields
.end(); ++fld_iter
) {
781 f_service_
<< (*fld_iter
)->get_name();
783 f_service_
<< ")" << endl
;
785 if (!(*f_iter
)->is_async()) {
786 f_service_
<< indent();
787 if (!(*f_iter
)->get_returntype()->is_void()) {
788 f_service_
<< "return ";
791 "self.recv_" << funname
<< "()" << endl
;
796 indent(f_service_
) <<
797 "def send_" << function_signature(*f_iter
) << ":" << endl
;
800 std::string argsname
= (*f_iter
)->get_name() + "_args";
802 // Serialize the request header
804 indent() << "self._oprot.writeMessageBegin('" << (*f_iter
)->get_name() << "', TMessageType.CALL, self._seqid)" << endl
;
807 indent() << "args = " << argsname
<< "()" << endl
;
809 for (fld_iter
= fields
.begin(); fld_iter
!= fields
.end(); ++fld_iter
) {
811 indent() << "args." << (*fld_iter
)->get_name() << " = " << (*fld_iter
)->get_name() << endl
;
814 // Write to the stream
816 indent() << "args.write(self._oprot)" << endl
<<
817 indent() << "self._oprot.writeMessageEnd()" << endl
<<
818 indent() << "self._oprot.trans.flush()" << endl
;
822 if (!(*f_iter
)->is_async()) {
823 std::string resultname
= (*f_iter
)->get_name() + "_result";
824 t_struct
noargs(program_
);
826 t_function
recv_function((*f_iter
)->get_returntype(),
827 string("recv_") + (*f_iter
)->get_name(),
832 indent() << "def " << function_signature(&recv_function
) << ":" << endl
;
835 // TODO(mcslee): Validate message reply here, seq ids etc.
838 indent() << "(fname, mtype, rseqid) = self._iprot.readMessageBegin()" << endl
<<
839 indent() << "if mtype == TMessageType.EXCEPTION:" << endl
<<
840 indent() << " x = TApplicationException()" << endl
<<
841 indent() << " x.read(self._iprot)" << endl
<<
842 indent() << " self._iprot.readMessageEnd()" << endl
<<
843 indent() << " raise x" << endl
;
846 indent() << "result = " << resultname
<< "()" << endl
<<
847 indent() << "result.read(self._iprot)" << endl
<<
848 indent() << "self._iprot.readMessageEnd()" << endl
;
850 // Careful, only return _result if not a void function
851 if (!(*f_iter
)->get_returntype()->is_void()) {
853 indent() << "if result.success != None:" << endl
<<
854 indent() << " return result.success" << endl
;
857 t_struct
* xs
= (*f_iter
)->get_xceptions();
858 const std::vector
<t_field
*>& xceptions
= xs
->get_members();
859 vector
<t_field
*>::const_iterator x_iter
;
860 for (x_iter
= xceptions
.begin(); x_iter
!= xceptions
.end(); ++x_iter
) {
862 indent() << "if result." << (*x_iter
)->get_name() << " != None:" << endl
<<
863 indent() << " raise result." << (*x_iter
)->get_name() << "" << endl
;
866 // Careful, only return _result if not a void function
867 if ((*f_iter
)->get_returntype()->is_void()) {
868 indent(f_service_
) <<
872 indent() << "raise TApplicationException(TApplicationException.MISSING_RESULT, \"" << (*f_iter
)->get_name() << " failed: unknown result\");" << endl
;
887 * Generates a command line tool for making remote requests
889 * @param tservice The service to generate a remote for.
891 void t_py_generator::generate_service_remote(t_service
* tservice
) {
892 vector
<t_function
*> functions
= tservice
->get_functions();
893 vector
<t_function
*>::iterator f_iter
;
895 string f_remote_name
= package_dir_
+"/"+service_name_
+"-remote";
897 f_remote
.open(f_remote_name
.c_str());
900 "#!/usr/bin/env python" << endl
<<
901 py_autogen_comment() << endl
<<
902 "import sys" << endl
<<
903 "import pprint" << endl
<<
904 "from urlparse import urlparse" << endl
<<
905 "from thrift.transport import TTransport" << endl
<<
906 "from thrift.transport import TSocket" << endl
<<
907 "from thrift.transport import THttpClient" << endl
<<
908 "from thrift.protocol import TBinaryProtocol" << endl
<<
912 "import " << service_name_
<< endl
<<
913 "from ttypes import *" << endl
<<
917 "if len(sys.argv) <= 1 or sys.argv[1] == '--help':" << endl
<<
918 " print ''" << endl
<<
919 " print 'Usage: ' + sys.argv[0] + ' [-h host:port] [-u url] [-f[ramed]] function [arg1 [arg2...]]'" << endl
<<
920 " print ''" << endl
<<
921 " print 'Functions:'" << endl
;
922 for (f_iter
= functions
.begin(); f_iter
!= functions
.end(); ++f_iter
) {
924 " print ' " << (*f_iter
)->get_returntype()->get_name() << " " << (*f_iter
)->get_name() << "(";
925 t_struct
* arg_struct
= (*f_iter
)->get_arglist();
926 const std::vector
<t_field
*>& args
= arg_struct
->get_members();
927 vector
<t_field
*>::const_iterator a_iter
;
928 int num_args
= args
.size();
930 for (int i
= 0; i
< num_args
; ++i
) {
937 args
[i
]->get_type()->get_name() << " " << args
[i
]->get_name();
939 f_remote
<< ")'" << endl
;
942 " print ''" << endl
<<
943 " sys.exit(0)" << endl
<<
947 "pp = pprint.PrettyPrinter(indent = 2)" << endl
<<
948 "host = 'localhost'" << endl
<<
949 "port = 9090" << endl
<<
950 "uri = ''" << endl
<<
951 "framed = False" << endl
<<
952 "http = False" << endl
<<
953 "argi = 1" << endl
<<
955 "if sys.argv[argi] == '-h':" << endl
<<
956 " parts = sys.argv[argi+1].split(':') " << endl
<<
957 " host = parts[0]" << endl
<<
958 " port = int(parts[1])" << endl
<<
959 " argi += 2" << endl
<<
961 "if sys.argv[argi] == '-u':" << endl
<<
962 " url = urlparse(sys.argv[argi+1])" << endl
<<
963 " parts = url[1].split(':') " << endl
<<
964 " host = parts[0]" << endl
<<
965 " if len(parts) > 1:" << endl
<<
966 " port = int(parts[1])" << endl
<<
968 " port = 80" << endl
<<
969 " uri = url[2]" << endl
<<
970 " http = True" << endl
<<
971 " argi += 2" << endl
<<
973 "if sys.argv[argi] == '-f' or sys.argv[argi] == '-framed':" << endl
<<
974 " framed = True" << endl
<<
975 " argi += 1" << endl
<<
977 "cmd = sys.argv[argi]" << endl
<<
978 "args = sys.argv[argi+1:]" << endl
<<
980 "if http:" << endl
<<
981 " transport = THttpClient.THttpClient(host, port, uri)" << endl
<<
983 " socket = TSocket.TSocket(host, port)" << endl
<<
984 " if framed:" << endl
<<
985 " transport = TTransport.TFramedTransport(socket)" << endl
<<
987 " transport = TTransport.TBufferedTransport(socket)" << endl
<<
988 "protocol = TBinaryProtocol.TBinaryProtocol(transport)" << endl
<<
989 "client = " << service_name_
<< ".Client(protocol)" << endl
<<
990 "transport.open()" << endl
<<
993 // Generate the dispatch methods
996 for (f_iter
= functions
.begin(); f_iter
!= functions
.end(); ++f_iter
) {
1003 t_struct
* arg_struct
= (*f_iter
)->get_arglist();
1004 const std::vector
<t_field
*>& args
= arg_struct
->get_members();
1005 vector
<t_field
*>::const_iterator a_iter
;
1006 int num_args
= args
.size();
1009 "if cmd == '" << (*f_iter
)->get_name() << "':" << endl
<<
1010 " if len(args) != " << num_args
<< ":" << endl
<<
1011 " print '" << (*f_iter
)->get_name() << " requires " << num_args
<< " args'" << endl
<<
1012 " sys.exit(1)" << endl
<<
1013 " pp.pprint(client." << (*f_iter
)->get_name() << "(";
1014 for (int i
= 0; i
< num_args
; ++i
) {
1015 if (args
[i
]->get_type()->is_string()) {
1016 f_remote
<< "args[" << i
<< "],";
1018 f_remote
<< "eval(args[" << i
<< "]),";
1021 f_remote
<< "))" << endl
;
1026 f_remote
<< "transport.close()" << endl
;
1028 // Close service file
1031 // Make file executable, love that bitwise OR action
1032 chmod(f_remote_name
.c_str(),
1043 * Generates a service server definition.
1045 * @param tservice The service to generate a server for.
1047 void t_py_generator::generate_service_server(t_service
* tservice
) {
1048 // Generate the dispatch methods
1049 vector
<t_function
*> functions
= tservice
->get_functions();
1050 vector
<t_function
*>::iterator f_iter
;
1052 string extends
= "";
1053 string extends_processor
= "";
1054 if (tservice
->get_extends() != NULL
) {
1055 extends
= type_name(tservice
->get_extends());
1056 extends_processor
= extends
+ ".Processor, ";
1059 // Generate the header portion
1061 "class Processor(" << extends_processor
<< "Iface, TProcessor):" << endl
;
1064 indent(f_service_
) <<
1065 "def __init__(self, handler):" << endl
;
1067 if (extends
.empty()) {
1069 indent() << "self._handler = handler" << endl
<<
1070 indent() << "self._processMap = {}" << endl
;
1073 indent() << extends
<< ".Processor.__init__(self, handler)" << endl
;
1075 for (f_iter
= functions
.begin(); f_iter
!= functions
.end(); ++f_iter
) {
1077 indent() << "self._processMap[\"" << (*f_iter
)->get_name() << "\"] = Processor.process_" << (*f_iter
)->get_name() << endl
;
1082 // Generate the server implementation
1083 indent(f_service_
) <<
1084 "def process(self, iprot, oprot):" << endl
;
1088 indent() << "(name, type, seqid) = iprot.readMessageBegin()" << endl
;
1090 // TODO(mcslee): validate message
1092 // HOT: dictionary function lookup
1094 indent() << "if name not in self._processMap:" << endl
<<
1095 indent() << " iprot.skip(TType.STRUCT)" << endl
<<
1096 indent() << " iprot.readMessageEnd()" << endl
<<
1097 indent() << " x = TApplicationException(TApplicationException.UNKNOWN_METHOD, 'Unknown function %s' % (name))" << endl
<<
1098 indent() << " oprot.writeMessageBegin(name, TMessageType.EXCEPTION, seqid)" << endl
<<
1099 indent() << " x.write(oprot)" << endl
<<
1100 indent() << " oprot.writeMessageEnd()" << endl
<<
1101 indent() << " oprot.trans.flush()" << endl
<<
1102 indent() << " return" << endl
<<
1103 indent() << "else:" << endl
<<
1104 indent() << " self._processMap[name](self, seqid, iprot, oprot)" << endl
;
1106 // Read end of args field, the T_STOP, and the struct close
1108 indent() << "return True" << endl
;
1113 // Generate the process subfunctions
1114 for (f_iter
= functions
.begin(); f_iter
!= functions
.end(); ++f_iter
) {
1115 generate_process_function(tservice
, *f_iter
);
1123 * Generates a process function definition.
1125 * @param tfunction The function to write a dispatcher for
1127 void t_py_generator::generate_process_function(t_service
* tservice
,
1128 t_function
* tfunction
) {
1130 indent(f_service_
) <<
1131 "def process_" << tfunction
->get_name() <<
1132 "(self, seqid, iprot, oprot):" << endl
;
1135 string argsname
= tfunction
->get_name() + "_args";
1136 string resultname
= tfunction
->get_name() + "_result";
1139 indent() << "args = " << argsname
<< "()" << endl
<<
1140 indent() << "args.read(iprot)" << endl
<<
1141 indent() << "iprot.readMessageEnd()" << endl
;
1143 t_struct
* xs
= tfunction
->get_xceptions();
1144 const std::vector
<t_field
*>& xceptions
= xs
->get_members();
1145 vector
<t_field
*>::const_iterator x_iter
;
1147 // Declare result for non async function
1148 if (!tfunction
->is_async()) {
1150 indent() << "result = " << resultname
<< "()" << endl
;
1153 // Try block for a function with exceptions
1154 if (xceptions
.size() > 0) {
1156 indent() << "try:" << endl
;
1160 // Generate the function call
1161 t_struct
* arg_struct
= tfunction
->get_arglist();
1162 const std::vector
<t_field
*>& fields
= arg_struct
->get_members();
1163 vector
<t_field
*>::const_iterator f_iter
;
1165 f_service_
<< indent();
1166 if (!tfunction
->is_async() && !tfunction
->get_returntype()->is_void()) {
1167 f_service_
<< "result.success = ";
1170 "self._handler." << tfunction
->get_name() << "(";
1172 for (f_iter
= fields
.begin(); f_iter
!= fields
.end(); ++f_iter
) {
1178 f_service_
<< "args." << (*f_iter
)->get_name();
1180 f_service_
<< ")" << endl
;
1182 if (!tfunction
->is_async() && xceptions
.size() > 0) {
1184 for (x_iter
= xceptions
.begin(); x_iter
!= xceptions
.end(); ++x_iter
) {
1186 indent() << "except " << (*x_iter
)->get_type()->get_name() << ", " << (*x_iter
)->get_name() << ":" << endl
;
1187 if (!tfunction
->is_async()) {
1190 indent() << "result." << (*x_iter
)->get_name() << " = " << (*x_iter
)->get_name() << endl
;
1194 indent() << "pass" << endl
;
1199 // Shortcut out here for async functions
1200 if (tfunction
->is_async()) {
1202 indent() << "return" << endl
;
1209 indent() << "oprot.writeMessageBegin(\"" << tfunction
->get_name() << "\", TMessageType.REPLY, seqid)" << endl
<<
1210 indent() << "result.write(oprot)" << endl
<<
1211 indent() << "oprot.writeMessageEnd()" << endl
<<
1212 indent() << "oprot.trans.flush()" << endl
;
1220 * Deserializes a field of any type.
1222 void t_py_generator::generate_deserialize_field(ofstream
&out
,
1226 t_type
* type
= get_true_type(tfield
->get_type());
1228 if (type
->is_void()) {
1229 throw "CANNOT GENERATE DESERIALIZE CODE FOR void TYPE: " +
1230 prefix
+ tfield
->get_name();
1233 string name
= prefix
+ tfield
->get_name();
1235 if (type
->is_struct() || type
->is_xception()) {
1236 generate_deserialize_struct(out
,
1239 } else if (type
->is_container()) {
1240 generate_deserialize_container(out
, type
, name
);
1241 } else if (type
->is_base_type() || type
->is_enum()) {
1243 name
<< " = iprot.";
1245 if (type
->is_base_type()) {
1246 t_base_type::t_base tbase
= ((t_base_type
*)type
)->get_base();
1248 case t_base_type::TYPE_VOID
:
1249 throw "compiler error: cannot serialize void field in a struct: " +
1252 case t_base_type::TYPE_STRING
:
1253 out
<< "readString();";
1255 case t_base_type::TYPE_BOOL
:
1256 out
<< "readBool();";
1258 case t_base_type::TYPE_BYTE
:
1259 out
<< "readByte();";
1261 case t_base_type::TYPE_I16
:
1262 out
<< "readI16();";
1264 case t_base_type::TYPE_I32
:
1265 out
<< "readI32();";
1267 case t_base_type::TYPE_I64
:
1268 out
<< "readI64();";
1270 case t_base_type::TYPE_DOUBLE
:
1271 out
<< "readDouble();";
1274 throw "compiler error: no PHP name for base type " + t_base_type::t_base_name(tbase
);
1276 } else if (type
->is_enum()) {
1277 out
<< "readI32();";
1282 printf("DO NOT KNOW HOW TO DESERIALIZE FIELD '%s' TYPE '%s'\n",
1283 tfield
->get_name().c_str(), type
->get_name().c_str());
1288 * Generates an unserializer for a struct, calling read()
1290 void t_py_generator::generate_deserialize_struct(ofstream
&out
,
1294 indent() << prefix
<< " = " << type_name(tstruct
) << "()" << endl
<<
1295 indent() << prefix
<< ".read(iprot)" << endl
;
1299 * Serialize a container by writing out the header followed by
1300 * data and then a footer.
1302 void t_py_generator::generate_deserialize_container(ofstream
&out
,
1305 string size
= tmp("_size");
1306 string ktype
= tmp("_ktype");
1307 string vtype
= tmp("_vtype");
1308 string etype
= tmp("_etype");
1310 t_field
fsize(g_type_i32
, size
);
1311 t_field
fktype(g_type_byte
, ktype
);
1312 t_field
fvtype(g_type_byte
, vtype
);
1313 t_field
fetype(g_type_byte
, etype
);
1315 // Declare variables, read header
1316 if (ttype
->is_map()) {
1318 indent() << prefix
<< " = {}" << endl
<<
1319 indent() << "(" << ktype
<< ", " << vtype
<< ", " << size
<< " ) = iprot.readMapBegin() " << endl
;
1320 } else if (ttype
->is_set()) {
1322 indent() << prefix
<< " = set()" << endl
<<
1323 indent() << "(" << etype
<< ", " << size
<< ") = iprot.readSetBegin()" << endl
;
1324 } else if (ttype
->is_list()) {
1326 indent() << prefix
<< " = []" << endl
<<
1327 indent() << "(" << etype
<< ", " << size
<< ") = iprot.readListBegin()" << endl
;
1330 // For loop iterates over elements
1331 string i
= tmp("_i");
1333 "for " << i
<< " in xrange(" << size
<< "):" << endl
;
1337 if (ttype
->is_map()) {
1338 generate_deserialize_map_element(out
, (t_map
*)ttype
, prefix
);
1339 } else if (ttype
->is_set()) {
1340 generate_deserialize_set_element(out
, (t_set
*)ttype
, prefix
);
1341 } else if (ttype
->is_list()) {
1342 generate_deserialize_list_element(out
, (t_list
*)ttype
, prefix
);
1347 // Read container end
1348 if (ttype
->is_map()) {
1349 indent(out
) << "iprot.readMapEnd()" << endl
;
1350 } else if (ttype
->is_set()) {
1351 indent(out
) << "iprot.readSetEnd()" << endl
;
1352 } else if (ttype
->is_list()) {
1353 indent(out
) << "iprot.readListEnd()" << endl
;
1359 * Generates code to deserialize a map
1361 void t_py_generator::generate_deserialize_map_element(ofstream
&out
,
1364 string key
= tmp("_key");
1365 string val
= tmp("_val");
1366 t_field
fkey(tmap
->get_key_type(), key
);
1367 t_field
fval(tmap
->get_val_type(), val
);
1369 generate_deserialize_field(out
, &fkey
);
1370 generate_deserialize_field(out
, &fval
);
1373 prefix
<< "[" << key
<< "] = " << val
<< endl
;
1377 * Write a set element
1379 void t_py_generator::generate_deserialize_set_element(ofstream
&out
,
1382 string elem
= tmp("_elem");
1383 t_field
felem(tset
->get_elem_type(), elem
);
1385 generate_deserialize_field(out
, &felem
);
1388 prefix
<< ".add(" << elem
<< ")" << endl
;
1392 * Write a list element
1394 void t_py_generator::generate_deserialize_list_element(ofstream
&out
,
1397 string elem
= tmp("_elem");
1398 t_field
felem(tlist
->get_elem_type(), elem
);
1400 generate_deserialize_field(out
, &felem
);
1403 prefix
<< ".append(" << elem
<< ")" << endl
;
1408 * Serializes a field of any type.
1410 * @param tfield The field to serialize
1411 * @param prefix Name to prepend to field name
1413 void t_py_generator::generate_serialize_field(ofstream
&out
,
1416 t_type
* type
= get_true_type(tfield
->get_type());
1418 // Do nothing for void types
1419 if (type
->is_void()) {
1420 throw "CANNOT GENERATE SERIALIZE CODE FOR void TYPE: " +
1421 prefix
+ tfield
->get_name();
1424 if (type
->is_struct() || type
->is_xception()) {
1425 generate_serialize_struct(out
,
1427 prefix
+ tfield
->get_name());
1428 } else if (type
->is_container()) {
1429 generate_serialize_container(out
,
1431 prefix
+ tfield
->get_name());
1432 } else if (type
->is_base_type() || type
->is_enum()) {
1434 string name
= prefix
+ tfield
->get_name();
1439 if (type
->is_base_type()) {
1440 t_base_type::t_base tbase
= ((t_base_type
*)type
)->get_base();
1442 case t_base_type::TYPE_VOID
:
1444 "compiler error: cannot serialize void field in a struct: " + name
;
1446 case t_base_type::TYPE_STRING
:
1447 out
<< "writeString(" << name
<< ")";
1449 case t_base_type::TYPE_BOOL
:
1450 out
<< "writeBool(" << name
<< ")";
1452 case t_base_type::TYPE_BYTE
:
1453 out
<< "writeByte(" << name
<< ")";
1455 case t_base_type::TYPE_I16
:
1456 out
<< "writeI16(" << name
<< ")";
1458 case t_base_type::TYPE_I32
:
1459 out
<< "writeI32(" << name
<< ")";
1461 case t_base_type::TYPE_I64
:
1462 out
<< "writeI64(" << name
<< ")";
1464 case t_base_type::TYPE_DOUBLE
:
1465 out
<< "writeDouble(" << name
<< ")";
1468 throw "compiler error: no PHP name for base type " + t_base_type::t_base_name(tbase
);
1470 } else if (type
->is_enum()) {
1471 out
<< "writeI32(" << name
<< ")";
1475 printf("DO NOT KNOW HOW TO SERIALIZE FIELD '%s%s' TYPE '%s'\n",
1477 tfield
->get_name().c_str(),
1478 type
->get_name().c_str());
1483 * Serializes all the members of a struct.
1485 * @param tstruct The struct to serialize
1486 * @param prefix String prefix to attach to all fields
1488 void t_py_generator::generate_serialize_struct(ofstream
&out
,
1492 prefix
<< ".write(oprot)" << endl
;
1495 void t_py_generator::generate_serialize_container(ofstream
&out
,
1498 if (ttype
->is_map()) {
1500 "oprot.writeMapBegin(" <<
1501 type_to_enum(((t_map
*)ttype
)->get_key_type()) << ", " <<
1502 type_to_enum(((t_map
*)ttype
)->get_val_type()) << ", " <<
1503 "len(" << prefix
<< "))" << endl
;
1504 } else if (ttype
->is_set()) {
1506 "oprot.writeSetBegin(" <<
1507 type_to_enum(((t_set
*)ttype
)->get_elem_type()) << ", " <<
1508 "len(" << prefix
<< "))" << endl
;
1509 } else if (ttype
->is_list()) {
1511 "oprot.writeListBegin(" <<
1512 type_to_enum(((t_list
*)ttype
)->get_elem_type()) << ", " <<
1513 "len(" << prefix
<< "))" << endl
;
1516 if (ttype
->is_map()) {
1517 string kiter
= tmp("kiter");
1518 string viter
= tmp("viter");
1520 "for " << kiter
<< "," << viter
<< " in " << prefix
<< ".items():" << endl
;
1522 generate_serialize_map_element(out
, (t_map
*)ttype
, kiter
, viter
);
1524 } else if (ttype
->is_set()) {
1525 string iter
= tmp("iter");
1527 "for " << iter
<< " in " << prefix
<< ":" << endl
;
1529 generate_serialize_set_element(out
, (t_set
*)ttype
, iter
);
1531 } else if (ttype
->is_list()) {
1532 string iter
= tmp("iter");
1534 "for " << iter
<< " in " << prefix
<< ":" << endl
;
1536 generate_serialize_list_element(out
, (t_list
*)ttype
, iter
);
1540 if (ttype
->is_map()) {
1542 "oprot.writeMapEnd()" << endl
;
1543 } else if (ttype
->is_set()) {
1545 "oprot.writeSetEnd()" << endl
;
1546 } else if (ttype
->is_list()) {
1548 "oprot.writeListEnd()" << endl
;
1553 * Serializes the members of a map.
1556 void t_py_generator::generate_serialize_map_element(ofstream
&out
,
1560 t_field
kfield(tmap
->get_key_type(), kiter
);
1561 generate_serialize_field(out
, &kfield
, "");
1563 t_field
vfield(tmap
->get_val_type(), viter
);
1564 generate_serialize_field(out
, &vfield
, "");
1568 * Serializes the members of a set.
1570 void t_py_generator::generate_serialize_set_element(ofstream
&out
,
1573 t_field
efield(tset
->get_elem_type(), iter
);
1574 generate_serialize_field(out
, &efield
, "");
1578 * Serializes the members of a list.
1580 void t_py_generator::generate_serialize_list_element(ofstream
&out
,
1583 t_field
efield(tlist
->get_elem_type(), iter
);
1584 generate_serialize_field(out
, &efield
, "");
1588 * Declares a field, which may include initialization as necessary.
1590 * @param ttype The type
1592 string
t_py_generator::declare_field(t_field
* tfield
) {
1593 string result
= "self." + tfield
->get_name();
1594 t_type
* type
= get_true_type(tfield
->get_type());
1595 if (tfield
->get_value() != NULL
) {
1596 result
+= " = " + render_const_value(type
, tfield
->get_value());
1598 result
+= " = None";
1604 * Renders a function signature of the form 'type name(args)'
1606 * @param tfunction Function definition
1607 * @return String of rendered function definition
1609 string
t_py_generator::function_signature(t_function
* tfunction
,
1611 // TODO(mcslee): Nitpicky, no ',' if argument_list is empty
1613 prefix
+ tfunction
->get_name() +
1614 "(self, " + argument_list(tfunction
->get_arglist()) + ")";
1618 * Renders a field list
1620 string
t_py_generator::argument_list(t_struct
* tstruct
) {
1623 const vector
<t_field
*>& fields
= tstruct
->get_members();
1624 vector
<t_field
*>::const_iterator f_iter
;
1626 for (f_iter
= fields
.begin(); f_iter
!= fields
.end(); ++f_iter
) {
1632 result
+= (*f_iter
)->get_name();
1637 string
t_py_generator::type_name(t_type
* ttype
) {
1638 t_program
* program
= ttype
->get_program();
1639 if (program
!= NULL
&& program
!= program_
) {
1640 if (ttype
->is_service()) {
1641 return get_real_py_module(program
) + "." + ttype
->get_name();
1643 return get_real_py_module(program
) + ".ttypes." + ttype
->get_name();
1646 return ttype
->get_name();
1650 * Converts the parse type to a Python tyoe
1652 string
t_py_generator::type_to_enum(t_type
* type
) {
1653 type
= get_true_type(type
);
1655 if (type
->is_base_type()) {
1656 t_base_type::t_base tbase
= ((t_base_type
*)type
)->get_base();
1658 case t_base_type::TYPE_VOID
:
1659 throw "NO T_VOID CONSTRUCT";
1660 case t_base_type::TYPE_STRING
:
1661 return "TType.STRING";
1662 case t_base_type::TYPE_BOOL
:
1663 return "TType.BOOL";
1664 case t_base_type::TYPE_BYTE
:
1665 return "TType.BYTE";
1666 case t_base_type::TYPE_I16
:
1668 case t_base_type::TYPE_I32
:
1670 case t_base_type::TYPE_I64
:
1672 case t_base_type::TYPE_DOUBLE
:
1673 return "TType.DOUBLE";
1675 } else if (type
->is_enum()) {
1677 } else if (type
->is_struct() || type
->is_xception()) {
1678 return "TType.STRUCT";
1679 } else if (type
->is_map()) {
1681 } else if (type
->is_set()) {
1683 } else if (type
->is_list()) {
1684 return "TType.LIST";
1687 throw "INVALID TYPE IN type_to_enum: " + type
->get_name();
1690 /** See the comment inside generate_py_struct_definition for what this is. */
1691 string
t_py_generator::type_to_spec_args(t_type
* ttype
) {
1692 while (ttype
->is_typedef()) {
1693 ttype
= ((t_typedef
*)ttype
)->get_type();
1696 if (ttype
->is_base_type() || ttype
->is_enum()) {
1698 } else if (ttype
->is_struct() || ttype
->is_xception()) {
1699 return "(" + type_name(ttype
) + ", " + type_name(ttype
) + ".thrift_spec)";
1700 } else if (ttype
->is_map()) {
1702 type_to_enum(((t_map
*)ttype
)->get_key_type()) + "," +
1703 type_to_spec_args(((t_map
*)ttype
)->get_key_type()) + "," +
1704 type_to_enum(((t_map
*)ttype
)->get_val_type()) + "," +
1705 type_to_spec_args(((t_map
*)ttype
)->get_val_type()) +
1708 } else if (ttype
->is_set()) {
1710 type_to_enum(((t_set
*)ttype
)->get_elem_type()) + "," +
1711 type_to_spec_args(((t_set
*)ttype
)->get_elem_type()) +
1714 } else if (ttype
->is_list()) {
1716 type_to_enum(((t_list
*)ttype
)->get_elem_type()) + "," +
1717 type_to_spec_args(((t_list
*)ttype
)->get_elem_type()) +
1721 throw "INVALID TYPE IN type_to_spec_args: " + ttype
->get_name();