test_slicing.py 文件源码

python
阅读 21 收藏 0 点赞 0 评论 0

项目:zippy 作者: securesystemslab 项目源码 文件源码
def test_wchar_ptr(self):
            s = "abcdefghijklmnopqrstuvwxyz\0"

            dll = CDLL(_ctypes_test.__file__)
            dll.my_wcsdup.restype = POINTER(c_wchar)
            dll.my_wcsdup.argtypes = POINTER(c_wchar),
            dll.my_free.restype = None
            res = dll.my_wcsdup(s)
            self.assertEqual(res[:len(s)], s)
            self.assertEqual(res[:len(s):], s)
            self.assertEqual(res[len(s)-1:-1:-1], s[::-1])
            self.assertEqual(res[len(s)-1:5:-7], s[:5:-7])

            import operator
            self.assertRaises(TypeError, operator.setitem,
                              res, slice(0, 5), "abcde")
            dll.my_free(res)

            if sizeof(c_wchar) == sizeof(c_short):
                dll.my_wcsdup.restype = POINTER(c_short)
            elif sizeof(c_wchar) == sizeof(c_int):
                dll.my_wcsdup.restype = POINTER(c_int)
            elif sizeof(c_wchar) == sizeof(c_long):
                dll.my_wcsdup.restype = POINTER(c_long)
            else:
                return
            res = dll.my_wcsdup(s)
            tmpl = list(range(ord("a"), ord("z")+1))
            self.assertEqual(res[:len(s)-1], tmpl)
            self.assertEqual(res[:len(s)-1:], tmpl)
            self.assertEqual(res[len(s)-2:-1:-1], tmpl[::-1])
            self.assertEqual(res[len(s)-2:5:-7], tmpl[:5:-7])
            dll.my_free(res)

################################################################
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号