-
Notifications
You must be signed in to change notification settings - Fork 15
/
utils.py
43 lines (33 loc) · 1.01 KB
/
utils.py
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
36
37
38
39
40
41
42
43
# -*- coding: utf-8 -*-
"""
"""
import os
import sys
import io
def btoi(b):
return int.from_bytes(b, "big")
def itob(i, baselen):
return int.to_bytes(int(i), length=baselen, byteorder="big")
class SuppressStream(object):
"""
Suppress errors (being printed by FPLLL, which are to be expected).
"""
def __init__(self, stream=sys.stderr):
try:
self.orig_stream_fileno = stream.fileno()
self.skip = False
except io.UnsupportedOperation:
self.skip = True
def __enter__(self):
if self.skip:
return
self.orig_stream_dup = os.dup(self.orig_stream_fileno)
self.devnull = open(os.devnull, "w")
os.dup2(self.devnull.fileno(), self.orig_stream_fileno)
def __exit__(self, type, value, traceback):
if self.skip:
return
os.close(self.orig_stream_fileno)
os.dup2(self.orig_stream_dup, self.orig_stream_fileno)
os.close(self.orig_stream_dup)
self.devnull.close()