Skip to content

Commit

Permalink
A couple of flake8 fixes (#866)
Browse files Browse the repository at this point in the history
  • Loading branch information
fniephaus authored and alex committed Mar 16, 2017
1 parent 586eb57 commit 751eb8b
Show file tree
Hide file tree
Showing 13 changed files with 286 additions and 166 deletions.
3 changes: 1 addition & 2 deletions topaz/celldict.py
Expand Up @@ -50,8 +50,7 @@ def getvalue(self, space, name):
def setvalue(self, space, name, w_value): def setvalue(self, space, name, w_value):
if self.setter is None: if self.setter is None:
raise space.error(space.w_NameError, raise space.error(space.w_NameError,
"%s is a read-only variable" % name "%s is a read-only variable" % name)
)
self.setter(space, w_value) self.setter(space, w_value)




Expand Down
23 changes: 15 additions & 8 deletions topaz/coerce.py
Expand Up @@ -8,14 +8,16 @@ def symbol(space, w_obj):
if space.is_kind_of(w_obj, space.w_symbol): if space.is_kind_of(w_obj, space.w_symbol):
return space.symbol_w(w_obj) return space.symbol_w(w_obj)
else: else:
w_str = space.convert_type(w_obj, space.w_string, "to_str", raise_error=False) w_str = space.convert_type(w_obj, space.w_string, "to_str",
raise_error=False)
if w_str is space.w_nil: if w_str is space.w_nil:
w_inspect_str = space.send(w_obj, "inspect") w_inspect_str = space.send(w_obj, "inspect")
if not space.is_kind_of(w_inspect_str, space.w_string): if not space.is_kind_of(w_inspect_str, space.w_string):
inspect_str = space.any_to_s(w_obj) inspect_str = space.any_to_s(w_obj)
else: else:
inspect_str = space.str_w(w_inspect_str) inspect_str = space.str_w(w_inspect_str)
raise space.error(space.w_TypeError, "%s is not a symbol" % inspect_str) raise space.error(space.w_TypeError,
"%s is not a symbol" % inspect_str)
else: else:
return space.str_w(w_str) return space.str_w(w_str)


Expand All @@ -24,11 +26,13 @@ def int(space, w_obj):
if space.is_kind_of(w_obj, space.w_fixnum): if space.is_kind_of(w_obj, space.w_fixnum):
return space.int_w(w_obj) return space.int_w(w_obj)
else: else:
return space.int_w(space.convert_type(w_obj, space.w_integer, "to_int")) return space.int_w(
space.convert_type(w_obj, space.w_integer, "to_int"))


@staticmethod @staticmethod
def bigint(space, w_obj): def bigint(space, w_obj):
return space.bigint_w(space.convert_type(w_obj, space.w_integer, "to_int")) return space.bigint_w(
space.convert_type(w_obj, space.w_integer, "to_int"))


@staticmethod @staticmethod
def float(space, w_obj): def float(space, w_obj):
Expand All @@ -42,19 +46,22 @@ def strictfloat(space, w_obj):
if not space.is_kind_of(w_obj, space.w_numeric): if not space.is_kind_of(w_obj, space.w_numeric):
clsname = w_obj.getclass(space).name clsname = w_obj.getclass(space).name
raise space.error(space.w_TypeError, raise space.error(space.w_TypeError,
"can't convert %s into Float" %clsname) "can't convert %s into Float" % clsname)
return Coerce.float(space, w_obj) return Coerce.float(space, w_obj)


@staticmethod @staticmethod
def str(space, w_obj): def str(space, w_obj):
if space.is_kind_of(w_obj, space.w_string) or space.is_kind_of(w_obj, space.w_symbol): if (space.is_kind_of(w_obj, space.w_string) or
space.is_kind_of(w_obj, space.w_symbol)):
return space.str_w(w_obj) return space.str_w(w_obj)
else: else:
return space.str_w(space.convert_type(w_obj, space.w_string, "to_str")) return space.str_w(
space.convert_type(w_obj, space.w_string, "to_str"))


@staticmethod @staticmethod
def path(space, w_obj): def path(space, w_obj):
w_string = space.convert_type(w_obj, space.w_string, "to_path", raise_error=False) w_string = space.convert_type(w_obj, space.w_string, "to_path",
raise_error=False)
if w_string is space.w_nil: if w_string is space.w_nil:
w_string = space.convert_type(w_obj, space.w_string, "to_str") w_string = space.convert_type(w_obj, space.w_string, "to_str")
return space.str0_w(w_string) return space.str0_w(w_string)
Expand Down
11 changes: 6 additions & 5 deletions topaz/error.py
@@ -1,8 +1,6 @@
import os import os
import errno import errno


from rpython.rlib import jit



class RubyError(Exception): class RubyError(Exception):
def __init__(self, w_value): def __init__(self, w_value):
Expand All @@ -16,11 +14,13 @@ def format_traceback(space, exc, top_filepath):
w_bt = space.send(exc, "backtrace") w_bt = space.send(exc, "backtrace")
bt_w = space.listview(w_bt) bt_w = space.listview(w_bt)
if bt_w: if bt_w:
yield "%s: %s (%s)\n" % (space.str_w(bt_w[0]), exc.msg, space.getclass(exc).name) yield "%s: %s (%s)\n" % (
space.str_w(bt_w[0]), exc.msg, space.getclass(exc).name)
for w_line in bt_w[1:]: for w_line in bt_w[1:]:
yield "\tfrom %s\n" % space.str_w(w_line) yield "\tfrom %s\n" % space.str_w(w_line)
else: else:
yield "%s: %s (%s)\n" % (top_filepath, exc.msg, space.getclass(exc).name) yield "%s: %s (%s)\n" % (
top_filepath, exc.msg, space.getclass(exc).name)




def print_traceback(space, w_exc, top_filepath=None): def print_traceback(space, w_exc, top_filepath=None):
Expand Down Expand Up @@ -51,7 +51,8 @@ def error_for_errno(space, errno):
except KeyError: except KeyError:
w_type = space.w_SystemCallError w_type = space.w_SystemCallError
else: else:
w_type = space.find_const(space.find_const(space.w_object, "Errno"), name) w_type = space.find_const(
space.find_const(space.w_object, "Errno"), name)
return space.error( return space.error(
w_type, w_type,
os.strerror(errno), os.strerror(errno),
Expand Down
9 changes: 6 additions & 3 deletions topaz/executioncontext.py
Expand Up @@ -57,10 +57,13 @@ def invoke_trace_proc(self, space, event, scope_id, classname, frame=None):
space.send(self.w_trace_proc, "call", [ space.send(self.w_trace_proc, "call", [
space.newstr_fromstr(event), space.newstr_fromstr(event),
space.newstr_fromstr(frame.bytecode.filepath), space.newstr_fromstr(frame.bytecode.filepath),
space.newint(frame.bytecode.lineno_table[frame.last_instr]), space.newint(
space.newstr_fromstr(scope_id) if scope_id is not None else space.w_nil, frame.bytecode.lineno_table[frame.last_instr]),
(space.newstr_fromstr(scope_id)
if scope_id is not None else space.w_nil),
space.newbinding_fromframe(frame), space.newbinding_fromframe(frame),
space.newstr_fromstr(classname) if classname is not None else space.w_nil, (space.newstr_fromstr(classname)
if classname is not None else space.w_nil),
]) ])
finally: finally:
self.in_trace_proc = False self.in_trace_proc = False
Expand Down
63 changes: 37 additions & 26 deletions topaz/frame.py
@@ -1,8 +1,6 @@
from rpython.rlib import jit from rpython.rlib import jit


from topaz.coerce import Coerce
from topaz.closure import LocalCell from topaz.closure import LocalCell
from topaz.objects.arrayobject import W_ArrayObject
from topaz.objects.hashobject import W_HashObject from topaz.objects.hashobject import W_HashObject
from topaz.objects.functionobject import W_FunctionObject from topaz.objects.functionobject import W_FunctionObject


Expand All @@ -18,8 +16,8 @@ def __init__(self):
class Frame(BaseFrame): class Frame(BaseFrame):
_virtualizable_ = [ _virtualizable_ = [
"bytecode", "localsstack_w[*]", "stackpos", "w_self", "block", "bytecode", "localsstack_w[*]", "stackpos", "w_self", "block",
"cells[*]", "lastblock", "lexical_scope", "last_instr", "parent_interp", "cells[*]", "lastblock", "lexical_scope", "last_instr",
"top_parent_interp", "parent_interp", "top_parent_interp",
] ]


@jit.unroll_safe @jit.unroll_safe
Expand All @@ -28,10 +26,12 @@ def __init__(self, bytecode, w_self, lexical_scope, block, parent_interp,
self = jit.hint(self, fresh_virtualizable=True, access_directly=True) self = jit.hint(self, fresh_virtualizable=True, access_directly=True)
BaseFrame.__init__(self) BaseFrame.__init__(self)
self.bytecode = bytecode self.bytecode = bytecode
self.localsstack_w = [None] * (len(bytecode.cellvars) + bytecode.max_stackdepth) self.localsstack_w = [None] * (
len(bytecode.cellvars) + bytecode.max_stackdepth)
self.stackpos = len(bytecode.cellvars) self.stackpos = len(bytecode.cellvars)
self.last_instr = 0 self.last_instr = 0
self.cells = [LocalCell() for _ in bytecode.cellvars] + [None] * len(bytecode.freevars) self.cells = ([LocalCell() for _ in bytecode.cellvars] +
[None] * len(bytecode.freevars))
self.regexp_match_cell = regexp_match_cell self.regexp_match_cell = regexp_match_cell
self.w_self = w_self self.w_self = w_self
self.lexical_scope = lexical_scope self.lexical_scope = lexical_scope
Expand All @@ -48,10 +48,14 @@ def _set_arg(self, space, pos, w_value):
def handle_block_args(self, space, bytecode, args_w, block): def handle_block_args(self, space, bytecode, args_w, block):
if (len(args_w) == 1 and ( if (len(args_w) == 1 and (
len(bytecode.arg_pos) >= 2 or ( len(bytecode.arg_pos) >= 2 or (
len(bytecode.arg_pos) > 0 and bytecode.splat_arg_pos != -1))): len(bytecode.arg_pos) > 0 and
bytecode.splat_arg_pos != -1))):
w_arg = args_w[0] w_arg = args_w[0]
if not space.is_kind_of(w_arg, space.w_array) and space.respond_to(w_arg, "to_ary"): if (not space.is_kind_of(w_arg, space.w_array) and
w_arg = space.convert_type(w_arg, space.w_array, "to_ary", raise_error=True, reraise_error=True) space.respond_to(w_arg, "to_ary")):
w_arg = space.convert_type(w_arg, space.w_array, "to_ary",
raise_error=True,
reraise_error=True)
if space.is_kind_of(w_arg, space.w_array): if space.is_kind_of(w_arg, space.w_array):
args_w = space.listview(w_arg) args_w = space.listview(w_arg)
minargc = len(bytecode.arg_pos) - len(bytecode.defaults) minargc = len(bytecode.arg_pos) - len(bytecode.defaults)
Expand All @@ -69,35 +73,42 @@ def handle_args(self, space, bytecode, args_w, block):
keywords_hash = None keywords_hash = None
if len(bytecode.kwarg_names) > 0 or bytecode.kwrest_pos != -1: if len(bytecode.kwarg_names) > 0 or bytecode.kwrest_pos != -1:
# we only take the hash if we have more than enough arguments # we only take the hash if we have more than enough arguments
if len(args_w) > 0 and len(args_w) > (len(bytecode.arg_pos) - len(bytecode.defaults)): min_args = max(len(bytecode.arg_pos) - len(bytecode.defaults), 0)
if len(args_w) > min_args:
w_obj = args_w[-1] w_obj = args_w[-1]
if not space.is_kind_of(w_obj, space.w_hash): if not space.is_kind_of(w_obj, space.w_hash):
w_obj = space.convert_type(w_obj, space.w_hash, "to_hash", reraise_error=True) w_obj = space.convert_type(
w_obj, space.w_hash, "to_hash", reraise_error=True)
if isinstance(w_obj, W_HashObject): if isinstance(w_obj, W_HashObject):
keywords_hash = space.send(w_obj, "clone") keywords_hash = space.send(w_obj, "clone")
assert isinstance(keywords_hash, W_HashObject) assert isinstance(keywords_hash, W_HashObject)


if len(bytecode.kw_defaults) < len(bytecode.kwarg_names) and not keywords_hash: if (len(bytecode.kw_defaults) < len(bytecode.kwarg_names) and
raise space.error(space.w_ArgumentError, not keywords_hash):
"missing keywords: %s" % ",".join(bytecode.kwarg_names) raise space.error(
) space.w_ArgumentError,
"missing keywords: %s" % ",".join(bytecode.kwarg_names))


pre = 0 pre = 0
post = len(args_w) if keywords_hash is None else len(args_w) - 1 post = len(args_w) if keywords_hash is None else len(args_w) - 1


if (post < (len(bytecode.arg_pos) - len(bytecode.defaults)) or if (post < (len(bytecode.arg_pos) - len(bytecode.defaults)) or
(bytecode.splat_arg_pos == -1 and post > len(bytecode.arg_pos))): (bytecode.splat_arg_pos == -1 and
raise space.error(space.w_ArgumentError, post > len(bytecode.arg_pos))):
"wrong number of arguments (%d for %d)" % (len(args_w), len(bytecode.arg_pos) - len(bytecode.defaults)) raise space.error(
) space.w_ArgumentError,
"wrong number of arguments (%d for %d)" % (
len(args_w),
len(bytecode.arg_pos) - len(bytecode.defaults)))


if bytecode.default_arg_begin != -1: if bytecode.default_arg_begin != -1:
len_pre_args = bytecode.default_arg_begin len_pre_args = bytecode.default_arg_begin
elif bytecode.splat_arg_pos != -1: elif bytecode.splat_arg_pos != -1:
len_pre_args = bytecode.splat_arg_pos len_pre_args = bytecode.splat_arg_pos
else: else:
len_pre_args = len(bytecode.arg_pos) len_pre_args = len(bytecode.arg_pos)
len_post_arg = len(bytecode.arg_pos) - len(bytecode.defaults) - len_pre_args len_post_arg = (len(bytecode.arg_pos) - len(bytecode.defaults) -
len_pre_args)


# [required args, optional args, splat arg, required args, keywords args, keyword rest, block] # [required args, optional args, splat arg, required args, keywords args, keyword rest, block]
# ^ ^ # ^ ^
Expand Down Expand Up @@ -153,9 +164,9 @@ def handle_args(self, space, bytecode, args_w, block):
try: try:
bc = bytecode.kw_defaults[i] bc = bytecode.kw_defaults[i]
except IndexError: except IndexError:
raise space.error(space.w_ArgumentError, raise space.error(
"missing keyword: %s" % name space.w_ArgumentError,
) "missing keyword: %s" % name)
self.bytecode = bc self.bytecode = bc
w_value = Interpreter().interpret(space, self, bc) w_value = Interpreter().interpret(space, self, bc)
self._set_arg(space, bytecode.cellvars.index(name), w_value) self._set_arg(space, bytecode.cellvars.index(name), w_value)
Expand All @@ -169,11 +180,11 @@ def handle_args(self, space, bytecode, args_w, block):
self._set_arg(space, bytecode.kwrest_pos, space.newhash()) self._set_arg(space, bytecode.kwrest_pos, space.newhash())
elif keywords_hash is not None: elif keywords_hash is not None:
if keywords_hash.size() > 0: if keywords_hash.size() > 0:
raise space.error(space.w_ArgumentError, raise space.error(
space.w_ArgumentError,
"unknown keywords: %s" % space.str_w( "unknown keywords: %s" % space.str_w(
space.send(space.send(keywords_hash, "keys"), "to_s") space.send(space.send(keywords_hash, "keys"), "to_s")
) ))
)


if bytecode.block_arg_pos != -1: if bytecode.block_arg_pos != -1:
if block is None: if block is None:
Expand Down
15 changes: 9 additions & 6 deletions topaz/gateway.py
Expand Up @@ -42,10 +42,11 @@ def generate_wrapper(self):
@functools.wraps(self.func) @functools.wraps(self.func)
def wrapper(self, space, args_w, block): def wrapper(self, space, args_w, block):
if (len(args_w) < min_args or if (len(args_w) < min_args or
(not takes_args_w and len(args_w) > argcount)): (not takes_args_w and len(args_w) > argcount)):
raise space.error(space.w_ArgumentError, raise space.error(
"wrong number of arguments (%d for %d)" % (len(args_w), min_args) space.w_ArgumentError,
) "wrong number of arguments (%d for %d)" % (
len(args_w), min_args))
args = () args = ()
arg_count = 0 arg_count = 0
args_w_seen = False args_w_seen = False
Expand All @@ -64,12 +65,14 @@ def wrapper(self, space, args_w, block):
args += (space,) args += (space,)
elif argname.startswith("w_") or argname in argspec: elif argname.startswith("w_") or argname in argspec:
if args_w_seen: if args_w_seen:
raise SystemError("args_w must be the last argument accepted") raise SystemError(
"args_w must be the last argument accepted")
if len(args_w) > arg_count: if len(args_w) > arg_count:
if argname.startswith("w_"): if argname.startswith("w_"):
args += (args_w[arg_count],) args += (args_w[arg_count],)
elif argname in argspec: elif argname in argspec:
args += (getattr(Coerce, argspec[argname])(space, args_w[arg_count]),) args += (getattr(Coerce, argspec[argname])(
space, args_w[arg_count]),)
elif default_start is not None and i >= default_start: elif default_start is not None and i >= default_start:
args += (defaults[i - default_start],) args += (defaults[i - default_start],)
else: else:
Expand Down

0 comments on commit 751eb8b

Please sign in to comment.