mirror of https://github.com/KLayout/klayout.git
Experimental: calling str(x) implicitly in RBA and pya
This commit is contained in:
parent
5961eab84b
commit
ef9d73f178
|
|
@ -43,9 +43,26 @@ class PythonBasedStringAdaptor
|
|||
{
|
||||
public:
|
||||
PythonBasedStringAdaptor (const PythonPtr &string)
|
||||
: m_stdstr (python2c<std::string> (string.get ())), m_string (string)
|
||||
: m_string (string)
|
||||
{
|
||||
// .. nothing yet ..
|
||||
#if PY_MAJOR_VERSION < 3
|
||||
if (PyString_Check (string.get ())) {
|
||||
m_stdstr = python2c<std::string> (string.get ());
|
||||
} else
|
||||
#else
|
||||
if (PyBytes_Check (string.get ())) {
|
||||
m_stdstr = python2c<std::string> (string.get ());
|
||||
} else
|
||||
#endif
|
||||
if (PyUnicode_Check (string.get ()) || PyByteArray_Check (string.get ())) {
|
||||
m_stdstr = python2c<std::string> (string.get ());
|
||||
} else {
|
||||
// use object protocol to get the string through str(...)
|
||||
PythonRef as_str (PyObject_Str (string.get ()));
|
||||
if (as_str) {
|
||||
m_stdstr = python2c<std::string> (as_str.get ());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
virtual const char *c_str () const
|
||||
|
|
|
|||
|
|
@ -46,7 +46,7 @@ class RubyBasedStringAdaptor
|
|||
public:
|
||||
RubyBasedStringAdaptor (VALUE value)
|
||||
{
|
||||
m_string = rba_safe_string_value (value);
|
||||
m_string = rba_safe_obj_as_string (value);
|
||||
gc_lock_object (m_string);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -26,6 +26,12 @@ import copy
|
|||
# Set this to True to disable some tests involving exceptions
|
||||
leak_check = "TEST_LEAK_CHECK" in os.environ
|
||||
|
||||
class ObjectWithStr:
|
||||
def __init__(self, s):
|
||||
self.s = s
|
||||
def __str__(self):
|
||||
return self.s
|
||||
|
||||
# see test_21
|
||||
class AEXT(pya.A):
|
||||
def __init__(self):
|
||||
|
|
@ -191,6 +197,7 @@ class BasicTest(unittest.TestCase):
|
|||
self.assertEqual( a.a1(), -0x80000000 )
|
||||
|
||||
self.assertEqual( a.a3("a"), 1 )
|
||||
self.assertEqual( a.a3(ObjectWithStr("abcde")), 5 ) # implicitly using to_s for string conversion
|
||||
self.assertEqual( a.a3("ab"), 2 )
|
||||
self.assertEqual( a.a3("µ"), 2 ) # two UTF8 bytes
|
||||
if "a3_qstr" in a.__dict__:
|
||||
|
|
@ -2279,13 +2286,8 @@ class BasicTest(unittest.TestCase):
|
|||
self.assertEqual(b.map1_cptr_null() == None, True);
|
||||
self.assertEqual(b.map1_ptr_null() == None, True);
|
||||
|
||||
try:
|
||||
# error converting 1 or True to string
|
||||
b.map1 = { 42: 1, -17: True }
|
||||
error_caught = False
|
||||
except:
|
||||
error_caught = True
|
||||
self.assertEqual(error_caught, True)
|
||||
b.map1 = { 42: 1, -17: True }
|
||||
self.assertEqual(map2str(b.map1), "{-17: True, 42: 1}")
|
||||
|
||||
b.map1 = { 42: "1", -17: "True" }
|
||||
self.assertEqual(map2str(b.map1), "{-17: True, 42: 1}")
|
||||
|
|
|
|||
|
|
@ -4,6 +4,19 @@
|
|||
# safe in multiple inclusions
|
||||
require File.expand_path('../basic_testcore_defs', __FILE__)
|
||||
|
||||
|
||||
class ObjectWithStr
|
||||
|
||||
def initialize(s)
|
||||
@s = s
|
||||
end
|
||||
|
||||
def to_s
|
||||
@s
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
class Basic_TestClass < TestBase
|
||||
|
||||
def test_FIRST
|
||||
|
|
@ -85,6 +98,7 @@ class Basic_TestClass < TestBase
|
|||
assert_equal( a.a1, -0x80000000 )
|
||||
|
||||
assert_equal( a.a3("a"), 1 )
|
||||
assert_equal( a.a3(ObjectWithStr::new("abcde")), 5 ) # implicitly using to_s for string conversion
|
||||
assert_equal( a.a3("ab"), 2 )
|
||||
assert_equal( a.a3("µ"), 2 ) # two UTF8 bytes
|
||||
if a.respond_to?(:a3_qstr)
|
||||
|
|
@ -2198,19 +2212,9 @@ class Basic_TestClass < TestBase
|
|||
assert_equal(b.map1_cptr_null == nil, true);
|
||||
assert_equal(b.map1_ptr_null == nil, true);
|
||||
|
||||
begin
|
||||
b.map1 = { 42 => 1, -17 => true }
|
||||
error = nil
|
||||
rescue => ex
|
||||
error = ex.message.split("\n")[0]
|
||||
end
|
||||
if error == "can't convert Fixnum into String"
|
||||
# Ok
|
||||
elsif error == "no implicit conversion of Fixnum into String" || error == "no implicit conversion of Integer into String"
|
||||
# Ok
|
||||
else
|
||||
assert_equal(error, "")
|
||||
end
|
||||
b.map1 = { 42 => 1, -17 => true }
|
||||
assert_equal(b.map1.inspect, "{-17=>\"true\", 42=>\"1\"}")
|
||||
|
||||
b.map1 = { 42 => "1", -17 => "true" }
|
||||
assert_equal(b.map1.inspect, "{-17=>\"true\", 42=>\"1\"}")
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue