Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from time import sleep
- from typing import Move, List
- from io import FileIO, BytesIO
- from dataclasses import dataclass
- from threading import Thread
- def read_and_close_file(file: Move[FileIO]):
- """ File will be closed, so we can not use after """
- file.read()
- file.close()
- @dataclass
- class Model:
- x: List[int]
- def make_list_from_model(model: Move[Model]) -> List[int]:
- """ This is not a pure function, which is bad,
- but if we move model here and won't use it after, then it's kinda pure """
- model.x.append(1) # x is big and we don't want to copy it, instead we just push another element
- return model.x
- def run_thread_unsafe_process(writer: Move[BytesIO]) -> Thread:
- thread = Thread(target=thread_unsafe_process, args=(writer,))
- thread.start()
- return thread
- def thread_unsafe_process(writer: BytesIO):
- while True:
- sleep(1)
- writer.write(b'Hello!')
- file = open('move.py')
- read_and_close_file(file)
- file.read() # Mistake, file is already closed, static analyzer cah check that
- model = Model([1, 2, 3])
- y = make_list_from_model(model)
- print(model.x) # Using of corrupted value, it might be unexpected for caller and cause some mistakes
- writer = open('buffer.tmp', 'wb')
- run_thread_unsafe_process(writer)
- writer.write(b'world') # mistake, can lead to data-race (in this case, two messages, "hello" and "world", might mix up)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement