Uploaded image for project: 'Thrift'
  1. Thrift
  2. THRIFT-4588

local variable referenced before assignment in immutable python structs

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Duplicate
    • 0.11.0
    • 0.12.0
    • Python - Compiler
    • None

    Description

      If an immutable struct has unset optional fields, the Python code generated for it will produce an UnboundLocalError when deserializing it. Consider this struct:

      struct TestStruct {
             1: optional i32 value;
      } (python.immutable="",)
      

      Compiling this to Python with Thrift 0.11.0, the ttypes.py has this read method:

          @classmethod
          def read(cls, iprot):
              if iprot._fast_decode is not None and isinstance(iprot.trans, TTransport.CReadableTransport) and cls.thrift_spec is not None:
                  return iprot._fast_decode(None, iprot, [cls, cls.thrift_spec])
              iprot.readStructBegin()
              while True:
                  (fname, ftype, fid) = iprot.readFieldBegin()
                  if ftype == TType.STOP:
                      break
                  if fid == 1:
                      if ftype == TType.I32:
                          value = iprot.readI32()
                      else:
                          iprot.skip(ftype)
                  else:
                      iprot.skip(ftype)
                  iprot.readFieldEnd()
              iprot.readStructEnd()
              return cls(
                  value=value,
              )
      

      Since no default is ever set for value, if it is never set while reading the input file, it will attempt to use an unbound variable when creating the class at the end of this method. For a non-immutable struct, the analogous code is:

          def __init__(self, value=None,):
              self.value = value
      
          def read(self, iprot):
              if iprot._fast_decode is not None and isinstance(iprot.trans, TTransport.CReadableTransport) and self.thrift_spec is not None:
                  iprot._fast_decode(self, iprot, [self.__class__, self.thrift_spec])
                  return
              iprot.readStructBegin()
              while True:
                  (fname, ftype, fid) = iprot.readFieldBegin()
                  if ftype == TType.STOP:
                      break
                  if fid == 1:
                      if ftype == TType.I32:
                          self.value = iprot.readI32()
                      else:
                          iprot.skip(ftype)
                  else:
                      iprot.skip(ftype)
                  iprot.readFieldEnd()
              iprot.readStructEnd()
      

      which works in this case because value is given a default value of None. It would probably make sense to set value to None at the start of this method.

      Attachments

        Activity

          People

            Unassigned Unassigned
            TV4Fun Joel Croteau
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: