code: def querying_context(self, packet_type):
          
          if self.set_state(tds_base.TDS_QUERYING) != tds_base.TDS_QUERYING:
              raise tds_base.Error("Couldn't switch to state")
          self._writer.begin_packet(packet_type)
          try:
              yield
          except:
              if self.state != tds_base.TDS_DEAD:
                  self.set_state(tds_base.TDS_IDLE)
              raise
          else:
              self.set_state(tds_base.TDS_PENDING)
              self._writer.flush()
docs: Context manager for querying.
  
          Sets state to TDS_QUERYING, and reverts it to TDS_IDLE if exception happens inside managed block,
          and to TDS_PENDING if managed block succeeds and flushes buffer.