Author: Brian Kearns <bdkea...@gmail.com> Branch: Changeset: r60746:4dc55ac8a8fa Date: 2013-01-30 22:07 -0500 http://bitbucket.org/pypy/pypy/changeset/4dc55ac8a8fa/
Log: fix PyObject_Str/Repr/Unicode for NULL argument, test diff --git a/lib-python/2.7/test/test_capi.py b/lib-python/2.7/test/test_capi.py --- a/lib-python/2.7/test/test_capi.py +++ b/lib-python/2.7/test/test_capi.py @@ -21,7 +21,6 @@ 'test_lazy_hash_inheritance', 'test_long_api', 'test_longlong_api', - 'test_null_strings', 'test_widechar', 'TestThreadState', 'TestPendingCalls', diff --git a/pypy/module/cpyext/object.py b/pypy/module/cpyext/object.py --- a/pypy/module/cpyext/object.py +++ b/pypy/module/cpyext/object.py @@ -218,6 +218,8 @@ @cpython_api([PyObject], PyObject) def PyObject_Str(space, w_obj): + if w_obj is None: + return space.wrap("<NULL>") return space.str(w_obj) @cpython_api([PyObject], PyObject) @@ -226,6 +228,8 @@ representation on success, NULL on failure. This is the equivalent of the Python expression repr(o). Called by the repr() built-in function and by reverse quotes.""" + if w_obj is None: + return space.wrap("<NULL>") return space.repr(w_obj) @cpython_api([PyObject], PyObject) @@ -234,6 +238,8 @@ string representation on success, NULL on failure. This is the equivalent of the Python expression unicode(o). Called by the unicode() built-in function.""" + if w_obj is None: + return space.wrap(u"<NULL>") return space.call_function(space.w_unicode, w_obj) @cpython_api([PyObject, PyObject], rffi.INT_real, error=-1) diff --git a/pypy/module/cpyext/test/test_object.py b/pypy/module/cpyext/test/test_object.py --- a/pypy/module/cpyext/test/test_object.py +++ b/pypy/module/cpyext/test/test_object.py @@ -94,27 +94,30 @@ def test_size(self, space, api): assert api.PyObject_Size(space.newlist([space.w_None])) == 1 - + + def test_str(self, space, api): + w_list = space.newlist([space.w_None, space.wrap(42)]) + assert space.str_w(api.PyObject_Str(None)) == "<NULL>" + assert space.str_w(api.PyObject_Str(w_list)) == "[None, 42]" + assert space.str_w(api.PyObject_Str(space.wrap("a"))) == "a" + def test_repr(self, space, api): w_list = space.newlist([space.w_None, space.wrap(42)]) + assert space.str_w(api.PyObject_Repr(None)) == "<NULL>" assert space.str_w(api.PyObject_Repr(w_list)) == "[None, 42]" assert space.str_w(api.PyObject_Repr(space.wrap("a"))) == "'a'" - - w_list = space.newlist([space.w_None, space.wrap(42)]) - assert space.str_w(api.PyObject_Str(w_list)) == "[None, 42]" - assert space.str_w(api.PyObject_Str(space.wrap("a"))) == "a" - + def test_RichCompare(self, space, api): def compare(w_o1, w_o2, opid): res = api.PyObject_RichCompareBool(w_o1, w_o2, opid) w_res = api.PyObject_RichCompare(w_o1, w_o2, opid) assert space.is_true(w_res) == res return res - + def test_compare(o1, o2): w_o1 = space.wrap(o1) w_o2 = space.wrap(o2) - + for opid, expected in [ (Py_LT, o1 < o2), (Py_LE, o1 <= o2), (Py_NE, o1 != o2), (Py_EQ, o1 == o2), @@ -190,6 +193,7 @@ api.PyErr_Clear() def test_unicode(self, space, api): + assert space.unwrap(api.PyObject_Unicode(None)) == u"<NULL>" assert space.unwrap(api.PyObject_Unicode(space.wrap([]))) == u"[]" assert space.unwrap(api.PyObject_Unicode(space.wrap("e"))) == u"e" assert api.PyObject_Unicode(space.wrap("\xe9")) is None _______________________________________________ pypy-commit mailing list pypy-commit@python.org http://mail.python.org/mailman/listinfo/pypy-commit