this repo has no description
1// Copyright (c) Facebook, Inc. and its affiliates. (http://www.facebook.com)
2#include "exception-builtins.h"
3
4#include "gtest/gtest.h"
5
6#include "builtins.h"
7#include "runtime.h"
8#include "test-utils.h"
9
10namespace py {
11namespace testing {
12
13using ExceptionBuiltinsTest = RuntimeFixture;
14
15TEST_F(ExceptionBuiltinsTest, BaseExceptionNoArguments) {
16 HandleScope scope(thread_);
17
18 ASSERT_FALSE(runFromCStr(runtime_, R"(
19exc = BaseException()
20)")
21 .isError());
22
23 Object exc(&scope, mainModuleAt(runtime_, "exc"));
24 ASSERT_TRUE(exc.isBaseException());
25 BaseException base_exception(&scope, *exc);
26
27 // No constructor arguments means args should contain an empty tuple.
28 ASSERT_TRUE(base_exception.args().isTuple());
29 ASSERT_EQ(base_exception.args(), runtime_->emptyTuple());
30}
31
32TEST_F(ExceptionBuiltinsTest, BaseExceptionManyArguments) {
33 HandleScope scope(thread_);
34
35 ASSERT_FALSE(runFromCStr(runtime_, R"(
36exc = BaseException(1,2,3)
37)")
38 .isError());
39
40 Object exc(&scope, mainModuleAt(runtime_, "exc"));
41 ASSERT_TRUE(exc.isBaseException());
42 BaseException base_exception(&scope, *exc);
43
44 // The args attribute contains a tuple of the constructor arguments.
45 ASSERT_TRUE(base_exception.args().isTuple());
46 Tuple args(&scope, base_exception.args());
47 EXPECT_EQ(args.at(0), SmallInt::fromWord(1));
48 EXPECT_EQ(args.at(1), SmallInt::fromWord(2));
49 EXPECT_EQ(args.at(2), SmallInt::fromWord(3));
50}
51
52TEST_F(ExceptionBuiltinsTest, StrFromBaseExceptionNoArgs) {
53 HandleScope scope(thread_);
54
55 ASSERT_FALSE(runFromCStr(runtime_, R"(
56a = BaseException().__str__()
57)")
58 .isError());
59
60 Object a(&scope, mainModuleAt(runtime_, "a"));
61 EXPECT_TRUE(isStrEqualsCStr(*a, ""));
62}
63
64TEST_F(ExceptionBuiltinsTest, StrFromBaseExceptionOneArg) {
65 HandleScope scope(thread_);
66
67 ASSERT_FALSE(runFromCStr(runtime_, R"(
68a = BaseException("hello").__str__()
69)")
70 .isError());
71
72 Object a(&scope, mainModuleAt(runtime_, "a"));
73 EXPECT_TRUE(isStrEqualsCStr(*a, "hello"));
74}
75
76TEST_F(ExceptionBuiltinsTest, StrFromBaseExceptionManyArgs) {
77 HandleScope scope(thread_);
78
79 ASSERT_FALSE(runFromCStr(runtime_, R"(
80a = BaseException("hello", "world").__str__()
81)")
82 .isError());
83
84 Object a(&scope, mainModuleAt(runtime_, "a"));
85 EXPECT_TRUE(isStrEqualsCStr(*a, "('hello', 'world')"));
86}
87
88TEST_F(ExceptionBuiltinsTest, ExceptionManyArguments) {
89 HandleScope scope(thread_);
90
91 ASSERT_FALSE(runFromCStr(runtime_, R"(
92exc = Exception(1,2,3)
93)")
94 .isError());
95
96 Object exc(&scope, mainModuleAt(runtime_, "exc"));
97 ASSERT_TRUE(exc.isException());
98 Exception exception(&scope, *exc);
99
100 // The args attribute contains a tuple of the constructor arguments.
101 ASSERT_TRUE(exception.args().isTuple());
102 Tuple args(&scope, exception.args());
103 EXPECT_EQ(args.at(0), SmallInt::fromWord(1));
104 EXPECT_EQ(args.at(1), SmallInt::fromWord(2));
105 EXPECT_EQ(args.at(2), SmallInt::fromWord(3));
106}
107
108TEST_F(ExceptionBuiltinsTest, SimpleExceptionTypesCanBeConstructed) {
109 HandleScope scope(thread_);
110
111 ASSERT_FALSE(runFromCStr(runtime_, R"(
112attr_error = AttributeError()
113name_error = NameError()
114value_error = ValueError()
115rt_error = RuntimeError()
116)")
117 .isError());
118
119 BaseException attr_error(&scope, mainModuleAt(runtime_, "attr_error"));
120 BaseException name_error(&scope, mainModuleAt(runtime_, "name_error"));
121 BaseException value_error(&scope, mainModuleAt(runtime_, "value_error"));
122 BaseException rt_error(&scope, mainModuleAt(runtime_, "rt_error"));
123
124 EXPECT_TRUE(runtime_->isInstanceOfBaseException(*attr_error));
125 EXPECT_EQ(attr_error.layoutId(), LayoutId::kAttributeError);
126 EXPECT_TRUE(runtime_->isInstanceOfBaseException(*name_error));
127 EXPECT_EQ(name_error.layoutId(), LayoutId::kNameError);
128 EXPECT_TRUE(runtime_->isInstanceOfBaseException(*value_error));
129 EXPECT_EQ(value_error.layoutId(), LayoutId::kValueError);
130 EXPECT_TRUE(runtime_->isInstanceOfBaseException(*rt_error));
131 EXPECT_EQ(rt_error.layoutId(), LayoutId::kRuntimeError);
132}
133
134TEST_F(ExceptionBuiltinsTest, LookupErrorAndSubclassesHaveCorrectHierarchy) {
135 HandleScope scope(thread_);
136
137 ASSERT_FALSE(runFromCStr(runtime_, R"(
138lookup_is_exc = issubclass(LookupError, Exception)
139index_is_lookup = issubclass(IndexError, LookupError)
140key_is_lookup = issubclass(KeyError, LookupError)
141)")
142 .isError());
143
144 Bool lookup_is_exc(&scope, mainModuleAt(runtime_, "lookup_is_exc"));
145 Bool index_is_lookup(&scope, mainModuleAt(runtime_, "index_is_lookup"));
146 Bool key_is_lookup(&scope, mainModuleAt(runtime_, "key_is_lookup"));
147
148 EXPECT_TRUE(lookup_is_exc.value());
149 EXPECT_TRUE(index_is_lookup.value());
150 EXPECT_TRUE(key_is_lookup.value());
151}
152
153TEST_F(ExceptionBuiltinsTest, LookupErrorAndSubclassesCanBeConstructed) {
154 HandleScope scope(thread_);
155
156 ASSERT_FALSE(runFromCStr(runtime_, R"(
157l = LookupError()
158i = IndexError()
159k = KeyError()
160)")
161 .isError());
162
163 LookupError l(&scope, mainModuleAt(runtime_, "l"));
164 IndexError i(&scope, mainModuleAt(runtime_, "i"));
165 KeyError k(&scope, mainModuleAt(runtime_, "k"));
166
167 EXPECT_TRUE(runtime_->isInstanceOfBaseException(*l));
168 EXPECT_TRUE(runtime_->isInstanceOfBaseException(*i));
169 EXPECT_TRUE(runtime_->isInstanceOfBaseException(*k));
170}
171
172TEST_F(ExceptionBuiltinsTest, KeyErrorStrPrintsMissingKey) {
173 HandleScope scope(thread_);
174
175 ASSERT_FALSE(runFromCStr(runtime_, R"(
176s = KeyError("key").__str__()
177)")
178 .isError());
179
180 Object s(&scope, mainModuleAt(runtime_, "s"));
181 EXPECT_TRUE(isStrEqualsCStr(*s, "'key'"));
182}
183
184TEST_F(ExceptionBuiltinsTest,
185 KeyErrorStrWithMoreThanOneArgPrintsBaseExceptionStr) {
186 HandleScope scope(thread_);
187
188 ASSERT_FALSE(runFromCStr(runtime_, R"(
189s = KeyError("key", "key2").__str__()
190b = BaseException("key", "key2").__str__()
191)")
192 .isError());
193
194 Str s(&scope, mainModuleAt(runtime_, "s"));
195 Str b(&scope, mainModuleAt(runtime_, "b"));
196 EXPECT_TRUE(isStrEquals(s, b));
197}
198
199TEST_F(ExceptionBuiltinsTest, TypeErrorReturnsTypeError) {
200 HandleScope scope(thread_);
201
202 ASSERT_FALSE(runFromCStr(runtime_, R"(
203exc = TypeError()
204)")
205 .isError());
206
207 Object exc(&scope, mainModuleAt(runtime_, "exc"));
208 BaseException exception(&scope, *exc);
209
210 // The args attribute contains a tuple of the constructor arguments.
211 ASSERT_TRUE(exception.args().isTuple());
212 Tuple args(&scope, exception.args());
213 EXPECT_EQ(args.length(), 0);
214}
215
216TEST_F(ExceptionBuiltinsTest, StopIterationNoArguments) {
217 HandleScope scope(thread_);
218
219 ASSERT_FALSE(runFromCStr(runtime_, R"(
220exc = StopIteration()
221)")
222 .isError());
223
224 Object exc(&scope, mainModuleAt(runtime_, "exc"));
225 ASSERT_TRUE(exc.isStopIteration());
226 StopIteration stop_iteration(&scope, *exc);
227
228 // No constructor arguments so value should be none.
229 EXPECT_TRUE(stop_iteration.value().isNoneType());
230
231 // No constructor arguments means args should contain an empty tuple.
232 ASSERT_TRUE(stop_iteration.args().isTuple());
233 Tuple args(&scope, stop_iteration.args());
234 EXPECT_EQ(args.length(), 0);
235}
236
237TEST_F(ExceptionBuiltinsTest, StopIterationOneArgument) {
238 HandleScope scope(thread_);
239
240 ASSERT_FALSE(runFromCStr(runtime_, R"(
241exc = StopIteration(1)
242)")
243 .isError());
244
245 Object exc(&scope, mainModuleAt(runtime_, "exc"));
246 ASSERT_TRUE(exc.isStopIteration());
247 StopIteration stop_iteration(&scope, *exc);
248
249 // The value attribute should contain the first constructor argument.
250 EXPECT_EQ(stop_iteration.value(), SmallInt::fromWord(1));
251
252 // The args attribute contains a tuple of the constructor arguments.
253 ASSERT_TRUE(stop_iteration.args().isTuple());
254 Tuple args(&scope, stop_iteration.args());
255 ASSERT_EQ(args.length(), 1);
256 EXPECT_EQ(args.at(0), SmallInt::fromWord(1));
257}
258
259TEST_F(ExceptionBuiltinsTest, StopIterationManyArguments) {
260 HandleScope scope(thread_);
261
262 ASSERT_FALSE(runFromCStr(runtime_, R"(
263exc = StopIteration(4, 5, 6)
264)")
265 .isError());
266
267 Object exc(&scope, mainModuleAt(runtime_, "exc"));
268 ASSERT_TRUE(exc.isStopIteration());
269 StopIteration stop_iteration(&scope, *exc);
270
271 // The value attribute should contain the first constructor argument.
272 EXPECT_EQ(stop_iteration.value(), SmallInt::fromWord(4));
273
274 // The args attribute contains a tuple of the constructor arguments.
275 ASSERT_TRUE(stop_iteration.args().isTuple());
276 Tuple args(&scope, stop_iteration.args());
277 ASSERT_EQ(args.length(), 3);
278 EXPECT_EQ(args.at(0), SmallInt::fromWord(4));
279 EXPECT_EQ(args.at(1), SmallInt::fromWord(5));
280 EXPECT_EQ(args.at(2), SmallInt::fromWord(6));
281}
282
283TEST_F(ExceptionBuiltinsTest, NotImplementedErrorNoArguments) {
284 HandleScope scope(thread_);
285
286 ASSERT_FALSE(runFromCStr(runtime_, R"(
287exc = NotImplementedError()
288exc_is_rt_error = issubclass(NotImplementedError, RuntimeError)
289)")
290 .isError());
291
292 NotImplementedError exc(&scope, mainModuleAt(runtime_, "exc"));
293 Bool exc_is_rt_error(&scope, mainModuleAt(runtime_, "exc_is_rt_error"));
294
295 EXPECT_TRUE(runtime_->isInstanceOfBaseException(*exc));
296
297 EXPECT_TRUE(exc_is_rt_error.value());
298}
299
300TEST_F(ExceptionBuiltinsTest, SystemExitNoArguments) {
301 HandleScope scope(thread_);
302
303 ASSERT_FALSE(runFromCStr(runtime_, R"(
304exc = SystemExit()
305)")
306 .isError());
307
308 Object exc(&scope, mainModuleAt(runtime_, "exc"));
309 ASSERT_TRUE(exc.isSystemExit());
310 SystemExit system_exit(&scope, *exc);
311 ASSERT_TRUE(system_exit.args().isTuple());
312
313 // No constructor arguments so code should be none.
314 EXPECT_TRUE(system_exit.code().isNoneType());
315
316 // No constructor arguments means args should contain an empty tuple.
317 Tuple args(&scope, system_exit.args());
318 EXPECT_EQ(args.length(), 0);
319}
320
321TEST_F(ExceptionBuiltinsTest, SystemExitOneArgument) {
322 HandleScope scope(thread_);
323
324 ASSERT_FALSE(runFromCStr(runtime_, R"(
325exc = SystemExit(1)
326)")
327 .isError());
328
329 Object exc(&scope, mainModuleAt(runtime_, "exc"));
330 ASSERT_TRUE(exc.isSystemExit());
331 SystemExit system_exit(&scope, *exc);
332 ASSERT_TRUE(system_exit.args().isTuple());
333
334 // The code attribute should contain the first constructor argument.
335 EXPECT_EQ(system_exit.code(), SmallInt::fromWord(1));
336
337 // The args attribute contains a tuple of the constructor arguments.
338 Tuple args(&scope, system_exit.args());
339 ASSERT_EQ(args.length(), 1);
340 EXPECT_EQ(args.at(0), SmallInt::fromWord(1));
341}
342
343TEST_F(ExceptionBuiltinsTest, SystemExitManyArguments) {
344 HandleScope scope(thread_);
345
346 ASSERT_FALSE(runFromCStr(runtime_, R"(
347exc = SystemExit(4, 5, 6)
348)")
349 .isError());
350
351 Object exc(&scope, mainModuleAt(runtime_, "exc"));
352 ASSERT_TRUE(exc.isSystemExit());
353 SystemExit system_exit(&scope, *exc);
354
355 // The code attribute should contain the first constructor argument.
356 EXPECT_EQ(system_exit.code(), SmallInt::fromWord(4));
357
358 // The args attribute contains a tuple of the constructor arguments.
359 ASSERT_TRUE(system_exit.args().isTuple());
360 Tuple args(&scope, system_exit.args());
361 ASSERT_EQ(args.length(), 3);
362 EXPECT_EQ(args.at(0), SmallInt::fromWord(4));
363 EXPECT_EQ(args.at(1), SmallInt::fromWord(5));
364 EXPECT_EQ(args.at(2), SmallInt::fromWord(6));
365}
366
367TEST_F(ExceptionBuiltinsTest, ImportErrorConstructEmpty) {
368 HandleScope scope(thread_);
369 ASSERT_FALSE(runFromCStr(runtime_, "x = ImportError()").isError());
370 Object data(&scope, mainModuleAt(runtime_, "x"));
371 ASSERT_TRUE(data.isImportError());
372
373 ImportError err(&scope, *data);
374 EXPECT_EQ(err.msg(), NoneType::object());
375 EXPECT_EQ(err.path(), NoneType::object());
376 EXPECT_EQ(err.name(), NoneType::object());
377
378 err.setMsg(SmallInt::fromWord(1111));
379 EXPECT_TRUE(isIntEqualsWord(err.msg(), 1111));
380
381 err.setPath(SmallInt::fromWord(2222));
382 EXPECT_TRUE(isIntEqualsWord(err.path(), 2222));
383
384 err.setName(SmallInt::fromWord(3333));
385 EXPECT_TRUE(isIntEqualsWord(err.name(), 3333));
386}
387
388TEST_F(ExceptionBuiltinsTest, ImportErrorConstructWithMsg) {
389 HandleScope scope(thread_);
390 ASSERT_FALSE(runFromCStr(runtime_, "x = ImportError(1111)").isError());
391 Object data(&scope, mainModuleAt(runtime_, "x"));
392 ASSERT_TRUE(data.isImportError());
393
394 ImportError err(&scope, *data);
395 EXPECT_TRUE(isIntEqualsWord(err.msg(), 1111));
396 EXPECT_EQ(err.path(), NoneType::object());
397 EXPECT_EQ(err.name(), NoneType::object());
398}
399
400TEST_F(ExceptionBuiltinsTest, ImportErrorConstructWithMsgNameAndPath) {
401 HandleScope scope(thread_);
402 ASSERT_FALSE(
403 runFromCStr(runtime_, "x = ImportError(1111, name=2222, path=3333)")
404 .isError());
405 Object data(&scope, mainModuleAt(runtime_, "x"));
406 ASSERT_TRUE(data.isImportError());
407
408 ImportError err(&scope, *data);
409 EXPECT_TRUE(isIntEqualsWord(err.msg(), 1111));
410 EXPECT_TRUE(isIntEqualsWord(err.name(), 2222));
411 EXPECT_TRUE(isIntEqualsWord(err.path(), 3333));
412}
413
414TEST_F(ExceptionBuiltinsTest, ImportErrorConstructWithInvalidKwargs) {
415 EXPECT_TRUE(raisedWithStr(
416 runFromCStr(runtime_, "x = ImportError(foo=123)"), LayoutId::kTypeError,
417 "ImportError.__init__() got an unexpected keyword argument 'foo'"));
418}
419
420TEST_F(ExceptionBuiltinsTest, ModuleNotFoundErrorManyArguments) {
421 HandleScope scope(thread_);
422
423 ASSERT_FALSE(runFromCStr(runtime_, R"(
424exc = ModuleNotFoundError(1111, name=2222, path=3333)
425)")
426 .isError());
427
428 Object data(&scope, mainModuleAt(runtime_, "exc"));
429 ASSERT_TRUE(data.isModuleNotFoundError());
430
431 ModuleNotFoundError err(&scope, *data);
432 EXPECT_TRUE(isIntEqualsWord(err.msg(), 1111));
433 EXPECT_TRUE(isIntEqualsWord(err.name(), 2222));
434 EXPECT_TRUE(isIntEqualsWord(err.path(), 3333));
435}
436
437TEST_F(ExceptionBuiltinsTest, DunderReprWithNoArgsHasEmptyParens) {
438 ASSERT_FALSE(runFromCStr(runtime_, R"(
439result = NameError().__repr__()
440)")
441 .isError());
442
443 EXPECT_TRUE(isStrEqualsCStr(mainModuleAt(runtime_, "result"), "NameError()"));
444}
445
446TEST_F(ExceptionBuiltinsTest, DunderReprCallsTupleRepr) {
447 ASSERT_FALSE(runFromCStr(runtime_, R"(
448n = NameError().__class__.__name__
449result = NameError(1, 2).__repr__()
450)")
451 .isError());
452
453 EXPECT_TRUE(isStrEqualsCStr(mainModuleAt(runtime_, "n"), "NameError"));
454 EXPECT_TRUE(
455 isStrEqualsCStr(mainModuleAt(runtime_, "result"), "NameError(1, 2)"));
456}
457
458TEST_F(ExceptionBuiltinsTest, UnicodeErrorsHaveProperBuiltinBases) {
459 HandleScope scope(thread_);
460 Type unic_error(&scope, runtime_->typeAt(LayoutId::kUnicodeError));
461 Type unic_dec_error(&scope, runtime_->typeAt(LayoutId::kUnicodeDecodeError));
462 Type unic_enc_error(&scope, runtime_->typeAt(LayoutId::kUnicodeEncodeError));
463 Type unic_trans_error(&scope,
464 runtime_->typeAt(LayoutId::kUnicodeTranslateError));
465 EXPECT_EQ(unic_error.builtinBase(), LayoutId::kValueError);
466 EXPECT_EQ(unic_dec_error.builtinBase(), LayoutId::kUnicodeDecodeError);
467 EXPECT_EQ(unic_enc_error.builtinBase(), LayoutId::kUnicodeEncodeError);
468 EXPECT_EQ(unic_trans_error.builtinBase(), LayoutId::kUnicodeTranslateError);
469}
470
471TEST_F(ExceptionBuiltinsTest,
472 UnicodeDecodeErrorWithImproperFirstArgumentsRaisesTypeError) {
473 const char* bad_arg = "exc = UnicodeDecodeError([], b'', 1, 1, '1')";
474 EXPECT_TRUE(raisedWithStr(runFromCStr(runtime_, bad_arg),
475 LayoutId::kTypeError,
476 "argument 1 must be str, not list"));
477}
478
479TEST_F(ExceptionBuiltinsTest,
480 UnicodeDecodeErrorWithImproperSecondArgumentsRaisesTypeError) {
481 const char* bad_arg = "exc = UnicodeDecodeError('1', [], 1, 1, '1')";
482 EXPECT_TRUE(raisedWithStr(runFromCStr(runtime_, bad_arg),
483 LayoutId::kTypeError,
484 "a bytes-like object is required, not 'list'"));
485}
486
487TEST_F(ExceptionBuiltinsTest,
488 UnicodeDecodeErrorWithImproperThirdArgumentsRaisesTypeError) {
489 const char* bad_arg = "exc = UnicodeDecodeError('1', b'', [], 1, '1')";
490 EXPECT_TRUE(
491 raisedWithStr(runFromCStr(runtime_, bad_arg), LayoutId::kTypeError,
492 "'list' object cannot be interpreted as an integer"));
493}
494
495TEST_F(ExceptionBuiltinsTest,
496 UnicodeDecodeErrorWithImproperFourthArgumentsRaisesTypeError) {
497 const char* bad_arg = "exc = UnicodeDecodeError('1', b'', 1, [], '1')";
498 EXPECT_TRUE(
499 raisedWithStr(runFromCStr(runtime_, bad_arg), LayoutId::kTypeError,
500 "'list' object cannot be interpreted as an integer"));
501}
502
503TEST_F(ExceptionBuiltinsTest,
504 UnicodeDecodeErrorWithImproperFifthArgumentsRaisesTypeError) {
505 const char* bad_arg = "exc = UnicodeDecodeError('1', b'', 1, 1, [])";
506 EXPECT_TRUE(raisedWithStr(runFromCStr(runtime_, bad_arg),
507 LayoutId::kTypeError,
508 "argument 5 must be str, not list"));
509}
510
511TEST_F(ExceptionBuiltinsTest, UnicodeDecodeErrorReturnsObjectWithFieldsSet) {
512 HandleScope scope(thread_);
513 ASSERT_FALSE(
514 runFromCStr(runtime_, "exc = UnicodeDecodeError('en', b'ob', 1, 2, 're')")
515 .isError());
516
517 Object data(&scope, mainModuleAt(runtime_, "exc"));
518 ASSERT_TRUE(data.isUnicodeDecodeError());
519
520 UnicodeDecodeError err(&scope, *data);
521 EXPECT_TRUE(isStrEqualsCStr(err.encoding(), "en"));
522 Object bytes(&scope, err.object());
523 EXPECT_TRUE(isBytesEqualsCStr(bytes, "ob"));
524 EXPECT_TRUE(isIntEqualsWord(err.start(), 1));
525 EXPECT_TRUE(isIntEqualsWord(err.end(), 2));
526 EXPECT_TRUE(isStrEqualsCStr(err.reason(), "re"));
527}
528
529TEST_F(ExceptionBuiltinsTest,
530 UnicodeDecodeErrorWithIndexSubclassReturnsObject) {
531 HandleScope scope(thread_);
532 ASSERT_FALSE(runFromCStr(runtime_, R"(
533class Ind():
534 def __index__(self):
535 return 1
536i = Ind()
537exc = UnicodeDecodeError('en', b'ob', i, i, 're')
538)")
539 .isError());
540
541 Object data(&scope, mainModuleAt(runtime_, "exc"));
542 ASSERT_TRUE(data.isUnicodeDecodeError());
543
544 UnicodeDecodeError err(&scope, *data);
545 EXPECT_TRUE(isIntEqualsWord(err.start(), 1));
546 EXPECT_TRUE(isIntEqualsWord(err.end(), 1));
547}
548
549TEST_F(ExceptionBuiltinsTest,
550 UnicodeEncodeErrorWithImproperFirstArgumentsRaisesTypeError) {
551 const char* bad_arg = "exc = UnicodeEncodeError([], '', 1, 1, '1')";
552 EXPECT_TRUE(raisedWithStr(runFromCStr(runtime_, bad_arg),
553 LayoutId::kTypeError,
554 "argument 1 must be str, not list"));
555}
556
557TEST_F(ExceptionBuiltinsTest,
558 UnicodeEncodeErrorWithImproperSecondArgumentsRaisesTypeError) {
559 const char* bad_arg = "exc = UnicodeEncodeError('1', [], 1, 1, '1')";
560 EXPECT_TRUE(raisedWithStr(runFromCStr(runtime_, bad_arg),
561 LayoutId::kTypeError,
562 "argument 2 must be str, not 'list'"));
563}
564
565TEST_F(ExceptionBuiltinsTest,
566 UnicodeEncodeErrorWithImproperThirdArgumentsRaisesTypeError) {
567 const char* bad_arg = "exc = UnicodeEncodeError('1', '', [], 1, '1')";
568 EXPECT_TRUE(
569 raisedWithStr(runFromCStr(runtime_, bad_arg), LayoutId::kTypeError,
570 "'list' object cannot be interpreted as an integer"));
571}
572
573TEST_F(ExceptionBuiltinsTest,
574 UnicodeEncodeErrorWithImproperFourthArgumentsRaisesTypeError) {
575 const char* bad_arg = "exc = UnicodeEncodeError('1', '', 1, [], '1')";
576 EXPECT_TRUE(
577 raisedWithStr(runFromCStr(runtime_, bad_arg), LayoutId::kTypeError,
578 "'list' object cannot be interpreted as an integer"));
579}
580
581TEST_F(ExceptionBuiltinsTest,
582 UnicodeEncodeErrorWithImproperFifthArgumentsRaisesTypeError) {
583 const char* bad_arg = "exc = UnicodeEncodeError('1', '', 1, 1, [])";
584 EXPECT_TRUE(raisedWithStr(runFromCStr(runtime_, bad_arg),
585 LayoutId::kTypeError,
586 "argument 5 must be str, not list"));
587}
588
589TEST_F(ExceptionBuiltinsTest, UnicodeEncodeErrorReturnsObjectWithFieldsSet) {
590 HandleScope scope(thread_);
591 ASSERT_FALSE(
592 runFromCStr(runtime_, "exc = UnicodeEncodeError('en', 'ob', 1, 2, 're')")
593 .isError());
594
595 Object data(&scope, mainModuleAt(runtime_, "exc"));
596 ASSERT_TRUE(data.isUnicodeEncodeError());
597
598 UnicodeEncodeError err(&scope, *data);
599 EXPECT_TRUE(isStrEqualsCStr(err.encoding(), "en"));
600 EXPECT_TRUE(isStrEqualsCStr(err.object(), "ob"));
601 EXPECT_TRUE(isIntEqualsWord(err.start(), 1));
602 EXPECT_TRUE(isIntEqualsWord(err.end(), 2));
603 EXPECT_TRUE(isStrEqualsCStr(err.reason(), "re"));
604}
605
606TEST_F(ExceptionBuiltinsTest,
607 UnicodeEncodeErrorWithIndexSubclassReturnsObject) {
608 HandleScope scope(thread_);
609 ASSERT_FALSE(runFromCStr(runtime_, R"(
610class Ind():
611 def __index__(self):
612 return 1
613i = Ind()
614exc = UnicodeEncodeError('en', 'ob', i, i, 're')
615)")
616 .isError());
617
618 Object data(&scope, mainModuleAt(runtime_, "exc"));
619 ASSERT_TRUE(data.isUnicodeEncodeError());
620
621 UnicodeEncodeError err(&scope, *data);
622 EXPECT_TRUE(isIntEqualsWord(err.start(), 1));
623 EXPECT_TRUE(isIntEqualsWord(err.end(), 1));
624}
625
626TEST_F(ExceptionBuiltinsTest,
627 UnicodeTranslateErrorWithImproperFirstArgumentsRaisesTypeError) {
628 const char* bad_arg = "exc = UnicodeTranslateError([], 1, 1, '1')";
629 EXPECT_TRUE(raisedWithStr(runFromCStr(runtime_, bad_arg),
630 LayoutId::kTypeError,
631 "argument 1 must be str, not list"));
632}
633
634TEST_F(ExceptionBuiltinsTest,
635 UnicodeTranslateErrorWithImproperSecondArgumentsRaisesTypeError) {
636 const char* bad_arg = "exc = UnicodeTranslateError('1', [], 1, '1')";
637 EXPECT_TRUE(
638 raisedWithStr(runFromCStr(runtime_, bad_arg), LayoutId::kTypeError,
639 "'list' object cannot be interpreted as an integer"));
640}
641
642TEST_F(ExceptionBuiltinsTest,
643 UnicodeTranslateErrorWithImproperThirdArgumentsRaisesTypeError) {
644 const char* bad_arg = "exc = UnicodeTranslateError('1', 1, [], '1')";
645 EXPECT_TRUE(
646 raisedWithStr(runFromCStr(runtime_, bad_arg), LayoutId::kTypeError,
647 "'list' object cannot be interpreted as an integer"));
648}
649
650TEST_F(ExceptionBuiltinsTest,
651 UnicodeTranslateErrorWithImproperFourthArgumentsRaisesTypeError) {
652 const char* bad_arg = "exc = UnicodeTranslateError('1', 1, 1, [])";
653 EXPECT_TRUE(raisedWithStr(runFromCStr(runtime_, bad_arg),
654 LayoutId::kTypeError,
655 "argument 4 must be str, not list"));
656}
657
658TEST_F(ExceptionBuiltinsTest, UnicodeTranslateErrorReturnsObjectWithFieldsSet) {
659 HandleScope scope(thread_);
660 ASSERT_FALSE(
661 runFromCStr(runtime_, "exc = UnicodeTranslateError('obj', 1, 2, 're')")
662 .isError());
663
664 Object data(&scope, mainModuleAt(runtime_, "exc"));
665 ASSERT_TRUE(data.isUnicodeTranslateError());
666
667 UnicodeTranslateError err(&scope, *data);
668 EXPECT_TRUE(isStrEqualsCStr(err.object(), "obj"));
669 EXPECT_TRUE(isIntEqualsWord(err.start(), 1));
670 EXPECT_TRUE(isIntEqualsWord(err.end(), 2));
671 EXPECT_TRUE(isStrEqualsCStr(err.reason(), "re"));
672}
673
674TEST_F(ExceptionBuiltinsTest,
675 UnicodeTranslateErrorWithIndexSubclassReturnsObject) {
676 HandleScope scope(thread_);
677 ASSERT_FALSE(runFromCStr(runtime_, R"(
678class Ind():
679 def __index__(self):
680 return 1
681i = Ind()
682exc = UnicodeTranslateError('en', i, i, 're')
683)")
684 .isError());
685
686 Object data(&scope, mainModuleAt(runtime_, "exc"));
687 ASSERT_TRUE(data.isUnicodeTranslateError());
688
689 UnicodeTranslateError err(&scope, *data);
690 EXPECT_TRUE(isIntEqualsWord(err.start(), 1));
691 EXPECT_TRUE(isIntEqualsWord(err.end(), 1));
692}
693
694} // namespace testing
695} // namespace py