def __call__(self, test): old = self.old or test.init_old(test.cloned_library) legacy = self.legacy or test.init_legacy(test.cloned_library) oldres = getattr(old, self.func_name)(*self.args, **self.kwargs) newres = getattr(legacy, self.func_name)(*self.args, **self.kwargs) test.assertEqual(oldres, newres, 'Equivalence test for %s with args: %s and kwargs: %s failed' % ( self.func_name, reprlib.repr(self.args), reprlib.repr(self.kwargs))) self.retval = newres return newres
def read_chunk_length(self, inheaders, line_buf, buf, bytes_read, event): line = self.readline(line_buf) if line is None: return bytes_read[0] += len(line) try: chunk_size = int(line.strip(), 16) except Exception: return self.simple_response( http_client.BAD_REQUEST, '%s is not a valid chunk size' % reprlib.repr(line.strip())) if bytes_read[0] + chunk_size + 2 > self.max_request_body_size: return self.simple_response( http_client.REQUEST_ENTITY_TOO_LARGE, 'Chunked request is larger than %d bytes' % self.max_request_body_size) if chunk_size == 0: self.set_state(READ, self.read_chunk_separator, inheaders, Accumulator(), buf, bytes_read, last=True) else: self.set_state(READ, self.read_chunk, inheaders, buf, chunk_size, buf.tell() + chunk_size, bytes_read)
def parse_part(): line = buf.readline() if not line: raise ValueError('Premature end of message') if not line.startswith(b'--' + sep): raise ValueError('Malformed start of multipart message: %s' % reprlib.repr(line)) if line.endswith(b'--'): return None headers = read_headers(buf.readline) cr = headers.get('Content-Range') if not cr: raise ValueError('Missing Content-Range header in sub-part') if not cr.startswith('bytes '): raise ValueError( 'Malformed Content-Range header in sub-part, no prefix') try: start, stop = map( lambda x: int(x.strip()), cr.partition(' ')[-1].partition('/')[0].partition('-')[::2]) except Exception: raise ValueError( 'Malformed Content-Range header in sub-part, failed to parse byte range' ) content_length = stop - start + 1 ret = buf.read(content_length) if len(ret) != content_length: raise ValueError( 'Malformed sub-part, length of body not equal to length specified in Content-Range' ) buf.readline() return (start, ret)
def run(self): try: self.connect() while True: func, args, kwargs = self.requests.get() if func == self.CLOSE: self.conn.close() break if func == 'dump': try: ok, res = True, tuple(self.conn.iterdump()) except Exception as err: ok, res = False, (err, traceback.format_exc()) elif func == 'create_dynamic_filter': try: f = DynamicFilter(args[0]) self.conn.create_function(args[0], 1, f) ok, res = True, f except Exception as err: ok, res = False, (err, traceback.format_exc()) else: bfunc = getattr(self.conn, func) try: for i in range(3): try: ok, res = True, bfunc(*args, **kwargs) break except OperationalError as err: # Retry if unable to open db file e = str(err) if 'unable to open' not in e or i == 2: if 'unable to open' in e: prints( 'Unable to open database for func', func, reprlib.repr(args), reprlib.repr(kwargs)) raise time.sleep(0.5) except Exception as err: ok, res = False, (err, traceback.format_exc()) self.results.put((ok, res)) except Exception as err: self.unhandled_error = (err, traceback.format_exc())
def run(self): try: self.connect() while True: func, args, kwargs = self.requests.get() if func == self.CLOSE: self.conn.close() break if func == 'dump': try: ok, res = True, tuple(self.conn.iterdump()) except Exception as err: ok, res = False, (err, traceback.format_exc()) elif func == 'create_dynamic_filter': try: f = DynamicFilter(args[0]) self.conn.create_function(args[0], 1, f) ok, res = True, f except Exception as err: ok, res = False, (err, traceback.format_exc()) else: bfunc = getattr(self.conn, func) try: for i in range(3): try: ok, res = True, bfunc(*args, **kwargs) break except OperationalError as err: # Retry if unable to open db file e = str(err) if 'unable to open' not in e or i == 2: if 'unable to open' in e: prints('Unable to open database for func', func, reprlib.repr(args), reprlib.repr(kwargs)) raise time.sleep(0.5) except Exception as err: ok, res = False, (err, traceback.format_exc()) self.results.put((ok, res)) except Exception as err: self.unhandled_error = (err, traceback.format_exc())
def commit(): if not self.lines: return line = b' '.join(self.lines) del self.lines[:] k, v = line.partition(b':')[::2] key = normalize_header_name(k.strip().decode('ascii')) val = safe_decode(key, v.strip()) if not key or not val: raise ValueError('Malformed header line: %s' % reprlib.repr(line)) if key in comma_separated_headers: existing = self.hdict.pop(key) if existing is not None: val = existing + ', ' + val self.hdict[key] = val
def parse_part(): line = buf.readline() if not line: raise ValueError('Premature end of message') if not line.startswith(b'--' + sep): raise ValueError('Malformed start of multipart message: %s' % reprlib.repr(line)) if line.endswith(b'--'): return None headers = read_headers(buf.readline) cr = headers.get('Content-Range') if not cr: raise ValueError('Missing Content-Range header in sub-part') if not cr.startswith('bytes '): raise ValueError('Malformed Content-Range header in sub-part, no prefix') try: start, stop = map(lambda x: int(x.strip()), cr.partition(' ')[-1].partition('/')[0].partition('-')[::2]) except Exception: raise ValueError('Malformed Content-Range header in sub-part, failed to parse byte range') content_length = stop - start + 1 ret = buf.read(content_length) if len(ret) != content_length: raise ValueError('Malformed sub-part, length of body not equal to length specified in Content-Range') buf.readline() return (start, ret)
def prints(*args, **kwargs): ''' Print unicode arguments safely by encoding them to preferred_encoding Has the same signature as the print function from Python 3, except for the additional keyword argument safe_encode, which if set to True will cause the function to use repr when encoding fails. Returns the number of bytes written. ''' file = kwargs.get('file', sys.stdout) file = getattr(file, 'buffer', file) enc = 'utf-8' if hasenv('CALIBRE_WORKER') else preferred_encoding sep = kwargs.get('sep', ' ') if not isinstance(sep, bytes): sep = sep.encode(enc) end = kwargs.get('end', '\n') if not isinstance(end, bytes): end = end.encode(enc) safe_encode = kwargs.get('safe_encode', False) count = 0 for i, arg in enumerate(args): if isinstance(arg, unicode_type): if iswindows: from calibre.utils.terminal import Detect cs = Detect(file) if cs.is_console: cs.write_unicode_text(arg) count += len(arg) if i != len(args)-1: file.write(sep) count += len(sep) continue try: arg = arg.encode(enc) except UnicodeEncodeError: try: arg = arg.encode('utf-8') except: if not safe_encode: raise arg = repr(arg) if not isinstance(arg, bytes): try: arg = native_string_type(arg) except ValueError: arg = unicode_type(arg) if isinstance(arg, unicode_type): try: arg = arg.encode(enc) except UnicodeEncodeError: try: arg = arg.encode('utf-8') except: if not safe_encode: raise arg = repr(arg) try: file.write(arg) count += len(arg) except: from polyglot import reprlib arg = reprlib.repr(arg) file.write(arg) count += len(arg) if i != len(args)-1: file.write(sep) count += len(sep) file.write(end) count += len(end) return count
def __repr__(self): return '{' + ', '.join('%s: %s' % (reprlib.repr(k), reprlib.repr(v)) for k, v in iteritems(self)) + '}'
def read_chunk_length(self, inheaders, line_buf, buf, bytes_read, event): line = self.readline(line_buf) if line is None: return bytes_read[0] += len(line) try: chunk_size = int(line.strip(), 16) except Exception: return self.simple_response(http_client.BAD_REQUEST, '%s is not a valid chunk size' % reprlib.repr(line.strip())) if bytes_read[0] + chunk_size + 2 > self.max_request_body_size: return self.simple_response(http_client.REQUEST_ENTITY_TOO_LARGE, 'Chunked request is larger than %d bytes' % self.max_request_body_size) if chunk_size == 0: self.set_state(READ, self.read_chunk_separator, inheaders, Accumulator(), buf, bytes_read, last=True) else: self.set_state(READ, self.read_chunk, inheaders, buf, chunk_size, buf.tell() + chunk_size, bytes_read)