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

Python 3.11 Immutable errors can't be used with contextlib.contextmanager

    XMLWordPrintableJSON

Details

    • Improvement
    • Status: Open
    • Major
    • Resolution: Unresolved
    • 0.14.1, 0.16.0, 0.18.1
    • None
    • Python - Compiler
    • None
    • The client runtime is the python:3.10 and python:3.11 docker images from dockerhub, installing the latest thrift library from pypi.

       

      The thrift compiler is installed on debian (v0.14.1 and v0.18.1) or ubuntu (v0.16.0).

    Description

      Generated Thrift IDLs create exceptions that are immutable. In Python 3.11 contextlib manipulates exceptions to update tracebacks. When a Thrift client raises an error within a context manager the context manager fails. This doesn't happen with Python 3.10.

      This example uses tutorial.thrift. I compiled the IDL with versions v0.14.1, v0.16. and v0.18.1, each with the same result.

       

      // contextmanager.py
      import contextlib
      import syssys.path.append('gen-py')
      from tutorial.ttypes import InvalidOperation
      
      @contextlib.contextmanager
      def example():
          yield
          return
      
      def main():
          with example():
              raise InvalidOperation
      
      if __name__ == "__main__":
          main() 

       

       

      # Run in script in Python 3.11
      
      py (master) $ docker run -it --rm --volume /home/katie/python/thrift:/code --workdir /code/tutorial/py python:3.11 /bin/sh -c "pip install thrift && python contextmanager.py" 
      Collecting thrift
        Downloading thrift-0.16.0.tar.gz (59 kB)
      [?25l     [90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━[0m [32m0.0/59.6 kB[0m [31m?[0m eta [36m-:--:--[0m
      [2K     [91m━━━━━━━━━━━━━━━━━━━━[0m[91m╸[0m[90m━━━━━━━━━━━━━━━━━━━[0m [32m30.7/59.6 kB[0m [31m1.2 MB/s[0m eta [36m0:00:01[0m
      [2K     [91m━━━━━━━━━━━━━━━━━━━━[0m[91m╸[0m[90m━━━━━━━━━━━━━━━━━━━[0m [32m30.7/59.6 kB[0m [31m1.2 MB/s[0m eta [36m0:00:01[0m
      [2K     [90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━[0m [32m59.6/59.6 kB[0m [31m535.6 kB/s[0m eta [36m0:00:00[0m
      [?25h  Preparing metadata (setup.py) ... [?25l- done
      [?25hCollecting six>=1.7.2
        Downloading six-1.16.0-py2.py3-none-any.whl (11 kB)
      Building wheels for collected packages: thrift
        Building wheel for thrift (setup.py) ... [?25l- \ | / - \ done
      [?25h  Created wheel for thrift: filename=thrift-0.16.0-cp311-cp311-linux_x86_64.whl size=509416 sha256=29702930fd4c49e8f70b568ae03ab3a2a0e46ce92c620ec6a7113432fb59d9e5
        Stored in directory: /root/.cache/pip/wheels/29/6e/43/fe5a143fd6ed0ca5c385a5dba8a445de49cb3e66013095074d
      Successfully built thrift
      Installing collected packages: six, thrift
      Successfully installed six-1.16.0 thrift-0.16.0
      [33mWARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv[0m[33m
      [0m
      [1m[[0m[34;49mnotice[0m[1;39;49m][0m[39;49m A new release of pip available: [0m[31;49m22.3.1[0m[39;49m -> [0m[32;49m23.1.2[0m
      [1m[[0m[34;49mnotice[0m[1;39;49m][0m[39;49m To update, run: [0m[32;49mpip install --upgrade pip[0m
      Traceback (most recent call last):
        File "/usr/local/lib/python3.11/contextlib.py", line 155, in __exit__
          self.gen.throw(typ, value, traceback)
        File "/code/tutorial/py/server_in_wrapper.py", line 9, in example
          yield
        File "/code/tutorial/py/server_in_wrapper.py", line 14, in main
          raise InvalidOperation
      tutorial.ttypes.InvalidOperation: InvalidOperation(whatOp=None, why=None)During handling of the above exception, another exception occurred:Traceback (most recent call last):
        File "/code/tutorial/py/server_in_wrapper.py", line 17, in <module>
          main()
        File "/code/tutorial/py/server_in_wrapper.py", line 13, in main
          with example():
        File "/usr/local/lib/python3.11/contextlib.py", line 188, in __exit__
          exc.__traceback__ = traceback
          ^^^^^^^^^^^^^^^^^
        File "/code/tutorial/py/gen-py/tutorial/ttypes.py", line 160, in __setattr__
          raise TypeError("can't modify immutable instance")
      TypeError: can't modify immutable instance
      

       

       

      # Running same script in Python 3.10
      
      py (master) $ docker run -it --rm --volume /home/katie/python/thrift:/code --workdir /code/tutorial/py python:3.10 /bin/sh -c "pip install thrift && python contextmanager.py"
      Collecting thrift
        Downloading thrift-0.16.0.tar.gz (59 kB)
      [?25l     [90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━[0m [32m0.0/59.6 kB[0m [31m?[0m eta [36m-:--:--[0m
      [2K     [91m━━━━━━━━━━━━━━━━━━━━[0m[91m╸[0m[90m━━━━━━━━━━━━━━━━━━━[0m [32m30.7/59.6 kB[0m [31m1.2 MB/s[0m eta [36m0:00:01[0m
      [2K     [91m━━━━━━━━━━━━━━━━━━━━━━━━━━[0m[91m╸[0m[90m━━━━━━━━━━━━[0m [32m41.0/59.6 kB[0m [31m479.1 kB/s[0m eta [36m0:00:01[0m
      [2K     [90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━[0m [32m59.6/59.6 kB[0m [31m566.4 kB/s[0m eta [36m0:00:00[0m
      [?25h  Preparing metadata (setup.py) ... [?25l- done
      [?25hCollecting six>=1.7.2
        Downloading six-1.16.0-py2.py3-none-any.whl (11 kB)
      Building wheels for collected packages: thrift
        Building wheel for thrift (setup.py) ... [?25l- \ | / - \ done
      [?25h  Created wheel for thrift: filename=thrift-0.16.0-cp310-cp310-linux_x86_64.whl size=464691 sha256=33b5b5fa6663ddaa6d1a122e0a660ef23f240a1f640beed412a6425183b6f2f9
        Stored in directory: /root/.cache/pip/wheels/52/f8/d2/acfd995e8247eb0cad372fa6a640a5fcf279ab2ed7c5c4490e
      Successfully built thrift
      Installing collected packages: six, thrift
      Successfully installed six-1.16.0 thrift-0.16.0
      [33mWARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv[0m[33m
      [0m
      [1m[[0m[34;49mnotice[0m[1;39;49m][0m[39;49m A new release of pip is available: [0m[31;49m23.0.1[0m[39;49m -> [0m[32;49m23.1.2[0m
      [1m[[0m[34;49mnotice[0m[1;39;49m][0m[39;49m To update, run: [0m[32;49mpip install --upgrade pip[0m
      Traceback (most recent call last):
        File "/code/tutorial/py/server_in_wrapper.py", line 17, in <module>
          main()
        File "/code/tutorial/py/server_in_wrapper.py", line 14, in main
          raise InvalidOperation
      tutorial.ttypes.InvalidOperation: InvalidOperation(whatOp=None, why=None)
      
       

       

       The problematic code generated by the compiler is this code:

      // code placeholder
      144 class InvalidOperation(TException):                                     
      ...
      159     def __setattr__(self, *args):                                       160         raise TypeError("can't modify immutable instance")

      Attachments

        Issue Links

          Activity

            People

              Unassigned Unassigned
              ktatkinson-reddit Katie Atkinson
              Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

              Dates

                Created:
                Updated:

                Time Tracking

                  Estimated:
                  Original Estimate - Not Specified
                  Not Specified
                  Remaining:
                  Remaining Estimate - 0h
                  0h
                  Logged:
                  Time Spent - 3.5h
                  3.5h