this repo has no description
at trunk 503 lines 16 kB view raw
1// Copyright (c) Facebook, Inc. and its affiliates. (http://www.facebook.com) 2#include "Python.h" 3#include "gtest/gtest.h" 4 5#include "capi-fixture.h" 6#include "capi-testing.h" 7 8namespace py { 9namespace testing { 10 11using ListExtensionApiTest = ExtensionApi; 12 13TEST_F(ListExtensionApiTest, NewWithBadLengthReturnsNull) { 14 Py_ssize_t invalid_length = -1; 15 PyObject* pyresult = PyList_New(invalid_length); 16 EXPECT_EQ(pyresult, nullptr); 17} 18 19TEST_F(ListExtensionApiTest, NewReturnsEmptyList) { 20 Py_ssize_t length = 0; 21 PyObject* pyresult = PyList_New(length); 22 EXPECT_TRUE(PyList_CheckExact(pyresult)); 23 EXPECT_EQ(PyList_Size(pyresult), length); 24 25 Py_DECREF(pyresult); 26} 27 28TEST_F(ListExtensionApiTest, NewReturnsList) { 29 Py_ssize_t length = 5; 30 PyObject* pyresult = PyList_New(length); 31 EXPECT_TRUE(PyList_CheckExact(pyresult)); 32 EXPECT_EQ(PyList_Size(pyresult), length); 33 34 Py_DECREF(pyresult); 35} 36 37TEST_F(ListExtensionApiTest, AppendToNonListReturnsNegative) { 38 PyObjectPtr dict(PyDict_New()); 39 PyObjectPtr pylong(PyLong_FromLong(10)); 40 int result = PyList_Append(dict, pylong); 41 EXPECT_EQ(result, -1); 42 ASSERT_NE(PyErr_Occurred(), nullptr); 43 EXPECT_TRUE(PyErr_ExceptionMatches(PyExc_SystemError)); 44} 45 46TEST_F(ListExtensionApiTest, AppendWithNullValueReturnsNegative) { 47 PyObjectPtr list(PyList_New(0)); 48 int result = PyList_Append(list, nullptr); 49 EXPECT_EQ(result, -1); 50 ASSERT_NE(PyErr_Occurred(), nullptr); 51 EXPECT_TRUE(PyErr_ExceptionMatches(PyExc_SystemError)); 52} 53 54TEST_F(ListExtensionApiTest, AppendReturnsZero) { 55 PyObjectPtr list(PyList_New(0)); 56 PyObjectPtr pylong(PyLong_FromLong(10)); 57 int result = PyList_Append(list, pylong); 58 EXPECT_EQ(result, 0); 59 EXPECT_EQ(PyErr_Occurred(), nullptr); 60} 61 62TEST_F(ListExtensionApiTest, SizeIncreasesAfterAppend) { 63 Py_ssize_t length = 4; 64 PyObject* list = PyList_New(length); 65 EXPECT_TRUE(PyList_CheckExact(list)); 66 EXPECT_EQ(PyList_Size(list), length); 67 68 PyObject* item = PyLong_FromLong(1); 69 EXPECT_EQ(PyList_Append(list, item), 0); 70 EXPECT_EQ(PyList_Size(list), length + 1); 71 72 Py_DECREF(item); 73 Py_DECREF(list); 74} 75 76TEST_F(ListExtensionApiTest, SizeWithNonListReturnsNegative) { 77 PyObject* dict = PyDict_New(); 78 EXPECT_EQ(PyList_Size(dict), -1); 79 ASSERT_NE(PyErr_Occurred(), nullptr); 80 EXPECT_TRUE(PyErr_ExceptionMatches(PyExc_SystemError)); 81 82 Py_DECREF(dict); 83} 84 85TEST_F(ListExtensionApiTest, AsTupleWithNullRaisesSystemError) { 86 ASSERT_EQ(PyList_AsTuple(nullptr), nullptr); 87 ASSERT_NE(PyErr_Occurred(), nullptr); 88 EXPECT_TRUE(PyErr_ExceptionMatches(PyExc_SystemError)); 89} 90 91TEST_F(ListExtensionApiTest, AsTupleWithNonListRaisesSystemError) { 92 ASSERT_EQ(PyList_AsTuple(Py_None), nullptr); 93 ASSERT_NE(PyErr_Occurred(), nullptr); 94 EXPECT_TRUE(PyErr_ExceptionMatches(PyExc_SystemError)); 95} 96 97TEST_F(ListExtensionApiTest, AsTupleWithListReturnsAllElementsFromList) { 98 PyObjectPtr list(PyList_New(0)); 99 PyObjectPtr one(PyLong_FromLong(1)); 100 PyObjectPtr two(PyLong_FromLong(2)); 101 PyObjectPtr three(PyLong_FromLong(3)); 102 PyList_Append(list, one); 103 PyList_Append(list, two); 104 PyList_Append(list, three); 105 106 PyObjectPtr tuple(PyList_AsTuple(list)); 107 ASSERT_EQ(PyErr_Occurred(), nullptr); 108 ASSERT_TRUE(PyTuple_CheckExact(tuple)); 109 ASSERT_EQ(PyTuple_Size(tuple), 3); 110 EXPECT_EQ(PyTuple_GetItem(tuple, 0), one); 111 EXPECT_EQ(PyTuple_GetItem(tuple, 1), two); 112 EXPECT_EQ(PyTuple_GetItem(tuple, 2), three); 113} 114 115TEST_F(ListExtensionApiTest, GetItemWithNonListReturnsNull) { 116 ASSERT_EQ(PyList_GetItem(Py_None, 0), nullptr); 117 ASSERT_NE(PyErr_Occurred(), nullptr); 118 EXPECT_TRUE(PyErr_ExceptionMatches(PyExc_SystemError)); 119} 120 121TEST_F(ListExtensionApiTest, GetItemWithBadIndexRaisesIndexError) { 122 Py_ssize_t size = 0; 123 PyObjectPtr list(PyList_New(size)); 124 ASSERT_EQ(PyList_GetItem(list, size + 1), nullptr); 125 ASSERT_NE(PyErr_Occurred(), nullptr); 126 EXPECT_TRUE(PyErr_ExceptionMatches(PyExc_IndexError)); 127} 128 129TEST_F(ListExtensionApiTest, GetItemWithListReturnsElementAtIndex) { 130 PyObjectPtr list(PyList_New(0)); 131 PyObjectPtr one(PyLong_FromLong(1)); 132 PyObjectPtr two(PyLong_FromLong(2)); 133 PyObjectPtr three(PyLong_FromLong(3)); 134 PyList_Append(list, one); 135 PyList_Append(list, two); 136 PyList_Append(list, three); 137 138 EXPECT_EQ(PyList_GetItem(list, 0), one); 139 ASSERT_EQ(PyErr_Occurred(), nullptr); 140 EXPECT_EQ(PyList_GetItem(list, 1), two); 141 ASSERT_EQ(PyErr_Occurred(), nullptr); 142 EXPECT_EQ(PyList_GetItem(list, 2), three); 143 ASSERT_EQ(PyErr_Occurred(), nullptr); 144} 145 146TEST_F(ListExtensionApiTest, SetItemWithNonListReturnsNegativeOne) { 147 ASSERT_EQ(PyList_SetItem(Py_None, 0, nullptr), -1); 148 ASSERT_NE(PyErr_Occurred(), nullptr); 149 EXPECT_TRUE(PyErr_ExceptionMatches(PyExc_SystemError)); 150} 151 152TEST_F(ListExtensionApiTest, SetItemWithBadIndexRaisesIndexError) { 153 Py_ssize_t size = 0; 154 PyObjectPtr list(PyList_New(size)); 155 ASSERT_EQ(PyList_SetItem(list, size + 1, nullptr), -1); 156 ASSERT_NE(PyErr_Occurred(), nullptr); 157 EXPECT_TRUE(PyErr_ExceptionMatches(PyExc_IndexError)); 158} 159 160TEST_F(ListExtensionApiTest, SetItemWithListSetsItemAtIndex) { 161 PyObjectPtr list(PyList_New(0)); 162 PyObjectPtr one(PyLong_FromLong(1)); 163 PyObjectPtr two(PyLong_FromLong(2)); 164 PyObjectPtr three(PyLong_FromLong(3)); 165 PyList_Append(list, one); 166 PyList_Append(list, two); 167 PyList_Append(list, three); 168 169 Py_ssize_t idx = 2; 170 PyObjectPtr four(PyLong_FromLong(4)); 171 Py_INCREF(four); 172 EXPECT_EQ(PyList_SetItem(list, idx, four), 0); 173 ASSERT_EQ(PyErr_Occurred(), nullptr); 174 EXPECT_EQ(PyList_GetItem(list, idx), four); 175 ASSERT_EQ(PyErr_Occurred(), nullptr); 176} 177 178TEST_F(ListExtensionApiTest, SETITEMWithListSetsItemAtIndex) { 179 PyObjectPtr list(PyList_New(0)); 180 PyObjectPtr one(PyLong_FromLong(1)); 181 PyObjectPtr two(PyLong_FromLong(2)); 182 PyObjectPtr three(PyLong_FromLong(3)); 183 PyList_Append(list, one); 184 PyList_Append(list, two); 185 PyList_Append(list, three); 186 187 // Replace three with four 188 Py_ssize_t three_refcnt = Py_REFCNT(three.get()); 189 Py_ssize_t idx = 2; 190 PyObjectPtr four(PyLong_FromLong(4)); 191 Py_INCREF(four); // keep an extra reference for checking below SetItem 192 PyList_SET_ITEM(list.get(), idx, four); 193 EXPECT_EQ(Py_REFCNT(three.get()), three_refcnt); 194 ASSERT_EQ(PyErr_Occurred(), nullptr); 195 EXPECT_EQ(PyList_GetItem(list, idx), four); 196 ASSERT_EQ(PyErr_Occurred(), nullptr); 197} 198 199TEST_F(ListExtensionApiTest, SetSliceOnNonListRaisesSystemError) { 200 PyObjectPtr rhs(PyList_New(0)); 201 ASSERT_EQ(PyList_SetSlice(Py_None, 0, 0, rhs), -1); 202 ASSERT_NE(PyErr_Occurred(), nullptr); 203 EXPECT_TRUE(PyErr_ExceptionMatches(PyExc_SystemError)); 204} 205 206TEST_F(ListExtensionApiTest, SetSliceWithNegativeLowStartsAtZero) { 207 PyObjectPtr lhs(PyList_New(0)); 208 PyObjectPtr zero(PyLong_FromLong(0)); 209 PyList_Append(lhs, zero); 210 PyObjectPtr one(PyLong_FromLong(1)); 211 PyList_Append(lhs, one); 212 PyObjectPtr two(PyLong_FromLong(2)); 213 PyList_Append(lhs, two); 214 215 PyObjectPtr rhs(PyList_New(0)); 216 PyObjectPtr five(PyLong_FromLong(5)); 217 PyList_Append(rhs, five); 218 219 ASSERT_EQ(PyList_SetSlice(lhs, -1, 1, rhs), 0); 220 ASSERT_EQ(PyErr_Occurred(), nullptr); 221 EXPECT_EQ(PyList_Size(lhs), 3); 222 EXPECT_EQ(PyList_GetItem(lhs, 0), five); 223 EXPECT_EQ(PyList_GetItem(lhs, 1), one); 224 EXPECT_EQ(PyList_GetItem(lhs, 2), two); 225} 226 227TEST_F(ListExtensionApiTest, SetSliceWithNullItemsDeletesSlice) { 228 PyObjectPtr lhs(PyList_New(0)); 229 PyObjectPtr zero(PyLong_FromLong(0)); 230 PyList_Append(lhs, zero); 231 PyObjectPtr one(PyLong_FromLong(1)); 232 PyList_Append(lhs, one); 233 PyObjectPtr two(PyLong_FromLong(2)); 234 PyList_Append(lhs, two); 235 236 ASSERT_EQ(PyList_SetSlice(lhs, 0, 1, nullptr), 0); 237 ASSERT_EQ(PyErr_Occurred(), nullptr); 238 EXPECT_EQ(PyList_Size(lhs), 2); 239 EXPECT_EQ(PyList_GetItem(lhs, 0), one); 240 EXPECT_EQ(PyList_GetItem(lhs, 1), two); 241} 242 243TEST_F(ListExtensionApiTest, GetSliceOnNonListRaisesSystemError) { 244 ASSERT_EQ(PyList_GetSlice(Py_None, 0, 0), nullptr); 245 ASSERT_NE(PyErr_Occurred(), nullptr); 246 EXPECT_TRUE(PyErr_ExceptionMatches(PyExc_SystemError)); 247} 248 249TEST_F(ListExtensionApiTest, GetSliceOnEmptyListReturnsEmptyList) { 250 PyObjectPtr list(PyList_New(0)); 251 PyObjectPtr result(PyList_GetSlice(list, 0, 0)); 252 ASSERT_NE(result, nullptr); 253 ASSERT_EQ(PyErr_Occurred(), nullptr); 254 EXPECT_EQ(PyList_Size(result), 0); 255} 256 257TEST_F(ListExtensionApiTest, GetSliceWithNegativeOutOfBoundsLowStartsAtZero) { 258 PyObjectPtr list(PyList_New(0)); 259 PyObjectPtr zero(PyLong_FromLong(0)); 260 PyList_Append(list, zero); 261 PyObjectPtr one(PyLong_FromLong(1)); 262 PyList_Append(list, one); 263 PyObjectPtr two(PyLong_FromLong(2)); 264 PyList_Append(list, two); 265 266 PyObjectPtr result(PyList_GetSlice(list, -5, 3)); 267 ASSERT_NE(result, nullptr); 268 ASSERT_EQ(PyErr_Occurred(), nullptr); 269 ASSERT_TRUE(PyList_CheckExact(result)); 270 EXPECT_EQ(PyList_Size(result), 3); 271 EXPECT_EQ(PyList_GetItem(result, 0), zero); 272 EXPECT_EQ(PyList_GetItem(result, 1), one); 273 EXPECT_EQ(PyList_GetItem(result, 2), two); 274} 275 276TEST_F(ListExtensionApiTest, GetSliceWithPositiveOutOfBoundsLowStartsAtLength) { 277 PyObjectPtr list(PyList_New(0)); 278 PyObjectPtr zero(PyLong_FromLong(0)); 279 PyList_Append(list, zero); 280 PyObjectPtr one(PyLong_FromLong(1)); 281 PyList_Append(list, one); 282 PyObjectPtr two(PyLong_FromLong(2)); 283 PyList_Append(list, two); 284 285 PyObjectPtr result(PyList_GetSlice(list, 15, 3)); 286 ASSERT_NE(result, nullptr); 287 ASSERT_EQ(PyErr_Occurred(), nullptr); 288 ASSERT_TRUE(PyList_CheckExact(result)); 289 EXPECT_EQ(PyList_Size(result), 0); 290} 291 292TEST_F(ListExtensionApiTest, GetSliceOutOfBoundsHighStartsAtLow) { 293 PyObjectPtr list(PyList_New(0)); 294 PyObjectPtr zero(PyLong_FromLong(0)); 295 PyList_Append(list, zero); 296 PyObjectPtr one(PyLong_FromLong(1)); 297 PyList_Append(list, one); 298 PyObjectPtr two(PyLong_FromLong(2)); 299 PyList_Append(list, two); 300 301 PyObjectPtr result(PyList_GetSlice(list, 5, 0)); 302 ASSERT_NE(result, nullptr); 303 ASSERT_EQ(PyErr_Occurred(), nullptr); 304 ASSERT_TRUE(PyList_CheckExact(result)); 305 EXPECT_EQ(PyList_Size(result), 0); 306} 307 308TEST_F(ListExtensionApiTest, GetSliceWithOutOfBoundsHighEndsAtLength) { 309 PyObjectPtr list(PyList_New(0)); 310 PyObjectPtr zero(PyLong_FromLong(0)); 311 PyList_Append(list, zero); 312 PyObjectPtr one(PyLong_FromLong(1)); 313 PyList_Append(list, one); 314 PyObjectPtr two(PyLong_FromLong(2)); 315 PyList_Append(list, two); 316 317 PyObjectPtr result(PyList_GetSlice(list, 0, 20)); 318 ASSERT_NE(result, nullptr); 319 ASSERT_EQ(PyErr_Occurred(), nullptr); 320 ASSERT_TRUE(PyList_CheckExact(result)); 321 EXPECT_EQ(PyList_Size(result), 3); 322 EXPECT_EQ(PyList_GetItem(result, 0), zero); 323 EXPECT_EQ(PyList_GetItem(result, 1), one); 324 EXPECT_EQ(PyList_GetItem(result, 2), two); 325} 326 327TEST_F(ListExtensionApiTest, InsertWithNonListRaisesSystemError) { 328 ASSERT_EQ(PyList_Insert(Py_None, 0, Py_None), -1); 329 ASSERT_NE(PyErr_Occurred(), nullptr); 330 EXPECT_TRUE(PyErr_ExceptionMatches(PyExc_SystemError)); 331} 332 333TEST_F(ListExtensionApiTest, InsertWithNullItemRaisesSystemError) { 334 PyObjectPtr list(PyList_New(0)); 335 ASSERT_EQ(PyList_Insert(list, 0, nullptr), -1); 336 ASSERT_NE(PyErr_Occurred(), nullptr); 337 EXPECT_TRUE(PyErr_ExceptionMatches(PyExc_SystemError)); 338} 339 340TEST_F(ListExtensionApiTest, InsertIncreasesSizeByOne) { 341 Py_ssize_t num_items = 0; 342 PyObjectPtr list(PyList_New(num_items)); 343 PyObjectPtr val(PyLong_FromLong(666)); 344 ASSERT_EQ(PyList_Insert(list, 0, val), 0); 345 ASSERT_EQ(PyErr_Occurred(), nullptr); 346 EXPECT_EQ(PyList_Size(list), num_items + 1); 347 EXPECT_EQ(PyList_GetItem(list, 0), val); 348} 349 350TEST_F(ListExtensionApiTest, InsertIntoListAtFrontShiftsItems) { 351 PyObjectPtr list(PyList_New(0)); 352 PyObjectPtr one(PyLong_FromLong(1)); 353 PyList_Append(list, one); 354 PyObjectPtr two(PyLong_FromLong(2)); 355 PyList_Append(list, two); 356 357 PyObjectPtr val(PyLong_FromLong(666)); 358 ASSERT_EQ(PyList_Insert(list, 0, val), 0); 359 ASSERT_EQ(PyErr_Occurred(), nullptr); 360 ASSERT_EQ(PyList_Size(list), 3); 361 EXPECT_EQ(PyList_GetItem(list, 0), val); 362 EXPECT_EQ(PyList_GetItem(list, 1), one); 363 EXPECT_EQ(PyList_GetItem(list, 2), two); 364} 365 366TEST_F(ListExtensionApiTest, InsertIntoListPastRearInsertsAtEnd) { 367 PyObjectPtr list(PyList_New(0)); 368 PyObjectPtr one(PyLong_FromLong(1)); 369 PyList_Append(list, one); 370 PyObjectPtr two(PyLong_FromLong(2)); 371 PyList_Append(list, two); 372 373 PyObjectPtr val(PyLong_FromLong(666)); 374 ASSERT_EQ(PyList_Insert(list, 100, val), 0); 375 ASSERT_EQ(PyErr_Occurred(), nullptr); 376 EXPECT_EQ(PyList_GetItem(list, 0), one); 377 EXPECT_EQ(PyList_GetItem(list, 1), two); 378 ASSERT_EQ(PyList_GetItem(list, 2), val); 379} 380 381TEST_F(ListExtensionApiTest, InsertIntoListNegativeInsertsIndexingFromEnd) { 382 PyObjectPtr list(PyList_New(0)); 383 PyObjectPtr one(PyLong_FromLong(1)); 384 PyList_Append(list, one); 385 PyObjectPtr two(PyLong_FromLong(2)); 386 PyList_Append(list, two); 387 388 PyObjectPtr val(PyLong_FromLong(666)); 389 ASSERT_EQ(PyList_Insert(list, -1, val), 0); 390 ASSERT_EQ(PyErr_Occurred(), nullptr); 391 EXPECT_EQ(PyList_GetItem(list, 0), one); 392 EXPECT_EQ(PyList_GetItem(list, 1), val); 393 ASSERT_EQ(PyList_GetItem(list, 2), two); 394} 395 396TEST_F(ListExtensionApiTest, InsertIntoListWayNegativeInsertsAtBeginning) { 397 PyObjectPtr list(PyList_New(0)); 398 PyObjectPtr one(PyLong_FromLong(1)); 399 PyList_Append(list, one); 400 PyObjectPtr two(PyLong_FromLong(2)); 401 PyList_Append(list, two); 402 403 PyObjectPtr val(PyLong_FromLong(666)); 404 ASSERT_EQ(PyList_Insert(list, -100, val), 0); 405 ASSERT_EQ(PyErr_Occurred(), nullptr); 406 EXPECT_EQ(PyList_GetItem(list, 0), val); 407 EXPECT_EQ(PyList_GetItem(list, 1), one); 408 ASSERT_EQ(PyList_GetItem(list, 2), two); 409} 410 411TEST_F(ListExtensionApiTest, ReverseWithNullRaisesSystemError) { 412 ASSERT_EQ(PyList_Reverse(nullptr), -1); 413 ASSERT_NE(PyErr_Occurred(), nullptr); 414 EXPECT_TRUE(PyErr_ExceptionMatches(PyExc_SystemError)); 415} 416 417TEST_F(ListExtensionApiTest, ReverseWithNonListRaisesSystemError) { 418 ASSERT_EQ(PyList_Reverse(Py_None), -1); 419 ASSERT_NE(PyErr_Occurred(), nullptr); 420 EXPECT_TRUE(PyErr_ExceptionMatches(PyExc_SystemError)); 421} 422 423TEST_F(ListExtensionApiTest, ReverseWithZeroLengthListSucceeds) { 424 PyObjectPtr list(PyList_New(0)); 425 ASSERT_EQ(PyList_Reverse(list), 0); 426 ASSERT_EQ(PyErr_Occurred(), nullptr); 427} 428 429TEST_F(ListExtensionApiTest, ReverseWithNonZeroLengthListSucceeds) { 430 PyObjectPtr list(PyList_New(0)); 431 PyObjectPtr val0(PyLong_FromLong(0)); 432 PyList_Append(list, val0); 433 PyObjectPtr val1(PyLong_FromLong(1)); 434 PyList_Append(list, val1); 435 PyObjectPtr val2(PyLong_FromLong(2)); 436 PyList_Append(list, val2); 437 PyObjectPtr val3(PyLong_FromLong(3)); 438 PyList_Append(list, val3); 439 PyObjectPtr val4(PyLong_FromLong(4)); 440 PyList_Append(list, val4); 441 442 ASSERT_EQ(PyList_Reverse(list), 0); 443 ASSERT_EQ(PyList_Size(list), 5); 444 ASSERT_EQ(PyErr_Occurred(), nullptr); 445 EXPECT_EQ(PyList_GetItem(list, 0), val4); 446 EXPECT_EQ(PyList_GetItem(list, 1), val3); 447 EXPECT_EQ(PyList_GetItem(list, 2), val2); 448 EXPECT_EQ(PyList_GetItem(list, 3), val1); 449 EXPECT_EQ(PyList_GetItem(list, 4), val0); 450} 451 452TEST_F(ListExtensionApiTest, SortWithNullListRaisesSystemError) { 453 ASSERT_EQ(PyList_Sort(nullptr), -1); 454 ASSERT_NE(PyErr_Occurred(), nullptr); 455 EXPECT_TRUE(PyErr_ExceptionMatches(PyExc_SystemError)); 456} 457 458TEST_F(ListExtensionApiTest, SortWithNonListRaisesSystemError) { 459 ASSERT_EQ(PyList_Sort(Py_None), -1); 460 ASSERT_NE(PyErr_Occurred(), nullptr); 461 EXPECT_TRUE(PyErr_ExceptionMatches(PyExc_SystemError)); 462} 463 464TEST_F(ListExtensionApiTest, SortSortsList) { 465 PyObjectPtr list(PyList_New(0)); 466 PyObjectPtr five(PyLong_FromLong(5)); 467 PyList_Append(list, five); 468 PyObjectPtr four(PyLong_FromLong(4)); 469 PyList_Append(list, four); 470 PyObjectPtr three(PyLong_FromLong(3)); 471 PyList_Append(list, three); 472 PyObjectPtr two(PyLong_FromLong(2)); 473 PyList_Append(list, two); 474 PyObjectPtr one(PyLong_FromLong(1)); 475 PyList_Append(list, one); 476 ASSERT_EQ(PyList_Size(list), 5); 477 ASSERT_EQ(PyList_Sort(list), 0); 478 ASSERT_EQ(PyErr_Occurred(), nullptr); 479 EXPECT_EQ(PyList_GetItem(list, 0), one); 480 EXPECT_EQ(PyList_GetItem(list, 1), two); 481 EXPECT_EQ(PyList_GetItem(list, 2), three); 482 EXPECT_EQ(PyList_GetItem(list, 3), four); 483 EXPECT_EQ(PyList_GetItem(list, 4), five); 484} 485 486TEST_F(ListExtensionApiTest, SortWithNonComparableElementsRaisesTypeError) { 487 PyObjectPtr list(PyList_New(0)); 488 PyObjectPtr three(PyLong_FromLong(3)); 489 PyList_Append(list, three); 490 PyObjectPtr two(PyLong_FromLong(2)); 491 PyList_Append(list, two); 492 PyObjectPtr one(PyLong_FromLong(1)); 493 PyList_Append(list, one); 494 PyObjectPtr bar(PyUnicode_FromString("bar")); 495 PyList_Append(list, bar); 496 ASSERT_EQ(PyList_Size(list), 4); 497 ASSERT_EQ(PyList_Sort(list), -1); 498 ASSERT_NE(PyErr_Occurred(), nullptr); 499 EXPECT_TRUE(PyErr_ExceptionMatches(PyExc_TypeError)); 500} 501 502} // namespace testing 503} // namespace py