[ 
https://issues.apache.org/jira/browse/THRIFT-3002?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16140549#comment-16140549
 ] 

The Alchemist commented on THRIFT-3002:
---------------------------------------

Wow, there must be so few users of `TZLibTransport` in Python.

Here's a small unit test :

{code:python}
#!/usr/bin/env python

#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
#

import sys
import unittest

from thrift.protocol.TJSONProtocol import TJSONProtocol
from thrift.transport import TTransport,TZlibTransport


class THRIFT_3002_tzlibtransport_bug(unittest.TestCase):

    def test_escaped_unicode_string(self):

        # first, write the data
        bufC = TTransport.TFileObjectTransport(open('test.thrift', 'wb'))
        transportC = TZlibTransport.TZlibTransport(bufC)
        protocolC = TJSONProtocol(transportC)

        changesFile = open('../../CHANGES').read()
        protocolC.writeString(changesFile)

        transportC.close()

        # then, read it and try to decompress it
        bufD = TTransport.TFileObjectTransport(open('test.thrift', 'rb"'))
        transportD = TZlibTransport.TZlibTransport(bufD)
        protocolD = TJSONProtocol(transportD)

        changesFileDecompressed = protocolD.readString()

        self.assertEquals(changesFile, changesFileDecompressed)


if __name__ == '__main__':
    unittest.main()

{code}

Warning: Running this will give you an infinite loop.

Workaround: "manually" compress the output by using `gzip.GzipFile`.

> Python loops forever with TZlibTransport and TFileObjectTransport
> -----------------------------------------------------------------
>
>                 Key: THRIFT-3002
>                 URL: https://issues.apache.org/jira/browse/THRIFT-3002
>             Project: Thrift
>          Issue Type: Bug
>          Components: Python - Library
>    Affects Versions: 0.9.2
>         Environment: Ubuntu Linux
>            Reporter: Tom Hunt
>              Labels: easyfix
>         Attachments: TZlibTransport.patch
>
>
> When using a TZlibTransport wrapping a TFileObjectTransport, at end of file 
> the TZlibTransport loops forever instead of doing anything sensible. This is 
> because the TZlibTransport does not check for 0-length reads itself and calls 
> TFileObjectTransport.read() rather than TFileObjectTransport.readAll() (the 
> latter does its own checking and raises EOFError()). Thus, TZlibTransport 
> spins forever on 0-length reads and hangs the program. Suggested fix: make 
> TZlibTransport call readAll() instead of read() in its readComp() method, or 
> check for 0-length reads on its own and raise EOFError().



--
This message was sent by Atlassian JIRA
(v6.4.14#64029)

Reply via email to