From 37913086aaf830e2a9daa5be0f65c8b0d4d19f24 Mon Sep 17 00:00:00 2001 From: "mhammond%skippinet.com.au" Date: Tue, 22 Apr 2003 11:54:40 +0000 Subject: [PATCH] PyXPCOM: some strings were being terminated at the first \0, and some extra tests. Not part of the build. --- extensions/python/xpcom/src/VariantUtils.cpp | 6 +-- .../test/test_component/py_test_component.idl | 3 ++ .../test/test_component/py_test_component.py | 3 ++ .../python/xpcom/test/test_test_component.py | 49 +++++++++++++------ 4 files changed, 42 insertions(+), 19 deletions(-) diff --git a/extensions/python/xpcom/src/VariantUtils.cpp b/extensions/python/xpcom/src/VariantUtils.cpp index 4454e8db37e..975bef6335b 100644 --- a/extensions/python/xpcom/src/VariantUtils.cpp +++ b/extensions/python/xpcom/src/VariantUtils.cpp @@ -2183,7 +2183,7 @@ nsresult PyXPCOM_GatewayVariantHelper::BackFillVariant( PyObject *val, int index val_use = PyUnicode_FromObject(val); NS_ABORT_IF_FALSE(PyUnicode_Check(val_use), "PyUnicode_FromObject didnt return a Unicode object!"); const PRUnichar *sz = PyUnicode_AS_UNICODE(val_use); - ws->Assign(sz); + ws->Assign(sz, PyUnicode_GET_SIZE(val_use)); } break; } @@ -2200,7 +2200,7 @@ nsresult PyXPCOM_GatewayVariantHelper::BackFillVariant( PyObject *val, int index val_use = PyObject_Str(val); NS_ABORT_IF_FALSE(PyString_Check(val_use), "PyObject_Str didnt return a string object!"); const char *sz = PyString_AS_STRING(val_use); - ws->Assign(sz); + ws->Assign(sz, PyString_Size(val_use)); } break; } @@ -2221,7 +2221,7 @@ nsresult PyXPCOM_GatewayVariantHelper::BackFillVariant( PyObject *val, int index } NS_ABORT_IF_FALSE(PyString_Check(val_use), "must have a string object!"); const char *sz = PyString_AS_STRING(val_use); - ws->Assign(sz); + ws->Assign(sz, PyString_Size(val_use)); } break; } diff --git a/extensions/python/xpcom/test/test_component/py_test_component.idl b/extensions/python/xpcom/test/test_component/py_test_component.idl index 07800b73267..34a08520aa1 100644 --- a/extensions/python/xpcom/test/test_component/py_test_component.idl +++ b/extensions/python/xpcom/test/test_component/py_test_component.idl @@ -48,6 +48,9 @@ interface nsIPythonTestInterface : nsISupports attribute wchar wchar_value; // PRUnichar attribute string string_value; // char * attribute wstring wstring_value; // PRUnichar* + attribute AString astring_value; // nsAString & + attribute ACString acstring_value; // nsACString & + attribute AUTF8String utf8string_value; // nsAUTF8String & attribute nsIIDRef iid_value; // an IID attribute nsIPythonTestInterface interface_value; // A specific interface attribute nsISupports isupports_value; // A generic interface diff --git a/extensions/python/xpcom/test/test_component/py_test_component.py b/extensions/python/xpcom/test/test_component/py_test_component.py index 596036b1a71..7c9c5aff48b 100644 --- a/extensions/python/xpcom/test/test_component/py_test_component.py +++ b/extensions/python/xpcom/test/test_component/py_test_component.py @@ -43,6 +43,9 @@ class PythonTestComponent: self.wchar_value = "b" self.string_value = "cee" self.wstring_value = "dee" + self.astring_value = "astring" + self.acstring_value = "acstring" + self.utf8string_value = "utf8string" self.iid_value = self._reg_clsid_ self.interface_value = None self.isupports_value = None diff --git a/extensions/python/xpcom/test/test_test_component.py b/extensions/python/xpcom/test/test_test_component.py index 47093abedaa..5781ecb59e8 100644 --- a/extensions/python/xpcom/test/test_test_component.py +++ b/extensions/python/xpcom/test/test_test_component.py @@ -29,6 +29,7 @@ contractid = "Python.TestComponent" really_big_string = "This is really repetitive!" * 10000 really_big_wstring = u"This is really repetitive!" * 10000 +extended_unicode_string = u"The Euro Symbol is '\u20ac'" # Exception raised when a -ve integer is converted to an unsigned C integer # (via an extension module). This changed in Python 2.2 @@ -62,6 +63,28 @@ def test_attribute(ob, attr_name, expected_init, new_value, new_value_really = N setattr(ob, attr_name, expected_init) _test_value( "getting back initial attribute value after change (%s)" % (attr_name,), getattr(ob, attr_name), expected_init) +def test_string_attribute(ob, attr_name, expected_init, is_dumb_sz = False, ascii_only = False): + test_attribute(ob, attr_name, expected_init, "normal value") + val = "a null >\0<" + if is_dumb_sz: + expected = "a null >" # dumb strings are \0 terminated. + else: + expected = val + test_attribute(ob, attr_name, expected_init, val, expected) + test_attribute(ob, attr_name, expected_init, "") + test_attribute(ob, attr_name, expected_init, really_big_string) + test_attribute(ob, attr_name, expected_init, u"normal unicode value") + val = u"a null >\0<" + if is_dumb_sz: + expected = "a null >" # dumb strings are \0 terminated. + else: + expected = val + test_attribute(ob, attr_name, expected_init, val, expected) + test_attribute(ob, attr_name, expected_init, u"") + test_attribute(ob, attr_name, expected_init, really_big_wstring) + if not ascii_only: + test_attribute(ob, attr_name, expected_init, extended_unicode_string) + def test_attribute_failure(ob, attr_name, new_value, expected_exception): try: setattr(ob, attr_name, new_value) @@ -184,21 +207,15 @@ def test_base_interface(c): test_attribute(c, "wchar_value", "b", u"\0") test_attribute_failure(c, "wchar_value", u"hi", ValueError) - test_attribute(c, "string_value", "cee", "dee") - test_attribute(c, "string_value", "cee", "a null >\0<", "a null >") # strings are NULL terminated!! - test_attribute(c, "string_value", "cee", "") - test_attribute(c, "string_value", "cee", u"dee") - test_attribute(c, "string_value", "cee", u"a null >\0<", "a null >") # strings are NULL terminated!! - test_attribute(c, "string_value", "cee", u"") + test_string_attribute(c, "string_value", "cee", is_dumb_sz = True, ascii_only = True) + test_string_attribute(c, "wstring_value", "dee", is_dumb_sz = True) + test_string_attribute(c, "astring_value", "astring") + test_string_attribute(c, "acstring_value", "acstring", ascii_only = True) + + test_string_attribute(c, "utf8string_value", "utf8string") + # Test a string already encoded gets through correctly. + test_attribute(c, "utf8string_value", "utf8string", extended_unicode_string.encode("utf8"), extended_unicode_string) - test_attribute(c, "wstring_value", "dee", "cee") - test_attribute(c, "wstring_value", "dee", "a null >\0<", "a null >") # strings are NULL terminated!! - test_attribute(c, "wstring_value", "dee", "") - test_attribute(c, "wstring_value", "dee", really_big_string) - test_attribute(c, "wstring_value", "dee", u"cee") - test_attribute(c, "wstring_value", "dee", u"a null >\0<", "a null >") # strings are NULL terminated!! - test_attribute(c, "wstring_value", "dee", u"") - test_attribute(c, "wstring_value", "dee", really_big_wstring) # This will fail internal string representation :( Test we don't crash try: c.wstring_value = "a big char >" + chr(129) + "<" @@ -292,7 +309,7 @@ def test_derived_interface(c, test_flat = 0): test_method(c.UpWideString, (val,), val.upper()) test_method(c.UpWideString2, (val,), val.upper()) test_method(c.GetFixedWideString, (20,), u"A"*20) - val = u"The Euro Symbol is '\u20ac'" + val = extended_unicode_string test_method(c.CopyUTF8String, ("foo",), "foo") test_method(c.CopyUTF8String, (u"foo",), "foo") test_method(c.CopyUTF8String, (val,), val) @@ -382,7 +399,7 @@ def test_derived_interface(c, test_flat = 0): test_method(c.ConcatDOMStrings, (val,val), val+val) test_attribute(c, "domstring_value", "dom", "new dom") if c.domstring_value_ro != "dom": - print "Read-only DOMString not currect - got", c.domstring_ro + print "Read-only DOMString not correct - got", c.domstring_ro try: c.dom_string_ro = "new dom" print "Managed to set a readonly attribute - eek!"