Skip to content
Permalink
Browse files

A couple of flake8 fixes (#866)

  • Loading branch information
fniephaus authored and alex committed Mar 16, 2017
1 parent 586eb57 commit 751eb8bce58e94e077e5dccd531a294c29adee1a
Showing with 286 additions and 166 deletions.
  1. +1 −2 topaz/celldict.py
  2. +15 −8 topaz/coerce.py
  3. +6 −5 topaz/error.py
  4. +6 −3 topaz/executioncontext.py
  5. +37 −26 topaz/frame.py
  6. +9 −6 topaz/gateway.py
  7. +82 −43 topaz/interpreter.py
  8. +5 −2 topaz/main.py
  9. +24 −10 topaz/mapdict.py
  10. +21 −10 topaz/module.py
  11. +74 −48 topaz/objspace.py
  12. +3 −2 topaz/parser.py
  13. +3 −1 topaz/scope.py
@@ -50,8 +50,7 @@ def getvalue(self, space, name):
def setvalue(self, space, name, w_value):
if self.setter is None:
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)


@@ -8,14 +8,16 @@ def symbol(space, w_obj):
if space.is_kind_of(w_obj, space.w_symbol):
return space.symbol_w(w_obj)
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:
w_inspect_str = space.send(w_obj, "inspect")
if not space.is_kind_of(w_inspect_str, space.w_string):
inspect_str = space.any_to_s(w_obj)
else:
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:
return space.str_w(w_str)

@@ -24,11 +26,13 @@ def int(space, w_obj):
if space.is_kind_of(w_obj, space.w_fixnum):
return space.int_w(w_obj)
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
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
def float(space, w_obj):
@@ -42,19 +46,22 @@ def strictfloat(space, w_obj):
if not space.is_kind_of(w_obj, space.w_numeric):
clsname = w_obj.getclass(space).name
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)

@staticmethod
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)
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
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:
w_string = space.convert_type(w_obj, space.w_string, "to_str")
return space.str0_w(w_string)
@@ -1,8 +1,6 @@
import os
import errno

from rpython.rlib import jit


class RubyError(Exception):
def __init__(self, w_value):
@@ -16,11 +14,13 @@ def format_traceback(space, exc, top_filepath):
w_bt = space.send(exc, "backtrace")
bt_w = space.listview(w_bt)
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:]:
yield "\tfrom %s\n" % space.str_w(w_line)
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):
@@ -51,7 +51,8 @@ def error_for_errno(space, errno):
except KeyError:
w_type = space.w_SystemCallError
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(
w_type,
os.strerror(errno),
@@ -57,10 +57,13 @@ def invoke_trace_proc(self, space, event, scope_id, classname, frame=None):
space.send(self.w_trace_proc, "call", [
space.newstr_fromstr(event),
space.newstr_fromstr(frame.bytecode.filepath),
space.newint(frame.bytecode.lineno_table[frame.last_instr]),
space.newstr_fromstr(scope_id) if scope_id is not None else space.w_nil,
space.newint(
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.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:
self.in_trace_proc = False
@@ -1,8 +1,6 @@
from rpython.rlib import jit

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

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

@jit.unroll_safe
@@ -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)
BaseFrame.__init__(self)
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.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.w_self = w_self
self.lexical_scope = lexical_scope
@@ -48,10 +48,14 @@ def _set_arg(self, space, pos, w_value):
def handle_block_args(self, space, bytecode, args_w, block):
if (len(args_w) == 1 and (
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]
if not space.is_kind_of(w_arg, space.w_array) and 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 (not space.is_kind_of(w_arg, space.w_array) and
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):
args_w = space.listview(w_arg)
minargc = len(bytecode.arg_pos) - len(bytecode.defaults)
@@ -69,35 +73,42 @@ def handle_args(self, space, bytecode, args_w, block):
keywords_hash = None
if len(bytecode.kwarg_names) > 0 or bytecode.kwrest_pos != -1:
# 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]
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):
keywords_hash = space.send(w_obj, "clone")
assert isinstance(keywords_hash, W_HashObject)

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

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

if (post < (len(bytecode.arg_pos) - len(bytecode.defaults)) or
(bytecode.splat_arg_pos == -1 and post > len(bytecode.arg_pos))):
raise space.error(space.w_ArgumentError,
"wrong number of arguments (%d for %d)" % (len(args_w), len(bytecode.arg_pos) - len(bytecode.defaults))
)
(bytecode.splat_arg_pos == -1 and
post > len(bytecode.arg_pos))):
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:
len_pre_args = bytecode.default_arg_begin
elif bytecode.splat_arg_pos != -1:
len_pre_args = bytecode.splat_arg_pos
else:
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]
# ^ ^
@@ -153,9 +164,9 @@ def handle_args(self, space, bytecode, args_w, block):
try:
bc = bytecode.kw_defaults[i]
except IndexError:
raise space.error(space.w_ArgumentError,
"missing keyword: %s" % name
)
raise space.error(
space.w_ArgumentError,
"missing keyword: %s" % name)
self.bytecode = bc
w_value = Interpreter().interpret(space, self, bc)
self._set_arg(space, bytecode.cellvars.index(name), w_value)
@@ -169,11 +180,11 @@ def handle_args(self, space, bytecode, args_w, block):
self._set_arg(space, bytecode.kwrest_pos, space.newhash())
elif keywords_hash is not None:
if keywords_hash.size() > 0:
raise space.error(space.w_ArgumentError,
raise space.error(
space.w_ArgumentError,
"unknown keywords: %s" % space.str_w(
space.send(space.send(keywords_hash, "keys"), "to_s")
)
)
))

if bytecode.block_arg_pos != -1:
if block is None:
@@ -42,10 +42,11 @@ def generate_wrapper(self):
@functools.wraps(self.func)
def wrapper(self, space, args_w, block):
if (len(args_w) < min_args or
(not takes_args_w and len(args_w) > argcount)):
raise space.error(space.w_ArgumentError,
"wrong number of arguments (%d for %d)" % (len(args_w), min_args)
)
(not takes_args_w and len(args_w) > argcount)):
raise space.error(
space.w_ArgumentError,
"wrong number of arguments (%d for %d)" % (
len(args_w), min_args))
args = ()
arg_count = 0
args_w_seen = False
@@ -64,12 +65,14 @@ def wrapper(self, space, args_w, block):
args += (space,)
elif argname.startswith("w_") or argname in argspec:
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 argname.startswith("w_"):
args += (args_w[arg_count],)
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:
args += (defaults[i - default_start],)
else:

0 comments on commit 751eb8b

Please sign in to comment.
You can’t perform that action at this time.