File size: 818 Bytes
0d7e8be
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This source code is licensed under the Chameleon License found in the
# LICENSE file in the root directory of this source tree.

import socket
from typing import Generator, Generic, Iterator, TypeVar

T = TypeVar("T")


class DynamicGenerator(Generic[T]):
    def __init__(self, gen: Generator[T, None, None]):
        self.gen = gen

    def __iter__(self) -> Iterator[T]:
        return self

    def __next__(self) -> T:
        return next(self.gen)


def advance(iterator: Iterator[T], steps: int):
    try:
        for _ in range(steps):
            next(iterator)
    except StopIteration:
        pass


def random_unused_port():
    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
        s.bind(("", 0))
        return s.getsockname()[1]