Skip to content
Permalink
Browse files

Merge pull request #511 from davidsch/class_to_s

Class to_s
  • Loading branch information
alex committed Mar 19, 2013
2 parents 8d5f4a1 + 7416224 commit 5c9d5fd045c8e491a722dccac60b3a696b3527f1
@@ -18,5 +18,6 @@ Authors
* Thomas Pelletier
* Greg Price
* Armin Rigo
* David Schneider
* Robin Schreiber
* Jeremy Thurgood
@@ -1,4 +1,8 @@
class TestClassObject(object):
import pytest
from ..base import BaseTopazTest


class TestClassObject(BaseTopazTest):
def test_name(self, space):
space.execute("Class")

@@ -9,6 +13,33 @@ def test_to_s(self, space):
w_res = space.execute("return 1.class.class.to_s")
assert space.str_w(w_res) == "Class"

def test_anon_class_to_s(self, space):
w_res = space.execute("return Class.new.to_s")
assert space.str_w(w_res).startswith("#<Class:0x")

w_res = space.execute("return Class.new.new.to_s")
assert space.str_w(w_res).startswith("#<#<Class:0x")

@pytest.mark.xfail
def test_singletonclass_to_s(self, space):
w_res = space.execute("Class.new.singleton_class.to_s")
assert space.str_w(w_res).startswith("#<Class:#<Class:0x")

def test_anon_class_name(self, space):
w_res = space.execute("return Class.new.name")
assert w_res is space.w_nil

def test_anon_class_method_missing_raises(self, space):
with self.raises(space, "NoMethodError"):
space.execute("""
class A; end
Class.new.does_not_exist
""")

def test_singletonclass_name(self, space):
w_res = space.execute("Class.new.singleton_class.name")
assert w_res is space.w_nil

def test_class_new(self, space):
w_res = space.execute("return Class.new.superclass.name")
assert space.str_w(w_res) == "Object"
@@ -2,6 +2,8 @@

from ..base import BaseTopazTest

import pytest


class TestModuleObject(BaseTopazTest):
def test_name(self, space):
@@ -350,6 +352,23 @@ def test_const_set(self, space):
""")
assert self.unwrap(space, w_res) == ['ZzŻżŹź', 'utf_8_is_legal']

def test_to_s(self, space):
w_res = space.execute("return Kernel.class.to_s")
assert space.str_w(w_res) == "Module"

def test_anon_module_to_s(self, space):
w_res = space.execute("return Module.new.to_s")
assert space.str_w(w_res).startswith("#<Module:0x")

@pytest.mark.xfail
def test_singletonclass_to_s(self, space):
w_res = space.execute("Module.new.singleton_class.to_s")
assert space.str_w(w_res).startswith("#<Class:#<Module:0x")

def test_anon_class_name(self, space):
w_res = space.execute("return Module.new.name")
assert w_res is space.w_nil


class TestMethodVisibility(object):
def test_private(self, space):
@@ -264,8 +264,9 @@ def LOAD_CLASS_VAR(self, space, bytecode, frame, pc, idx):
assert isinstance(w_module, W_ModuleObject)
w_value = space.find_class_var(w_module, name)
if w_value is None:
module_name = space.obj_to_s(w_module)
raise space.error(space.w_NameError,
"uninitialized class variable %s in %s" % (name, w_module.name)
"uninitialized class variable %s in %s" % (name, module_name)
)
frame.push(w_value)

@@ -370,8 +371,9 @@ def BUILD_CLASS(self, space, bytecode, frame, pc):
if superclass is space.w_nil:
superclass = space.w_object
if not space.is_kind_of(superclass, space.w_class):
cls_name = space.obj_to_s(space.getclass(superclass))
raise space.error(space.w_TypeError,
"wrong argument type %s (expected Class)" % space.getclass(superclass).name
"wrong argument type %s (expected Class)" % cls_name
)
assert isinstance(superclass, W_ClassObject)
w_cls = space.newclass(name, superclass)
@@ -23,7 +23,9 @@ def lt(self, a, b):
if w_cmp_res is self.space.w_nil:
raise self.space.error(
self.space.w_ArgumentError,
"comparison of %s with %s failed" % (self.space.getclass(a).name, self.space.getclass(b).name)
"comparison of %s with %s failed" %
(self.space.obj_to_s(self.space.getclass(a)),
self.space.obj_to_s(self.space.getclass(b)))
)
else:
return self.space.int_w(w_cmp_res) < 0
@@ -119,7 +119,8 @@ def method_coerce(self, space, w_other):
])
else:
raise space.error(space.w_TypeError,
"can't coerce %s to Bignum" % space.getclass(w_other).name
"can't coerce %s to Bignum" %
space.obj_to_s(space.getclass(w_other))
)

@classdef.method("**")
@@ -148,5 +149,6 @@ def method_pow(self, space, w_other):
else:
raise space.error(
space.w_TypeError,
"%s can't be coerced into Bignum" % space.getclass(w_other).name
"%s can't be coerced into Bignum" %
space.obj_to_s(space.getclass(w_other))
)
@@ -33,8 +33,12 @@ def getsingletonclass(self, space):
singleton_superclass = space.w_class
else:
singleton_superclass = self.superclass.getsingletonclass(space)
if self.name is None:
name = None
else:
name = "#<Class:%s>" % self.name
self.klass = space.newclass(
"#<Class:%s>" % self.name, singleton_superclass, is_singleton=True
name, singleton_superclass, is_singleton=True
)
return self.klass

@@ -76,7 +80,7 @@ def singleton_method_allocate(self, space, w_superclass=None):
assert isinstance(w_superclass, W_ClassObject)
else:
w_superclass = space.w_object
return space.newclass("", w_superclass)
return space.newclass(None, w_superclass)

@classdef.method("initialize")
def method_initialize(self, space, args_w):
@@ -155,7 +155,8 @@ def method_pow(self, space, w_other):
else:
raise space.error(
space.w_TypeError,
"%s can't be coerced into Fixnum" % space.getclass(w_other).name
"%s can't be coerced into Fixnum" %
space.obj_to_s(space.getclass(w_other))
)

def method_pow_int_impl(self, space, w_other):
@@ -256,13 +256,14 @@ def set_method_visibility(self, space, name, visibility):

@classdef.singleton_method("allocate")
def method_allocate(self, space):
# TODO: this should really store None for the name and all places
# reading the name should handle None
return W_ModuleObject(space, "")
return W_ModuleObject(space, None)

@classdef.method("to_s")
def method_to_s(self, space):
return space.newstr_fromstr(self.name)
name = self.name
if name is None:
return space.newstr_fromstr(space.any_to_s(self))
return space.newstr_fromstr(name)

@classdef.method("include")
def method_include(self, space, w_mod):
@@ -353,7 +354,10 @@ def method_included(self, space, w_mod):

@classdef.method("name")
def method_name(self, space):
return space.newstr_fromstr(self.name)
name = self.name
if name is None:
return space.w_nil
return space.newstr_fromstr(name)

@classdef.method("private")
def method_private(self, space, args_w):
@@ -403,8 +407,9 @@ def method_const_get(self, space, const, inherit=True):
else:
w_res = self.find_local_const(space, const)
if w_res is None:
name = space.obj_to_s(self)
raise space.error(space.w_NameError,
"uninitialized constant %s::%s" % (self.name, const)
"uninitialized constant %s::%s" % (name, const)
)
return w_res

@@ -433,8 +438,9 @@ def method_instance_method(self, space, name):
def method_undef_method(self, space, name):
w_method = self.find_method(space, name)
if w_method is None or isinstance(w_method, UndefMethod):
cls_name = space.obj_to_s(self)
raise space.error(space.w_NameError,
"undefined method `%s' for class `%s'" % (name, self.name)
"undefined method `%s' for class `%s'" % (name, cls_name)
)
self.define_method(space, name, UndefMethod(name))
return self
@@ -443,8 +449,9 @@ def method_undef_method(self, space, name):
def method_remove_method(self, space, name):
w_method = self._find_method_pure(space, name, self.version)
if w_method is None or isinstance(w_method, UndefMethod):
cls_name = space.obj_to_s(self)
raise space.error(space.w_NameError,
"method `%s' not defined in %s" % (name, self.name)
"method `%s' not defined in %s" % (name, cls_name)
)
self.define_method(space, name, UndefMethod(name))
return self
@@ -19,7 +19,8 @@ def retry_binop_coercing(space, w_recv, w_arg, binop, raise_error=True):
if raise_error:
raise space.error(space.w_ArgumentError,
"comparison of %s with %s failed" % (
space.getclass(w_recv).name, space.getclass(w_arg).name
space.obj_to_s(space.getclass(w_recv)),
space.obj_to_s(space.getclass(w_arg))
)
)
if space.getclass(w_ary) is space.w_array:
@@ -65,7 +65,8 @@ def method___id__(self, space):
@classdef.method("method_missing")
def method_method_missing(self, space, w_name, args_w):
name = space.symbol_w(w_name)
class_name = space.str_w(space.send(self.getclass(space), space.newsymbol("name")))
class_name = space.str_w(space.send(self.getclass(space),
space.newsymbol("to_s")))
raise space.error(space.w_NoMethodError,
"undefined method `%s' for %s" % (name, class_name)
)
@@ -133,7 +134,7 @@ def method_extend(self, space, w_mod):
if space.is_kind_of(w_mod, space.w_class):
name = "Class"
else:
name = space.getclass(w_mod).name
name = space.obj_to_s(space.getclass(w_mod))
raise space.error(
space.w_TypeError,
"wrong argument type %s (expected Module)" % name
@@ -535,7 +535,8 @@ def method_index(self, space, w_sub, offset=0):
else:
raise space.error(
space.w_TypeError,
"type mismatch: %s given" % space.getclass(w_sub).name
"type mismatch: %s given" %
space.obj_to_s(space.getclass(w_sub))
)

@classdef.method("rindex", end="int")
@@ -562,7 +563,8 @@ def method_rindex(self, space, w_sub, end=0):
else:
raise space.error(
space.w_TypeError,
"type mismatch: %s given" % space.getclass(w_sub).name
"type mismatch: %s given" %
space.obj_to_s(space.getclass(w_sub))
)
if idx < 0:
return space.w_nil
@@ -636,7 +638,8 @@ def method_split(self, space, w_sep=None, limit=0):
else:
raise space.error(
space.w_TypeError,
"wrong argument type %s (expected Regexp)" % space.getclass(w_sep).name
"wrong argument type %s (expected Regexp)" %
space.obj_to_s(space.getclass(w_sep))
)

@classdef.method("swapcase!")
@@ -853,7 +856,8 @@ def gsub_main(self, space, w_pattern, w_replacement, block, first_only):
else:
raise space.error(
space.w_TypeError,
"wrong argument type %s (expected Regexp)" % space.getclass(w_replacement).name
"wrong argument type %s (expected Regexp)" %
space.obj_to_s(space.getclass(w_replacement))
)

def gsub_regexp(self, space, w_pattern, replacement, w_hash, block, first_only):
@@ -350,6 +350,7 @@ def newstr_fromchars(self, chars):
return W_StringObject.newstr_fromchars(self, chars)

def newstr_fromstr(self, strvalue):
assert strvalue is not None
return W_StringObject.newstr_fromstr(self, strvalue)

def newstr_fromstrs(self, strs_w):
@@ -383,7 +384,8 @@ def newmethod(self, name, w_cls):
if w_function is None:
raise self.error(
self.w_NameError,
"undefined method `%s' for class `%s'" % (name, w_cls.name)
"undefined method `%s' for class `%s'" % (name,
self.obj_to_s(w_cls))
)
else:
return W_UnboundMethodObject(self, w_cls, w_function)
@@ -656,26 +658,31 @@ def convert_type(self, w_obj, w_cls, method, raise_error=True):
except RubyError:
if not raise_error:
return self.w_nil
src_cls = self.getclass(w_obj).name
src_cls_name = self.obj_to_s(self.getclass(w_obj))
w_cls_name = self.obj_to_s(w_cls)
raise self.error(
self.w_TypeError, "can't convert %s into %s" % (src_cls, w_cls.name)
self.w_TypeError, "can't convert %s into %s" % (src_cls_name, w_cls_name)
)

if not w_res or w_res is self.w_nil and not raise_error:
return self.w_nil
elif not self.is_kind_of(w_res, w_cls):
src_cls = self.getclass(w_obj).name
res_cls = self.getclass(w_res).name
src_cls = self.obj_to_s(self.getclass(w_obj))
res_cls = self.obj_to_s(self.getclass(w_res))
w_cls_name = self.obj_to_s(w_cls)
raise self.error(self.w_TypeError,
"can't convert %s to %s (%s#%s gives %s)" % (
src_cls, w_cls.name, src_cls, method, res_cls
src_cls, w_cls_name, src_cls, method, res_cls
)
)
else:
return w_res

def any_to_s(self, w_obj):
return "#<%s:0x%x>" % (
self.str_w(self.send(self.getclass(w_obj), self.newsymbol("name"))),
self.obj_to_s(self.getnonsingletonclass(w_obj)),
self.int_w(self.send(w_obj, self.newsymbol("__id__")))
)

def obj_to_s(self, w_obj):
return self.str_w(self.send(w_obj, self.newsymbol("to_s")))
@@ -12,7 +12,7 @@ def pack_float(space, packer, repetitions):
w_item = packer.args_w[i]
if not (isinstance(w_item, W_FloatObject) or isinstance(w_item, W_FixnumObject)):
raise space.error(space.w_TypeError,
"can't convert %s into Float" % space.getclass(w_item).name
"can't convert %s into Float" % space.obj_to_s(space.getclass(w_item))
)
doubleval = space.float_w(w_item)
l = ["\0"] * size

0 comments on commit 5c9d5fd

Please sign in to comment.