| 1 |
# Argument Clinic |
| 2 |
# Copyright 2012-2013 by Larry Hastings. |
| 3 |
# Licensed to the PSF under a contributor agreement. |
| 4 |
|
| 5 |
from functools import partial |
| 6 |
from test import support, test_tools |
| 7 |
from test.support import os_helper |
| 8 |
from test.support.os_helper import TESTFN, unlink, rmtree |
| 9 |
from textwrap import dedent |
| 10 |
from unittest import TestCase |
| 11 |
import inspect |
| 12 |
import os.path |
| 13 |
import re |
| 14 |
import sys |
| 15 |
import unittest |
| 16 |
|
| 17 |
test_tools.skip_if_missing('clinic') |
| 18 |
with test_tools.imports_under_tool('clinic'): |
| 19 |
import libclinic |
| 20 |
from libclinic import ClinicError, unspecified, NULL, fail |
| 21 |
from libclinic.converters import int_converter, str_converter, self_converter |
| 22 |
from libclinic.function import ( |
| 23 |
Module, Class, Function, FunctionKind, Parameter, |
| 24 |
permute_optional_groups, permute_right_option_groups, |
| 25 |
permute_left_option_groups) |
| 26 |
import clinic |
| 27 |
from libclinic.clanguage import CLanguage |
| 28 |
from libclinic.converter import converters, legacy_converters |
| 29 |
from libclinic.return_converters import return_converters, int_return_converter |
| 30 |
from libclinic.block_parser import Block, BlockParser |
| 31 |
from libclinic.codegen import BlockPrinter, Destination |
| 32 |
from libclinic.dsl_parser import DSLParser |
| 33 |
from libclinic.cli import parse_file, Clinic |
| 34 |
|
| 35 |
|
| 36 |
def repeat_fn(*functions): |
| 37 |
def wrapper(test): |
| 38 |
def wrapped(self): |
| 39 |
for fn in functions: |
| 40 |
with self.subTest(fn=fn): |
| 41 |
test(self, fn) |
| 42 |
return wrapped |
| 43 |
return wrapper |
| 44 |
|
| 45 |
def _make_clinic(*, filename='clinic_tests', limited_capi=False): |
| 46 |
clang = CLanguage(filename) |
| 47 |
c = Clinic(clang, filename=filename, limited_capi=limited_capi) |
| 48 |
c.block_parser = BlockParser('', clang) |
| 49 |
return c |
| 50 |
|
| 51 |
|
| 52 |
def _expect_failure(tc, parser, code, errmsg, *, filename=None, lineno=None, |
| 53 |
strip=True): |
| 54 |
"""Helper for the parser tests. |
| 55 |
|
| 56 |
tc: unittest.TestCase; passed self in the wrapper |
| 57 |
parser: the clinic parser used for this test case |
| 58 |
code: a str with input text (clinic code) |
| 59 |
errmsg: the expected error message |
| 60 |
filename: str, optional filename |
| 61 |
lineno: int, optional line number |
| 62 |
""" |
| 63 |
code = dedent(code) |
| 64 |
if strip: |
| 65 |
code = code.strip() |
| 66 |
errmsg = re.escape(errmsg) |
| 67 |
with tc.assertRaisesRegex(ClinicError, errmsg) as cm: |
| 68 |
parser(code) |
| 69 |
if filename is not None: |
| 70 |
tc.assertEqual(cm.exception.filename, filename) |
| 71 |
if lineno is not None: |
| 72 |
tc.assertEqual(cm.exception.lineno, lineno) |
| 73 |
return cm.exception |
| 74 |
|
| 75 |
|
| 76 |
def restore_dict(converters, old_converters): |
| 77 |
converters.clear() |
| 78 |
converters.update(old_converters) |
| 79 |
|
| 80 |
|
| 81 |
def save_restore_converters(testcase): |
| 82 |
testcase.addCleanup(restore_dict, converters, |
| 83 |
converters.copy()) |
| 84 |
testcase.addCleanup(restore_dict, legacy_converters, |
| 85 |
legacy_converters.copy()) |
| 86 |
testcase.addCleanup(restore_dict, return_converters, |
| 87 |
return_converters.copy()) |
| 88 |
|
| 89 |
|
| 90 |
class ClinicWholeFileTest(TestCase): |
| 91 |
maxDiff = None |
| 92 |
|
| 93 |
def expect_failure(self, raw, errmsg, *, filename=None, lineno=None): |
| 94 |
_expect_failure(self, self.clinic.parse, raw, errmsg, |
| 95 |
filename=filename, lineno=lineno) |
| 96 |
|
| 97 |
def setUp(self): |
| 98 |
save_restore_converters(self) |
| 99 |
self.clinic = _make_clinic(filename="test.c") |
| 100 |
|
| 101 |
def test_eol(self): |
| 102 |
# regression test: |
| 103 |
# clinic's block parser didn't recognize |
| 104 |
# the "end line" for the block if it |
| 105 |
# didn't end in "\n" (as in, the last) |
| 106 |
# byte of the file was '/'. |
| 107 |
# so it would spit out an end line for you. |
| 108 |
# and since you really already had one, |
| 109 |
# the last line of the block got corrupted. |
| 110 |
raw = "/*[clinic]\nfoo\n[clinic]*/" |
| 111 |
cooked = self.clinic.parse(raw).splitlines() |
| 112 |
end_line = cooked[2].rstrip() |
| 113 |
# this test is redundant, it's just here explicitly to catch |
| 114 |
# the regression test so we don't forget what it looked like |
| 115 |
self.assertNotEqual(end_line, "[clinic]*/[clinic]*/") |
| 116 |
self.assertEqual(end_line, "[clinic]*/") |
| 117 |
|
| 118 |
def test_mangled_marker_line(self): |
| 119 |
raw = """ |
| 120 |
/*[clinic input] |
| 121 |
[clinic start generated code]*/ |
| 122 |
/*[clinic end generated code: foo]*/ |
| 123 |
""" |
| 124 |
err = ( |
| 125 |
"Mangled Argument Clinic marker line: " |
| 126 |
"'/*[clinic end generated code: foo]*/'" |
| 127 |
) |
| 128 |
self.expect_failure(raw, err, filename="test.c", lineno=3) |
| 129 |
|
| 130 |
def test_checksum_mismatch(self): |
| 131 |
raw = """ |
| 132 |
/*[clinic input] |
| 133 |
[clinic start generated code]*/ |
| 134 |
/*[clinic end generated code: output=0123456789abcdef input=fedcba9876543210]*/ |
| 135 |
""" |
| 136 |
err = ("Checksum mismatch! " |
| 137 |
"Expected '0123456789abcdef', computed 'da39a3ee5e6b4b0d'") |
| 138 |
self.expect_failure(raw, err, filename="test.c", lineno=3) |
| 139 |
|
| 140 |
def test_garbage_after_stop_line(self): |
| 141 |
raw = """ |
| 142 |
/*[clinic input] |
| 143 |
[clinic start generated code]*/foobarfoobar! |
| 144 |
""" |
| 145 |
err = "Garbage after stop line: 'foobarfoobar!'" |
| 146 |
self.expect_failure(raw, err, filename="test.c", lineno=2) |
| 147 |
|
| 148 |
def test_whitespace_before_stop_line(self): |
| 149 |
raw = """ |
| 150 |
/*[clinic input] |
| 151 |
[clinic start generated code]*/ |
| 152 |
""" |
| 153 |
err = ( |
| 154 |
"Whitespace is not allowed before the stop line: " |
| 155 |
"' [clinic start generated code]*/'" |
| 156 |
) |
| 157 |
self.expect_failure(raw, err, filename="test.c", lineno=2) |
| 158 |
|
| 159 |
def test_parse_with_body_prefix(self): |
| 160 |
clang = CLanguage(None) |
| 161 |
clang.body_prefix = "//" |
| 162 |
clang.start_line = "//[{dsl_name} start]" |
| 163 |
clang.stop_line = "//[{dsl_name} stop]" |
| 164 |
cl = Clinic(clang, filename="test.c", limited_capi=False) |
| 165 |
raw = dedent(""" |
| 166 |
//[clinic start] |
| 167 |
//module test |
| 168 |
//[clinic stop] |
| 169 |
""").strip() |
| 170 |
out = cl.parse(raw) |
| 171 |
expected = dedent(""" |
| 172 |
//[clinic start] |
| 173 |
//module test |
| 174 |
// |
| 175 |
//[clinic stop] |
| 176 |
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=65fab8adff58cf08]*/ |
| 177 |
""").lstrip() # Note, lstrip() because of the newline |
| 178 |
self.assertEqual(out, expected) |
| 179 |
|
| 180 |
def test_cpp_monitor_fail_nested_block_comment(self): |
| 181 |
raw = """ |
| 182 |
/* start |
| 183 |
/* nested |
| 184 |
*/ |
| 185 |
*/ |
| 186 |
""" |
| 187 |
err = 'Nested block comment!' |
| 188 |
self.expect_failure(raw, err, filename="test.c", lineno=2) |
| 189 |
|
| 190 |
def test_cpp_monitor_fail_invalid_format_noarg(self): |
| 191 |
raw = """ |
| 192 |
#if |
| 193 |
a() |
| 194 |
#endif |
| 195 |
""" |
| 196 |
err = 'Invalid format for #if line: no argument!' |
| 197 |
self.expect_failure(raw, err, filename="test.c", lineno=1) |
| 198 |
|
| 199 |
def test_cpp_monitor_fail_invalid_format_toomanyargs(self): |
| 200 |
raw = """ |
| 201 |
#ifdef A B |
| 202 |
a() |
| 203 |
#endif |
| 204 |
""" |
| 205 |
err = 'Invalid format for #ifdef line: should be exactly one argument!' |
| 206 |
self.expect_failure(raw, err, filename="test.c", lineno=1) |
| 207 |
|
| 208 |
def test_cpp_monitor_fail_no_matching_if(self): |
| 209 |
raw = '#else' |
| 210 |
err = '#else without matching #if / #ifdef / #ifndef!' |
| 211 |
self.expect_failure(raw, err, filename="test.c", lineno=1) |
| 212 |
|
| 213 |
def test_directive_output_unknown_preset(self): |
| 214 |
raw = """ |
| 215 |
/*[clinic input] |
| 216 |
output preset nosuchpreset |
| 217 |
[clinic start generated code]*/ |
| 218 |
""" |
| 219 |
err = "Unknown preset 'nosuchpreset'" |
| 220 |
self.expect_failure(raw, err) |
| 221 |
|
| 222 |
def test_directive_output_cant_pop(self): |
| 223 |
raw = """ |
| 224 |
/*[clinic input] |
| 225 |
output pop |
| 226 |
[clinic start generated code]*/ |
| 227 |
""" |
| 228 |
err = "Can't 'output pop', stack is empty" |
| 229 |
self.expect_failure(raw, err) |
| 230 |
|
| 231 |
def test_directive_output_print(self): |
| 232 |
raw = dedent(""" |
| 233 |
/*[clinic input] |
| 234 |
output print 'I told you once.' |
| 235 |
[clinic start generated code]*/ |
| 236 |
""") |
| 237 |
out = self.clinic.parse(raw) |
| 238 |
# The generated output will differ for every run, but we can check that |
| 239 |
# it starts with the clinic block, we check that it contains all the |
| 240 |
# expected fields, and we check that it contains the checksum line. |
| 241 |
self.assertStartsWith(out, dedent(""" |
| 242 |
/*[clinic input] |
| 243 |
output print 'I told you once.' |
| 244 |
[clinic start generated code]*/ |
| 245 |
""")) |
| 246 |
fields = { |
| 247 |
"cpp_endif", |
| 248 |
"cpp_if", |
| 249 |
"docstring_definition", |
| 250 |
"docstring_prototype", |
| 251 |
"impl_definition", |
| 252 |
"impl_prototype", |
| 253 |
"methoddef_define", |
| 254 |
"methoddef_ifndef", |
| 255 |
"parser_definition", |
| 256 |
"parser_prototype", |
| 257 |
} |
| 258 |
for field in fields: |
| 259 |
with self.subTest(field=field): |
| 260 |
self.assertIn(field, out) |
| 261 |
last_line = out.rstrip().split("\n")[-1] |
| 262 |
self.assertStartsWith(last_line, "/*[clinic end generated code: output=") |
| 263 |
|
| 264 |
def test_directive_wrong_arg_number(self): |
| 265 |
raw = dedent(""" |
| 266 |
/*[clinic input] |
| 267 |
preserve foo bar baz eggs spam ham mushrooms |
| 268 |
[clinic start generated code]*/ |
| 269 |
""") |
| 270 |
err = "takes 1 positional argument but 8 were given" |
| 271 |
self.expect_failure(raw, err) |
| 272 |
|
| 273 |
def test_unknown_destination_command(self): |
| 274 |
raw = """ |
| 275 |
/*[clinic input] |
| 276 |
destination buffer nosuchcommand |
| 277 |
[clinic start generated code]*/ |
| 278 |
""" |
| 279 |
err = "unknown destination command 'nosuchcommand'" |
| 280 |
self.expect_failure(raw, err) |
| 281 |
|
| 282 |
def test_no_access_to_members_in_converter_init(self): |
| 283 |
raw = """ |
| 284 |
/*[python input] |
| 285 |
class Custom_converter(CConverter): |
| 286 |
converter = "some_c_function" |
| 287 |
def converter_init(self): |
| 288 |
self.function.noaccess |
| 289 |
[python start generated code]*/ |
| 290 |
/*[clinic input] |
| 291 |
module test |
| 292 |
test.fn |
| 293 |
a: Custom |
| 294 |
[clinic start generated code]*/ |
| 295 |
""" |
| 296 |
err = ( |
| 297 |
"accessing self.function inside converter_init is disallowed!" |
| 298 |
) |
| 299 |
self.expect_failure(raw, err) |
| 300 |
|
| 301 |
def test_clone_mismatch(self): |
| 302 |
err = "'kind' of function and cloned function don't match!" |
| 303 |
block = """ |
| 304 |
/*[clinic input] |
| 305 |
module m |
| 306 |
@classmethod |
| 307 |
m.f1 |
| 308 |
a: object |
| 309 |
[clinic start generated code]*/ |
| 310 |
/*[clinic input] |
| 311 |
@staticmethod |
| 312 |
m.f2 = m.f1 |
| 313 |
[clinic start generated code]*/ |
| 314 |
""" |
| 315 |
self.expect_failure(block, err, lineno=9) |
| 316 |
|
| 317 |
def test_badly_formed_return_annotation(self): |
| 318 |
err = "Badly formed annotation for 'm.f': 'Custom'" |
| 319 |
block = """ |
| 320 |
/*[python input] |
| 321 |
class Custom_return_converter(CReturnConverter): |
| 322 |
def __init__(self): |
| 323 |
raise ValueError("abc") |
| 324 |
[python start generated code]*/ |
| 325 |
/*[clinic input] |
| 326 |
module m |
| 327 |
m.f -> Custom |
| 328 |
[clinic start generated code]*/ |
| 329 |
""" |
| 330 |
self.expect_failure(block, err, lineno=8) |
| 331 |
|
| 332 |
def test_star_after_vararg(self): |
| 333 |
err = "'my_test_func' uses '*' more than once." |
| 334 |
block = """ |
| 335 |
/*[clinic input] |
| 336 |
my_test_func |
| 337 |
|
| 338 |
pos_arg: object |
| 339 |
*args: tuple |
| 340 |
* |
| 341 |
kw_arg: object |
| 342 |
[clinic start generated code]*/ |
| 343 |
""" |
| 344 |
self.expect_failure(block, err, lineno=6) |
| 345 |
|
| 346 |
def test_vararg_after_star(self): |
| 347 |
err = "'my_test_func' uses '*' more than once." |
| 348 |
block = """ |
| 349 |
/*[clinic input] |
| 350 |
my_test_func |
| 351 |
|
| 352 |
pos_arg: object |
| 353 |
* |
| 354 |
*args: tuple |
| 355 |
kw_arg: object |
| 356 |
[clinic start generated code]*/ |
| 357 |
""" |
| 358 |
self.expect_failure(block, err, lineno=6) |
| 359 |
|
| 360 |
def test_module_already_got_one(self): |
| 361 |
err = "Already defined module 'm'!" |
| 362 |
block = """ |
| 363 |
/*[clinic input] |
| 364 |
module m |
| 365 |
module m |
| 366 |
[clinic start generated code]*/ |
| 367 |
""" |
| 368 |
self.expect_failure(block, err, lineno=3) |
| 369 |
|
| 370 |
def test_destination_already_got_one(self): |
| 371 |
err = "Destination already exists: 'test'" |
| 372 |
block = """ |
| 373 |
/*[clinic input] |
| 374 |
destination test new buffer |
| 375 |
destination test new buffer |
| 376 |
[clinic start generated code]*/ |
| 377 |
""" |
| 378 |
self.expect_failure(block, err, lineno=3) |
| 379 |
|
| 380 |
def test_destination_does_not_exist(self): |
| 381 |
err = "Destination does not exist: '/dev/null'" |
| 382 |
block = """ |
| 383 |
/*[clinic input] |
| 384 |
output everything /dev/null |
| 385 |
[clinic start generated code]*/ |
| 386 |
""" |
| 387 |
self.expect_failure(block, err, lineno=2) |
| 388 |
|
| 389 |
def test_class_already_got_one(self): |
| 390 |
err = "Already defined class 'C'!" |
| 391 |
block = """ |
| 392 |
/*[clinic input] |
| 393 |
class C "" "" |
| 394 |
class C "" "" |
| 395 |
[clinic start generated code]*/ |
| 396 |
""" |
| 397 |
self.expect_failure(block, err, lineno=3) |
| 398 |
|
| 399 |
def test_cant_nest_module_inside_class(self): |
| 400 |
err = "Can't nest a module inside a class!" |
| 401 |
block = """ |
| 402 |
/*[clinic input] |
| 403 |
class C "" "" |
| 404 |
module C.m |
| 405 |
[clinic start generated code]*/ |
| 406 |
""" |
| 407 |
self.expect_failure(block, err, lineno=3) |
| 408 |
|
| 409 |
def test_dest_buffer_not_empty_at_eof(self): |
| 410 |
expected_warning = ("Destination buffer 'buffer' not empty at " |
| 411 |
"end of file, emptying.") |
| 412 |
expected_generated = dedent(""" |
| 413 |
/*[clinic input] |
| 414 |
output everything buffer |
| 415 |
fn |
| 416 |
a: object |
| 417 |
/ |
| 418 |
[clinic start generated code]*/ |
| 419 |
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=1c4668687f5fd002]*/ |
| 420 |
|
| 421 |
/*[clinic input] |
| 422 |
dump buffer |
| 423 |
[clinic start generated code]*/ |
| 424 |
|
| 425 |
PyDoc_VAR(fn__doc__); |
| 426 |
|
| 427 |
PyDoc_STRVAR(fn__doc__, |
| 428 |
"fn($module, a, /)\\n" |
| 429 |
"--\\n" |
| 430 |
"\\n"); |
| 431 |
|
| 432 |
#define FN_METHODDEF \\ |
| 433 |
{"fn", (PyCFunction)fn, METH_O, fn__doc__}, |
| 434 |
|
| 435 |
static PyObject * |
| 436 |
fn(PyObject *module, PyObject *a) |
| 437 |
/*[clinic end generated code: output=be6798b148ab4e53 input=524ce2e021e4eba6]*/ |
| 438 |
""") |
| 439 |
block = dedent(""" |
| 440 |
/*[clinic input] |
| 441 |
output everything buffer |
| 442 |
fn |
| 443 |
a: object |
| 444 |
/ |
| 445 |
[clinic start generated code]*/ |
| 446 |
""") |
| 447 |
with support.captured_stdout() as stdout: |
| 448 |
generated = self.clinic.parse(block) |
| 449 |
self.assertIn(expected_warning, stdout.getvalue()) |
| 450 |
self.assertEqual(generated, expected_generated) |
| 451 |
|
| 452 |
def test_dest_clear(self): |
| 453 |
err = "Can't clear destination 'file': it's not of type 'buffer'" |
| 454 |
block = """ |
| 455 |
/*[clinic input] |
| 456 |
destination file clear |
| 457 |
[clinic start generated code]*/ |
| 458 |
""" |
| 459 |
self.expect_failure(block, err, lineno=2) |
| 460 |
|
| 461 |
def test_directive_set_misuse(self): |
| 462 |
err = "unknown variable 'ets'" |
| 463 |
block = """ |
| 464 |
/*[clinic input] |
| 465 |
set ets tse |
| 466 |
[clinic start generated code]*/ |
| 467 |
""" |
| 468 |
self.expect_failure(block, err, lineno=2) |
| 469 |
|
| 470 |
def test_directive_set_prefix(self): |
| 471 |
block = dedent(""" |
| 472 |
/*[clinic input] |
| 473 |
set line_prefix '// ' |
| 474 |
output everything suppress |
| 475 |
output docstring_prototype buffer |
| 476 |
fn |
| 477 |
a: object |
| 478 |
/ |
| 479 |
[clinic start generated code]*/ |
| 480 |
/* We need to dump the buffer. |
| 481 |
* If not, Argument Clinic will emit a warning */ |
| 482 |
/*[clinic input] |
| 483 |
dump buffer |
| 484 |
[clinic start generated code]*/ |
| 485 |
""") |
| 486 |
generated = self.clinic.parse(block) |
| 487 |
expected_docstring_prototype = "// PyDoc_VAR(fn__doc__);" |
| 488 |
self.assertIn(expected_docstring_prototype, generated) |
| 489 |
|
| 490 |
def test_directive_set_suffix(self): |
| 491 |
block = dedent(""" |
| 492 |
/*[clinic input] |
| 493 |
set line_suffix ' // test' |
| 494 |
output everything suppress |
| 495 |
output docstring_prototype buffer |
| 496 |
fn |
| 497 |
a: object |
| 498 |
/ |
| 499 |
[clinic start generated code]*/ |
| 500 |
/* We need to dump the buffer. |
| 501 |
* If not, Argument Clinic will emit a warning */ |
| 502 |
/*[clinic input] |
| 503 |
dump buffer |
| 504 |
[clinic start generated code]*/ |
| 505 |
""") |
| 506 |
generated = self.clinic.parse(block) |
| 507 |
expected_docstring_prototype = "PyDoc_VAR(fn__doc__); // test" |
| 508 |
self.assertIn(expected_docstring_prototype, generated) |
| 509 |
|
| 510 |
def test_directive_set_prefix_and_suffix(self): |
| 511 |
block = dedent(""" |
| 512 |
/*[clinic input] |
| 513 |
set line_prefix '{block comment start} ' |
| 514 |
set line_suffix ' {block comment end}' |
| 515 |
output everything suppress |
| 516 |
output docstring_prototype buffer |
| 517 |
fn |
| 518 |
a: object |
| 519 |
/ |
| 520 |
[clinic start generated code]*/ |
| 521 |
/* We need to dump the buffer. |
| 522 |
* If not, Argument Clinic will emit a warning */ |
| 523 |
/*[clinic input] |
| 524 |
dump buffer |
| 525 |
[clinic start generated code]*/ |
| 526 |
""") |
| 527 |
generated = self.clinic.parse(block) |
| 528 |
expected_docstring_prototype = "/* PyDoc_VAR(fn__doc__); */" |
| 529 |
self.assertIn(expected_docstring_prototype, generated) |
| 530 |
|
| 531 |
def test_directive_printout(self): |
| 532 |
block = dedent(""" |
| 533 |
/*[clinic input] |
| 534 |
output everything buffer |
| 535 |
printout test |
| 536 |
[clinic start generated code]*/ |
| 537 |
""") |
| 538 |
expected = dedent(""" |
| 539 |
/*[clinic input] |
| 540 |
output everything buffer |
| 541 |
printout test |
| 542 |
[clinic start generated code]*/ |
| 543 |
test |
| 544 |
/*[clinic end generated code: output=4e1243bd22c66e76 input=898f1a32965d44ca]*/ |
| 545 |
""") |
| 546 |
generated = self.clinic.parse(block) |
| 547 |
self.assertEqual(generated, expected) |
| 548 |
|
| 549 |
def test_directive_preserve_twice(self): |
| 550 |
err = "Can't have 'preserve' twice in one block!" |
| 551 |
block = """ |
| 552 |
/*[clinic input] |
| 553 |
preserve |
| 554 |
preserve |
| 555 |
[clinic start generated code]*/ |
| 556 |
""" |
| 557 |
self.expect_failure(block, err, lineno=3) |
| 558 |
|
| 559 |
def test_directive_preserve_input(self): |
| 560 |
err = "'preserve' only works for blocks that don't produce any output!" |
| 561 |
block = """ |
| 562 |
/*[clinic input] |
| 563 |
preserve |
| 564 |
fn |
| 565 |
a: object |
| 566 |
/ |
| 567 |
[clinic start generated code]*/ |
| 568 |
""" |
| 569 |
self.expect_failure(block, err, lineno=6) |
| 570 |
|
| 571 |
def test_directive_preserve_output(self): |
| 572 |
block = dedent(""" |
| 573 |
/*[clinic input] |
| 574 |
output everything buffer |
| 575 |
preserve |
| 576 |
[clinic start generated code]*/ |
| 577 |
// Preserve this |
| 578 |
/*[clinic end generated code: output=eaa49677ae4c1f7d input=559b5db18fddae6a]*/ |
| 579 |
/*[clinic input] |
| 580 |
dump buffer |
| 581 |
[clinic start generated code]*/ |
| 582 |
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=524ce2e021e4eba6]*/ |
| 583 |
""") |
| 584 |
generated = self.clinic.parse(block) |
| 585 |
self.assertEqual(generated, block) |
| 586 |
|
| 587 |
def test_directive_output_invalid_command(self): |
| 588 |
err = dedent(""" |
| 589 |
Invalid command or destination name 'cmd'. Must be one of: |
| 590 |
- 'preset' |
| 591 |
- 'push' |
| 592 |
- 'pop' |
| 593 |
- 'print' |
| 594 |
- 'everything' |
| 595 |
- 'cpp_if' |
| 596 |
- 'docstring_prototype' |
| 597 |
- 'docstring_definition' |
| 598 |
- 'methoddef_define' |
| 599 |
- 'impl_prototype' |
| 600 |
- 'parser_prototype' |
| 601 |
- 'parser_definition' |
| 602 |
- 'cpp_endif' |
| 603 |
- 'methoddef_ifndef' |
| 604 |
- 'impl_definition' |
| 605 |
""").strip() |
| 606 |
block = """ |
| 607 |
/*[clinic input] |
| 608 |
output cmd buffer |
| 609 |
[clinic start generated code]*/ |
| 610 |
""" |
| 611 |
self.expect_failure(block, err, lineno=2) |
| 612 |
|
| 613 |
def test_validate_cloned_init(self): |
| 614 |
block = """ |
| 615 |
/*[clinic input] |
| 616 |
class C "void *" "" |
| 617 |
C.meth |
| 618 |
a: int |
| 619 |
[clinic start generated code]*/ |
| 620 |
/*[clinic input] |
| 621 |
@classmethod |
| 622 |
C.__init__ = C.meth |
| 623 |
[clinic start generated code]*/ |
| 624 |
""" |
| 625 |
err = "'__init__' must be a normal method; got 'FunctionKind.CLASS_METHOD'!" |
| 626 |
self.expect_failure(block, err, lineno=8) |
| 627 |
|
| 628 |
def test_validate_cloned_new(self): |
| 629 |
block = """ |
| 630 |
/*[clinic input] |
| 631 |
class C "void *" "" |
| 632 |
C.meth |
| 633 |
a: int |
| 634 |
[clinic start generated code]*/ |
| 635 |
/*[clinic input] |
| 636 |
C.__new__ = C.meth |
| 637 |
[clinic start generated code]*/ |
| 638 |
""" |
| 639 |
err = "'__new__' must be a class method" |
| 640 |
self.expect_failure(block, err, lineno=7) |
| 641 |
|
| 642 |
def test_no_c_basename_cloned(self): |
| 643 |
block = """ |
| 644 |
/*[clinic input] |
| 645 |
foo2 |
| 646 |
[clinic start generated code]*/ |
| 647 |
/*[clinic input] |
| 648 |
foo as = foo2 |
| 649 |
[clinic start generated code]*/ |
| 650 |
""" |
| 651 |
err = "No C basename provided after 'as' keyword" |
| 652 |
self.expect_failure(block, err, lineno=5) |
| 653 |
|
| 654 |
def test_cloned_with_custom_c_basename(self): |
| 655 |
raw = dedent(""" |
| 656 |
/*[clinic input] |
| 657 |
# Make sure we don't create spurious clinic/ directories. |
| 658 |
output everything suppress |
| 659 |
foo2 |
| 660 |
[clinic start generated code]*/ |
| 661 |
|
| 662 |
/*[clinic input] |
| 663 |
foo as foo1 = foo2 |
| 664 |
[clinic start generated code]*/ |
| 665 |
""") |
| 666 |
self.clinic.parse(raw) |
| 667 |
funcs = self.clinic.functions |
| 668 |
self.assertEqual(len(funcs), 2) |
| 669 |
self.assertEqual(funcs[1].name, "foo") |
| 670 |
self.assertEqual(funcs[1].c_basename, "foo1") |
| 671 |
|
| 672 |
def test_cloned_with_illegal_c_basename(self): |
| 673 |
block = """ |
| 674 |
/*[clinic input] |
| 675 |
class C "void *" "" |
| 676 |
foo1 |
| 677 |
[clinic start generated code]*/ |
| 678 |
|
| 679 |
/*[clinic input] |
| 680 |
foo2 as .illegal. = foo1 |
| 681 |
[clinic start generated code]*/ |
| 682 |
""" |
| 683 |
err = "Illegal C basename: '.illegal.'" |
| 684 |
self.expect_failure(block, err, lineno=7) |
| 685 |
|
| 686 |
def test_cloned_forced_text_signature(self): |
| 687 |
block = dedent(""" |
| 688 |
/*[clinic input] |
| 689 |
@text_signature "($module, a[, b])" |
| 690 |
src |
| 691 |
a: object |
| 692 |
param a |
| 693 |
b: object = NULL |
| 694 |
/ |
| 695 |
|
| 696 |
docstring |
| 697 |
[clinic start generated code]*/ |
| 698 |
|
| 699 |
/*[clinic input] |
| 700 |
dst = src |
| 701 |
[clinic start generated code]*/ |
| 702 |
""") |
| 703 |
self.clinic.parse(block) |
| 704 |
self.addCleanup(rmtree, "clinic") |
| 705 |
funcs = self.clinic.functions |
| 706 |
self.assertEqual(len(funcs), 2) |
| 707 |
|
| 708 |
src_docstring_lines = funcs[0].docstring.split("\n") |
| 709 |
dst_docstring_lines = funcs[1].docstring.split("\n") |
| 710 |
|
| 711 |
# Signatures are copied. |
| 712 |
self.assertEqual(src_docstring_lines[0], "src($module, a[, b])") |
| 713 |
self.assertEqual(dst_docstring_lines[0], "dst($module, a[, b])") |
| 714 |
|
| 715 |
# Param docstrings are copied. |
| 716 |
self.assertIn(" param a", src_docstring_lines) |
| 717 |
self.assertIn(" param a", dst_docstring_lines) |
| 718 |
|
| 719 |
# Docstrings are not copied. |
| 720 |
self.assertIn("docstring", src_docstring_lines) |
| 721 |
self.assertNotIn("docstring", dst_docstring_lines) |
| 722 |
|
| 723 |
def test_cloned_forced_text_signature_illegal(self): |
| 724 |
block = """ |
| 725 |
/*[clinic input] |
| 726 |
@text_signature "($module, a[, b])" |
| 727 |
src |
| 728 |
a: object |
| 729 |
b: object = NULL |
| 730 |
/ |
| 731 |
[clinic start generated code]*/ |
| 732 |
|
| 733 |
/*[clinic input] |
| 734 |
@text_signature "($module, a_override[, b])" |
| 735 |
dst = src |
| 736 |
[clinic start generated code]*/ |
| 737 |
""" |
| 738 |
err = "Cannot use @text_signature when cloning a function" |
| 739 |
self.expect_failure(block, err, lineno=11) |
| 740 |
|
| 741 |
def test_ignore_preprocessor_in_comments(self): |
| 742 |
for dsl in "clinic", "python": |
| 743 |
raw = dedent(f"""\ |
| 744 |
/*[{dsl} input] |
| 745 |
# CPP directives, valid or not, should be ignored in C comments. |
| 746 |
# |
| 747 |
[{dsl} start generated code]*/ |
| 748 |
""") |
| 749 |
self.clinic.parse(raw) |
| 750 |
|
| 751 |
|
| 752 |
class ParseFileUnitTest(TestCase): |
| 753 |
def expect_parsing_failure( |
| 754 |
self, *, filename, expected_error, verify=True, output=None |
| 755 |
): |
| 756 |
errmsg = re.escape(dedent(expected_error).strip()) |
| 757 |
with self.assertRaisesRegex(ClinicError, errmsg): |
| 758 |
parse_file(filename, limited_capi=False) |
| 759 |
|
| 760 |
def test_parse_file_no_extension(self) -> None: |
| 761 |
self.expect_parsing_failure( |
| 762 |
filename="foo", |
| 763 |
expected_error="Can't extract file type for file 'foo'" |
| 764 |
) |
| 765 |
|
| 766 |
def test_parse_file_strange_extension(self) -> None: |
| 767 |
filenames_to_errors = { |
| 768 |
"foo.rs": "Can't identify file type for file 'foo.rs'", |
| 769 |
"foo.hs": "Can't identify file type for file 'foo.hs'", |
| 770 |
"foo.js": "Can't identify file type for file 'foo.js'", |
| 771 |
} |
| 772 |
for filename, errmsg in filenames_to_errors.items(): |
| 773 |
with self.subTest(filename=filename): |
| 774 |
self.expect_parsing_failure(filename=filename, expected_error=errmsg) |
| 775 |
|
| 776 |
|
| 777 |
class ClinicGroupPermuterTest(TestCase): |
| 778 |
def _test(self, l, m, r, output): |
| 779 |
computed = permute_optional_groups(l, m, r) |
| 780 |
self.assertEqual(output, computed) |
| 781 |
|
| 782 |
def test_range(self): |
| 783 |
self._test([['start']], ['stop'], [['step']], |
| 784 |
( |
| 785 |
('stop',), |
| 786 |
('start', 'stop',), |
| 787 |
('start', 'stop', 'step',), |
| 788 |
)) |
| 789 |
|
| 790 |
def test_add_window(self): |
| 791 |
self._test([['x', 'y']], ['ch'], [['attr']], |
| 792 |
( |
| 793 |
('ch',), |
| 794 |
('ch', 'attr'), |
| 795 |
('x', 'y', 'ch',), |
| 796 |
('x', 'y', 'ch', 'attr'), |
| 797 |
)) |
| 798 |
|
| 799 |
def test_ludicrous(self): |
| 800 |
self._test([['a1', 'a2', 'a3'], ['b1', 'b2']], ['c1'], [['d1', 'd2'], ['e1', 'e2', 'e3']], |
| 801 |
( |
| 802 |
('c1',), |
| 803 |
('b1', 'b2', 'c1'), |
| 804 |
('b1', 'b2', 'c1', 'd1', 'd2'), |
| 805 |
('a1', 'a2', 'a3', 'b1', 'b2', 'c1'), |
| 806 |
('a1', 'a2', 'a3', 'b1', 'b2', 'c1', 'd1', 'd2'), |
| 807 |
('a1', 'a2', 'a3', 'b1', 'b2', 'c1', 'd1', 'd2', 'e1', 'e2', 'e3'), |
| 808 |
)) |
| 809 |
|
| 810 |
def test_right_only(self): |
| 811 |
self._test([], [], [['a'],['b'],['c']], |
| 812 |
( |
| 813 |
(), |
| 814 |
('a',), |
| 815 |
('a', 'b'), |
| 816 |
('a', 'b', 'c') |
| 817 |
)) |
| 818 |
|
| 819 |
def test_have_left_options_but_required_is_empty(self): |
| 820 |
def fn(): |
| 821 |
permute_optional_groups(['a'], [], []) |
| 822 |
self.assertRaises(ValueError, fn) |
| 823 |
|
| 824 |
|
| 825 |
class ClinicLinearFormatTest(TestCase): |
| 826 |
def _test(self, input, output, **kwargs): |
| 827 |
computed = libclinic.linear_format(input, **kwargs) |
| 828 |
self.assertEqual(output, computed) |
| 829 |
|
| 830 |
def test_empty_strings(self): |
| 831 |
self._test('', '') |
| 832 |
|
| 833 |
def test_solo_newline(self): |
| 834 |
self._test('\n', '\n') |
| 835 |
|
| 836 |
def test_no_substitution(self): |
| 837 |
self._test(""" |
| 838 |
abc |
| 839 |
""", """ |
| 840 |
abc |
| 841 |
""") |
| 842 |
|
| 843 |
def test_empty_substitution(self): |
| 844 |
self._test(""" |
| 845 |
abc |
| 846 |
{name} |
| 847 |
def |
| 848 |
""", """ |
| 849 |
abc |
| 850 |
def |
| 851 |
""", name='') |
| 852 |
|
| 853 |
def test_single_line_substitution(self): |
| 854 |
self._test(""" |
| 855 |
abc |
| 856 |
{name} |
| 857 |
def |
| 858 |
""", """ |
| 859 |
abc |
| 860 |
GARGLE |
| 861 |
def |
| 862 |
""", name='GARGLE') |
| 863 |
|
| 864 |
def test_multiline_substitution(self): |
| 865 |
self._test(""" |
| 866 |
abc |
| 867 |
{name} |
| 868 |
def |
| 869 |
""", """ |
| 870 |
abc |
| 871 |
bingle |
| 872 |
bungle |
| 873 |
|
| 874 |
def |
| 875 |
""", name='bingle\nbungle\n') |
| 876 |
|
| 877 |
def test_text_before_block_marker(self): |
| 878 |
regex = re.escape("found before '{marker}'") |
| 879 |
with self.assertRaisesRegex(ClinicError, regex): |
| 880 |
libclinic.linear_format("no text before marker for you! {marker}", |
| 881 |
marker="not allowed!") |
| 882 |
|
| 883 |
def test_text_after_block_marker(self): |
| 884 |
regex = re.escape("found after '{marker}'") |
| 885 |
with self.assertRaisesRegex(ClinicError, regex): |
| 886 |
libclinic.linear_format("{marker} no text after marker for you!", |
| 887 |
marker="not allowed!") |
| 888 |
|
| 889 |
|
| 890 |
class InertParser: |
| 891 |
def __init__(self, clinic): |
| 892 |
pass |
| 893 |
|
| 894 |
def parse(self, block): |
| 895 |
pass |
| 896 |
|
| 897 |
class CopyParser: |
| 898 |
def __init__(self, clinic): |
| 899 |
pass |
| 900 |
|
| 901 |
def parse(self, block): |
| 902 |
block.output = block.input |
| 903 |
|
| 904 |
|
| 905 |
class ClinicBlockParserTest(TestCase): |
| 906 |
def _test(self, input, output): |
| 907 |
language = CLanguage(None) |
| 908 |
|
| 909 |
blocks = list(BlockParser(input, language)) |
| 910 |
writer = BlockPrinter(language) |
| 911 |
for block in blocks: |
| 912 |
writer.print_block(block) |
| 913 |
output = writer.f.getvalue() |
| 914 |
assert output == input, "output != input!\n\noutput " + repr(output) + "\n\n input " + repr(input) |
| 915 |
|
| 916 |
def round_trip(self, input): |
| 917 |
return self._test(input, input) |
| 918 |
|
| 919 |
def test_round_trip_1(self): |
| 920 |
self.round_trip(""" |
| 921 |
verbatim text here |
| 922 |
lah dee dah |
| 923 |
""") |
| 924 |
def test_round_trip_2(self): |
| 925 |
self.round_trip(""" |
| 926 |
verbatim text here |
| 927 |
lah dee dah |
| 928 |
/*[inert] |
| 929 |
abc |
| 930 |
[inert]*/ |
| 931 |
def |
| 932 |
/*[inert checksum: 7b18d017f89f61cf17d47f92749ea6930a3f1deb]*/ |
| 933 |
xyz |
| 934 |
""") |
| 935 |
|
| 936 |
def _test_clinic(self, input, output): |
| 937 |
language = CLanguage(None) |
| 938 |
c = Clinic(language, filename="file", limited_capi=False) |
| 939 |
c.parsers['inert'] = InertParser(c) |
| 940 |
c.parsers['copy'] = CopyParser(c) |
| 941 |
computed = c.parse(input) |
| 942 |
self.assertEqual(output, computed) |
| 943 |
|
| 944 |
def test_clinic_1(self): |
| 945 |
self._test_clinic(""" |
| 946 |
verbatim text here |
| 947 |
lah dee dah |
| 948 |
/*[copy input] |
| 949 |
def |
| 950 |
[copy start generated code]*/ |
| 951 |
abc |
| 952 |
/*[copy end generated code: output=03cfd743661f0797 input=7b18d017f89f61cf]*/ |
| 953 |
xyz |
| 954 |
""", """ |
| 955 |
verbatim text here |
| 956 |
lah dee dah |
| 957 |
/*[copy input] |
| 958 |
def |
| 959 |
[copy start generated code]*/ |
| 960 |
def |
| 961 |
/*[copy end generated code: output=7b18d017f89f61cf input=7b18d017f89f61cf]*/ |
| 962 |
xyz |
| 963 |
""") |
| 964 |
|
| 965 |
|
| 966 |
class ClinicParserTest(TestCase): |
| 967 |
|
| 968 |
def parse(self, text): |
| 969 |
c = _make_clinic() |
| 970 |
parser = DSLParser(c) |
| 971 |
block = Block(text) |
| 972 |
parser.parse(block) |
| 973 |
return block |
| 974 |
|
| 975 |
def parse_function(self, text, signatures_in_block=2, function_index=1): |
| 976 |
block = self.parse(text) |
| 977 |
s = block.signatures |
| 978 |
self.assertEqual(len(s), signatures_in_block) |
| 979 |
assert isinstance(s[0], Module) |
| 980 |
assert isinstance(s[function_index], Function) |
| 981 |
return s[function_index] |
| 982 |
|
| 983 |
def expect_failure(self, block, err, *, |
| 984 |
filename=None, lineno=None, strip=True): |
| 985 |
return _expect_failure(self, self.parse_function, block, err, |
| 986 |
filename=filename, lineno=lineno, strip=strip) |
| 987 |
|
| 988 |
def checkDocstring(self, fn, expected): |
| 989 |
self.assertTrue(hasattr(fn, "docstring")) |
| 990 |
self.assertEqual(dedent(expected).strip(), |
| 991 |
fn.docstring.strip()) |
| 992 |
|
| 993 |
def test_trivial(self): |
| 994 |
parser = DSLParser(_make_clinic()) |
| 995 |
block = Block(""" |
| 996 |
module os |
| 997 |
os.access |
| 998 |
""") |
| 999 |
parser.parse(block) |
| 1000 |
module, function = block.signatures |
| 1001 |
self.assertEqual("access", function.name) |
| 1002 |
self.assertEqual("os", module.name) |
| 1003 |
|
| 1004 |
def test_ignore_line(self): |
| 1005 |
block = self.parse(dedent(""" |
| 1006 |
# |
| 1007 |
module os |
| 1008 |
os.access |
| 1009 |
""")) |
| 1010 |
module, function = block.signatures |
| 1011 |
self.assertEqual("access", function.name) |
| 1012 |
self.assertEqual("os", module.name) |
| 1013 |
|
| 1014 |
def test_param(self): |
| 1015 |
function = self.parse_function(""" |
| 1016 |
module os |
| 1017 |
os.access |
| 1018 |
path: int |
| 1019 |
""") |
| 1020 |
self.assertEqual("access", function.name) |
| 1021 |
self.assertEqual(2, len(function.parameters)) |
| 1022 |
p = function.parameters['path'] |
| 1023 |
self.assertEqual('path', p.name) |
| 1024 |
self.assertIsInstance(p.converter, int_converter) |
| 1025 |
|
| 1026 |
def test_param_default(self): |
| 1027 |
function = self.parse_function(""" |
| 1028 |
module os |
| 1029 |
os.access |
| 1030 |
follow_symlinks: bool = True |
| 1031 |
""") |
| 1032 |
p = function.parameters['follow_symlinks'] |
| 1033 |
self.assertEqual(True, p.default) |
| 1034 |
|
| 1035 |
def test_param_with_continuations(self): |
| 1036 |
function = self.parse_function(r""" |
| 1037 |
module os |
| 1038 |
os.access |
| 1039 |
follow_symlinks: \ |
| 1040 |
bool \ |
| 1041 |
= \ |
| 1042 |
True |
| 1043 |
""") |
| 1044 |
p = function.parameters['follow_symlinks'] |
| 1045 |
self.assertEqual(True, p.default) |
| 1046 |
|
| 1047 |
def test_param_default_none(self): |
| 1048 |
function = self.parse_function(r""" |
| 1049 |
module test |
| 1050 |
test.func |
| 1051 |
obj: object = None |
| 1052 |
str: str(accept={str, NoneType}) = None |
| 1053 |
buf: Py_buffer(accept={str, buffer, NoneType}) = None |
| 1054 |
""") |
| 1055 |
p = function.parameters['obj'] |
| 1056 |
self.assertIs(p.default, None) |
| 1057 |
self.assertEqual(p.converter.py_default, 'None') |
| 1058 |
self.assertEqual(p.converter.c_default, 'Py_None') |
| 1059 |
|
| 1060 |
p = function.parameters['str'] |
| 1061 |
self.assertIs(p.default, None) |
| 1062 |
self.assertEqual(p.converter.py_default, 'None') |
| 1063 |
self.assertEqual(p.converter.c_default, 'NULL') |
| 1064 |
|
| 1065 |
p = function.parameters['buf'] |
| 1066 |
self.assertIs(p.default, None) |
| 1067 |
self.assertEqual(p.converter.py_default, 'None') |
| 1068 |
self.assertEqual(p.converter.c_default, '{NULL, NULL}') |
| 1069 |
|
| 1070 |
def test_param_default_null(self): |
| 1071 |
function = self.parse_function(r""" |
| 1072 |
module test |
| 1073 |
test.func |
| 1074 |
obj: object = NULL |
| 1075 |
str: str = NULL |
| 1076 |
buf: Py_buffer = NULL |
| 1077 |
fsencoded: unicode_fs_encoded = NULL |
| 1078 |
fsdecoded: unicode_fs_decoded = NULL |
| 1079 |
""") |
| 1080 |
p = function.parameters['obj'] |
| 1081 |
self.assertIs(p.default, NULL) |
| 1082 |
self.assertEqual(p.converter.py_default, '<unrepresentable>') |
| 1083 |
self.assertEqual(p.converter.c_default, 'NULL') |
| 1084 |
|
| 1085 |
p = function.parameters['str'] |
| 1086 |
self.assertIs(p.default, NULL) |
| 1087 |
self.assertEqual(p.converter.py_default, '<unrepresentable>') |
| 1088 |
self.assertEqual(p.converter.c_default, 'NULL') |
| 1089 |
|
| 1090 |
p = function.parameters['buf'] |
| 1091 |
self.assertIs(p.default, NULL) |
| 1092 |
self.assertEqual(p.converter.py_default, '<unrepresentable>') |
| 1093 |
self.assertEqual(p.converter.c_default, '{NULL, NULL}') |
| 1094 |
|
| 1095 |
p = function.parameters['fsencoded'] |
| 1096 |
self.assertIs(p.default, NULL) |
| 1097 |
self.assertEqual(p.converter.py_default, '<unrepresentable>') |
| 1098 |
self.assertEqual(p.converter.c_default, 'NULL') |
| 1099 |
|
| 1100 |
p = function.parameters['fsdecoded'] |
| 1101 |
self.assertIs(p.default, NULL) |
| 1102 |
self.assertEqual(p.converter.py_default, '<unrepresentable>') |
| 1103 |
self.assertEqual(p.converter.c_default, 'NULL') |
| 1104 |
|
| 1105 |
def test_param_default_str_literal(self): |
| 1106 |
function = self.parse_function(r""" |
| 1107 |
module test |
| 1108 |
test.func |
| 1109 |
str: str = ' \t\n\r\v\f\xa0' |
| 1110 |
buf: Py_buffer(accept={str, buffer}) = ' \t\n\r\v\f\xa0' |
| 1111 |
""") |
| 1112 |
p = function.parameters['str'] |
| 1113 |
self.assertEqual(p.default, ' \t\n\r\v\f\xa0') |
| 1114 |
self.assertEqual(p.converter.py_default, r"' \t\n\r\x0b\x0c\xa0'") |
| 1115 |
self.assertEqual(p.converter.c_default, r'" \t\n\r\v\f\u00a0"') |
| 1116 |
|
| 1117 |
p = function.parameters['buf'] |
| 1118 |
self.assertEqual(p.default, ' \t\n\r\v\f\xa0') |
| 1119 |
self.assertEqual(p.converter.py_default, r"' \t\n\r\x0b\x0c\xa0'") |
| 1120 |
self.assertEqual(p.converter.c_default, |
| 1121 |
r'{.buf = " \t\n\r\v\f\302\240", .obj = NULL, .len = 8}') |
| 1122 |
|
| 1123 |
def test_param_default_bytes_literal(self): |
| 1124 |
function = self.parse_function(r""" |
| 1125 |
module test |
| 1126 |
test.func |
| 1127 |
str: str(accept={robuffer}) = b' \t\n\r\v\f\xa0' |
| 1128 |
buf: Py_buffer = b' \t\n\r\v\f\xa0' |
| 1129 |
""") |
| 1130 |
p = function.parameters['str'] |
| 1131 |
self.assertEqual(p.default, b' \t\n\r\v\f\xa0') |
| 1132 |
self.assertEqual(p.converter.py_default, r"b' \t\n\r\x0b\x0c\xa0'") |
| 1133 |
self.assertEqual(p.converter.c_default, r'" \t\n\r\v\f\240"') |
| 1134 |
|
| 1135 |
p = function.parameters['buf'] |
| 1136 |
self.assertEqual(p.default, b' \t\n\r\v\f\xa0') |
| 1137 |
self.assertEqual(p.converter.py_default, r"b' \t\n\r\x0b\x0c\xa0'") |
| 1138 |
self.assertEqual(p.converter.c_default, |
| 1139 |
r'{.buf = " \t\n\r\v\f\240", .obj = NULL, .len = 7}') |
| 1140 |
|
| 1141 |
def test_param_default_byte_literal(self): |
| 1142 |
function = self.parse_function(r""" |
| 1143 |
module test |
| 1144 |
test.func |
| 1145 |
zero: char = b'\0' |
| 1146 |
one: char = b'\1' |
| 1147 |
lf: char = b'\n' |
| 1148 |
nbsp: char = b'\xa0' |
| 1149 |
""") |
| 1150 |
p = function.parameters['zero'] |
| 1151 |
self.assertEqual(p.default, b'\0') |
| 1152 |
self.assertEqual(p.converter.py_default, r"b'\x00'") |
| 1153 |
self.assertEqual(p.converter.c_default, r"'\0'") |
| 1154 |
|
| 1155 |
p = function.parameters['one'] |
| 1156 |
self.assertEqual(p.default, b'\1') |
| 1157 |
self.assertEqual(p.converter.py_default, r"b'\x01'") |
| 1158 |
self.assertEqual(p.converter.c_default, r"'\001'") |
| 1159 |
|
| 1160 |
p = function.parameters['lf'] |
| 1161 |
self.assertEqual(p.default, b'\n') |
| 1162 |
self.assertEqual(p.converter.py_default, r"b'\n'") |
| 1163 |
self.assertEqual(p.converter.c_default, r"'\n'") |
| 1164 |
|
| 1165 |
p = function.parameters['nbsp'] |
| 1166 |
self.assertEqual(p.default, b'\xa0') |
| 1167 |
self.assertEqual(p.converter.py_default, r"b'\xa0'") |
| 1168 |
self.assertEqual(p.converter.c_default, r"'\240'") |
| 1169 |
|
| 1170 |
def test_param_default_unicode_char(self): |
| 1171 |
function = self.parse_function(r""" |
| 1172 |
module test |
| 1173 |
test.func |
| 1174 |
zero: int(accept={str}) = '\0' |
| 1175 |
one: int(accept={str}) = '\1' |
| 1176 |
lf: int(accept={str}) = '\n' |
| 1177 |
nbsp: int(accept={str}) = '\xa0' |
| 1178 |
snake: int(accept={str}) = '\U0001f40d' |
| 1179 |
""") |
| 1180 |
p = function.parameters['zero'] |
| 1181 |
self.assertEqual(p.default, '\0') |
| 1182 |
self.assertEqual(p.converter.py_default, r"'\x00'") |
| 1183 |
self.assertEqual(p.converter.c_default, '0') |
| 1184 |
|
| 1185 |
p = function.parameters['one'] |
| 1186 |
self.assertEqual(p.default, '\1') |
| 1187 |
self.assertEqual(p.converter.py_default, r"'\x01'") |
| 1188 |
self.assertEqual(p.converter.c_default, '0x01') |
| 1189 |
|
| 1190 |
p = function.parameters['lf'] |
| 1191 |
self.assertEqual(p.default, '\n') |
| 1192 |
self.assertEqual(p.converter.py_default, r"'\n'") |
| 1193 |
self.assertEqual(p.converter.c_default, r"'\n'") |
| 1194 |
|
| 1195 |
p = function.parameters['nbsp'] |
| 1196 |
self.assertEqual(p.default, '\xa0') |
| 1197 |
self.assertEqual(p.converter.py_default, r"'\xa0'") |
| 1198 |
self.assertEqual(p.converter.c_default, '0xa0') |
| 1199 |
|
| 1200 |
p = function.parameters['snake'] |
| 1201 |
self.assertEqual(p.default, '\U0001f40d') |
| 1202 |
self.assertEqual(p.converter.py_default, "'\U0001f40d'") |
| 1203 |
self.assertEqual(p.converter.c_default, '0x1f40d') |
| 1204 |
|
| 1205 |
def test_param_default_bool(self): |
| 1206 |
function = self.parse_function(r""" |
| 1207 |
module test |
| 1208 |
test.func |
| 1209 |
bool: bool = True |
| 1210 |
intbool: bool(accept={int}) = True |
| 1211 |
intbool2: bool(accept={int}) = 2 |
| 1212 |
""") |
| 1213 |
p = function.parameters['bool'] |
| 1214 |
self.assertIs(p.default, True) |
| 1215 |
self.assertEqual(p.converter.py_default, 'True') |
| 1216 |
self.assertEqual(p.converter.c_default, '1') |
| 1217 |
|
| 1218 |
p = function.parameters['intbool'] |
| 1219 |
self.assertIs(p.default, True) |
| 1220 |
self.assertEqual(p.converter.py_default, 'True') |
| 1221 |
self.assertEqual(p.converter.c_default, '1') |
| 1222 |
|
| 1223 |
p = function.parameters['intbool2'] |
| 1224 |
self.assertEqual(p.default, 2) |
| 1225 |
self.assertEqual(p.converter.py_default, '2') |
| 1226 |
self.assertEqual(p.converter.c_default, '2') |
| 1227 |
|
| 1228 |
def test_param_default_expr_named_constant(self): |
| 1229 |
function = self.parse_function(""" |
| 1230 |
module os |
| 1231 |
os.access |
| 1232 |
follow_symlinks: int(c_default='MAXSIZE') = sys.maxsize |
| 1233 |
""") |
| 1234 |
p = function.parameters['follow_symlinks'] |
| 1235 |
self.assertEqual(sys.maxsize, p.default) |
| 1236 |
self.assertEqual("MAXSIZE", p.converter.c_default) |
| 1237 |
|
| 1238 |
err = ( |
| 1239 |
"When you specify a named constant ('sys.maxsize') as your default value, " |
| 1240 |
"you MUST specify a valid c_default." |
| 1241 |
) |
| 1242 |
block = """ |
| 1243 |
module os |
| 1244 |
os.access |
| 1245 |
follow_symlinks: int = sys.maxsize |
| 1246 |
""" |
| 1247 |
self.expect_failure(block, err, lineno=2) |
| 1248 |
|
| 1249 |
def test_param_with_bizarre_default_fails_correctly(self): |
| 1250 |
template = """ |
| 1251 |
module os |
| 1252 |
os.access |
| 1253 |
follow_symlinks: int = {default} |
| 1254 |
""" |
| 1255 |
err = "Unsupported expression as default value" |
| 1256 |
for bad_default_value in ( |
| 1257 |
"{1, 2, 3}", |
| 1258 |
"3 if bool() else 4", |
| 1259 |
"[x for x in range(42)]" |
| 1260 |
): |
| 1261 |
with self.subTest(bad_default=bad_default_value): |
| 1262 |
block = template.format(default=bad_default_value) |
| 1263 |
self.expect_failure(block, err, lineno=2) |
| 1264 |
|
| 1265 |
def test_unspecified_not_allowed_as_default_value(self): |
| 1266 |
block = """ |
| 1267 |
module os |
| 1268 |
os.access |
| 1269 |
follow_symlinks: int(c_default='MAXSIZE') = unspecified |
| 1270 |
""" |
| 1271 |
err = "'unspecified' is not a legal default value!" |
| 1272 |
exc = self.expect_failure(block, err, lineno=2) |
| 1273 |
self.assertNotIn('Malformed expression given as default value', str(exc)) |
| 1274 |
|
| 1275 |
def test_malformed_expression_as_default_value(self): |
| 1276 |
block = """ |
| 1277 |
module os |
| 1278 |
os.access |
| 1279 |
follow_symlinks: int(c_default='MAXSIZE') = 1/0 |
| 1280 |
""" |
| 1281 |
err = "Malformed expression given as default value" |
| 1282 |
self.expect_failure(block, err, lineno=2) |
| 1283 |
|
| 1284 |
def test_param_default_expr_binop(self): |
| 1285 |
err = ( |
| 1286 |
"When you specify an expression ('a + b') as your default value, " |
| 1287 |
"you MUST specify a valid c_default." |
| 1288 |
) |
| 1289 |
block = """ |
| 1290 |
fn |
| 1291 |
follow_symlinks: int = a + b |
| 1292 |
""" |
| 1293 |
self.expect_failure(block, err, lineno=1) |
| 1294 |
|
| 1295 |
def test_param_no_docstring(self): |
| 1296 |
function = self.parse_function(""" |
| 1297 |
module os |
| 1298 |
os.access |
| 1299 |
follow_symlinks: bool = True |
| 1300 |
something_else: str = '' |
| 1301 |
""") |
| 1302 |
self.assertEqual(3, len(function.parameters)) |
| 1303 |
conv = function.parameters['something_else'].converter |
| 1304 |
self.assertIsInstance(conv, str_converter) |
| 1305 |
|
| 1306 |
def test_param_default_parameters_out_of_order(self): |
| 1307 |
err = ( |
| 1308 |
"Can't have a parameter without a default ('something_else') " |
| 1309 |
"after a parameter with a default!" |
| 1310 |
) |
| 1311 |
block = """ |
| 1312 |
module os |
| 1313 |
os.access |
| 1314 |
follow_symlinks: bool = True |
| 1315 |
something_else: str |
| 1316 |
""" |
| 1317 |
self.expect_failure(block, err, lineno=3) |
| 1318 |
|
| 1319 |
def disabled_test_converter_arguments(self): |
| 1320 |
function = self.parse_function(""" |
| 1321 |
module os |
| 1322 |
os.access |
| 1323 |
path: path_t(allow_fd=1) |
| 1324 |
""") |
| 1325 |
p = function.parameters['path'] |
| 1326 |
self.assertEqual(1, p.converter.args['allow_fd']) |
| 1327 |
|
| 1328 |
def test_function_docstring(self): |
| 1329 |
function = self.parse_function(""" |
| 1330 |
module os |
| 1331 |
os.stat as os_stat_fn |
| 1332 |
|
| 1333 |
path: str |
| 1334 |
Path to be examined |
| 1335 |
Ensure that multiple lines are indented correctly. |
| 1336 |
|
| 1337 |
Perform a stat system call on the given path. |
| 1338 |
|
| 1339 |
Ensure that multiple lines are indented correctly. |
| 1340 |
Ensure that multiple lines are indented correctly. |
| 1341 |
""") |
| 1342 |
self.checkDocstring(function, """ |
| 1343 |
stat($module, /, path) |
| 1344 |
-- |
| 1345 |
|
| 1346 |
Perform a stat system call on the given path. |
| 1347 |
|
| 1348 |
path |
| 1349 |
Path to be examined |
| 1350 |
Ensure that multiple lines are indented correctly. |
| 1351 |
|
| 1352 |
Ensure that multiple lines are indented correctly. |
| 1353 |
Ensure that multiple lines are indented correctly. |
| 1354 |
""") |
| 1355 |
|
| 1356 |
def test_docstring_trailing_whitespace(self): |
| 1357 |
function = self.parse_function( |
| 1358 |
"module t\n" |
| 1359 |
"t.s\n" |
| 1360 |
" a: object\n" |
| 1361 |
" Param docstring with trailing whitespace \n" |
| 1362 |
"Func docstring summary with trailing whitespace \n" |
| 1363 |
" \n" |
| 1364 |
"Func docstring body with trailing whitespace \n" |
| 1365 |
) |
| 1366 |
self.checkDocstring(function, """ |
| 1367 |
s($module, /, a) |
| 1368 |
-- |
| 1369 |
|
| 1370 |
Func docstring summary with trailing whitespace |
| 1371 |
|
| 1372 |
a |
| 1373 |
Param docstring with trailing whitespace |
| 1374 |
|
| 1375 |
Func docstring body with trailing whitespace |
| 1376 |
""") |
| 1377 |
|
| 1378 |
def test_explicit_parameters_in_docstring(self): |
| 1379 |
function = self.parse_function(dedent(""" |
| 1380 |
module foo |
| 1381 |
foo.bar |
| 1382 |
x: int |
| 1383 |
Documentation for x. |
| 1384 |
y: int |
| 1385 |
|
| 1386 |
This is the documentation for foo. |
| 1387 |
|
| 1388 |
Okay, we're done here. |
| 1389 |
""")) |
| 1390 |
self.checkDocstring(function, """ |
| 1391 |
bar($module, /, x, y) |
| 1392 |
-- |
| 1393 |
|
| 1394 |
This is the documentation for foo. |
| 1395 |
|
| 1396 |
x |
| 1397 |
Documentation for x. |
| 1398 |
|
| 1399 |
Okay, we're done here. |
| 1400 |
""") |
| 1401 |
|
| 1402 |
def test_docstring_with_comments(self): |
| 1403 |
function = self.parse_function(dedent(""" |
| 1404 |
module foo |
| 1405 |
foo.bar |
| 1406 |
x: int |
| 1407 |
# We're about to have |
| 1408 |
# the documentation for x. |
| 1409 |
Documentation for x. |
| 1410 |
# We've just had |
| 1411 |
# the documentation for x. |
| 1412 |
y: int |
| 1413 |
|
| 1414 |
# We're about to have |
| 1415 |
# the documentation for foo. |
| 1416 |
This is the documentation for foo. |
| 1417 |
# We've just had |
| 1418 |
# the documentation for foo. |
| 1419 |
|
| 1420 |
Okay, we're done here. |
| 1421 |
""")) |
| 1422 |
self.checkDocstring(function, """ |
| 1423 |
bar($module, /, x, y) |
| 1424 |
-- |
| 1425 |
|
| 1426 |
This is the documentation for foo. |
| 1427 |
|
| 1428 |
x |
| 1429 |
Documentation for x. |
| 1430 |
|
| 1431 |
Okay, we're done here. |
| 1432 |
""") |
| 1433 |
|
| 1434 |
def test_parser_regression_special_character_in_parameter_column_of_docstring_first_line(self): |
| 1435 |
function = self.parse_function(dedent(""" |
| 1436 |
module os |
| 1437 |
os.stat |
| 1438 |
path: str |
| 1439 |
This/used to break Clinic! |
| 1440 |
""")) |
| 1441 |
self.checkDocstring(function, """ |
| 1442 |
stat($module, /, path) |
| 1443 |
-- |
| 1444 |
|
| 1445 |
This/used to break Clinic! |
| 1446 |
""") |
| 1447 |
|
| 1448 |
def test_c_name(self): |
| 1449 |
function = self.parse_function(""" |
| 1450 |
module os |
| 1451 |
os.stat as os_stat_fn |
| 1452 |
""") |
| 1453 |
self.assertEqual("os_stat_fn", function.c_basename) |
| 1454 |
|
| 1455 |
def test_base_invalid_syntax(self): |
| 1456 |
block = """ |
| 1457 |
module os |
| 1458 |
os.stat |
| 1459 |
invalid syntax: int = 42 |
| 1460 |
""" |
| 1461 |
err = "Function 'stat' has an invalid parameter declaration: 'invalid syntax: int = 42'" |
| 1462 |
self.expect_failure(block, err, lineno=2) |
| 1463 |
|
| 1464 |
def test_param_default_invalid_syntax(self): |
| 1465 |
block = """ |
| 1466 |
module os |
| 1467 |
os.stat |
| 1468 |
x: int = invalid syntax |
| 1469 |
""" |
| 1470 |
err = "Function 'stat' has an invalid parameter declaration:" |
| 1471 |
self.expect_failure(block, err, lineno=2) |
| 1472 |
|
| 1473 |
def test_cloning_nonexistent_function_correctly_fails(self): |
| 1474 |
block = """ |
| 1475 |
cloned = fooooooooooooooooo |
| 1476 |
This is trying to clone a nonexistent function!! |
| 1477 |
""" |
| 1478 |
err = "Couldn't find existing function 'fooooooooooooooooo'!" |
| 1479 |
with support.captured_stderr() as stderr: |
| 1480 |
self.expect_failure(block, err, lineno=0) |
| 1481 |
expected_debug_print = dedent("""\ |
| 1482 |
cls=None, module=<clinic.Clinic object>, existing='fooooooooooooooooo' |
| 1483 |
(cls or module).functions=[] |
| 1484 |
""") |
| 1485 |
stderr = stderr.getvalue() |
| 1486 |
self.assertIn(expected_debug_print, stderr) |
| 1487 |
|
| 1488 |
def test_return_converter(self): |
| 1489 |
function = self.parse_function(""" |
| 1490 |
module os |
| 1491 |
os.stat -> int |
| 1492 |
""") |
| 1493 |
self.assertIsInstance(function.return_converter, int_return_converter) |
| 1494 |
|
| 1495 |
def test_return_converter_invalid_syntax(self): |
| 1496 |
block = """ |
| 1497 |
module os |
| 1498 |
os.stat -> invalid syntax |
| 1499 |
""" |
| 1500 |
err = "Badly formed annotation for 'os.stat': 'invalid syntax'" |
| 1501 |
self.expect_failure(block, err) |
| 1502 |
|
| 1503 |
def test_legacy_converter_disallowed_in_return_annotation(self): |
| 1504 |
block = """ |
| 1505 |
module os |
| 1506 |
os.stat -> "s" |
| 1507 |
""" |
| 1508 |
err = "Legacy converter 's' not allowed as a return converter" |
| 1509 |
self.expect_failure(block, err) |
| 1510 |
|
| 1511 |
def test_unknown_return_converter(self): |
| 1512 |
block = """ |
| 1513 |
module os |
| 1514 |
os.stat -> fooooooooooooooooooooooo |
| 1515 |
""" |
| 1516 |
err = "No available return converter called 'fooooooooooooooooooooooo'" |
| 1517 |
self.expect_failure(block, err) |
| 1518 |
|
| 1519 |
def test_star(self): |
| 1520 |
function = self.parse_function(""" |
| 1521 |
module os |
| 1522 |
os.access |
| 1523 |
* |
| 1524 |
follow_symlinks: bool = True |
| 1525 |
""") |
| 1526 |
p = function.parameters['follow_symlinks'] |
| 1527 |
self.assertEqual(inspect.Parameter.KEYWORD_ONLY, p.kind) |
| 1528 |
self.assertEqual(0, p.group) |
| 1529 |
|
| 1530 |
def test_group(self): |
| 1531 |
function = self.parse_function(""" |
| 1532 |
module window |
| 1533 |
window.border |
| 1534 |
[ |
| 1535 |
ls: int |
| 1536 |
] |
| 1537 |
/ |
| 1538 |
""") |
| 1539 |
p = function.parameters['ls'] |
| 1540 |
self.assertEqual(1, p.group) |
| 1541 |
|
| 1542 |
def test_left_group(self): |
| 1543 |
function = self.parse_function(""" |
| 1544 |
module curses |
| 1545 |
curses.addch |
| 1546 |
[ |
| 1547 |
y: int |
| 1548 |
Y-coordinate. |
| 1549 |
x: int |
| 1550 |
X-coordinate. |
| 1551 |
] |
| 1552 |
ch: char |
| 1553 |
Character to add. |
| 1554 |
[ |
| 1555 |
attr: long |
| 1556 |
Attributes for the character. |
| 1557 |
] |
| 1558 |
/ |
| 1559 |
""") |
| 1560 |
dataset = ( |
| 1561 |
('y', -1), ('x', -1), |
| 1562 |
('ch', 0), |
| 1563 |
('attr', 1), |
| 1564 |
) |
| 1565 |
for name, group in dataset: |
| 1566 |
with self.subTest(name=name, group=group): |
| 1567 |
p = function.parameters[name] |
| 1568 |
self.assertEqual(p.group, group) |
| 1569 |
self.assertEqual(p.kind, inspect.Parameter.POSITIONAL_ONLY) |
| 1570 |
self.checkDocstring(function, """ |
| 1571 |
addch([y, x,] ch, [attr]) |
| 1572 |
|
| 1573 |
|
| 1574 |
y |
| 1575 |
Y-coordinate. |
| 1576 |
x |
| 1577 |
X-coordinate. |
| 1578 |
ch |
| 1579 |
Character to add. |
| 1580 |
attr |
| 1581 |
Attributes for the character. |
| 1582 |
""") |
| 1583 |
|
| 1584 |
def test_nested_groups(self): |
| 1585 |
function = self.parse_function(""" |
| 1586 |
module curses |
| 1587 |
curses.imaginary |
| 1588 |
[ |
| 1589 |
[ |
| 1590 |
y1: int |
| 1591 |
Y-coordinate. |
| 1592 |
y2: int |
| 1593 |
Y-coordinate. |
| 1594 |
] |
| 1595 |
x1: int |
| 1596 |
X-coordinate. |
| 1597 |
x2: int |
| 1598 |
X-coordinate. |
| 1599 |
] |
| 1600 |
ch: char |
| 1601 |
Character to add. |
| 1602 |
[ |
| 1603 |
attr1: long |
| 1604 |
Attributes for the character. |
| 1605 |
attr2: long |
| 1606 |
Attributes for the character. |
| 1607 |
attr3: long |
| 1608 |
Attributes for the character. |
| 1609 |
[ |
| 1610 |
attr4: long |
| 1611 |
Attributes for the character. |
| 1612 |
attr5: long |
| 1613 |
Attributes for the character. |
| 1614 |
attr6: long |
| 1615 |
Attributes for the character. |
| 1616 |
] |
| 1617 |
] |
| 1618 |
/ |
| 1619 |
""") |
| 1620 |
dataset = ( |
| 1621 |
('y1', -2), ('y2', -2), |
| 1622 |
('x1', -1), ('x2', -1), |
| 1623 |
('ch', 0), |
| 1624 |
('attr1', 1), ('attr2', 1), ('attr3', 1), |
| 1625 |
('attr4', 2), ('attr5', 2), ('attr6', 2), |
| 1626 |
) |
| 1627 |
for name, group in dataset: |
| 1628 |
with self.subTest(name=name, group=group): |
| 1629 |
p = function.parameters[name] |
| 1630 |
self.assertEqual(p.group, group) |
| 1631 |
self.assertEqual(p.kind, inspect.Parameter.POSITIONAL_ONLY) |
| 1632 |
|
| 1633 |
self.checkDocstring(function, """ |
| 1634 |
imaginary([[y1, y2,] x1, x2,] ch, [attr1, attr2, attr3, [attr4, attr5, |
| 1635 |
attr6]]) |
| 1636 |
|
| 1637 |
|
| 1638 |
y1 |
| 1639 |
Y-coordinate. |
| 1640 |
y2 |
| 1641 |
Y-coordinate. |
| 1642 |
x1 |
| 1643 |
X-coordinate. |
| 1644 |
x2 |
| 1645 |
X-coordinate. |
| 1646 |
ch |
| 1647 |
Character to add. |
| 1648 |
attr1 |
| 1649 |
Attributes for the character. |
| 1650 |
attr2 |
| 1651 |
Attributes for the character. |
| 1652 |
attr3 |
| 1653 |
Attributes for the character. |
| 1654 |
attr4 |
| 1655 |
Attributes for the character. |
| 1656 |
attr5 |
| 1657 |
Attributes for the character. |
| 1658 |
attr6 |
| 1659 |
Attributes for the character. |
| 1660 |
""") |
| 1661 |
|
| 1662 |
def test_disallowed_grouping__two_top_groups_on_left(self): |
| 1663 |
err = ( |
| 1664 |
"Function 'two_top_groups_on_left' has an unsupported group " |
| 1665 |
"configuration. (Unexpected state 2.b)" |
| 1666 |
) |
| 1667 |
block = """ |
| 1668 |
module foo |
| 1669 |
foo.two_top_groups_on_left |
| 1670 |
[ |
| 1671 |
group1 : int |
| 1672 |
] |
| 1673 |
[ |
| 1674 |
group2 : int |
| 1675 |
] |
| 1676 |
param: int |
| 1677 |
""" |
| 1678 |
self.expect_failure(block, err, lineno=5) |
| 1679 |
|
| 1680 |
def test_disallowed_grouping__two_top_groups_on_right(self): |
| 1681 |
block = """ |
| 1682 |
module foo |
| 1683 |
foo.two_top_groups_on_right |
| 1684 |
param: int |
| 1685 |
[ |
| 1686 |
group1 : int |
| 1687 |
] |
| 1688 |
[ |
| 1689 |
group2 : int |
| 1690 |
] |
| 1691 |
""" |
| 1692 |
err = ( |
| 1693 |
"Function 'two_top_groups_on_right' has an unsupported group " |
| 1694 |
"configuration. (Unexpected state 6.b)" |
| 1695 |
) |
| 1696 |
self.expect_failure(block, err) |
| 1697 |
|
| 1698 |
def test_disallowed_grouping__parameter_after_group_on_right(self): |
| 1699 |
block = """ |
| 1700 |
module foo |
| 1701 |
foo.parameter_after_group_on_right |
| 1702 |
param: int |
| 1703 |
[ |
| 1704 |
[ |
| 1705 |
group1 : int |
| 1706 |
] |
| 1707 |
group2 : int |
| 1708 |
] |
| 1709 |
""" |
| 1710 |
err = ( |
| 1711 |
"Function parameter_after_group_on_right has an unsupported group " |
| 1712 |
"configuration. (Unexpected state 6.a)" |
| 1713 |
) |
| 1714 |
self.expect_failure(block, err) |
| 1715 |
|
| 1716 |
def test_disallowed_grouping__group_after_parameter_on_left(self): |
| 1717 |
block = """ |
| 1718 |
module foo |
| 1719 |
foo.group_after_parameter_on_left |
| 1720 |
[ |
| 1721 |
group2 : int |
| 1722 |
[ |
| 1723 |
group1 : int |
| 1724 |
] |
| 1725 |
] |
| 1726 |
param: int |
| 1727 |
""" |
| 1728 |
err = ( |
| 1729 |
"Function 'group_after_parameter_on_left' has an unsupported group " |
| 1730 |
"configuration. (Unexpected state 2.b)" |
| 1731 |
) |
| 1732 |
self.expect_failure(block, err) |
| 1733 |
|
| 1734 |
def test_disallowed_grouping__empty_group_on_left(self): |
| 1735 |
block = """ |
| 1736 |
module foo |
| 1737 |
foo.empty_group |
| 1738 |
[ |
| 1739 |
[ |
| 1740 |
] |
| 1741 |
group2 : int |
| 1742 |
] |
| 1743 |
param: int |
| 1744 |
""" |
| 1745 |
err = ( |
| 1746 |
"Function 'empty_group' has an empty group. " |
| 1747 |
"All groups must contain at least one parameter." |
| 1748 |
) |
| 1749 |
self.expect_failure(block, err) |
| 1750 |
|
| 1751 |
def test_disallowed_grouping__empty_group_on_right(self): |
| 1752 |
block = """ |
| 1753 |
module foo |
| 1754 |
foo.empty_group |
| 1755 |
param: int |
| 1756 |
[ |
| 1757 |
[ |
| 1758 |
] |
| 1759 |
group2 : int |
| 1760 |
] |
| 1761 |
""" |
| 1762 |
err = ( |
| 1763 |
"Function 'empty_group' has an empty group. " |
| 1764 |
"All groups must contain at least one parameter." |
| 1765 |
) |
| 1766 |
self.expect_failure(block, err) |
| 1767 |
|
| 1768 |
def test_disallowed_grouping__no_matching_bracket(self): |
| 1769 |
block = """ |
| 1770 |
module foo |
| 1771 |
foo.empty_group |
| 1772 |
param: int |
| 1773 |
] |
| 1774 |
group2: int |
| 1775 |
] |
| 1776 |
""" |
| 1777 |
err = "Function 'empty_group' has a ']' without a matching '['" |
| 1778 |
self.expect_failure(block, err) |
| 1779 |
|
| 1780 |
def test_disallowed_grouping__must_be_position_only(self): |
| 1781 |
dataset = (""" |
| 1782 |
with_kwds |
| 1783 |
[ |
| 1784 |
* |
| 1785 |
a: object |
| 1786 |
] |
| 1787 |
""", """ |
| 1788 |
with_kwds |
| 1789 |
[ |
| 1790 |
a: object |
| 1791 |
] |
| 1792 |
""") |
| 1793 |
err = ( |
| 1794 |
"You cannot use optional groups ('[' and ']') unless all " |
| 1795 |
"parameters are positional-only ('/')" |
| 1796 |
) |
| 1797 |
for block in dataset: |
| 1798 |
with self.subTest(block=block): |
| 1799 |
self.expect_failure(block, err) |
| 1800 |
|
| 1801 |
def test_no_parameters(self): |
| 1802 |
function = self.parse_function(""" |
| 1803 |
module foo |
| 1804 |
foo.bar |
| 1805 |
|
| 1806 |
Docstring |
| 1807 |
|
| 1808 |
""") |
| 1809 |
self.assertEqual("bar($module, /)\n--\n\nDocstring", function.docstring) |
| 1810 |
self.assertEqual(1, len(function.parameters)) # self! |
| 1811 |
|
| 1812 |
def test_init_with_no_parameters(self): |
| 1813 |
function = self.parse_function(""" |
| 1814 |
module foo |
| 1815 |
class foo.Bar "unused" "notneeded" |
| 1816 |
foo.Bar.__init__ |
| 1817 |
|
| 1818 |
Docstring |
| 1819 |
|
| 1820 |
""", signatures_in_block=3, function_index=2) |
| 1821 |
|
| 1822 |
# self is not in the signature |
| 1823 |
self.assertEqual("Bar()\n--\n\nDocstring", function.docstring) |
| 1824 |
# but it *is* a parameter |
| 1825 |
self.assertEqual(1, len(function.parameters)) |
| 1826 |
|
| 1827 |
def test_illegal_module_line(self): |
| 1828 |
block = """ |
| 1829 |
module foo |
| 1830 |
foo.bar => int |
| 1831 |
/ |
| 1832 |
""" |
| 1833 |
err = "Illegal function name: 'foo.bar => int'" |
| 1834 |
self.expect_failure(block, err) |
| 1835 |
|
| 1836 |
def test_illegal_c_basename(self): |
| 1837 |
block = """ |
| 1838 |
module foo |
| 1839 |
foo.bar as 935 |
| 1840 |
/ |
| 1841 |
""" |
| 1842 |
err = "Illegal C basename: '935'" |
| 1843 |
self.expect_failure(block, err) |
| 1844 |
|
| 1845 |
def test_no_c_basename(self): |
| 1846 |
block = "foo as " |
| 1847 |
err = "No C basename provided after 'as' keyword" |
| 1848 |
self.expect_failure(block, err, strip=False) |
| 1849 |
|
| 1850 |
def test_single_star(self): |
| 1851 |
block = """ |
| 1852 |
module foo |
| 1853 |
foo.bar |
| 1854 |
* |
| 1855 |
* |
| 1856 |
""" |
| 1857 |
err = "Function 'bar' uses '*' more than once." |
| 1858 |
self.expect_failure(block, err) |
| 1859 |
|
| 1860 |
def test_parameters_required_after_star(self): |
| 1861 |
dataset = ( |
| 1862 |
"module foo\nfoo.bar\n *", |
| 1863 |
"module foo\nfoo.bar\n *\nDocstring here.", |
| 1864 |
"module foo\nfoo.bar\n this: int\n *", |
| 1865 |
"module foo\nfoo.bar\n this: int\n *\nDocstring.", |
| 1866 |
) |
| 1867 |
err = "Function 'bar' specifies '*' without following parameters." |
| 1868 |
for block in dataset: |
| 1869 |
with self.subTest(block=block): |
| 1870 |
self.expect_failure(block, err) |
| 1871 |
|
| 1872 |
def test_fulldisplayname_class(self): |
| 1873 |
dataset = ( |
| 1874 |
("T", """ |
| 1875 |
class T "void *" "" |
| 1876 |
T.__init__ |
| 1877 |
"""), |
| 1878 |
("m.T", """ |
| 1879 |
module m |
| 1880 |
class m.T "void *" "" |
| 1881 |
@classmethod |
| 1882 |
m.T.__new__ |
| 1883 |
"""), |
| 1884 |
("m.T.C", """ |
| 1885 |
module m |
| 1886 |
class m.T "void *" "" |
| 1887 |
class m.T.C "void *" "" |
| 1888 |
m.T.C.__init__ |
| 1889 |
"""), |
| 1890 |
) |
| 1891 |
for name, code in dataset: |
| 1892 |
with self.subTest(name=name, code=code): |
| 1893 |
block = self.parse(code) |
| 1894 |
func = block.signatures[-1] |
| 1895 |
self.assertEqual(func.fulldisplayname, name) |
| 1896 |
|
| 1897 |
def test_fulldisplayname_meth(self): |
| 1898 |
dataset = ( |
| 1899 |
("func", "func"), |
| 1900 |
("m.func", """ |
| 1901 |
module m |
| 1902 |
m.func |
| 1903 |
"""), |
| 1904 |
("T.meth", """ |
| 1905 |
class T "void *" "" |
| 1906 |
T.meth |
| 1907 |
"""), |
| 1908 |
("m.T.meth", """ |
| 1909 |
module m |
| 1910 |
class m.T "void *" "" |
| 1911 |
m.T.meth |
| 1912 |
"""), |
| 1913 |
("m.T.C.meth", """ |
| 1914 |
module m |
| 1915 |
class m.T "void *" "" |
| 1916 |
class m.T.C "void *" "" |
| 1917 |
m.T.C.meth |
| 1918 |
"""), |
| 1919 |
) |
| 1920 |
for name, code in dataset: |
| 1921 |
with self.subTest(name=name, code=code): |
| 1922 |
block = self.parse(code) |
| 1923 |
func = block.signatures[-1] |
| 1924 |
self.assertEqual(func.fulldisplayname, name) |
| 1925 |
|
| 1926 |
def test_depr_star_invalid_format_1(self): |
| 1927 |
block = """ |
| 1928 |
module foo |
| 1929 |
foo.bar |
| 1930 |
this: int |
| 1931 |
* [from 3] |
| 1932 |
Docstring. |
| 1933 |
""" |
| 1934 |
err = ( |
| 1935 |
"Function 'bar': expected format '[from major.minor]' " |
| 1936 |
"where 'major' and 'minor' are integers; got '3'" |
| 1937 |
) |
| 1938 |
self.expect_failure(block, err, lineno=3) |
| 1939 |
|
| 1940 |
def test_depr_star_invalid_format_2(self): |
| 1941 |
block = """ |
| 1942 |
module foo |
| 1943 |
foo.bar |
| 1944 |
this: int |
| 1945 |
* [from a.b] |
| 1946 |
Docstring. |
| 1947 |
""" |
| 1948 |
err = ( |
| 1949 |
"Function 'bar': expected format '[from major.minor]' " |
| 1950 |
"where 'major' and 'minor' are integers; got 'a.b'" |
| 1951 |
) |
| 1952 |
self.expect_failure(block, err, lineno=3) |
| 1953 |
|
| 1954 |
def test_depr_star_invalid_format_3(self): |
| 1955 |
block = """ |
| 1956 |
module foo |
| 1957 |
foo.bar |
| 1958 |
this: int |
| 1959 |
* [from 1.2.3] |
| 1960 |
Docstring. |
| 1961 |
""" |
| 1962 |
err = ( |
| 1963 |
"Function 'bar': expected format '[from major.minor]' " |
| 1964 |
"where 'major' and 'minor' are integers; got '1.2.3'" |
| 1965 |
) |
| 1966 |
self.expect_failure(block, err, lineno=3) |
| 1967 |
|
| 1968 |
def test_parameters_required_after_depr_star(self): |
| 1969 |
block = """ |
| 1970 |
module foo |
| 1971 |
foo.bar |
| 1972 |
this: int |
| 1973 |
* [from 3.14] |
| 1974 |
Docstring. |
| 1975 |
""" |
| 1976 |
err = ( |
| 1977 |
"Function 'bar' specifies '* [from ...]' without " |
| 1978 |
"following parameters." |
| 1979 |
) |
| 1980 |
self.expect_failure(block, err, lineno=4) |
| 1981 |
|
| 1982 |
def test_parameters_required_after_depr_star2(self): |
| 1983 |
block = """ |
| 1984 |
module foo |
| 1985 |
foo.bar |
| 1986 |
a: int |
| 1987 |
* [from 3.14] |
| 1988 |
* |
| 1989 |
b: int |
| 1990 |
Docstring. |
| 1991 |
""" |
| 1992 |
err = ( |
| 1993 |
"Function 'bar' specifies '* [from ...]' without " |
| 1994 |
"following parameters." |
| 1995 |
) |
| 1996 |
self.expect_failure(block, err, lineno=4) |
| 1997 |
|
| 1998 |
def test_parameters_required_after_depr_star3(self): |
| 1999 |
block = """ |
| 2000 |
module foo |
| 2001 |
foo.bar |
| 2002 |
a: int |
| 2003 |
* [from 3.14] |
| 2004 |
*args: tuple |
| 2005 |
b: int |
| 2006 |
Docstring. |
| 2007 |
""" |
| 2008 |
err = ( |
| 2009 |
"Function 'bar' specifies '* [from ...]' without " |
| 2010 |
"following parameters." |
| 2011 |
) |
| 2012 |
self.expect_failure(block, err, lineno=4) |
| 2013 |
|
| 2014 |
def test_depr_star_must_come_before_star(self): |
| 2015 |
block = """ |
| 2016 |
module foo |
| 2017 |
foo.bar |
| 2018 |
a: int |
| 2019 |
* |
| 2020 |
* [from 3.14] |
| 2021 |
b: int |
| 2022 |
Docstring. |
| 2023 |
""" |
| 2024 |
err = "Function 'bar': '* [from ...]' must precede '*'" |
| 2025 |
self.expect_failure(block, err, lineno=4) |
| 2026 |
|
| 2027 |
def test_depr_star_must_come_before_vararg(self): |
| 2028 |
block = """ |
| 2029 |
module foo |
| 2030 |
foo.bar |
| 2031 |
a: int |
| 2032 |
*args: tuple |
| 2033 |
* [from 3.14] |
| 2034 |
b: int |
| 2035 |
Docstring. |
| 2036 |
""" |
| 2037 |
err = "Function 'bar': '* [from ...]' must precede '*'" |
| 2038 |
self.expect_failure(block, err, lineno=4) |
| 2039 |
|
| 2040 |
def test_depr_star_duplicate(self): |
| 2041 |
block = """ |
| 2042 |
module foo |
| 2043 |
foo.bar |
| 2044 |
a: int |
| 2045 |
* [from 3.14] |
| 2046 |
b: int |
| 2047 |
* [from 3.14] |
| 2048 |
c: int |
| 2049 |
Docstring. |
| 2050 |
""" |
| 2051 |
err = "Function 'bar' uses '* [from 3.14]' more than once." |
| 2052 |
self.expect_failure(block, err, lineno=5) |
| 2053 |
|
| 2054 |
def test_depr_star_duplicate2(self): |
| 2055 |
block = """ |
| 2056 |
module foo |
| 2057 |
foo.bar |
| 2058 |
a: int |
| 2059 |
* [from 3.14] |
| 2060 |
b: int |
| 2061 |
* [from 3.15] |
| 2062 |
c: int |
| 2063 |
Docstring. |
| 2064 |
""" |
| 2065 |
err = "Function 'bar': '* [from 3.15]' must precede '* [from 3.14]'" |
| 2066 |
self.expect_failure(block, err, lineno=5) |
| 2067 |
|
| 2068 |
def test_depr_slash_duplicate(self): |
| 2069 |
block = """ |
| 2070 |
module foo |
| 2071 |
foo.bar |
| 2072 |
a: int |
| 2073 |
/ [from 3.14] |
| 2074 |
b: int |
| 2075 |
/ [from 3.14] |
| 2076 |
c: int |
| 2077 |
Docstring. |
| 2078 |
""" |
| 2079 |
err = "Function 'bar' uses '/ [from 3.14]' more than once." |
| 2080 |
self.expect_failure(block, err, lineno=5) |
| 2081 |
|
| 2082 |
def test_depr_slash_duplicate2(self): |
| 2083 |
block = """ |
| 2084 |
module foo |
| 2085 |
foo.bar |
| 2086 |
a: int |
| 2087 |
/ [from 3.15] |
| 2088 |
b: int |
| 2089 |
/ [from 3.14] |
| 2090 |
c: int |
| 2091 |
Docstring. |
| 2092 |
""" |
| 2093 |
err = "Function 'bar': '/ [from 3.14]' must precede '/ [from 3.15]'" |
| 2094 |
self.expect_failure(block, err, lineno=5) |
| 2095 |
|
| 2096 |
def test_single_slash(self): |
| 2097 |
block = """ |
| 2098 |
module foo |
| 2099 |
foo.bar |
| 2100 |
/ |
| 2101 |
/ |
| 2102 |
""" |
| 2103 |
err = ( |
| 2104 |
"Function 'bar' has an unsupported group configuration. " |
| 2105 |
"(Unexpected state 0.d)" |
| 2106 |
) |
| 2107 |
self.expect_failure(block, err) |
| 2108 |
|
| 2109 |
def test_parameters_required_before_depr_slash(self): |
| 2110 |
block = """ |
| 2111 |
module foo |
| 2112 |
foo.bar |
| 2113 |
/ [from 3.14] |
| 2114 |
Docstring. |
| 2115 |
""" |
| 2116 |
err = ( |
| 2117 |
"Function 'bar' specifies '/ [from ...]' without " |
| 2118 |
"preceding parameters." |
| 2119 |
) |
| 2120 |
self.expect_failure(block, err, lineno=2) |
| 2121 |
|
| 2122 |
def test_parameters_required_before_depr_slash2(self): |
| 2123 |
block = """ |
| 2124 |
module foo |
| 2125 |
foo.bar |
| 2126 |
a: int |
| 2127 |
/ |
| 2128 |
/ [from 3.14] |
| 2129 |
Docstring. |
| 2130 |
""" |
| 2131 |
err = ( |
| 2132 |
"Function 'bar' specifies '/ [from ...]' without " |
| 2133 |
"preceding parameters." |
| 2134 |
) |
| 2135 |
self.expect_failure(block, err, lineno=4) |
| 2136 |
|
| 2137 |
def test_double_slash(self): |
| 2138 |
block = """ |
| 2139 |
module foo |
| 2140 |
foo.bar |
| 2141 |
a: int |
| 2142 |
/ |
| 2143 |
b: int |
| 2144 |
/ |
| 2145 |
""" |
| 2146 |
err = "Function 'bar' uses '/' more than once." |
| 2147 |
self.expect_failure(block, err) |
| 2148 |
|
| 2149 |
def test_slash_after_star(self): |
| 2150 |
block = """ |
| 2151 |
module foo |
| 2152 |
foo.bar |
| 2153 |
x: int |
| 2154 |
y: int |
| 2155 |
* |
| 2156 |
z: int |
| 2157 |
/ |
| 2158 |
""" |
| 2159 |
err = "Function 'bar': '/' must precede '*'" |
| 2160 |
self.expect_failure(block, err) |
| 2161 |
|
| 2162 |
def test_slash_after_vararg(self): |
| 2163 |
block = """ |
| 2164 |
module foo |
| 2165 |
foo.bar |
| 2166 |
x: int |
| 2167 |
y: int |
| 2168 |
*args: tuple |
| 2169 |
z: int |
| 2170 |
/ |
| 2171 |
""" |
| 2172 |
err = "Function 'bar': '/' must precede '*'" |
| 2173 |
self.expect_failure(block, err) |
| 2174 |
|
| 2175 |
def test_depr_star_must_come_after_slash(self): |
| 2176 |
block = """ |
| 2177 |
module foo |
| 2178 |
foo.bar |
| 2179 |
a: int |
| 2180 |
* [from 3.14] |
| 2181 |
/ |
| 2182 |
b: int |
| 2183 |
Docstring. |
| 2184 |
""" |
| 2185 |
err = "Function 'bar': '/' must precede '* [from ...]'" |
| 2186 |
self.expect_failure(block, err, lineno=4) |
| 2187 |
|
| 2188 |
def test_depr_star_must_come_after_depr_slash(self): |
| 2189 |
block = """ |
| 2190 |
module foo |
| 2191 |
foo.bar |
| 2192 |
a: int |
| 2193 |
* [from 3.14] |
| 2194 |
/ [from 3.14] |
| 2195 |
b: int |
| 2196 |
Docstring. |
| 2197 |
""" |
| 2198 |
err = "Function 'bar': '/ [from ...]' must precede '* [from ...]'" |
| 2199 |
self.expect_failure(block, err, lineno=4) |
| 2200 |
|
| 2201 |
def test_star_must_come_after_depr_slash(self): |
| 2202 |
block = """ |
| 2203 |
module foo |
| 2204 |
foo.bar |
| 2205 |
a: int |
| 2206 |
* |
| 2207 |
/ [from 3.14] |
| 2208 |
b: int |
| 2209 |
Docstring. |
| 2210 |
""" |
| 2211 |
err = "Function 'bar': '/ [from ...]' must precede '*'" |
| 2212 |
self.expect_failure(block, err, lineno=4) |
| 2213 |
|
| 2214 |
def test_vararg_must_come_after_depr_slash(self): |
| 2215 |
block = """ |
| 2216 |
module foo |
| 2217 |
foo.bar |
| 2218 |
a: int |
| 2219 |
*args: tuple |
| 2220 |
/ [from 3.14] |
| 2221 |
b: int |
| 2222 |
Docstring. |
| 2223 |
""" |
| 2224 |
err = "Function 'bar': '/ [from ...]' must precede '*'" |
| 2225 |
self.expect_failure(block, err, lineno=4) |
| 2226 |
|
| 2227 |
def test_depr_slash_must_come_after_slash(self): |
| 2228 |
block = """ |
| 2229 |
module foo |
| 2230 |
foo.bar |
| 2231 |
a: int |
| 2232 |
/ [from 3.14] |
| 2233 |
/ |
| 2234 |
b: int |
| 2235 |
Docstring. |
| 2236 |
""" |
| 2237 |
err = "Function 'bar': '/' must precede '/ [from ...]'" |
| 2238 |
self.expect_failure(block, err, lineno=4) |
| 2239 |
|
| 2240 |
def test_parameters_not_permitted_after_slash_for_now(self): |
| 2241 |
block = """ |
| 2242 |
module foo |
| 2243 |
foo.bar |
| 2244 |
/ |
| 2245 |
x: int |
| 2246 |
""" |
| 2247 |
err = ( |
| 2248 |
"Function 'bar' has an unsupported group configuration. " |
| 2249 |
"(Unexpected state 0.d)" |
| 2250 |
) |
| 2251 |
self.expect_failure(block, err) |
| 2252 |
|
| 2253 |
def test_parameters_no_more_than_one_vararg(self): |
| 2254 |
err = "Function 'bar' uses '*' more than once." |
| 2255 |
block = """ |
| 2256 |
module foo |
| 2257 |
foo.bar |
| 2258 |
*vararg1: tuple |
| 2259 |
*vararg2: tuple |
| 2260 |
""" |
| 2261 |
self.expect_failure(block, err, lineno=3) |
| 2262 |
|
| 2263 |
def test_function_not_at_column_0(self): |
| 2264 |
function = self.parse_function(""" |
| 2265 |
module foo |
| 2266 |
foo.bar |
| 2267 |
x: int |
| 2268 |
Nested docstring here, goeth. |
| 2269 |
* |
| 2270 |
y: str |
| 2271 |
Not at column 0! |
| 2272 |
""") |
| 2273 |
self.checkDocstring(function, """ |
| 2274 |
bar($module, /, x, *, y) |
| 2275 |
-- |
| 2276 |
|
| 2277 |
Not at column 0! |
| 2278 |
|
| 2279 |
x |
| 2280 |
Nested docstring here, goeth. |
| 2281 |
""") |
| 2282 |
|
| 2283 |
def test_docstring_only_summary(self): |
| 2284 |
function = self.parse_function(""" |
| 2285 |
module m |
| 2286 |
m.f |
| 2287 |
summary |
| 2288 |
""") |
| 2289 |
self.checkDocstring(function, """ |
| 2290 |
f($module, /) |
| 2291 |
-- |
| 2292 |
|
| 2293 |
summary |
| 2294 |
""") |
| 2295 |
|
| 2296 |
def test_docstring_empty_lines(self): |
| 2297 |
function = self.parse_function(""" |
| 2298 |
module m |
| 2299 |
m.f |
| 2300 |
|
| 2301 |
|
| 2302 |
""") |
| 2303 |
self.checkDocstring(function, """ |
| 2304 |
f($module, /) |
| 2305 |
-- |
| 2306 |
""") |
| 2307 |
|
| 2308 |
def test_docstring_explicit_params_placement(self): |
| 2309 |
function = self.parse_function(""" |
| 2310 |
module m |
| 2311 |
m.f |
| 2312 |
a: int |
| 2313 |
Param docstring for 'a' will be included |
| 2314 |
b: int |
| 2315 |
c: int |
| 2316 |
Param docstring for 'c' will be included |
| 2317 |
This is the summary line. |
| 2318 |
|
| 2319 |
We'll now place the params section here: |
| 2320 |
{parameters} |
| 2321 |
And now for something completely different! |
| 2322 |
(Note the added newline) |
| 2323 |
""") |
| 2324 |
self.checkDocstring(function, """ |
| 2325 |
f($module, /, a, b, c) |
| 2326 |
-- |
| 2327 |
|
| 2328 |
This is the summary line. |
| 2329 |
|
| 2330 |
We'll now place the params section here: |
| 2331 |
a |
| 2332 |
Param docstring for 'a' will be included |
| 2333 |
c |
| 2334 |
Param docstring for 'c' will be included |
| 2335 |
|
| 2336 |
And now for something completely different! |
| 2337 |
(Note the added newline) |
| 2338 |
""") |
| 2339 |
|
| 2340 |
def test_indent_stack_no_tabs(self): |
| 2341 |
block = """ |
| 2342 |
module foo |
| 2343 |
foo.bar |
| 2344 |
*vararg1: tuple |
| 2345 |
\t*vararg2: tuple |
| 2346 |
""" |
| 2347 |
err = ("Tab characters are illegal in the Clinic DSL: " |
| 2348 |
r"'\t*vararg2: tuple'") |
| 2349 |
self.expect_failure(block, err) |
| 2350 |
|
| 2351 |
def test_indent_stack_illegal_outdent(self): |
| 2352 |
block = """ |
| 2353 |
module foo |
| 2354 |
foo.bar |
| 2355 |
a: object |
| 2356 |
b: object |
| 2357 |
""" |
| 2358 |
err = "Illegal outdent" |
| 2359 |
self.expect_failure(block, err) |
| 2360 |
|
| 2361 |
def test_directive(self): |
| 2362 |
parser = DSLParser(_make_clinic()) |
| 2363 |
parser.flag = False |
| 2364 |
parser.directives['setflag'] = lambda : setattr(parser, 'flag', True) |
| 2365 |
block = Block("setflag") |
| 2366 |
parser.parse(block) |
| 2367 |
self.assertTrue(parser.flag) |
| 2368 |
|
| 2369 |
def test_legacy_converters(self): |
| 2370 |
block = self.parse('module os\nos.access\n path: "s"') |
| 2371 |
module, function = block.signatures |
| 2372 |
conv = (function.parameters['path']).converter |
| 2373 |
self.assertIsInstance(conv, str_converter) |
| 2374 |
|
| 2375 |
def test_legacy_converters_non_string_constant_annotation(self): |
| 2376 |
err = "Annotations must be either a name, a function call, or a string" |
| 2377 |
dataset = ( |
| 2378 |
'module os\nos.access\n path: 42', |
| 2379 |
'module os\nos.access\n path: 42.42', |
| 2380 |
'module os\nos.access\n path: 42j', |
| 2381 |
'module os\nos.access\n path: b"42"', |
| 2382 |
) |
| 2383 |
for block in dataset: |
| 2384 |
with self.subTest(block=block): |
| 2385 |
self.expect_failure(block, err, lineno=2) |
| 2386 |
|
| 2387 |
def test_other_bizarre_things_in_annotations_fail(self): |
| 2388 |
err = "Annotations must be either a name, a function call, or a string" |
| 2389 |
dataset = ( |
| 2390 |
'module os\nos.access\n path: {"some": "dictionary"}', |
| 2391 |
'module os\nos.access\n path: ["list", "of", "strings"]', |
| 2392 |
'module os\nos.access\n path: (x for x in range(42))', |
| 2393 |
) |
| 2394 |
for block in dataset: |
| 2395 |
with self.subTest(block=block): |
| 2396 |
self.expect_failure(block, err, lineno=2) |
| 2397 |
|
| 2398 |
def test_kwarg_splats_disallowed_in_function_call_annotations(self): |
| 2399 |
err = "Cannot use a kwarg splat in a function-call annotation" |
| 2400 |
dataset = ( |
| 2401 |
'module fo\nfo.barbaz\n o: bool(**{None: "bang!"})', |
| 2402 |
'module fo\nfo.barbaz -> bool(**{None: "bang!"})', |
| 2403 |
'module fo\nfo.barbaz -> bool(**{"bang": 42})', |
| 2404 |
'module fo\nfo.barbaz\n o: bool(**{"bang": None})', |
| 2405 |
) |
| 2406 |
for block in dataset: |
| 2407 |
with self.subTest(block=block): |
| 2408 |
self.expect_failure(block, err) |
| 2409 |
|
| 2410 |
def test_self_param_placement(self): |
| 2411 |
err = ( |
| 2412 |
"A 'self' parameter, if specified, must be the very first thing " |
| 2413 |
"in the parameter block." |
| 2414 |
) |
| 2415 |
block = """ |
| 2416 |
module foo |
| 2417 |
foo.func |
| 2418 |
a: int |
| 2419 |
self: self(type="PyObject *") |
| 2420 |
""" |
| 2421 |
self.expect_failure(block, err, lineno=3) |
| 2422 |
|
| 2423 |
def test_self_param_cannot_be_optional(self): |
| 2424 |
err = "A 'self' parameter cannot be marked optional." |
| 2425 |
block = """ |
| 2426 |
module foo |
| 2427 |
foo.func |
| 2428 |
self: self(type="PyObject *") = None |
| 2429 |
""" |
| 2430 |
self.expect_failure(block, err, lineno=2) |
| 2431 |
|
| 2432 |
def test_defining_class_param_placement(self): |
| 2433 |
err = ( |
| 2434 |
"A 'defining_class' parameter, if specified, must either be the " |
| 2435 |
"first thing in the parameter block, or come just after 'self'." |
| 2436 |
) |
| 2437 |
block = """ |
| 2438 |
module foo |
| 2439 |
foo.func |
| 2440 |
self: self(type="PyObject *") |
| 2441 |
a: int |
| 2442 |
cls: defining_class |
| 2443 |
""" |
| 2444 |
self.expect_failure(block, err, lineno=4) |
| 2445 |
|
| 2446 |
def test_defining_class_param_cannot_be_optional(self): |
| 2447 |
err = "A 'defining_class' parameter cannot be marked optional." |
| 2448 |
block = """ |
| 2449 |
module foo |
| 2450 |
foo.func |
| 2451 |
cls: defining_class(type="PyObject *") = None |
| 2452 |
""" |
| 2453 |
self.expect_failure(block, err, lineno=2) |
| 2454 |
|
| 2455 |
def test_slot_methods_cannot_access_defining_class(self): |
| 2456 |
block = """ |
| 2457 |
module foo |
| 2458 |
class Foo "" "" |
| 2459 |
Foo.__init__ |
| 2460 |
cls: defining_class |
| 2461 |
a: object |
| 2462 |
""" |
| 2463 |
err = "Slot methods cannot access their defining class." |
| 2464 |
with self.assertRaisesRegex(ValueError, err): |
| 2465 |
self.parse_function(block) |
| 2466 |
|
| 2467 |
def test_new_must_be_a_class_method(self): |
| 2468 |
err = "'__new__' must be a class method!" |
| 2469 |
block = """ |
| 2470 |
module foo |
| 2471 |
class Foo "" "" |
| 2472 |
Foo.__new__ |
| 2473 |
""" |
| 2474 |
self.expect_failure(block, err, lineno=2) |
| 2475 |
|
| 2476 |
def test_init_must_be_a_normal_method(self): |
| 2477 |
err_template = "'__init__' must be a normal method; got 'FunctionKind.{}'!" |
| 2478 |
annotations = { |
| 2479 |
"@classmethod": "CLASS_METHOD", |
| 2480 |
"@staticmethod": "STATIC_METHOD", |
| 2481 |
"@getter": "GETTER", |
| 2482 |
} |
| 2483 |
for annotation, invalid_kind in annotations.items(): |
| 2484 |
with self.subTest(annotation=annotation, invalid_kind=invalid_kind): |
| 2485 |
block = f""" |
| 2486 |
module foo |
| 2487 |
class Foo "" "" |
| 2488 |
{annotation} |
| 2489 |
Foo.__init__ |
| 2490 |
""" |
| 2491 |
expected_error = err_template.format(invalid_kind) |
| 2492 |
self.expect_failure(block, expected_error, lineno=3) |
| 2493 |
|
| 2494 |
def test_init_cannot_define_a_return_type(self): |
| 2495 |
block = """ |
| 2496 |
class Foo "" "" |
| 2497 |
Foo.__init__ -> long |
| 2498 |
""" |
| 2499 |
expected_error = "__init__ methods cannot define a return type" |
| 2500 |
self.expect_failure(block, expected_error, lineno=1) |
| 2501 |
|
| 2502 |
def test_invalid_getset(self): |
| 2503 |
annotations = ["@getter", "@setter"] |
| 2504 |
for annotation in annotations: |
| 2505 |
with self.subTest(annotation=annotation): |
| 2506 |
block = f""" |
| 2507 |
module foo |
| 2508 |
class Foo "" "" |
| 2509 |
{annotation} |
| 2510 |
Foo.property -> int |
| 2511 |
""" |
| 2512 |
expected_error = f"{annotation} method cannot define a return type" |
| 2513 |
self.expect_failure(block, expected_error, lineno=3) |
| 2514 |
|
| 2515 |
block = f""" |
| 2516 |
module foo |
| 2517 |
class Foo "" "" |
| 2518 |
{annotation} |
| 2519 |
Foo.property |
| 2520 |
obj: int |
| 2521 |
/ |
| 2522 |
""" |
| 2523 |
expected_error = f"{annotation} methods cannot define parameters" |
| 2524 |
self.expect_failure(block, expected_error) |
| 2525 |
|
| 2526 |
def test_setter_docstring(self): |
| 2527 |
block = """ |
| 2528 |
module foo |
| 2529 |
class Foo "" "" |
| 2530 |
@setter |
| 2531 |
Foo.property |
| 2532 |
|
| 2533 |
foo |
| 2534 |
|
| 2535 |
bar |
| 2536 |
[clinic start generated code]*/ |
| 2537 |
""" |
| 2538 |
expected_error = "docstrings are only supported for @getter, not @setter" |
| 2539 |
self.expect_failure(block, expected_error) |
| 2540 |
|
| 2541 |
def test_duplicate_getset(self): |
| 2542 |
annotations = ["@getter", "@setter"] |
| 2543 |
for annotation in annotations: |
| 2544 |
with self.subTest(annotation=annotation): |
| 2545 |
block = f""" |
| 2546 |
module foo |
| 2547 |
class Foo "" "" |
| 2548 |
{annotation} |
| 2549 |
{annotation} |
| 2550 |
Foo.property -> int |
| 2551 |
""" |
| 2552 |
expected_error = f"Cannot apply {annotation} twice to the same function!" |
| 2553 |
self.expect_failure(block, expected_error, lineno=3) |
| 2554 |
|
| 2555 |
def test_getter_and_setter_disallowed_on_same_function(self): |
| 2556 |
dup_annotations = [("@getter", "@setter"), ("@setter", "@getter")] |
| 2557 |
for dup in dup_annotations: |
| 2558 |
with self.subTest(dup=dup): |
| 2559 |
block = f""" |
| 2560 |
module foo |
| 2561 |
class Foo "" "" |
| 2562 |
{dup[0]} |
| 2563 |
{dup[1]} |
| 2564 |
Foo.property -> int |
| 2565 |
""" |
| 2566 |
expected_error = "Cannot apply both @getter and @setter to the same function!" |
| 2567 |
self.expect_failure(block, expected_error, lineno=3) |
| 2568 |
|
| 2569 |
def test_getset_no_class(self): |
| 2570 |
for annotation in "@getter", "@setter": |
| 2571 |
with self.subTest(annotation=annotation): |
| 2572 |
block = f""" |
| 2573 |
module m |
| 2574 |
{annotation} |
| 2575 |
m.func |
| 2576 |
""" |
| 2577 |
expected_error = "@getter and @setter must be methods" |
| 2578 |
self.expect_failure(block, expected_error, lineno=2) |
| 2579 |
|
| 2580 |
def test_duplicate_coexist(self): |
| 2581 |
err = "Called @coexist twice" |
| 2582 |
block = """ |
| 2583 |
module m |
| 2584 |
@coexist |
| 2585 |
@coexist |
| 2586 |
m.fn |
| 2587 |
""" |
| 2588 |
self.expect_failure(block, err, lineno=2) |
| 2589 |
|
| 2590 |
def test_unused_param(self): |
| 2591 |
block = self.parse(""" |
| 2592 |
module foo |
| 2593 |
foo.func |
| 2594 |
fn: object |
| 2595 |
k: float |
| 2596 |
i: float(unused=True) |
| 2597 |
/ |
| 2598 |
* |
| 2599 |
flag: bool(unused=True) = False |
| 2600 |
""") |
| 2601 |
sig = block.signatures[1] # Function index == 1 |
| 2602 |
params = sig.parameters |
| 2603 |
conv = lambda fn: params[fn].converter |
| 2604 |
dataset = ( |
| 2605 |
{"name": "fn", "unused": False}, |
| 2606 |
{"name": "k", "unused": False}, |
| 2607 |
{"name": "i", "unused": True}, |
| 2608 |
{"name": "flag", "unused": True}, |
| 2609 |
) |
| 2610 |
for param in dataset: |
| 2611 |
name, unused = param.values() |
| 2612 |
with self.subTest(name=name, unused=unused): |
| 2613 |
p = conv(name) |
| 2614 |
# Verify that the unused flag is parsed correctly. |
| 2615 |
self.assertEqual(unused, p.unused) |
| 2616 |
|
| 2617 |
# Now, check that we'll produce correct code. |
| 2618 |
decl = p.simple_declaration(in_parser=False) |
| 2619 |
if unused: |
| 2620 |
self.assertIn("Py_UNUSED", decl) |
| 2621 |
else: |
| 2622 |
self.assertNotIn("Py_UNUSED", decl) |
| 2623 |
|
| 2624 |
# Make sure the Py_UNUSED macro is not used in the parser body. |
| 2625 |
parser_decl = p.simple_declaration(in_parser=True) |
| 2626 |
self.assertNotIn("Py_UNUSED", parser_decl) |
| 2627 |
|
| 2628 |
def test_scaffolding(self): |
| 2629 |
# test repr on special values |
| 2630 |
self.assertEqual(repr(unspecified), '<Unspecified>') |
| 2631 |
self.assertEqual(repr(NULL), '<Null>') |
| 2632 |
|
| 2633 |
# test that fail fails |
| 2634 |
with support.captured_stdout() as stdout: |
| 2635 |
errmsg = 'The igloos are melting' |
| 2636 |
with self.assertRaisesRegex(ClinicError, errmsg) as cm: |
| 2637 |
fail(errmsg, filename='clown.txt', line_number=69) |
| 2638 |
exc = cm.exception |
| 2639 |
self.assertEqual(exc.filename, 'clown.txt') |
| 2640 |
self.assertEqual(exc.lineno, 69) |
| 2641 |
self.assertEqual(stdout.getvalue(), "") |
| 2642 |
|
| 2643 |
def test_non_ascii_character_in_docstring(self): |
| 2644 |
block = """ |
| 2645 |
module test |
| 2646 |
test.fn |
| 2647 |
a: int |
| 2648 |
á param docstring |
| 2649 |
docstring fü bár baß |
| 2650 |
""" |
| 2651 |
with support.captured_stdout() as stdout: |
| 2652 |
self.parse(block) |
| 2653 |
# The line numbers are off; this is a known limitation. |
| 2654 |
expected = dedent("""\ |
| 2655 |
Warning: |
| 2656 |
Non-ascii characters are not allowed in docstrings: 'á' |
| 2657 |
|
| 2658 |
Warning: |
| 2659 |
Non-ascii characters are not allowed in docstrings: 'ü', 'á', 'ß' |
| 2660 |
|
| 2661 |
""") |
| 2662 |
self.assertEqual(stdout.getvalue(), expected) |
| 2663 |
|
| 2664 |
def test_illegal_c_identifier(self): |
| 2665 |
err = "Illegal C identifier: 17a" |
| 2666 |
block = """ |
| 2667 |
module test |
| 2668 |
test.fn |
| 2669 |
a as 17a: int |
| 2670 |
""" |
| 2671 |
self.expect_failure(block, err, lineno=2) |
| 2672 |
|
| 2673 |
def test_cannot_convert_special_method(self): |
| 2674 |
err = "'__len__' is a special method and cannot be converted" |
| 2675 |
block = """ |
| 2676 |
class T "" "" |
| 2677 |
T.__len__ |
| 2678 |
""" |
| 2679 |
self.expect_failure(block, err, lineno=1) |
| 2680 |
|
| 2681 |
def test_cannot_specify_pydefault_without_default(self): |
| 2682 |
err = "You can't specify py_default without specifying a default value!" |
| 2683 |
block = """ |
| 2684 |
fn |
| 2685 |
a: object(py_default='NULL') |
| 2686 |
""" |
| 2687 |
self.expect_failure(block, err, lineno=1) |
| 2688 |
|
| 2689 |
def test_vararg_cannot_take_default_value(self): |
| 2690 |
err = "Function 'fn' has an invalid parameter declaration:" |
| 2691 |
block = """ |
| 2692 |
fn |
| 2693 |
*args: tuple = None |
| 2694 |
""" |
| 2695 |
self.expect_failure(block, err, lineno=1) |
| 2696 |
|
| 2697 |
def test_default_is_not_of_correct_type(self): |
| 2698 |
err = ("int_converter: default value 2.5 for field 'a' " |
| 2699 |
"is not of type 'int'") |
| 2700 |
block = """ |
| 2701 |
fn |
| 2702 |
a: int = 2.5 |
| 2703 |
""" |
| 2704 |
self.expect_failure(block, err, lineno=1) |
| 2705 |
|
| 2706 |
def test_invalid_legacy_converter(self): |
| 2707 |
err = "'fhi' is not a valid legacy converter" |
| 2708 |
block = """ |
| 2709 |
fn |
| 2710 |
a: 'fhi' |
| 2711 |
""" |
| 2712 |
self.expect_failure(block, err, lineno=1) |
| 2713 |
|
| 2714 |
def test_parent_class_or_module_does_not_exist(self): |
| 2715 |
err = "Parent class or module 'baz' does not exist" |
| 2716 |
block = """ |
| 2717 |
module m |
| 2718 |
baz.func |
| 2719 |
""" |
| 2720 |
self.expect_failure(block, err, lineno=1) |
| 2721 |
|
| 2722 |
def test_duplicate_param_name(self): |
| 2723 |
err = "You can't have two parameters named 'a'" |
| 2724 |
block = """ |
| 2725 |
module m |
| 2726 |
m.func |
| 2727 |
a: int |
| 2728 |
a: float |
| 2729 |
""" |
| 2730 |
self.expect_failure(block, err, lineno=3) |
| 2731 |
|
| 2732 |
def test_param_requires_custom_c_name(self): |
| 2733 |
err = "Parameter 'module' requires a custom C name" |
| 2734 |
block = """ |
| 2735 |
module m |
| 2736 |
m.func |
| 2737 |
module: int |
| 2738 |
""" |
| 2739 |
self.expect_failure(block, err, lineno=2) |
| 2740 |
|
| 2741 |
def test_state_func_docstring_assert_no_group(self): |
| 2742 |
err = "Function 'func' has a ']' without a matching '['" |
| 2743 |
block = """ |
| 2744 |
module m |
| 2745 |
m.func |
| 2746 |
] |
| 2747 |
docstring |
| 2748 |
""" |
| 2749 |
self.expect_failure(block, err, lineno=2) |
| 2750 |
|
| 2751 |
def test_state_func_docstring_no_summary(self): |
| 2752 |
err = "Docstring for 'm.func' does not have a summary line!" |
| 2753 |
block = """ |
| 2754 |
module m |
| 2755 |
m.func |
| 2756 |
docstring1 |
| 2757 |
docstring2 |
| 2758 |
""" |
| 2759 |
self.expect_failure(block, err, lineno=3) |
| 2760 |
|
| 2761 |
def test_state_func_docstring_only_one_param_template(self): |
| 2762 |
err = "You may not specify {parameters} more than once in a docstring!" |
| 2763 |
block = """ |
| 2764 |
module m |
| 2765 |
m.func |
| 2766 |
docstring summary |
| 2767 |
|
| 2768 |
these are the params: |
| 2769 |
{parameters} |
| 2770 |
these are the params again: |
| 2771 |
{parameters} |
| 2772 |
""" |
| 2773 |
self.expect_failure(block, err, lineno=7) |
| 2774 |
|
| 2775 |
def test_kind_defining_class(self): |
| 2776 |
function = self.parse_function(""" |
| 2777 |
module m |
| 2778 |
class m.C "PyObject *" "" |
| 2779 |
m.C.meth |
| 2780 |
cls: defining_class |
| 2781 |
""", signatures_in_block=3, function_index=2) |
| 2782 |
p = function.parameters['cls'] |
| 2783 |
self.assertEqual(p.kind, inspect.Parameter.POSITIONAL_ONLY) |
| 2784 |
|
| 2785 |
def test_disallow_defining_class_at_module_level(self): |
| 2786 |
err = "A 'defining_class' parameter cannot be defined at module level." |
| 2787 |
block = """ |
| 2788 |
module m |
| 2789 |
m.func |
| 2790 |
cls: defining_class |
| 2791 |
""" |
| 2792 |
self.expect_failure(block, err, lineno=2) |
| 2793 |
|
| 2794 |
|
| 2795 |
class ClinicExternalTest(TestCase): |
| 2796 |
maxDiff = None |
| 2797 |
|
| 2798 |
def setUp(self): |
| 2799 |
save_restore_converters(self) |
| 2800 |
|
| 2801 |
def run_clinic(self, *args): |
| 2802 |
with ( |
| 2803 |
support.captured_stdout() as out, |
| 2804 |
support.captured_stderr() as err, |
| 2805 |
self.assertRaises(SystemExit) as cm |
| 2806 |
): |
| 2807 |
clinic.main(args) |
| 2808 |
return out.getvalue(), err.getvalue(), cm.exception.code |
| 2809 |
|
| 2810 |
def expect_success(self, *args): |
| 2811 |
out, err, code = self.run_clinic(*args) |
| 2812 |
if code != 0: |
| 2813 |
self.fail("\n".join([f"Unexpected failure: {args=}", out, err])) |
| 2814 |
self.assertEqual(err, "") |
| 2815 |
return out |
| 2816 |
|
| 2817 |
def expect_failure(self, *args): |
| 2818 |
out, err, code = self.run_clinic(*args) |
| 2819 |
self.assertNotEqual(code, 0, f"Unexpected success: {args=}") |
| 2820 |
return out, err |
| 2821 |
|
| 2822 |
def test_external(self): |
| 2823 |
CLINIC_TEST = 'clinic.test.c' |
| 2824 |
source = support.findfile(CLINIC_TEST) |
| 2825 |
with open(source, encoding='utf-8') as f: |
| 2826 |
orig_contents = f.read() |
| 2827 |
|
| 2828 |
# Run clinic CLI and verify that it does not complain. |
| 2829 |
self.addCleanup(unlink, TESTFN) |
| 2830 |
out = self.expect_success("-f", "-o", TESTFN, source) |
| 2831 |
self.assertEqual(out, "") |
| 2832 |
|
| 2833 |
with open(TESTFN, encoding='utf-8') as f: |
| 2834 |
new_contents = f.read() |
| 2835 |
|
| 2836 |
self.assertEqual(new_contents, orig_contents) |
| 2837 |
|
| 2838 |
def test_no_change(self): |
| 2839 |
# bpo-42398: Test that the destination file is left unchanged if the |
| 2840 |
# content does not change. Moreover, check also that the file |
| 2841 |
# modification time does not change in this case. |
| 2842 |
code = dedent(""" |
| 2843 |
/*[clinic input] |
| 2844 |
[clinic start generated code]*/ |
| 2845 |
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=da39a3ee5e6b4b0d]*/ |
| 2846 |
""") |
| 2847 |
with os_helper.temp_dir() as tmp_dir: |
| 2848 |
fn = os.path.join(tmp_dir, "test.c") |
| 2849 |
with open(fn, "w", encoding="utf-8") as f: |
| 2850 |
f.write(code) |
| 2851 |
pre_mtime = os.stat(fn).st_mtime_ns |
| 2852 |
self.expect_success(fn) |
| 2853 |
post_mtime = os.stat(fn).st_mtime_ns |
| 2854 |
# Don't change the file modification time |
| 2855 |
# if the content does not change |
| 2856 |
self.assertEqual(pre_mtime, post_mtime) |
| 2857 |
|
| 2858 |
def test_cli_force(self): |
| 2859 |
invalid_input = dedent(""" |
| 2860 |
/*[clinic input] |
| 2861 |
output preset block |
| 2862 |
module test |
| 2863 |
test.fn |
| 2864 |
a: int |
| 2865 |
[clinic start generated code]*/ |
| 2866 |
|
| 2867 |
const char *hand_edited = "output block is overwritten"; |
| 2868 |
/*[clinic end generated code: output=bogus input=bogus]*/ |
| 2869 |
""") |
| 2870 |
fail_msg = ( |
| 2871 |
"Checksum mismatch! Expected 'bogus', computed '2ed19'. " |
| 2872 |
"Suggested fix: remove all generated code including the end marker, " |
| 2873 |
"or use the '-f' option.\n" |
| 2874 |
) |
| 2875 |
with os_helper.temp_dir() as tmp_dir: |
| 2876 |
fn = os.path.join(tmp_dir, "test.c") |
| 2877 |
with open(fn, "w", encoding="utf-8") as f: |
| 2878 |
f.write(invalid_input) |
| 2879 |
# First, run the CLI without -f and expect failure. |
| 2880 |
# Note, we cannot check the entire fail msg, because the path to |
| 2881 |
# the tmp file will change for every run. |
| 2882 |
_, err = self.expect_failure(fn) |
| 2883 |
self.assertEndsWith(err, fail_msg) |
| 2884 |
# Then, force regeneration; success expected. |
| 2885 |
out = self.expect_success("-f", fn) |
| 2886 |
self.assertEqual(out, "") |
| 2887 |
# Verify by checking the checksum. |
| 2888 |
checksum = ( |
| 2889 |
"/*[clinic end generated code: " |
| 2890 |
"output=a2957bc4d43a3c2f input=9543a8d2da235301]*/\n" |
| 2891 |
) |
| 2892 |
with open(fn, encoding='utf-8') as f: |
| 2893 |
generated = f.read() |
| 2894 |
self.assertEndsWith(generated, checksum) |
| 2895 |
|
| 2896 |
def test_cli_make(self): |
| 2897 |
c_code = dedent(""" |
| 2898 |
/*[clinic input] |
| 2899 |
[clinic start generated code]*/ |
| 2900 |
""") |
| 2901 |
py_code = "pass" |
| 2902 |
c_files = "file1.c", "file2.c" |
| 2903 |
py_files = "file1.py", "file2.py" |
| 2904 |
|
| 2905 |
def create_files(files, srcdir, code): |
| 2906 |
for fn in files: |
| 2907 |
path = os.path.join(srcdir, fn) |
| 2908 |
with open(path, "w", encoding="utf-8") as f: |
| 2909 |
f.write(code) |
| 2910 |
|
| 2911 |
with os_helper.temp_dir() as tmp_dir: |
| 2912 |
# add some folders, some C files and a Python file |
| 2913 |
create_files(c_files, tmp_dir, c_code) |
| 2914 |
create_files(py_files, tmp_dir, py_code) |
| 2915 |
|
| 2916 |
# create C files in externals/ dir |
| 2917 |
ext_path = os.path.join(tmp_dir, "externals") |
| 2918 |
with os_helper.temp_dir(path=ext_path) as externals: |
| 2919 |
create_files(c_files, externals, c_code) |
| 2920 |
|
| 2921 |
# run clinic in verbose mode with --make on tmpdir |
| 2922 |
out = self.expect_success("-v", "--make", "--srcdir", tmp_dir) |
| 2923 |
|
| 2924 |
# expect verbose mode to only mention the C files in tmp_dir |
| 2925 |
for filename in c_files: |
| 2926 |
with self.subTest(filename=filename): |
| 2927 |
path = os.path.join(tmp_dir, filename) |
| 2928 |
self.assertIn(path, out) |
| 2929 |
for filename in py_files: |
| 2930 |
with self.subTest(filename=filename): |
| 2931 |
path = os.path.join(tmp_dir, filename) |
| 2932 |
self.assertNotIn(path, out) |
| 2933 |
# don't expect C files from the externals dir |
| 2934 |
for filename in c_files: |
| 2935 |
with self.subTest(filename=filename): |
| 2936 |
path = os.path.join(ext_path, filename) |
| 2937 |
self.assertNotIn(path, out) |
| 2938 |
|
| 2939 |
def test_cli_make_exclude(self): |
| 2940 |
code = dedent(""" |
| 2941 |
/*[clinic input] |
| 2942 |
[clinic start generated code]*/ |
| 2943 |
""") |
| 2944 |
with os_helper.temp_dir(quiet=False) as tmp_dir: |
| 2945 |
# add some folders, some C files and a Python file |
| 2946 |
for fn in "file1.c", "file2.c", "file3.c", "file4.c": |
| 2947 |
path = os.path.join(tmp_dir, fn) |
| 2948 |
with open(path, "w", encoding="utf-8") as f: |
| 2949 |
f.write(code) |
| 2950 |
|
| 2951 |
# Run clinic in verbose mode with --make on tmpdir. |
| 2952 |
# Exclude file2.c and file3.c. |
| 2953 |
out = self.expect_success( |
| 2954 |
"-v", "--make", "--srcdir", tmp_dir, |
| 2955 |
"--exclude", os.path.join(tmp_dir, "file2.c"), |
| 2956 |
# The added ./ should be normalised away. |
| 2957 |
"--exclude", os.path.join(tmp_dir, "./file3.c"), |
| 2958 |
# Relative paths should also work. |
| 2959 |
"--exclude", "file4.c" |
| 2960 |
) |
| 2961 |
|
| 2962 |
# expect verbose mode to only mention the C files in tmp_dir |
| 2963 |
self.assertIn("file1.c", out) |
| 2964 |
self.assertNotIn("file2.c", out) |
| 2965 |
self.assertNotIn("file3.c", out) |
| 2966 |
self.assertNotIn("file4.c", out) |
| 2967 |
|
| 2968 |
def test_cli_verbose(self): |
| 2969 |
with os_helper.temp_dir() as tmp_dir: |
| 2970 |
fn = os.path.join(tmp_dir, "test.c") |
| 2971 |
with open(fn, "w", encoding="utf-8") as f: |
| 2972 |
f.write("") |
| 2973 |
out = self.expect_success("-v", fn) |
| 2974 |
self.assertEqual(out.strip(), fn) |
| 2975 |
|
| 2976 |
@support.force_not_colorized |
| 2977 |
def test_cli_help(self): |
| 2978 |
out = self.expect_success("-h") |
| 2979 |
self.assertIn("usage: clinic.py", out) |
| 2980 |
|
| 2981 |
def test_cli_converters(self): |
| 2982 |
prelude = dedent(""" |
| 2983 |
Legacy converters: |
| 2984 |
B C D L O S U Y Z Z# |
| 2985 |
b c d f h i l p s s# s* u u# w* y y# y* z z# z* |
| 2986 |
|
| 2987 |
Converters: |
| 2988 |
""") |
| 2989 |
expected_converters = ( |
| 2990 |
"bool", |
| 2991 |
"byte", |
| 2992 |
"char", |
| 2993 |
"defining_class", |
| 2994 |
"double", |
| 2995 |
"fildes", |
| 2996 |
"float", |
| 2997 |
"int", |
| 2998 |
"long", |
| 2999 |
"long_long", |
| 3000 |
"object", |
| 3001 |
"Py_buffer", |
| 3002 |
"Py_complex", |
| 3003 |
"Py_ssize_t", |
| 3004 |
"Py_UNICODE", |
| 3005 |
"PyByteArrayObject", |
| 3006 |
"PyBytesObject", |
| 3007 |
"self", |
| 3008 |
"short", |
| 3009 |
"size_t", |
| 3010 |
"slice_index", |
| 3011 |
"str", |
| 3012 |
"uint16", |
| 3013 |
"uint32", |
| 3014 |
"uint64", |
| 3015 |
"uint8", |
| 3016 |
"unicode", |
| 3017 |
"unicode_fs_decoded", |
| 3018 |
"unicode_fs_encoded", |
| 3019 |
"unsigned_char", |
| 3020 |
"unsigned_int", |
| 3021 |
"unsigned_long", |
| 3022 |
"unsigned_long_long", |
| 3023 |
"unsigned_short", |
| 3024 |
) |
| 3025 |
finale = dedent(""" |
| 3026 |
Return converters: |
| 3027 |
bool() |
| 3028 |
double() |
| 3029 |
float() |
| 3030 |
int() |
| 3031 |
long() |
| 3032 |
object() |
| 3033 |
Py_ssize_t() |
| 3034 |
size_t() |
| 3035 |
unsigned_int() |
| 3036 |
unsigned_long() |
| 3037 |
|
| 3038 |
All converters also accept (c_default=None, py_default=None, annotation=None). |
| 3039 |
All return converters also accept (py_default=None). |
| 3040 |
""") |
| 3041 |
out = self.expect_success("--converters") |
| 3042 |
# We cannot simply compare the output, because the repr of the *accept* |
| 3043 |
# param may change (it's a set, thus unordered). So, let's compare the |
| 3044 |
# start and end of the expected output, and then assert that the |
| 3045 |
# converters appear lined up in alphabetical order. |
| 3046 |
self.assertStartsWith(out, prelude) |
| 3047 |
self.assertEndsWith(out, finale) |
| 3048 |
|
| 3049 |
out = out.removeprefix(prelude) |
| 3050 |
out = out.removesuffix(finale) |
| 3051 |
lines = out.split("\n") |
| 3052 |
for converter, line in zip(expected_converters, lines): |
| 3053 |
line = line.lstrip() |
| 3054 |
with self.subTest(converter=converter): |
| 3055 |
self.assertStartsWith(line, converter) |
| 3056 |
|
| 3057 |
def test_cli_fail_converters_and_filename(self): |
| 3058 |
_, err = self.expect_failure("--converters", "test.c") |
| 3059 |
msg = "can't specify --converters and a filename at the same time" |
| 3060 |
self.assertIn(msg, err) |
| 3061 |
|
| 3062 |
def test_cli_fail_no_filename(self): |
| 3063 |
_, err = self.expect_failure() |
| 3064 |
self.assertIn("no input files", err) |
| 3065 |
|
| 3066 |
def test_cli_fail_output_and_multiple_files(self): |
| 3067 |
_, err = self.expect_failure("-o", "out.c", "input.c", "moreinput.c") |
| 3068 |
msg = "error: can't use -o with multiple filenames" |
| 3069 |
self.assertIn(msg, err) |
| 3070 |
|
| 3071 |
def test_cli_fail_filename_or_output_and_make(self): |
| 3072 |
msg = "can't use -o or filenames with --make" |
| 3073 |
for opts in ("-o", "out.c"), ("filename.c",): |
| 3074 |
with self.subTest(opts=opts): |
| 3075 |
_, err = self.expect_failure("--make", *opts) |
| 3076 |
self.assertIn(msg, err) |
| 3077 |
|
| 3078 |
def test_cli_fail_make_without_srcdir(self): |
| 3079 |
_, err = self.expect_failure("--make", "--srcdir", "") |
| 3080 |
msg = "error: --srcdir must not be empty with --make" |
| 3081 |
self.assertIn(msg, err) |
| 3082 |
|
| 3083 |
def test_file_dest(self): |
| 3084 |
block = dedent(""" |
| 3085 |
/*[clinic input] |
| 3086 |
destination test new file {path}.h |
| 3087 |
output everything test |
| 3088 |
func |
| 3089 |
a: object |
| 3090 |
/ |
| 3091 |
[clinic start generated code]*/ |
| 3092 |
""") |
| 3093 |
expected_checksum_line = ( |
| 3094 |
"/*[clinic end generated code: " |
| 3095 |
"output=da39a3ee5e6b4b0d input=b602ab8e173ac3bd]*/\n" |
| 3096 |
) |
| 3097 |
expected_output = dedent("""\ |
| 3098 |
/*[clinic input] |
| 3099 |
preserve |
| 3100 |
[clinic start generated code]*/ |
| 3101 |
|
| 3102 |
PyDoc_VAR(func__doc__); |
| 3103 |
|
| 3104 |
PyDoc_STRVAR(func__doc__, |
| 3105 |
"func($module, a, /)\\n" |
| 3106 |
"--\\n" |
| 3107 |
"\\n"); |
| 3108 |
|
| 3109 |
#define FUNC_METHODDEF \\ |
| 3110 |
{"func", (PyCFunction)func, METH_O, func__doc__}, |
| 3111 |
|
| 3112 |
static PyObject * |
| 3113 |
func(PyObject *module, PyObject *a) |
| 3114 |
/*[clinic end generated code: output=3dde2d13002165b9 input=a9049054013a1b77]*/ |
| 3115 |
""") |
| 3116 |
with os_helper.temp_dir() as tmp_dir: |
| 3117 |
in_fn = os.path.join(tmp_dir, "test.c") |
| 3118 |
out_fn = os.path.join(tmp_dir, "test.c.h") |
| 3119 |
with open(in_fn, "w", encoding="utf-8") as f: |
| 3120 |
f.write(block) |
| 3121 |
with open(out_fn, "w", encoding="utf-8") as f: |
| 3122 |
f.write("") # Write an empty output file! |
| 3123 |
# Clinic should complain about the empty output file. |
| 3124 |
_, err = self.expect_failure(in_fn) |
| 3125 |
expected_err = (f"Modified destination file {out_fn!r}; " |
| 3126 |
"not overwriting!") |
| 3127 |
self.assertIn(expected_err, err) |
| 3128 |
# Run clinic again, this time with the -f option. |
| 3129 |
_ = self.expect_success("-f", in_fn) |
| 3130 |
# Read back the generated output. |
| 3131 |
with open(in_fn, encoding="utf-8") as f: |
| 3132 |
data = f.read() |
| 3133 |
expected_block = f"{block}{expected_checksum_line}" |
| 3134 |
self.assertEqual(data, expected_block) |
| 3135 |
with open(out_fn, encoding="utf-8") as f: |
| 3136 |
data = f.read() |
| 3137 |
self.assertEqual(data, expected_output) |
| 3138 |
|
| 3139 |
try: |
| 3140 |
import _testclinic as ac_tester |
| 3141 |
except ImportError: |
| 3142 |
ac_tester = None |
| 3143 |
|
| 3144 |
@unittest.skipIf(ac_tester is None, "_testclinic is missing") |
| 3145 |
class ClinicFunctionalTest(unittest.TestCase): |
| 3146 |
locals().update((name, getattr(ac_tester, name)) |
| 3147 |
for name in dir(ac_tester) if name.startswith('test_')) |
| 3148 |
|
| 3149 |
def check_depr(self, regex, fn, /, *args, **kwds): |
| 3150 |
with self.assertWarnsRegex(DeprecationWarning, regex) as cm: |
| 3151 |
# Record the line number, so we're sure we've got the correct stack |
| 3152 |
# level on the deprecation warning. |
| 3153 |
_, lineno = fn(*args, **kwds), sys._getframe().f_lineno |
| 3154 |
self.assertEqual(cm.filename, __file__) |
| 3155 |
self.assertEqual(cm.lineno, lineno) |
| 3156 |
|
| 3157 |
def check_depr_star(self, pnames, fn, /, *args, name=None, **kwds): |
| 3158 |
if name is None: |
| 3159 |
name = fn.__qualname__ |
| 3160 |
if isinstance(fn, type): |
| 3161 |
name = f'{fn.__module__}.{name}' |
| 3162 |
regex = ( |
| 3163 |
fr"Passing( more than)?( [0-9]+)? positional argument(s)? to " |
| 3164 |
fr"{re.escape(name)}\(\) is deprecated. Parameters? {pnames} will " |
| 3165 |
fr"become( a)? keyword-only parameters? in Python 3\.14" |
| 3166 |
) |
| 3167 |
self.check_depr(regex, fn, *args, **kwds) |
| 3168 |
|
| 3169 |
def check_depr_kwd(self, pnames, fn, *args, name=None, **kwds): |
| 3170 |
if name is None: |
| 3171 |
name = fn.__qualname__ |
| 3172 |
if isinstance(fn, type): |
| 3173 |
name = f'{fn.__module__}.{name}' |
| 3174 |
pl = 's' if ' ' in pnames else '' |
| 3175 |
regex = ( |
| 3176 |
fr"Passing keyword argument{pl} {pnames} to " |
| 3177 |
fr"{re.escape(name)}\(\) is deprecated. Parameter{pl} {pnames} " |
| 3178 |
fr"will become positional-only in Python 3\.14." |
| 3179 |
) |
| 3180 |
self.check_depr(regex, fn, *args, **kwds) |
| 3181 |
|
| 3182 |
def test_objects_converter(self): |
| 3183 |
with self.assertRaises(TypeError): |
| 3184 |
ac_tester.objects_converter() |
| 3185 |
self.assertEqual(ac_tester.objects_converter(1, 2), (1, 2)) |
| 3186 |
self.assertEqual(ac_tester.objects_converter([], 'whatever class'), ([], 'whatever class')) |
| 3187 |
self.assertEqual(ac_tester.objects_converter(1), (1, None)) |
| 3188 |
|
| 3189 |
def test_bytes_object_converter(self): |
| 3190 |
with self.assertRaises(TypeError): |
| 3191 |
ac_tester.bytes_object_converter(1) |
| 3192 |
self.assertEqual(ac_tester.bytes_object_converter(b'BytesObject'), (b'BytesObject',)) |
| 3193 |
|
| 3194 |
def test_byte_array_object_converter(self): |
| 3195 |
with self.assertRaises(TypeError): |
| 3196 |
ac_tester.byte_array_object_converter(1) |
| 3197 |
byte_arr = bytearray(b'ByteArrayObject') |
| 3198 |
self.assertEqual(ac_tester.byte_array_object_converter(byte_arr), (byte_arr,)) |
| 3199 |
|
| 3200 |
def test_unicode_converter(self): |
| 3201 |
with self.assertRaises(TypeError): |
| 3202 |
ac_tester.unicode_converter(1) |
| 3203 |
self.assertEqual(ac_tester.unicode_converter('unicode'), ('unicode',)) |
| 3204 |
|
| 3205 |
def test_bool_converter(self): |
| 3206 |
with self.assertRaises(TypeError): |
| 3207 |
ac_tester.bool_converter(False, False, 'not a int') |
| 3208 |
self.assertEqual(ac_tester.bool_converter(), (True, True, True)) |
| 3209 |
self.assertEqual(ac_tester.bool_converter('', [], 5), (False, False, True)) |
| 3210 |
self.assertEqual(ac_tester.bool_converter(('not empty',), {1: 2}, 0), (True, True, False)) |
| 3211 |
|
| 3212 |
def test_bool_converter_c_default(self): |
| 3213 |
self.assertEqual(ac_tester.bool_converter_c_default(), (1, 0, -2, -3)) |
| 3214 |
self.assertEqual(ac_tester.bool_converter_c_default(False, True, False, True), |
| 3215 |
(0, 1, 0, 1)) |
| 3216 |
|
| 3217 |
def test_char_converter(self): |
| 3218 |
with self.assertRaises(TypeError): |
| 3219 |
ac_tester.char_converter(1) |
| 3220 |
with self.assertRaises(TypeError): |
| 3221 |
ac_tester.char_converter(b'ab') |
| 3222 |
chars = [b'A', b'\a', b'\b', b'\t', b'\n', b'\v', b'\f', b'\r', b'"', b"'", b'?', b'\\', b'\000', b'\377'] |
| 3223 |
expected = tuple(ord(c) for c in chars) |
| 3224 |
self.assertEqual(ac_tester.char_converter(), expected) |
| 3225 |
chars = [b'1', b'2', b'3', b'4', b'5', b'6', b'7', b'8', b'9', b'0', b'a', b'b', b'c', b'd'] |
| 3226 |
expected = tuple(ord(c) for c in chars) |
| 3227 |
self.assertEqual(ac_tester.char_converter(*chars), expected) |
| 3228 |
|
| 3229 |
def test_unsigned_char_converter(self): |
| 3230 |
from _testcapi import UCHAR_MAX |
| 3231 |
with self.assertRaises(OverflowError): |
| 3232 |
ac_tester.unsigned_char_converter(-1) |
| 3233 |
with self.assertRaises(OverflowError): |
| 3234 |
ac_tester.unsigned_char_converter(UCHAR_MAX + 1) |
| 3235 |
with self.assertRaises(OverflowError): |
| 3236 |
ac_tester.unsigned_char_converter(0, UCHAR_MAX + 1) |
| 3237 |
with self.assertRaises(TypeError): |
| 3238 |
ac_tester.unsigned_char_converter([]) |
| 3239 |
self.assertEqual(ac_tester.unsigned_char_converter(), (12, 34, 56)) |
| 3240 |
self.assertEqual(ac_tester.unsigned_char_converter(0, 0, UCHAR_MAX + 1), (0, 0, 0)) |
| 3241 |
self.assertEqual(ac_tester.unsigned_char_converter(0, 0, (UCHAR_MAX + 1) * 3 + 123), (0, 0, 123)) |
| 3242 |
|
| 3243 |
def test_short_converter(self): |
| 3244 |
from _testcapi import SHRT_MIN, SHRT_MAX |
| 3245 |
with self.assertRaises(OverflowError): |
| 3246 |
ac_tester.short_converter(SHRT_MIN - 1) |
| 3247 |
with self.assertRaises(OverflowError): |
| 3248 |
ac_tester.short_converter(SHRT_MAX + 1) |
| 3249 |
with self.assertRaises(TypeError): |
| 3250 |
ac_tester.short_converter([]) |
| 3251 |
self.assertEqual(ac_tester.short_converter(-1234), (-1234,)) |
| 3252 |
self.assertEqual(ac_tester.short_converter(4321), (4321,)) |
| 3253 |
|
| 3254 |
def test_unsigned_short_converter(self): |
| 3255 |
from _testcapi import USHRT_MAX |
| 3256 |
with self.assertRaises(ValueError): |
| 3257 |
ac_tester.unsigned_short_converter(-1) |
| 3258 |
with self.assertRaises(OverflowError): |
| 3259 |
ac_tester.unsigned_short_converter(USHRT_MAX + 1) |
| 3260 |
with self.assertRaises(OverflowError): |
| 3261 |
ac_tester.unsigned_short_converter(0, USHRT_MAX + 1) |
| 3262 |
with self.assertRaises(TypeError): |
| 3263 |
ac_tester.unsigned_short_converter([]) |
| 3264 |
self.assertEqual(ac_tester.unsigned_short_converter(), (12, 34, 56)) |
| 3265 |
self.assertEqual(ac_tester.unsigned_short_converter(0, 0, USHRT_MAX + 1), (0, 0, 0)) |
| 3266 |
self.assertEqual(ac_tester.unsigned_short_converter(0, 0, (USHRT_MAX + 1) * 3 + 123), (0, 0, 123)) |
| 3267 |
|
| 3268 |
def test_int_converter(self): |
| 3269 |
from _testcapi import INT_MIN, INT_MAX |
| 3270 |
with self.assertRaises(OverflowError): |
| 3271 |
ac_tester.int_converter(INT_MIN - 1) |
| 3272 |
with self.assertRaises(OverflowError): |
| 3273 |
ac_tester.int_converter(INT_MAX + 1) |
| 3274 |
with self.assertRaises(TypeError): |
| 3275 |
ac_tester.int_converter(1, 2, 3) |
| 3276 |
with self.assertRaises(TypeError): |
| 3277 |
ac_tester.int_converter([]) |
| 3278 |
self.assertEqual(ac_tester.int_converter(), (12, 34, 45)) |
| 3279 |
self.assertEqual(ac_tester.int_converter(1, 2, '3'), (1, 2, ord('3'))) |
| 3280 |
|
| 3281 |
def test_unsigned_int_converter(self): |
| 3282 |
from _testcapi import UINT_MAX |
| 3283 |
with self.assertRaises(ValueError): |
| 3284 |
ac_tester.unsigned_int_converter(-1) |
| 3285 |
with self.assertRaises(OverflowError): |
| 3286 |
ac_tester.unsigned_int_converter(UINT_MAX + 1) |
| 3287 |
with self.assertRaises(OverflowError): |
| 3288 |
ac_tester.unsigned_int_converter(0, UINT_MAX + 1) |
| 3289 |
with self.assertRaises(TypeError): |
| 3290 |
ac_tester.unsigned_int_converter([]) |
| 3291 |
self.assertEqual(ac_tester.unsigned_int_converter(), (12, 34, 56)) |
| 3292 |
self.assertEqual(ac_tester.unsigned_int_converter(0, 0, UINT_MAX + 1), (0, 0, 0)) |
| 3293 |
self.assertEqual(ac_tester.unsigned_int_converter(0, 0, (UINT_MAX + 1) * 3 + 123), (0, 0, 123)) |
| 3294 |
|
| 3295 |
def test_long_converter(self): |
| 3296 |
from _testcapi import LONG_MIN, LONG_MAX |
| 3297 |
with self.assertRaises(OverflowError): |
| 3298 |
ac_tester.long_converter(LONG_MIN - 1) |
| 3299 |
with self.assertRaises(OverflowError): |
| 3300 |
ac_tester.long_converter(LONG_MAX + 1) |
| 3301 |
with self.assertRaises(TypeError): |
| 3302 |
ac_tester.long_converter([]) |
| 3303 |
self.assertEqual(ac_tester.long_converter(), (12,)) |
| 3304 |
self.assertEqual(ac_tester.long_converter(-1234), (-1234,)) |
| 3305 |
|
| 3306 |
def test_unsigned_long_converter(self): |
| 3307 |
from _testcapi import ULONG_MAX |
| 3308 |
with self.assertRaises(ValueError): |
| 3309 |
ac_tester.unsigned_long_converter(-1) |
| 3310 |
with self.assertRaises(OverflowError): |
| 3311 |
ac_tester.unsigned_long_converter(ULONG_MAX + 1) |
| 3312 |
with self.assertRaises(OverflowError): |
| 3313 |
ac_tester.unsigned_long_converter(0, ULONG_MAX + 1) |
| 3314 |
with self.assertRaises(TypeError): |
| 3315 |
ac_tester.unsigned_long_converter([]) |
| 3316 |
self.assertEqual(ac_tester.unsigned_long_converter(), (12, 34, 56)) |
| 3317 |
self.assertEqual(ac_tester.unsigned_long_converter(0, 0, ULONG_MAX + 1), (0, 0, 0)) |
| 3318 |
self.assertEqual(ac_tester.unsigned_long_converter(0, 0, (ULONG_MAX + 1) * 3 + 123), (0, 0, 123)) |
| 3319 |
|
| 3320 |
def test_long_long_converter(self): |
| 3321 |
from _testcapi import LLONG_MIN, LLONG_MAX |
| 3322 |
with self.assertRaises(OverflowError): |
| 3323 |
ac_tester.long_long_converter(LLONG_MIN - 1) |
| 3324 |
with self.assertRaises(OverflowError): |
| 3325 |
ac_tester.long_long_converter(LLONG_MAX + 1) |
| 3326 |
with self.assertRaises(TypeError): |
| 3327 |
ac_tester.long_long_converter([]) |
| 3328 |
self.assertEqual(ac_tester.long_long_converter(), (12,)) |
| 3329 |
self.assertEqual(ac_tester.long_long_converter(-1234), (-1234,)) |
| 3330 |
|
| 3331 |
def test_unsigned_long_long_converter(self): |
| 3332 |
from _testcapi import ULLONG_MAX |
| 3333 |
with self.assertRaises(ValueError): |
| 3334 |
ac_tester.unsigned_long_long_converter(-1) |
| 3335 |
with self.assertRaises(OverflowError): |
| 3336 |
ac_tester.unsigned_long_long_converter(ULLONG_MAX + 1) |
| 3337 |
with self.assertRaises(OverflowError): |
| 3338 |
ac_tester.unsigned_long_long_converter(0, ULLONG_MAX + 1) |
| 3339 |
with self.assertRaises(TypeError): |
| 3340 |
ac_tester.unsigned_long_long_converter([]) |
| 3341 |
self.assertEqual(ac_tester.unsigned_long_long_converter(), (12, 34, 56)) |
| 3342 |
self.assertEqual(ac_tester.unsigned_long_long_converter(0, 0, ULLONG_MAX + 1), (0, 0, 0)) |
| 3343 |
self.assertEqual(ac_tester.unsigned_long_long_converter(0, 0, (ULLONG_MAX + 1) * 3 + 123), (0, 0, 123)) |
| 3344 |
|
| 3345 |
def test_py_ssize_t_converter(self): |
| 3346 |
from _testcapi import PY_SSIZE_T_MIN, PY_SSIZE_T_MAX |
| 3347 |
with self.assertRaises(OverflowError): |
| 3348 |
ac_tester.py_ssize_t_converter(PY_SSIZE_T_MIN - 1) |
| 3349 |
with self.assertRaises(OverflowError): |
| 3350 |
ac_tester.py_ssize_t_converter(PY_SSIZE_T_MAX + 1) |
| 3351 |
with self.assertRaises(TypeError): |
| 3352 |
ac_tester.py_ssize_t_converter([]) |
| 3353 |
self.assertEqual(ac_tester.py_ssize_t_converter(), (12, 34, 56)) |
| 3354 |
self.assertEqual(ac_tester.py_ssize_t_converter(1, 2, None), (1, 2, 56)) |
| 3355 |
|
| 3356 |
def test_slice_index_converter(self): |
| 3357 |
from _testcapi import PY_SSIZE_T_MIN, PY_SSIZE_T_MAX |
| 3358 |
with self.assertRaises(TypeError): |
| 3359 |
ac_tester.slice_index_converter([]) |
| 3360 |
self.assertEqual(ac_tester.slice_index_converter(), (12, 34, 56)) |
| 3361 |
self.assertEqual(ac_tester.slice_index_converter(1, 2, None), (1, 2, 56)) |
| 3362 |
self.assertEqual(ac_tester.slice_index_converter(PY_SSIZE_T_MAX, PY_SSIZE_T_MAX + 1, PY_SSIZE_T_MAX + 1234), |
| 3363 |
(PY_SSIZE_T_MAX, PY_SSIZE_T_MAX, PY_SSIZE_T_MAX)) |
| 3364 |
self.assertEqual(ac_tester.slice_index_converter(PY_SSIZE_T_MIN, PY_SSIZE_T_MIN - 1, PY_SSIZE_T_MIN - 1234), |
| 3365 |
(PY_SSIZE_T_MIN, PY_SSIZE_T_MIN, PY_SSIZE_T_MIN)) |
| 3366 |
|
| 3367 |
def test_size_t_converter(self): |
| 3368 |
with self.assertRaises(ValueError): |
| 3369 |
ac_tester.size_t_converter(-1) |
| 3370 |
with self.assertRaises(TypeError): |
| 3371 |
ac_tester.size_t_converter([]) |
| 3372 |
self.assertEqual(ac_tester.size_t_converter(), (12,)) |
| 3373 |
|
| 3374 |
def test_float_converter(self): |
| 3375 |
with self.assertRaises(TypeError): |
| 3376 |
ac_tester.float_converter([]) |
| 3377 |
self.assertEqual(ac_tester.float_converter(), (12.5,)) |
| 3378 |
self.assertEqual(ac_tester.float_converter(-0.5), (-0.5,)) |
| 3379 |
|
| 3380 |
def test_double_converter(self): |
| 3381 |
with self.assertRaises(TypeError): |
| 3382 |
ac_tester.double_converter([]) |
| 3383 |
self.assertEqual(ac_tester.double_converter(), (12.5,)) |
| 3384 |
self.assertEqual(ac_tester.double_converter(-0.5), (-0.5,)) |
| 3385 |
|
| 3386 |
def test_py_complex_converter(self): |
| 3387 |
with self.assertRaises(TypeError): |
| 3388 |
ac_tester.py_complex_converter([]) |
| 3389 |
self.assertEqual(ac_tester.py_complex_converter(complex(1, 2)), (complex(1, 2),)) |
| 3390 |
self.assertEqual(ac_tester.py_complex_converter(complex('-1-2j')), (complex('-1-2j'),)) |
| 3391 |
self.assertEqual(ac_tester.py_complex_converter(-0.5), (-0.5,)) |
| 3392 |
self.assertEqual(ac_tester.py_complex_converter(10), (10,)) |
| 3393 |
|
| 3394 |
def test_str_converter(self): |
| 3395 |
with self.assertRaises(TypeError): |
| 3396 |
ac_tester.str_converter(1) |
| 3397 |
with self.assertRaises(TypeError): |
| 3398 |
ac_tester.str_converter('a', 'b', 'c') |
| 3399 |
with self.assertRaises(ValueError): |
| 3400 |
ac_tester.str_converter('a', b'b\0b', 'c') |
| 3401 |
self.assertEqual(ac_tester.str_converter('a', b'b', 'c'), ('a', 'b', 'c')) |
| 3402 |
self.assertEqual(ac_tester.str_converter('a', b'b', b'c'), ('a', 'b', 'c')) |
| 3403 |
self.assertEqual(ac_tester.str_converter('a', b'b', 'c\0c'), ('a', 'b', 'c\0c')) |
| 3404 |
|
| 3405 |
def test_str_converter_encoding(self): |
| 3406 |
with self.assertRaises(TypeError): |
| 3407 |
ac_tester.str_converter_encoding(1) |
| 3408 |
self.assertEqual(ac_tester.str_converter_encoding('a', 'b', 'c'), ('a', 'b', 'c')) |
| 3409 |
with self.assertRaises(TypeError): |
| 3410 |
ac_tester.str_converter_encoding('a', b'b\0b', 'c') |
| 3411 |
self.assertEqual(ac_tester.str_converter_encoding('a', b'b', bytearray([ord('c')])), ('a', 'b', 'c')) |
| 3412 |
self.assertEqual(ac_tester.str_converter_encoding('a', b'b', bytearray([ord('c'), 0, ord('c')])), |
| 3413 |
('a', 'b', 'c\x00c')) |
| 3414 |
self.assertEqual(ac_tester.str_converter_encoding('a', b'b', b'c\x00c'), ('a', 'b', 'c\x00c')) |
| 3415 |
|
| 3416 |
def test_py_buffer_converter(self): |
| 3417 |
with self.assertRaises(TypeError): |
| 3418 |
ac_tester.py_buffer_converter('a', 'b') |
| 3419 |
self.assertEqual(ac_tester.py_buffer_converter('abc', bytearray([1, 2, 3])), (b'abc', b'\x01\x02\x03')) |
| 3420 |
|
| 3421 |
def test_keywords(self): |
| 3422 |
self.assertEqual(ac_tester.keywords(1, 2), (1, 2)) |
| 3423 |
self.assertEqual(ac_tester.keywords(1, b=2), (1, 2)) |
| 3424 |
self.assertEqual(ac_tester.keywords(a=1, b=2), (1, 2)) |
| 3425 |
|
| 3426 |
def test_keywords_kwonly(self): |
| 3427 |
with self.assertRaises(TypeError): |
| 3428 |
ac_tester.keywords_kwonly(1, 2) |
| 3429 |
self.assertEqual(ac_tester.keywords_kwonly(1, b=2), (1, 2)) |
| 3430 |
self.assertEqual(ac_tester.keywords_kwonly(a=1, b=2), (1, 2)) |
| 3431 |
|
| 3432 |
def test_keywords_opt(self): |
| 3433 |
self.assertEqual(ac_tester.keywords_opt(1), (1, None, None)) |
| 3434 |
self.assertEqual(ac_tester.keywords_opt(1, 2), (1, 2, None)) |
| 3435 |
self.assertEqual(ac_tester.keywords_opt(1, 2, 3), (1, 2, 3)) |
| 3436 |
self.assertEqual(ac_tester.keywords_opt(1, b=2), (1, 2, None)) |
| 3437 |
self.assertEqual(ac_tester.keywords_opt(1, 2, c=3), (1, 2, 3)) |
| 3438 |
self.assertEqual(ac_tester.keywords_opt(a=1, c=3), (1, None, 3)) |
| 3439 |
self.assertEqual(ac_tester.keywords_opt(a=1, b=2, c=3), (1, 2, 3)) |
| 3440 |
|
| 3441 |
def test_keywords_opt_kwonly(self): |
| 3442 |
self.assertEqual(ac_tester.keywords_opt_kwonly(1), (1, None, None, None)) |
| 3443 |
self.assertEqual(ac_tester.keywords_opt_kwonly(1, 2), (1, 2, None, None)) |
| 3444 |
with self.assertRaises(TypeError): |
| 3445 |
ac_tester.keywords_opt_kwonly(1, 2, 3) |
| 3446 |
self.assertEqual(ac_tester.keywords_opt_kwonly(1, b=2), (1, 2, None, None)) |
| 3447 |
self.assertEqual(ac_tester.keywords_opt_kwonly(1, 2, c=3), (1, 2, 3, None)) |
| 3448 |
self.assertEqual(ac_tester.keywords_opt_kwonly(a=1, c=3), (1, None, 3, None)) |
| 3449 |
self.assertEqual(ac_tester.keywords_opt_kwonly(a=1, b=2, c=3, d=4), (1, 2, 3, 4)) |
| 3450 |
|
| 3451 |
def test_keywords_kwonly_opt(self): |
| 3452 |
self.assertEqual(ac_tester.keywords_kwonly_opt(1), (1, None, None)) |
| 3453 |
with self.assertRaises(TypeError): |
| 3454 |
ac_tester.keywords_kwonly_opt(1, 2) |
| 3455 |
self.assertEqual(ac_tester.keywords_kwonly_opt(1, b=2), (1, 2, None)) |
| 3456 |
self.assertEqual(ac_tester.keywords_kwonly_opt(a=1, c=3), (1, None, 3)) |
| 3457 |
self.assertEqual(ac_tester.keywords_kwonly_opt(a=1, b=2, c=3), (1, 2, 3)) |
| 3458 |
|
| 3459 |
def test_posonly_keywords(self): |
| 3460 |
with self.assertRaises(TypeError): |
| 3461 |
ac_tester.posonly_keywords(1) |
| 3462 |
with self.assertRaises(TypeError): |
| 3463 |
ac_tester.posonly_keywords(a=1, b=2) |
| 3464 |
self.assertEqual(ac_tester.posonly_keywords(1, 2), (1, 2)) |
| 3465 |
self.assertEqual(ac_tester.posonly_keywords(1, b=2), (1, 2)) |
| 3466 |
|
| 3467 |
def test_posonly_kwonly(self): |
| 3468 |
with self.assertRaises(TypeError): |
| 3469 |
ac_tester.posonly_kwonly(1) |
| 3470 |
with self.assertRaises(TypeError): |
| 3471 |
ac_tester.posonly_kwonly(1, 2) |
| 3472 |
with self.assertRaises(TypeError): |
| 3473 |
ac_tester.posonly_kwonly(a=1, b=2) |
| 3474 |
self.assertEqual(ac_tester.posonly_kwonly(1, b=2), (1, 2)) |
| 3475 |
|
| 3476 |
def test_posonly_keywords_kwonly(self): |
| 3477 |
with self.assertRaises(TypeError): |
| 3478 |
ac_tester.posonly_keywords_kwonly(1) |
| 3479 |
with self.assertRaises(TypeError): |
| 3480 |
ac_tester.posonly_keywords_kwonly(1, 2, 3) |
| 3481 |
with self.assertRaises(TypeError): |
| 3482 |
ac_tester.posonly_keywords_kwonly(a=1, b=2, c=3) |
| 3483 |
self.assertEqual(ac_tester.posonly_keywords_kwonly(1, 2, c=3), (1, 2, 3)) |
| 3484 |
self.assertEqual(ac_tester.posonly_keywords_kwonly(1, b=2, c=3), (1, 2, 3)) |
| 3485 |
|
| 3486 |
def test_posonly_keywords_opt(self): |
| 3487 |
with self.assertRaises(TypeError): |
| 3488 |
ac_tester.posonly_keywords_opt(1) |
| 3489 |
self.assertEqual(ac_tester.posonly_keywords_opt(1, 2), (1, 2, None, None)) |
| 3490 |
self.assertEqual(ac_tester.posonly_keywords_opt(1, 2, 3), (1, 2, 3, None)) |
| 3491 |
self.assertEqual(ac_tester.posonly_keywords_opt(1, 2, 3, 4), (1, 2, 3, 4)) |
| 3492 |
self.assertEqual(ac_tester.posonly_keywords_opt(1, b=2), (1, 2, None, None)) |
| 3493 |
self.assertEqual(ac_tester.posonly_keywords_opt(1, 2, c=3), (1, 2, 3, None)) |
| 3494 |
with self.assertRaises(TypeError): |
| 3495 |
ac_tester.posonly_keywords_opt(a=1, b=2, c=3, d=4) |
| 3496 |
self.assertEqual(ac_tester.posonly_keywords_opt(1, b=2, c=3, d=4), (1, 2, 3, 4)) |
| 3497 |
|
| 3498 |
def test_posonly_opt_keywords_opt(self): |
| 3499 |
self.assertEqual(ac_tester.posonly_opt_keywords_opt(1), (1, None, None, None)) |
| 3500 |
self.assertEqual(ac_tester.posonly_opt_keywords_opt(1, 2), (1, 2, None, None)) |
| 3501 |
self.assertEqual(ac_tester.posonly_opt_keywords_opt(1, 2, 3), (1, 2, 3, None)) |
| 3502 |
self.assertEqual(ac_tester.posonly_opt_keywords_opt(1, 2, 3, 4), (1, 2, 3, 4)) |
| 3503 |
with self.assertRaises(TypeError): |
| 3504 |
ac_tester.posonly_opt_keywords_opt(1, b=2) |
| 3505 |
self.assertEqual(ac_tester.posonly_opt_keywords_opt(1, 2, c=3), (1, 2, 3, None)) |
| 3506 |
self.assertEqual(ac_tester.posonly_opt_keywords_opt(1, 2, c=3, d=4), (1, 2, 3, 4)) |
| 3507 |
with self.assertRaises(TypeError): |
| 3508 |
ac_tester.posonly_opt_keywords_opt(a=1, b=2, c=3, d=4) |
| 3509 |
|
| 3510 |
def test_posonly_kwonly_opt(self): |
| 3511 |
with self.assertRaises(TypeError): |
| 3512 |
ac_tester.posonly_kwonly_opt(1) |
| 3513 |
with self.assertRaises(TypeError): |
| 3514 |
ac_tester.posonly_kwonly_opt(1, 2) |
| 3515 |
self.assertEqual(ac_tester.posonly_kwonly_opt(1, b=2), (1, 2, None, None)) |
| 3516 |
self.assertEqual(ac_tester.posonly_kwonly_opt(1, b=2, c=3), (1, 2, 3, None)) |
| 3517 |
self.assertEqual(ac_tester.posonly_kwonly_opt(1, b=2, c=3, d=4), (1, 2, 3, 4)) |
| 3518 |
with self.assertRaises(TypeError): |
| 3519 |
ac_tester.posonly_kwonly_opt(a=1, b=2, c=3, d=4) |
| 3520 |
|
| 3521 |
def test_posonly_opt_kwonly_opt(self): |
| 3522 |
self.assertEqual(ac_tester.posonly_opt_kwonly_opt(1), (1, None, None, None)) |
| 3523 |
self.assertEqual(ac_tester.posonly_opt_kwonly_opt(1, 2), (1, 2, None, None)) |
| 3524 |
with self.assertRaises(TypeError): |
| 3525 |
ac_tester.posonly_opt_kwonly_opt(1, 2, 3) |
| 3526 |
with self.assertRaises(TypeError): |
| 3527 |
ac_tester.posonly_opt_kwonly_opt(1, b=2) |
| 3528 |
self.assertEqual(ac_tester.posonly_opt_kwonly_opt(1, 2, c=3), (1, 2, 3, None)) |
| 3529 |
self.assertEqual(ac_tester.posonly_opt_kwonly_opt(1, 2, c=3, d=4), (1, 2, 3, 4)) |
| 3530 |
|
| 3531 |
def test_posonly_keywords_kwonly_opt(self): |
| 3532 |
with self.assertRaises(TypeError): |
| 3533 |
ac_tester.posonly_keywords_kwonly_opt(1) |
| 3534 |
with self.assertRaises(TypeError): |
| 3535 |
ac_tester.posonly_keywords_kwonly_opt(1, 2) |
| 3536 |
with self.assertRaises(TypeError): |
| 3537 |
ac_tester.posonly_keywords_kwonly_opt(1, b=2) |
| 3538 |
with self.assertRaises(TypeError): |
| 3539 |
ac_tester.posonly_keywords_kwonly_opt(1, 2, 3) |
| 3540 |
with self.assertRaises(TypeError): |
| 3541 |
ac_tester.posonly_keywords_kwonly_opt(a=1, b=2, c=3) |
| 3542 |
self.assertEqual(ac_tester.posonly_keywords_kwonly_opt(1, 2, c=3), (1, 2, 3, None, None)) |
| 3543 |
self.assertEqual(ac_tester.posonly_keywords_kwonly_opt(1, b=2, c=3), (1, 2, 3, None, None)) |
| 3544 |
self.assertEqual(ac_tester.posonly_keywords_kwonly_opt(1, 2, c=3, d=4), (1, 2, 3, 4, None)) |
| 3545 |
self.assertEqual(ac_tester.posonly_keywords_kwonly_opt(1, 2, c=3, d=4, e=5), (1, 2, 3, 4, 5)) |
| 3546 |
|
| 3547 |
def test_posonly_keywords_opt_kwonly_opt(self): |
| 3548 |
with self.assertRaises(TypeError): |
| 3549 |
ac_tester.posonly_keywords_opt_kwonly_opt(1) |
| 3550 |
self.assertEqual(ac_tester.posonly_keywords_opt_kwonly_opt(1, 2), (1, 2, None, None, None)) |
| 3551 |
self.assertEqual(ac_tester.posonly_keywords_opt_kwonly_opt(1, b=2), (1, 2, None, None, None)) |
| 3552 |
with self.assertRaises(TypeError): |
| 3553 |
ac_tester.posonly_keywords_opt_kwonly_opt(1, 2, 3, 4) |
| 3554 |
with self.assertRaises(TypeError): |
| 3555 |
ac_tester.posonly_keywords_opt_kwonly_opt(a=1, b=2) |
| 3556 |
self.assertEqual(ac_tester.posonly_keywords_opt_kwonly_opt(1, 2, c=3), (1, 2, 3, None, None)) |
| 3557 |
self.assertEqual(ac_tester.posonly_keywords_opt_kwonly_opt(1, b=2, c=3), (1, 2, 3, None, None)) |
| 3558 |
self.assertEqual(ac_tester.posonly_keywords_opt_kwonly_opt(1, 2, 3, d=4), (1, 2, 3, 4, None)) |
| 3559 |
self.assertEqual(ac_tester.posonly_keywords_opt_kwonly_opt(1, 2, c=3, d=4), (1, 2, 3, 4, None)) |
| 3560 |
self.assertEqual(ac_tester.posonly_keywords_opt_kwonly_opt(1, 2, 3, d=4, e=5), (1, 2, 3, 4, 5)) |
| 3561 |
self.assertEqual(ac_tester.posonly_keywords_opt_kwonly_opt(1, 2, c=3, d=4, e=5), (1, 2, 3, 4, 5)) |
| 3562 |
|
| 3563 |
def test_posonly_opt_keywords_opt_kwonly_opt(self): |
| 3564 |
self.assertEqual(ac_tester.posonly_opt_keywords_opt_kwonly_opt(1), (1, None, None, None)) |
| 3565 |
self.assertEqual(ac_tester.posonly_opt_keywords_opt_kwonly_opt(1, 2), (1, 2, None, None)) |
| 3566 |
with self.assertRaises(TypeError): |
| 3567 |
ac_tester.posonly_opt_keywords_opt_kwonly_opt(1, b=2) |
| 3568 |
self.assertEqual(ac_tester.posonly_opt_keywords_opt_kwonly_opt(1, 2, 3), (1, 2, 3, None)) |
| 3569 |
self.assertEqual(ac_tester.posonly_opt_keywords_opt_kwonly_opt(1, 2, c=3), (1, 2, 3, None)) |
| 3570 |
self.assertEqual(ac_tester.posonly_opt_keywords_opt_kwonly_opt(1, 2, 3, d=4), (1, 2, 3, 4)) |
| 3571 |
self.assertEqual(ac_tester.posonly_opt_keywords_opt_kwonly_opt(1, 2, c=3, d=4), (1, 2, 3, 4)) |
| 3572 |
with self.assertRaises(TypeError): |
| 3573 |
ac_tester.posonly_opt_keywords_opt_kwonly_opt(1, 2, 3, 4) |
| 3574 |
|
| 3575 |
def test_keyword_only_parameter(self): |
| 3576 |
with self.assertRaises(TypeError): |
| 3577 |
ac_tester.keyword_only_parameter() |
| 3578 |
with self.assertRaises(TypeError): |
| 3579 |
ac_tester.keyword_only_parameter(1) |
| 3580 |
self.assertEqual(ac_tester.keyword_only_parameter(a=1), (1,)) |
| 3581 |
|
| 3582 |
if ac_tester is not None: |
| 3583 |
@repeat_fn(ac_tester.varpos, |
| 3584 |
ac_tester.varpos_array, |
| 3585 |
ac_tester.TestClass.varpos_no_fastcall, |
| 3586 |
ac_tester.TestClass.varpos_array_no_fastcall) |
| 3587 |
def test_varpos(self, fn): |
| 3588 |
# fn(*args) |
| 3589 |
self.assertEqual(fn(), ()) |
| 3590 |
self.assertEqual(fn(1, 2), (1, 2)) |
| 3591 |
|
| 3592 |
@repeat_fn(ac_tester.posonly_varpos, |
| 3593 |
ac_tester.posonly_varpos_array, |
| 3594 |
ac_tester.TestClass.posonly_varpos_no_fastcall, |
| 3595 |
ac_tester.TestClass.posonly_varpos_array_no_fastcall) |
| 3596 |
def test_posonly_varpos(self, fn): |
| 3597 |
# fn(a, b, /, *args) |
| 3598 |
self.assertRaises(TypeError, fn) |
| 3599 |
self.assertRaises(TypeError, fn, 1) |
| 3600 |
self.assertRaises(TypeError, fn, 1, b=2) |
| 3601 |
self.assertEqual(fn(1, 2), (1, 2, ())) |
| 3602 |
self.assertEqual(fn(1, 2, 3, 4), (1, 2, (3, 4))) |
| 3603 |
|
| 3604 |
@repeat_fn(ac_tester.posonly_req_opt_varpos, |
| 3605 |
ac_tester.posonly_req_opt_varpos_array, |
| 3606 |
ac_tester.TestClass.posonly_req_opt_varpos_no_fastcall, |
| 3607 |
ac_tester.TestClass.posonly_req_opt_varpos_array_no_fastcall) |
| 3608 |
def test_posonly_req_opt_varpos(self, fn): |
| 3609 |
# fn(a, b=False, /, *args) |
| 3610 |
self.assertRaises(TypeError, fn) |
| 3611 |
self.assertRaises(TypeError, fn, a=1) |
| 3612 |
self.assertEqual(fn(1), (1, False, ())) |
| 3613 |
self.assertEqual(fn(1, 2), (1, 2, ())) |
| 3614 |
self.assertEqual(fn(1, 2, 3, 4), (1, 2, (3, 4))) |
| 3615 |
|
| 3616 |
@repeat_fn(ac_tester.posonly_poskw_varpos, |
| 3617 |
ac_tester.posonly_poskw_varpos_array, |
| 3618 |
ac_tester.TestClass.posonly_poskw_varpos_no_fastcall, |
| 3619 |
ac_tester.TestClass.posonly_poskw_varpos_array_no_fastcall) |
| 3620 |
def test_posonly_poskw_varpos(self, fn): |
| 3621 |
# fn(a, /, b, *args) |
| 3622 |
self.assertRaises(TypeError, fn) |
| 3623 |
self.assertEqual(fn(1, 2), (1, 2, ())) |
| 3624 |
self.assertEqual(fn(1, b=2), (1, 2, ())) |
| 3625 |
self.assertEqual(fn(1, 2, 3, 4), (1, 2, (3, 4))) |
| 3626 |
self.assertRaises(TypeError, fn, b=4) |
| 3627 |
errmsg = re.escape("given by name ('b') and position (2)") |
| 3628 |
self.assertRaisesRegex(TypeError, errmsg, fn, 1, 2, 3, b=4) |
| 3629 |
|
| 3630 |
def test_poskw_varpos(self): |
| 3631 |
# fn(a, *args) |
| 3632 |
fn = ac_tester.poskw_varpos |
| 3633 |
self.assertRaises(TypeError, fn) |
| 3634 |
self.assertRaises(TypeError, fn, 1, b=2) |
| 3635 |
self.assertEqual(fn(a=1), (1, ())) |
| 3636 |
errmsg = re.escape("given by name ('a') and position (1)") |
| 3637 |
self.assertRaisesRegex(TypeError, errmsg, fn, 1, a=2) |
| 3638 |
self.assertEqual(fn(1), (1, ())) |
| 3639 |
self.assertEqual(fn(1, 2, 3, 4), (1, (2, 3, 4))) |
| 3640 |
|
| 3641 |
def test_poskw_varpos_kwonly_opt(self): |
| 3642 |
# fn(a, *args, b=False) |
| 3643 |
fn = ac_tester.poskw_varpos_kwonly_opt |
| 3644 |
self.assertRaises(TypeError, fn) |
| 3645 |
errmsg = re.escape("given by name ('a') and position (1)") |
| 3646 |
self.assertRaisesRegex(TypeError, errmsg, fn, 1, a=2) |
| 3647 |
self.assertEqual(fn(1, b=2), (1, (), True)) |
| 3648 |
self.assertEqual(fn(1, 2, 3, 4), (1, (2, 3, 4), False)) |
| 3649 |
self.assertEqual(fn(1, 2, 3, 4, b=5), (1, (2, 3, 4), True)) |
| 3650 |
self.assertEqual(fn(a=1), (1, (), False)) |
| 3651 |
self.assertEqual(fn(a=1, b=2), (1, (), True)) |
| 3652 |
|
| 3653 |
def test_poskw_varpos_kwonly_opt2(self): |
| 3654 |
# fn(a, *args, b=False, c=False) |
| 3655 |
fn = ac_tester.poskw_varpos_kwonly_opt2 |
| 3656 |
self.assertRaises(TypeError, fn) |
| 3657 |
errmsg = re.escape("given by name ('a') and position (1)") |
| 3658 |
self.assertRaisesRegex(TypeError, errmsg, fn, 1, a=2) |
| 3659 |
self.assertEqual(fn(1, b=2), (1, (), 2, False)) |
| 3660 |
self.assertEqual(fn(1, b=2, c=3), (1, (), 2, 3)) |
| 3661 |
self.assertEqual(fn(1, 2, 3), (1, (2, 3), False, False)) |
| 3662 |
self.assertEqual(fn(1, 2, 3, b=4), (1, (2, 3), 4, False)) |
| 3663 |
self.assertEqual(fn(1, 2, 3, b=4, c=5), (1, (2, 3), 4, 5)) |
| 3664 |
self.assertEqual(fn(a=1), (1, (), False, False)) |
| 3665 |
self.assertEqual(fn(a=1, b=2), (1, (), 2, False)) |
| 3666 |
self.assertEqual(fn(a=1, b=2, c=3), (1, (), 2, 3)) |
| 3667 |
|
| 3668 |
def test_varpos_kwonly_opt(self): |
| 3669 |
# fn(*args, b=False) |
| 3670 |
fn = ac_tester.varpos_kwonly_opt |
| 3671 |
self.assertEqual(fn(), ((), False)) |
| 3672 |
self.assertEqual(fn(b=2), ((), 2)) |
| 3673 |
self.assertEqual(fn(1, b=2), ((1, ), 2)) |
| 3674 |
self.assertEqual(fn(1, 2, 3, 4), ((1, 2, 3, 4), False)) |
| 3675 |
self.assertEqual(fn(1, 2, 3, 4, b=5), ((1, 2, 3, 4), 5)) |
| 3676 |
|
| 3677 |
def test_varpos_kwonly_req_opt(self): |
| 3678 |
fn = ac_tester.varpos_kwonly_req_opt |
| 3679 |
self.assertRaises(TypeError, fn) |
| 3680 |
self.assertEqual(fn(a=1), ((), 1, False, False)) |
| 3681 |
self.assertEqual(fn(a=1, b=2), ((), 1, 2, False)) |
| 3682 |
self.assertEqual(fn(a=1, b=2, c=3), ((), 1, 2, 3)) |
| 3683 |
self.assertRaises(TypeError, fn, 1) |
| 3684 |
self.assertEqual(fn(1, a=2), ((1,), 2, False, False)) |
| 3685 |
self.assertEqual(fn(1, a=2, b=3), ((1,), 2, 3, False)) |
| 3686 |
self.assertEqual(fn(1, a=2, b=3, c=4), ((1,), 2, 3, 4)) |
| 3687 |
|
| 3688 |
def test_gh_32092_oob(self): |
| 3689 |
ac_tester.gh_32092_oob(1, 2, 3, 4, kw1=5, kw2=6) |
| 3690 |
|
| 3691 |
def test_gh_32092_kw_pass(self): |
| 3692 |
ac_tester.gh_32092_kw_pass(1, 2, 3) |
| 3693 |
|
| 3694 |
def test_gh_99233_refcount(self): |
| 3695 |
arg = '*A unique string is not referenced by anywhere else.*' |
| 3696 |
arg_refcount_origin = sys.getrefcount(arg) |
| 3697 |
ac_tester.gh_99233_refcount(arg) |
| 3698 |
arg_refcount_after = sys.getrefcount(arg) |
| 3699 |
self.assertEqual(arg_refcount_origin, arg_refcount_after) |
| 3700 |
|
| 3701 |
def test_gh_99240_double_free(self): |
| 3702 |
err = re.escape( |
| 3703 |
"gh_99240_double_free() argument 2 must be encoded string " |
| 3704 |
"without null bytes, not str" |
| 3705 |
) |
| 3706 |
with self.assertRaisesRegex(TypeError, err): |
| 3707 |
ac_tester.gh_99240_double_free('a', '\0b') |
| 3708 |
|
| 3709 |
def test_null_or_tuple_for_varargs(self): |
| 3710 |
# fn(name, *constraints, covariant=False) |
| 3711 |
fn = ac_tester.null_or_tuple_for_varargs |
| 3712 |
# All of these should not crash: |
| 3713 |
self.assertEqual(fn('a'), ('a', (), False)) |
| 3714 |
self.assertEqual(fn('a', 1, 2, 3, covariant=True), ('a', (1, 2, 3), True)) |
| 3715 |
self.assertEqual(fn(name='a'), ('a', (), False)) |
| 3716 |
self.assertEqual(fn(name='a', covariant=True), ('a', (), True)) |
| 3717 |
self.assertEqual(fn(covariant=True, name='a'), ('a', (), True)) |
| 3718 |
|
| 3719 |
self.assertRaises(TypeError, fn, covariant=True) |
| 3720 |
errmsg = re.escape("given by name ('name') and position (1)") |
| 3721 |
self.assertRaisesRegex(TypeError, errmsg, fn, 1, name='a') |
| 3722 |
self.assertRaisesRegex(TypeError, errmsg, fn, 1, 2, 3, name='a', covariant=True) |
| 3723 |
self.assertRaisesRegex(TypeError, errmsg, fn, 1, 2, 3, covariant=True, name='a') |
| 3724 |
|
| 3725 |
def test_cloned_func_exception_message(self): |
| 3726 |
incorrect_arg = -1 # f1() and f2() accept a single str |
| 3727 |
with self.assertRaisesRegex(TypeError, "clone_f1"): |
| 3728 |
ac_tester.clone_f1(incorrect_arg) |
| 3729 |
with self.assertRaisesRegex(TypeError, "clone_f2"): |
| 3730 |
ac_tester.clone_f2(incorrect_arg) |
| 3731 |
|
| 3732 |
def test_cloned_func_with_converter_exception_message(self): |
| 3733 |
for name in "clone_with_conv_f1", "clone_with_conv_f2": |
| 3734 |
with self.subTest(name=name): |
| 3735 |
func = getattr(ac_tester, name) |
| 3736 |
self.assertEqual(func(), name) |
| 3737 |
|
| 3738 |
def test_get_defining_class(self): |
| 3739 |
obj = ac_tester.TestClass() |
| 3740 |
meth = obj.get_defining_class |
| 3741 |
self.assertIs(obj.get_defining_class(), ac_tester.TestClass) |
| 3742 |
|
| 3743 |
# 'defining_class' argument is a positional only argument |
| 3744 |
with self.assertRaises(TypeError): |
| 3745 |
obj.get_defining_class_arg(cls=ac_tester.TestClass) |
| 3746 |
|
| 3747 |
check = partial(self.assertRaisesRegex, TypeError, "no arguments") |
| 3748 |
check(meth, 1) |
| 3749 |
check(meth, a=1) |
| 3750 |
|
| 3751 |
def test_get_defining_class_capi(self): |
| 3752 |
from _testcapi import pyobject_vectorcall |
| 3753 |
obj = ac_tester.TestClass() |
| 3754 |
meth = obj.get_defining_class |
| 3755 |
pyobject_vectorcall(meth, None, None) |
| 3756 |
pyobject_vectorcall(meth, (), None) |
| 3757 |
pyobject_vectorcall(meth, (), ()) |
| 3758 |
pyobject_vectorcall(meth, None, ()) |
| 3759 |
self.assertIs(pyobject_vectorcall(meth, (), ()), ac_tester.TestClass) |
| 3760 |
|
| 3761 |
check = partial(self.assertRaisesRegex, TypeError, "no arguments") |
| 3762 |
check(pyobject_vectorcall, meth, (1,), None) |
| 3763 |
check(pyobject_vectorcall, meth, (1,), ("a",)) |
| 3764 |
|
| 3765 |
def test_get_defining_class_arg(self): |
| 3766 |
obj = ac_tester.TestClass() |
| 3767 |
self.assertEqual(obj.get_defining_class_arg("arg"), |
| 3768 |
(ac_tester.TestClass, "arg")) |
| 3769 |
self.assertEqual(obj.get_defining_class_arg(arg=123), |
| 3770 |
(ac_tester.TestClass, 123)) |
| 3771 |
|
| 3772 |
# 'defining_class' argument is a positional only argument |
| 3773 |
with self.assertRaises(TypeError): |
| 3774 |
obj.get_defining_class_arg(cls=ac_tester.TestClass, arg="arg") |
| 3775 |
|
| 3776 |
# wrong number of arguments |
| 3777 |
with self.assertRaises(TypeError): |
| 3778 |
obj.get_defining_class_arg() |
| 3779 |
with self.assertRaises(TypeError): |
| 3780 |
obj.get_defining_class_arg("arg1", "arg2") |
| 3781 |
|
| 3782 |
def test_defclass_varpos(self): |
| 3783 |
# fn(*args) |
| 3784 |
cls = ac_tester.TestClass |
| 3785 |
obj = cls() |
| 3786 |
fn = obj.defclass_varpos |
| 3787 |
self.assertEqual(fn(), (cls, ())) |
| 3788 |
self.assertEqual(fn(1, 2), (cls, (1, 2))) |
| 3789 |
fn = cls.defclass_varpos |
| 3790 |
self.assertRaises(TypeError, fn) |
| 3791 |
self.assertEqual(fn(obj), (cls, ())) |
| 3792 |
self.assertEqual(fn(obj, 1, 2), (cls, (1, 2))) |
| 3793 |
|
| 3794 |
def test_defclass_posonly_varpos(self): |
| 3795 |
# fn(a, b, /, *args) |
| 3796 |
cls = ac_tester.TestClass |
| 3797 |
obj = cls() |
| 3798 |
fn = obj.defclass_posonly_varpos |
| 3799 |
errmsg = 'takes at least 2 positional arguments' |
| 3800 |
self.assertRaisesRegex(TypeError, errmsg, fn) |
| 3801 |
self.assertRaisesRegex(TypeError, errmsg, fn, 1) |
| 3802 |
self.assertEqual(fn(1, 2), (cls, 1, 2, ())) |
| 3803 |
self.assertEqual(fn(1, 2, 3, 4), (cls, 1, 2, (3, 4))) |
| 3804 |
fn = cls.defclass_posonly_varpos |
| 3805 |
self.assertRaises(TypeError, fn) |
| 3806 |
self.assertRaisesRegex(TypeError, errmsg, fn, obj) |
| 3807 |
self.assertRaisesRegex(TypeError, errmsg, fn, obj, 1) |
| 3808 |
self.assertEqual(fn(obj, 1, 2), (cls, 1, 2, ())) |
| 3809 |
self.assertEqual(fn(obj, 1, 2, 3, 4), (cls, 1, 2, (3, 4))) |
| 3810 |
|
| 3811 |
def test_depr_star_new(self): |
| 3812 |
cls = ac_tester.DeprStarNew |
| 3813 |
cls() |
| 3814 |
cls(a=None) |
| 3815 |
self.check_depr_star("'a'", cls, None) |
| 3816 |
|
| 3817 |
def test_depr_star_new_cloned(self): |
| 3818 |
fn = ac_tester.DeprStarNew().cloned |
| 3819 |
fn() |
| 3820 |
fn(a=None) |
| 3821 |
self.check_depr_star("'a'", fn, None, name='_testclinic.DeprStarNew.cloned') |
| 3822 |
|
| 3823 |
def test_depr_star_init(self): |
| 3824 |
cls = ac_tester.DeprStarInit |
| 3825 |
cls() |
| 3826 |
cls(a=None) |
| 3827 |
self.check_depr_star("'a'", cls, None) |
| 3828 |
|
| 3829 |
def test_depr_star_init_cloned(self): |
| 3830 |
fn = ac_tester.DeprStarInit().cloned |
| 3831 |
fn() |
| 3832 |
fn(a=None) |
| 3833 |
self.check_depr_star("'a'", fn, None, name='_testclinic.DeprStarInit.cloned') |
| 3834 |
|
| 3835 |
def test_depr_star_init_noinline(self): |
| 3836 |
cls = ac_tester.DeprStarInitNoInline |
| 3837 |
self.assertRaises(TypeError, cls, "a") |
| 3838 |
cls(a="a", b="b") |
| 3839 |
cls(a="a", b="b", c="c") |
| 3840 |
cls("a", b="b") |
| 3841 |
cls("a", b="b", c="c") |
| 3842 |
check = partial(self.check_depr_star, "'b' and 'c'", cls) |
| 3843 |
check("a", "b") |
| 3844 |
check("a", "b", "c") |
| 3845 |
check("a", "b", c="c") |
| 3846 |
self.assertRaises(TypeError, cls, "a", "b", "c", "d") |
| 3847 |
|
| 3848 |
def test_depr_kwd_new(self): |
| 3849 |
cls = ac_tester.DeprKwdNew |
| 3850 |
cls() |
| 3851 |
cls(None) |
| 3852 |
self.check_depr_kwd("'a'", cls, a=None) |
| 3853 |
|
| 3854 |
def test_depr_kwd_init(self): |
| 3855 |
cls = ac_tester.DeprKwdInit |
| 3856 |
cls() |
| 3857 |
cls(None) |
| 3858 |
self.check_depr_kwd("'a'", cls, a=None) |
| 3859 |
|
| 3860 |
def test_depr_kwd_init_noinline(self): |
| 3861 |
cls = ac_tester.DeprKwdInitNoInline |
| 3862 |
cls = ac_tester.depr_star_noinline |
| 3863 |
self.assertRaises(TypeError, cls, "a") |
| 3864 |
cls(a="a", b="b") |
| 3865 |
cls(a="a", b="b", c="c") |
| 3866 |
cls("a", b="b") |
| 3867 |
cls("a", b="b", c="c") |
| 3868 |
check = partial(self.check_depr_star, "'b' and 'c'", cls) |
| 3869 |
check("a", "b") |
| 3870 |
check("a", "b", "c") |
| 3871 |
check("a", "b", c="c") |
| 3872 |
self.assertRaises(TypeError, cls, "a", "b", "c", "d") |
| 3873 |
|
| 3874 |
def test_depr_star_pos0_len1(self): |
| 3875 |
fn = ac_tester.depr_star_pos0_len1 |
| 3876 |
fn(a=None) |
| 3877 |
self.check_depr_star("'a'", fn, "a") |
| 3878 |
|
| 3879 |
def test_depr_star_pos0_len2(self): |
| 3880 |
fn = ac_tester.depr_star_pos0_len2 |
| 3881 |
fn(a=0, b=0) |
| 3882 |
check = partial(self.check_depr_star, "'a' and 'b'", fn) |
| 3883 |
check("a", b=0) |
| 3884 |
check("a", "b") |
| 3885 |
|
| 3886 |
def test_depr_star_pos0_len3_with_kwd(self): |
| 3887 |
fn = ac_tester.depr_star_pos0_len3_with_kwd |
| 3888 |
fn(a=0, b=0, c=0, d=0) |
| 3889 |
check = partial(self.check_depr_star, "'a', 'b' and 'c'", fn) |
| 3890 |
check("a", b=0, c=0, d=0) |
| 3891 |
check("a", "b", c=0, d=0) |
| 3892 |
check("a", "b", "c", d=0) |
| 3893 |
|
| 3894 |
def test_depr_star_pos1_len1_opt(self): |
| 3895 |
fn = ac_tester.depr_star_pos1_len1_opt |
| 3896 |
fn(a=0, b=0) |
| 3897 |
fn("a", b=0) |
| 3898 |
fn(a=0) # b is optional |
| 3899 |
check = partial(self.check_depr_star, "'b'", fn) |
| 3900 |
check("a", "b") |
| 3901 |
|
| 3902 |
def test_depr_star_pos1_len1(self): |
| 3903 |
fn = ac_tester.depr_star_pos1_len1 |
| 3904 |
fn(a=0, b=0) |
| 3905 |
fn("a", b=0) |
| 3906 |
check = partial(self.check_depr_star, "'b'", fn) |
| 3907 |
check("a", "b") |
| 3908 |
|
| 3909 |
def test_depr_star_pos1_len2_with_kwd(self): |
| 3910 |
fn = ac_tester.depr_star_pos1_len2_with_kwd |
| 3911 |
fn(a=0, b=0, c=0, d=0), |
| 3912 |
fn("a", b=0, c=0, d=0), |
| 3913 |
check = partial(self.check_depr_star, "'b' and 'c'", fn) |
| 3914 |
check("a", "b", c=0, d=0), |
| 3915 |
check("a", "b", "c", d=0), |
| 3916 |
|
| 3917 |
def test_depr_star_pos2_len1(self): |
| 3918 |
fn = ac_tester.depr_star_pos2_len1 |
| 3919 |
fn(a=0, b=0, c=0) |
| 3920 |
fn("a", b=0, c=0) |
| 3921 |
fn("a", "b", c=0) |
| 3922 |
check = partial(self.check_depr_star, "'c'", fn) |
| 3923 |
check("a", "b", "c") |
| 3924 |
|
| 3925 |
def test_depr_star_pos2_len2(self): |
| 3926 |
fn = ac_tester.depr_star_pos2_len2 |
| 3927 |
fn(a=0, b=0, c=0, d=0) |
| 3928 |
fn("a", b=0, c=0, d=0) |
| 3929 |
fn("a", "b", c=0, d=0) |
| 3930 |
check = partial(self.check_depr_star, "'c' and 'd'", fn) |
| 3931 |
check("a", "b", "c", d=0) |
| 3932 |
check("a", "b", "c", "d") |
| 3933 |
|
| 3934 |
def test_depr_star_pos2_len2_with_kwd(self): |
| 3935 |
fn = ac_tester.depr_star_pos2_len2_with_kwd |
| 3936 |
fn(a=0, b=0, c=0, d=0, e=0) |
| 3937 |
fn("a", b=0, c=0, d=0, e=0) |
| 3938 |
fn("a", "b", c=0, d=0, e=0) |
| 3939 |
check = partial(self.check_depr_star, "'c' and 'd'", fn) |
| 3940 |
check("a", "b", "c", d=0, e=0) |
| 3941 |
check("a", "b", "c", "d", e=0) |
| 3942 |
|
| 3943 |
def test_depr_star_noinline(self): |
| 3944 |
fn = ac_tester.depr_star_noinline |
| 3945 |
self.assertRaises(TypeError, fn, "a") |
| 3946 |
fn(a="a", b="b") |
| 3947 |
fn(a="a", b="b", c="c") |
| 3948 |
fn("a", b="b") |
| 3949 |
fn("a", b="b", c="c") |
| 3950 |
check = partial(self.check_depr_star, "'b' and 'c'", fn) |
| 3951 |
check("a", "b") |
| 3952 |
check("a", "b", "c") |
| 3953 |
check("a", "b", c="c") |
| 3954 |
self.assertRaises(TypeError, fn, "a", "b", "c", "d") |
| 3955 |
|
| 3956 |
def test_depr_star_multi(self): |
| 3957 |
fn = ac_tester.depr_star_multi |
| 3958 |
self.assertRaises(TypeError, fn, "a") |
| 3959 |
fn("a", b="b", c="c", d="d", e="e", f="f", g="g", h="h") |
| 3960 |
errmsg = ( |
| 3961 |
"Passing more than 1 positional argument to depr_star_multi() is deprecated. " |
| 3962 |
"Parameter 'b' will become a keyword-only parameter in Python 3.16. " |
| 3963 |
"Parameters 'c' and 'd' will become keyword-only parameters in Python 3.15. " |
| 3964 |
"Parameters 'e', 'f' and 'g' will become keyword-only parameters in Python 3.14.") |
| 3965 |
check = partial(self.check_depr, re.escape(errmsg), fn) |
| 3966 |
check("a", "b", c="c", d="d", e="e", f="f", g="g", h="h") |
| 3967 |
check("a", "b", "c", d="d", e="e", f="f", g="g", h="h") |
| 3968 |
check("a", "b", "c", "d", e="e", f="f", g="g", h="h") |
| 3969 |
check("a", "b", "c", "d", "e", f="f", g="g", h="h") |
| 3970 |
check("a", "b", "c", "d", "e", "f", g="g", h="h") |
| 3971 |
check("a", "b", "c", "d", "e", "f", "g", h="h") |
| 3972 |
self.assertRaises(TypeError, fn, "a", "b", "c", "d", "e", "f", "g", "h") |
| 3973 |
|
| 3974 |
def test_depr_kwd_required_1(self): |
| 3975 |
fn = ac_tester.depr_kwd_required_1 |
| 3976 |
fn("a", "b") |
| 3977 |
self.assertRaises(TypeError, fn, "a") |
| 3978 |
self.assertRaises(TypeError, fn, "a", "b", "c") |
| 3979 |
check = partial(self.check_depr_kwd, "'b'", fn) |
| 3980 |
check("a", b="b") |
| 3981 |
self.assertRaises(TypeError, fn, a="a", b="b") |
| 3982 |
|
| 3983 |
def test_depr_kwd_required_2(self): |
| 3984 |
fn = ac_tester.depr_kwd_required_2 |
| 3985 |
fn("a", "b", "c") |
| 3986 |
self.assertRaises(TypeError, fn, "a", "b") |
| 3987 |
self.assertRaises(TypeError, fn, "a", "b", "c", "d") |
| 3988 |
check = partial(self.check_depr_kwd, "'b' and 'c'", fn) |
| 3989 |
check("a", "b", c="c") |
| 3990 |
check("a", b="b", c="c") |
| 3991 |
self.assertRaises(TypeError, fn, a="a", b="b", c="c") |
| 3992 |
|
| 3993 |
def test_depr_kwd_optional_1(self): |
| 3994 |
fn = ac_tester.depr_kwd_optional_1 |
| 3995 |
fn("a") |
| 3996 |
fn("a", "b") |
| 3997 |
self.assertRaises(TypeError, fn) |
| 3998 |
self.assertRaises(TypeError, fn, "a", "b", "c") |
| 3999 |
check = partial(self.check_depr_kwd, "'b'", fn) |
| 4000 |
check("a", b="b") |
| 4001 |
self.assertRaises(TypeError, fn, a="a", b="b") |
| 4002 |
|
| 4003 |
def test_depr_kwd_optional_2(self): |
| 4004 |
fn = ac_tester.depr_kwd_optional_2 |
| 4005 |
fn("a") |
| 4006 |
fn("a", "b") |
| 4007 |
fn("a", "b", "c") |
| 4008 |
self.assertRaises(TypeError, fn) |
| 4009 |
self.assertRaises(TypeError, fn, "a", "b", "c", "d") |
| 4010 |
check = partial(self.check_depr_kwd, "'b' and 'c'", fn) |
| 4011 |
check("a", b="b") |
| 4012 |
check("a", c="c") |
| 4013 |
check("a", b="b", c="c") |
| 4014 |
check("a", c="c", b="b") |
| 4015 |
check("a", "b", c="c") |
| 4016 |
self.assertRaises(TypeError, fn, a="a", b="b", c="c") |
| 4017 |
|
| 4018 |
def test_depr_kwd_optional_3(self): |
| 4019 |
fn = ac_tester.depr_kwd_optional_3 |
| 4020 |
fn() |
| 4021 |
fn("a") |
| 4022 |
fn("a", "b") |
| 4023 |
fn("a", "b", "c") |
| 4024 |
self.assertRaises(TypeError, fn, "a", "b", "c", "d") |
| 4025 |
check = partial(self.check_depr_kwd, "'a', 'b' and 'c'", fn) |
| 4026 |
check("a", "b", c="c") |
| 4027 |
check("a", b="b") |
| 4028 |
check(a="a") |
| 4029 |
|
| 4030 |
def test_depr_kwd_required_optional(self): |
| 4031 |
fn = ac_tester.depr_kwd_required_optional |
| 4032 |
fn("a", "b") |
| 4033 |
fn("a", "b", "c") |
| 4034 |
self.assertRaises(TypeError, fn) |
| 4035 |
self.assertRaises(TypeError, fn, "a") |
| 4036 |
self.assertRaises(TypeError, fn, "a", "b", "c", "d") |
| 4037 |
check = partial(self.check_depr_kwd, "'b' and 'c'", fn) |
| 4038 |
check("a", b="b") |
| 4039 |
check("a", b="b", c="c") |
| 4040 |
check("a", c="c", b="b") |
| 4041 |
check("a", "b", c="c") |
| 4042 |
self.assertRaises(TypeError, fn, "a", c="c") |
| 4043 |
self.assertRaises(TypeError, fn, a="a", b="b", c="c") |
| 4044 |
|
| 4045 |
def test_depr_kwd_noinline(self): |
| 4046 |
fn = ac_tester.depr_kwd_noinline |
| 4047 |
fn("a", "b") |
| 4048 |
fn("a", "b", "c") |
| 4049 |
self.assertRaises(TypeError, fn, "a") |
| 4050 |
check = partial(self.check_depr_kwd, "'b' and 'c'", fn) |
| 4051 |
check("a", b="b") |
| 4052 |
check("a", b="b", c="c") |
| 4053 |
check("a", c="c", b="b") |
| 4054 |
check("a", "b", c="c") |
| 4055 |
self.assertRaises(TypeError, fn, "a", c="c") |
| 4056 |
self.assertRaises(TypeError, fn, a="a", b="b", c="c") |
| 4057 |
|
| 4058 |
def test_depr_kwd_multi(self): |
| 4059 |
fn = ac_tester.depr_kwd_multi |
| 4060 |
fn("a", "b", "c", "d", "e", "f", "g", h="h") |
| 4061 |
errmsg = ( |
| 4062 |
"Passing keyword arguments 'b', 'c', 'd', 'e', 'f' and 'g' to depr_kwd_multi() is deprecated. " |
| 4063 |
"Parameter 'b' will become positional-only in Python 3.14. " |
| 4064 |
"Parameters 'c' and 'd' will become positional-only in Python 3.15. " |
| 4065 |
"Parameters 'e', 'f' and 'g' will become positional-only in Python 3.16.") |
| 4066 |
check = partial(self.check_depr, re.escape(errmsg), fn) |
| 4067 |
check("a", "b", "c", "d", "e", "f", g="g", h="h") |
| 4068 |
check("a", "b", "c", "d", "e", f="f", g="g", h="h") |
| 4069 |
check("a", "b", "c", "d", e="e", f="f", g="g", h="h") |
| 4070 |
check("a", "b", "c", d="d", e="e", f="f", g="g", h="h") |
| 4071 |
check("a", "b", c="c", d="d", e="e", f="f", g="g", h="h") |
| 4072 |
check("a", b="b", c="c", d="d", e="e", f="f", g="g", h="h") |
| 4073 |
self.assertRaises(TypeError, fn, a="a", b="b", c="c", d="d", e="e", f="f", g="g", h="h") |
| 4074 |
|
| 4075 |
def test_depr_multi(self): |
| 4076 |
fn = ac_tester.depr_multi |
| 4077 |
self.assertRaises(TypeError, fn, "a", "b", "c", "d", "e", "f", "g") |
| 4078 |
errmsg = ( |
| 4079 |
"Passing more than 4 positional arguments to depr_multi() is deprecated. " |
| 4080 |
"Parameter 'e' will become a keyword-only parameter in Python 3.15. " |
| 4081 |
"Parameter 'f' will become a keyword-only parameter in Python 3.14.") |
| 4082 |
check = partial(self.check_depr, re.escape(errmsg), fn) |
| 4083 |
check("a", "b", "c", "d", "e", "f", g="g") |
| 4084 |
check("a", "b", "c", "d", "e", f="f", g="g") |
| 4085 |
fn("a", "b", "c", "d", e="e", f="f", g="g") |
| 4086 |
fn("a", "b", "c", d="d", e="e", f="f", g="g") |
| 4087 |
errmsg = ( |
| 4088 |
"Passing keyword arguments 'b' and 'c' to depr_multi() is deprecated. " |
| 4089 |
"Parameter 'b' will become positional-only in Python 3.14. " |
| 4090 |
"Parameter 'c' will become positional-only in Python 3.15.") |
| 4091 |
check = partial(self.check_depr, re.escape(errmsg), fn) |
| 4092 |
check("a", "b", c="c", d="d", e="e", f="f", g="g") |
| 4093 |
check("a", b="b", c="c", d="d", e="e", f="f", g="g") |
| 4094 |
self.assertRaises(TypeError, fn, a="a", b="b", c="c", d="d", e="e", f="f", g="g") |
| 4095 |
|
| 4096 |
|
| 4097 |
class LimitedCAPIOutputTests(unittest.TestCase): |
| 4098 |
|
| 4099 |
def setUp(self): |
| 4100 |
self.clinic = _make_clinic(limited_capi=True) |
| 4101 |
|
| 4102 |
@staticmethod |
| 4103 |
def wrap_clinic_input(block): |
| 4104 |
return dedent(f""" |
| 4105 |
/*[clinic input] |
| 4106 |
output everything buffer |
| 4107 |
{block} |
| 4108 |
[clinic start generated code]*/ |
| 4109 |
/*[clinic input] |
| 4110 |
dump buffer |
| 4111 |
[clinic start generated code]*/ |
| 4112 |
""") |
| 4113 |
|
| 4114 |
def test_limited_capi_float(self): |
| 4115 |
block = self.wrap_clinic_input(""" |
| 4116 |
func |
| 4117 |
f: float |
| 4118 |
/ |
| 4119 |
""") |
| 4120 |
generated = self.clinic.parse(block) |
| 4121 |
self.assertNotIn("PyFloat_AS_DOUBLE", generated) |
| 4122 |
self.assertIn("float f;", generated) |
| 4123 |
self.assertIn("f = (float) PyFloat_AsDouble", generated) |
| 4124 |
|
| 4125 |
def test_limited_capi_double(self): |
| 4126 |
block = self.wrap_clinic_input(""" |
| 4127 |
func |
| 4128 |
f: double |
| 4129 |
/ |
| 4130 |
""") |
| 4131 |
generated = self.clinic.parse(block) |
| 4132 |
self.assertNotIn("PyFloat_AS_DOUBLE", generated) |
| 4133 |
self.assertIn("double f;", generated) |
| 4134 |
self.assertIn("f = PyFloat_AsDouble", generated) |
| 4135 |
|
| 4136 |
|
| 4137 |
try: |
| 4138 |
import _testclinic_limited |
| 4139 |
except ImportError: |
| 4140 |
_testclinic_limited = None |
| 4141 |
|
| 4142 |
@unittest.skipIf(_testclinic_limited is None, "_testclinic_limited is missing") |
| 4143 |
class LimitedCAPIFunctionalTest(unittest.TestCase): |
| 4144 |
locals().update((name, getattr(_testclinic_limited, name)) |
| 4145 |
for name in dir(_testclinic_limited) if name.startswith('test_')) |
| 4146 |
|
| 4147 |
def test_my_int_func(self): |
| 4148 |
with self.assertRaises(TypeError): |
| 4149 |
_testclinic_limited.my_int_func() |
| 4150 |
self.assertEqual(_testclinic_limited.my_int_func(3), 3) |
| 4151 |
with self.assertRaises(TypeError): |
| 4152 |
_testclinic_limited.my_int_func(1.0) |
| 4153 |
with self.assertRaises(TypeError): |
| 4154 |
_testclinic_limited.my_int_func("xyz") |
| 4155 |
|
| 4156 |
def test_my_int_sum(self): |
| 4157 |
with self.assertRaises(TypeError): |
| 4158 |
_testclinic_limited.my_int_sum() |
| 4159 |
with self.assertRaises(TypeError): |
| 4160 |
_testclinic_limited.my_int_sum(1) |
| 4161 |
self.assertEqual(_testclinic_limited.my_int_sum(1, 2), 3) |
| 4162 |
with self.assertRaises(TypeError): |
| 4163 |
_testclinic_limited.my_int_sum(1.0, 2) |
| 4164 |
with self.assertRaises(TypeError): |
| 4165 |
_testclinic_limited.my_int_sum(1, "str") |
| 4166 |
|
| 4167 |
def test_my_double_sum(self): |
| 4168 |
for func in ( |
| 4169 |
_testclinic_limited.my_float_sum, |
| 4170 |
_testclinic_limited.my_double_sum, |
| 4171 |
): |
| 4172 |
with self.subTest(func=func.__name__): |
| 4173 |
self.assertEqual(func(1.0, 2.5), 3.5) |
| 4174 |
with self.assertRaises(TypeError): |
| 4175 |
func() |
| 4176 |
with self.assertRaises(TypeError): |
| 4177 |
func(1) |
| 4178 |
with self.assertRaises(TypeError): |
| 4179 |
func(1., "2") |
| 4180 |
|
| 4181 |
def test_get_file_descriptor(self): |
| 4182 |
# test 'file descriptor' converter: call PyObject_AsFileDescriptor() |
| 4183 |
get_fd = _testclinic_limited.get_file_descriptor |
| 4184 |
|
| 4185 |
class MyInt(int): |
| 4186 |
pass |
| 4187 |
|
| 4188 |
class MyFile: |
| 4189 |
def __init__(self, fd): |
| 4190 |
self._fd = fd |
| 4191 |
def fileno(self): |
| 4192 |
return self._fd |
| 4193 |
|
| 4194 |
for fd in (0, 1, 2, 5, 123_456): |
| 4195 |
self.assertEqual(get_fd(fd), fd) |
| 4196 |
|
| 4197 |
myint = MyInt(fd) |
| 4198 |
self.assertEqual(get_fd(myint), fd) |
| 4199 |
|
| 4200 |
myfile = MyFile(fd) |
| 4201 |
self.assertEqual(get_fd(myfile), fd) |
| 4202 |
|
| 4203 |
with self.assertRaises(OverflowError): |
| 4204 |
get_fd(2**256) |
| 4205 |
with self.assertWarnsRegex(RuntimeWarning, |
| 4206 |
"bool is used as a file descriptor"): |
| 4207 |
get_fd(True) |
| 4208 |
with self.assertRaises(TypeError): |
| 4209 |
get_fd(1.0) |
| 4210 |
with self.assertRaises(TypeError): |
| 4211 |
get_fd("abc") |
| 4212 |
with self.assertRaises(TypeError): |
| 4213 |
get_fd(None) |
| 4214 |
|
| 4215 |
|
| 4216 |
class PermutationTests(unittest.TestCase): |
| 4217 |
"""Test permutation support functions.""" |
| 4218 |
|
| 4219 |
def test_permute_left_option_groups(self): |
| 4220 |
expected = ( |
| 4221 |
(), |
| 4222 |
(3,), |
| 4223 |
(2, 3), |
| 4224 |
(1, 2, 3), |
| 4225 |
) |
| 4226 |
data = list(zip([1, 2, 3])) # Generate a list of 1-tuples. |
| 4227 |
actual = tuple(permute_left_option_groups(data)) |
| 4228 |
self.assertEqual(actual, expected) |
| 4229 |
|
| 4230 |
def test_permute_right_option_groups(self): |
| 4231 |
expected = ( |
| 4232 |
(), |
| 4233 |
(1,), |
| 4234 |
(1, 2), |
| 4235 |
(1, 2, 3), |
| 4236 |
) |
| 4237 |
data = list(zip([1, 2, 3])) # Generate a list of 1-tuples. |
| 4238 |
actual = tuple(permute_right_option_groups(data)) |
| 4239 |
self.assertEqual(actual, expected) |
| 4240 |
|
| 4241 |
def test_permute_optional_groups(self): |
| 4242 |
empty = { |
| 4243 |
"left": (), "required": (), "right": (), |
| 4244 |
"expected": ((),), |
| 4245 |
} |
| 4246 |
noleft1 = { |
| 4247 |
"left": (), "required": ("b",), "right": ("c",), |
| 4248 |
"expected": ( |
| 4249 |
("b",), |
| 4250 |
("b", "c"), |
| 4251 |
), |
| 4252 |
} |
| 4253 |
noleft2 = { |
| 4254 |
"left": (), "required": ("b", "c",), "right": ("d",), |
| 4255 |
"expected": ( |
| 4256 |
("b", "c"), |
| 4257 |
("b", "c", "d"), |
| 4258 |
), |
| 4259 |
} |
| 4260 |
noleft3 = { |
| 4261 |
"left": (), "required": ("b", "c",), "right": ("d", "e"), |
| 4262 |
"expected": ( |
| 4263 |
("b", "c"), |
| 4264 |
("b", "c", "d"), |
| 4265 |
("b", "c", "d", "e"), |
| 4266 |
), |
| 4267 |
} |
| 4268 |
noright1 = { |
| 4269 |
"left": ("a",), "required": ("b",), "right": (), |
| 4270 |
"expected": ( |
| 4271 |
("b",), |
| 4272 |
("a", "b"), |
| 4273 |
), |
| 4274 |
} |
| 4275 |
noright2 = { |
| 4276 |
"left": ("a",), "required": ("b", "c"), "right": (), |
| 4277 |
"expected": ( |
| 4278 |
("b", "c"), |
| 4279 |
("a", "b", "c"), |
| 4280 |
), |
| 4281 |
} |
| 4282 |
noright3 = { |
| 4283 |
"left": ("a", "b"), "required": ("c",), "right": (), |
| 4284 |
"expected": ( |
| 4285 |
("c",), |
| 4286 |
("b", "c"), |
| 4287 |
("a", "b", "c"), |
| 4288 |
), |
| 4289 |
} |
| 4290 |
leftandright1 = { |
| 4291 |
"left": ("a",), "required": ("b",), "right": ("c",), |
| 4292 |
"expected": ( |
| 4293 |
("b",), |
| 4294 |
("a", "b"), # Prefer left. |
| 4295 |
("a", "b", "c"), |
| 4296 |
), |
| 4297 |
} |
| 4298 |
leftandright2 = { |
| 4299 |
"left": ("a", "b"), "required": ("c", "d"), "right": ("e", "f"), |
| 4300 |
"expected": ( |
| 4301 |
("c", "d"), |
| 4302 |
("b", "c", "d"), # Prefer left. |
| 4303 |
("a", "b", "c", "d"), # Prefer left. |
| 4304 |
("a", "b", "c", "d", "e"), |
| 4305 |
("a", "b", "c", "d", "e", "f"), |
| 4306 |
), |
| 4307 |
} |
| 4308 |
dataset = ( |
| 4309 |
empty, |
| 4310 |
noleft1, noleft2, noleft3, |
| 4311 |
noright1, noright2, noright3, |
| 4312 |
leftandright1, leftandright2, |
| 4313 |
) |
| 4314 |
for params in dataset: |
| 4315 |
with self.subTest(**params): |
| 4316 |
left, required, right, expected = params.values() |
| 4317 |
permutations = permute_optional_groups(left, required, right) |
| 4318 |
actual = tuple(permutations) |
| 4319 |
self.assertEqual(actual, expected) |
| 4320 |
|
| 4321 |
|
| 4322 |
class FormatHelperTests(unittest.TestCase): |
| 4323 |
|
| 4324 |
def test_strip_leading_and_trailing_blank_lines(self): |
| 4325 |
dataset = ( |
| 4326 |
# Input lines, expected output. |
| 4327 |
("a\nb", "a\nb"), |
| 4328 |
("a\nb\n", "a\nb"), |
| 4329 |
("a\nb ", "a\nb"), |
| 4330 |
("\na\nb\n\n", "a\nb"), |
| 4331 |
("\n\na\nb\n\n", "a\nb"), |
| 4332 |
("\n\na\n\nb\n\n", "a\n\nb"), |
| 4333 |
# Note, leading whitespace is preserved: |
| 4334 |
(" a\nb", " a\nb"), |
| 4335 |
(" a\nb ", " a\nb"), |
| 4336 |
(" \n \n a\nb \n \n ", " a\nb"), |
| 4337 |
) |
| 4338 |
for lines, expected in dataset: |
| 4339 |
with self.subTest(lines=lines, expected=expected): |
| 4340 |
out = libclinic.normalize_snippet(lines) |
| 4341 |
self.assertEqual(out, expected) |
| 4342 |
|
| 4343 |
def test_normalize_snippet(self): |
| 4344 |
snippet = """ |
| 4345 |
one |
| 4346 |
two |
| 4347 |
three |
| 4348 |
""" |
| 4349 |
|
| 4350 |
# Expected outputs: |
| 4351 |
zero_indent = ( |
| 4352 |
"one\n" |
| 4353 |
"two\n" |
| 4354 |
"three" |
| 4355 |
) |
| 4356 |
four_indent = ( |
| 4357 |
" one\n" |
| 4358 |
" two\n" |
| 4359 |
" three" |
| 4360 |
) |
| 4361 |
eight_indent = ( |
| 4362 |
" one\n" |
| 4363 |
" two\n" |
| 4364 |
" three" |
| 4365 |
) |
| 4366 |
expected_outputs = {0: zero_indent, 4: four_indent, 8: eight_indent} |
| 4367 |
for indent, expected in expected_outputs.items(): |
| 4368 |
with self.subTest(indent=indent): |
| 4369 |
actual = libclinic.normalize_snippet(snippet, indent=indent) |
| 4370 |
self.assertEqual(actual, expected) |
| 4371 |
|
| 4372 |
def test_escaped_docstring(self): |
| 4373 |
dataset = ( |
| 4374 |
# input, expected |
| 4375 |
(r"abc", r'"abc"'), |
| 4376 |
(r"\abc", r'"\\abc"'), |
| 4377 |
(r"\a\bc", r'"\\a\\bc"'), |
| 4378 |
(r"\a\\bc", r'"\\a\\\\bc"'), |
| 4379 |
(r'"abc"', r'"\"abc\""'), |
| 4380 |
(r"'a'", r'"\'a\'"'), |
| 4381 |
) |
| 4382 |
for line, expected in dataset: |
| 4383 |
with self.subTest(line=line, expected=expected): |
| 4384 |
out = libclinic.docstring_for_c_string(line) |
| 4385 |
self.assertEqual(out, expected) |
| 4386 |
|
| 4387 |
def test_format_escape(self): |
| 4388 |
line = "{}, {a}" |
| 4389 |
expected = "{{}}, {{a}}" |
| 4390 |
out = libclinic.format_escape(line) |
| 4391 |
self.assertEqual(out, expected) |
| 4392 |
|
| 4393 |
def test_c_bytes_repr(self): |
| 4394 |
c_bytes_repr = libclinic.c_bytes_repr |
| 4395 |
self.assertEqual(c_bytes_repr(b''), '""') |
| 4396 |
self.assertEqual(c_bytes_repr(b'abc'), '"abc"') |
| 4397 |
self.assertEqual(c_bytes_repr(b'\a\b\f\n\r\t\v'), r'"\a\b\f\n\r\t\v"') |
| 4398 |
self.assertEqual(c_bytes_repr(b' \0\x7f'), r'" \000\177"') |
| 4399 |
self.assertEqual(c_bytes_repr(b'"'), r'"\""') |
| 4400 |
self.assertEqual(c_bytes_repr(b"'"), r'''"'"''') |
| 4401 |
self.assertEqual(c_bytes_repr(b'\\'), r'"\\"') |
| 4402 |
self.assertEqual(c_bytes_repr(b'??/'), r'"?\?/"') |
| 4403 |
self.assertEqual(c_bytes_repr(b'???/'), r'"?\?\?/"') |
| 4404 |
self.assertEqual(c_bytes_repr(b'/*****/ /*/ */*'), r'"/\*****\/ /\*\/ *\/\*"') |
| 4405 |
self.assertEqual(c_bytes_repr(b'\xa0'), r'"\240"') |
| 4406 |
self.assertEqual(c_bytes_repr(b'\xff'), r'"\377"') |
| 4407 |
|
| 4408 |
def test_c_str_repr(self): |
| 4409 |
c_str_repr = libclinic.c_str_repr |
| 4410 |
self.assertEqual(c_str_repr(''), '""') |
| 4411 |
self.assertEqual(c_str_repr('abc'), '"abc"') |
| 4412 |
self.assertEqual(c_str_repr('\a\b\f\n\r\t\v'), r'"\a\b\f\n\r\t\v"') |
| 4413 |
self.assertEqual(c_str_repr(' \0\x7f'), r'" \000\177"') |
| 4414 |
self.assertEqual(c_str_repr('"'), r'"\""') |
| 4415 |
self.assertEqual(c_str_repr("'"), r'''"'"''') |
| 4416 |
self.assertEqual(c_str_repr('\\'), r'"\\"') |
| 4417 |
self.assertEqual(c_str_repr('??/'), r'"?\?/"') |
| 4418 |
self.assertEqual(c_str_repr('???/'), r'"?\?\?/"') |
| 4419 |
self.assertEqual(c_str_repr('/*****/ /*/ */*'), r'"/\*****\/ /\*\/ *\/\*"') |
| 4420 |
self.assertEqual(c_str_repr('\xa0'), r'"\u00a0"') |
| 4421 |
self.assertEqual(c_str_repr('\xff'), r'"\u00ff"') |
| 4422 |
self.assertEqual(c_str_repr('\u20ac'), r'"\u20ac"') |
| 4423 |
self.assertEqual(c_str_repr('\U0001f40d'), r'"\U0001f40d"') |
| 4424 |
|
| 4425 |
def test_c_unichar_repr(self): |
| 4426 |
c_unichar_repr = libclinic.c_unichar_repr |
| 4427 |
self.assertEqual(c_unichar_repr('a'), "'a'") |
| 4428 |
self.assertEqual(c_unichar_repr('\n'), r"'\n'") |
| 4429 |
self.assertEqual(c_unichar_repr('\b'), r"'\b'") |
| 4430 |
self.assertEqual(c_unichar_repr('\0'), '0') |
| 4431 |
self.assertEqual(c_unichar_repr('\1'), '0x01') |
| 4432 |
self.assertEqual(c_unichar_repr('\x7f'), '0x7f') |
| 4433 |
self.assertEqual(c_unichar_repr(' '), "' '") |
| 4434 |
self.assertEqual(c_unichar_repr('"'), """'"'""") |
| 4435 |
self.assertEqual(c_unichar_repr("'"), r"'\''") |
| 4436 |
self.assertEqual(c_unichar_repr('\\'), r"'\\'") |
| 4437 |
self.assertEqual(c_unichar_repr('?'), "'?'") |
| 4438 |
self.assertEqual(c_unichar_repr('\xa0'), '0xa0') |
| 4439 |
self.assertEqual(c_unichar_repr('\xff'), '0xff') |
| 4440 |
self.assertEqual(c_unichar_repr('\u20ac'), '0x20ac') |
| 4441 |
self.assertEqual(c_unichar_repr('\U0001f40d'), '0x1f40d') |
| 4442 |
|
| 4443 |
def test_indent_all_lines(self): |
| 4444 |
# Blank lines are expected to be unchanged. |
| 4445 |
self.assertEqual(libclinic.indent_all_lines("", prefix="bar"), "") |
| 4446 |
|
| 4447 |
lines = ( |
| 4448 |
"one\n" |
| 4449 |
"two" # The missing newline is deliberate. |
| 4450 |
) |
| 4451 |
expected = ( |
| 4452 |
"barone\n" |
| 4453 |
"bartwo" |
| 4454 |
) |
| 4455 |
out = libclinic.indent_all_lines(lines, prefix="bar") |
| 4456 |
self.assertEqual(out, expected) |
| 4457 |
|
| 4458 |
# If last line is empty, expect it to be unchanged. |
| 4459 |
lines = ( |
| 4460 |
"\n" |
| 4461 |
"one\n" |
| 4462 |
"two\n" |
| 4463 |
"" |
| 4464 |
) |
| 4465 |
expected = ( |
| 4466 |
"bar\n" |
| 4467 |
"barone\n" |
| 4468 |
"bartwo\n" |
| 4469 |
"" |
| 4470 |
) |
| 4471 |
out = libclinic.indent_all_lines(lines, prefix="bar") |
| 4472 |
self.assertEqual(out, expected) |
| 4473 |
|
| 4474 |
def test_suffix_all_lines(self): |
| 4475 |
# Blank lines are expected to be unchanged. |
| 4476 |
self.assertEqual(libclinic.suffix_all_lines("", suffix="foo"), "") |
| 4477 |
|
| 4478 |
lines = ( |
| 4479 |
"one\n" |
| 4480 |
"two" # The missing newline is deliberate. |
| 4481 |
) |
| 4482 |
expected = ( |
| 4483 |
"onefoo\n" |
| 4484 |
"twofoo" |
| 4485 |
) |
| 4486 |
out = libclinic.suffix_all_lines(lines, suffix="foo") |
| 4487 |
self.assertEqual(out, expected) |
| 4488 |
|
| 4489 |
# If last line is empty, expect it to be unchanged. |
| 4490 |
lines = ( |
| 4491 |
"\n" |
| 4492 |
"one\n" |
| 4493 |
"two\n" |
| 4494 |
"" |
| 4495 |
) |
| 4496 |
expected = ( |
| 4497 |
"foo\n" |
| 4498 |
"onefoo\n" |
| 4499 |
"twofoo\n" |
| 4500 |
"" |
| 4501 |
) |
| 4502 |
out = libclinic.suffix_all_lines(lines, suffix="foo") |
| 4503 |
self.assertEqual(out, expected) |
| 4504 |
|
| 4505 |
|
| 4506 |
class ClinicReprTests(unittest.TestCase): |
| 4507 |
def test_Block_repr(self): |
| 4508 |
block = Block("foo") |
| 4509 |
expected_repr = "<clinic.Block 'text' input='foo' output=None>" |
| 4510 |
self.assertEqual(repr(block), expected_repr) |
| 4511 |
|
| 4512 |
block2 = Block("bar", "baz", [], "eggs", "spam") |
| 4513 |
expected_repr_2 = "<clinic.Block 'baz' input='bar' output='eggs'>" |
| 4514 |
self.assertEqual(repr(block2), expected_repr_2) |
| 4515 |
|
| 4516 |
block3 = Block( |
| 4517 |
input="longboi_" * 100, |
| 4518 |
dsl_name="wow_so_long", |
| 4519 |
signatures=[], |
| 4520 |
output="very_long_" * 100, |
| 4521 |
indent="" |
| 4522 |
) |
| 4523 |
expected_repr_3 = ( |
| 4524 |
"<clinic.Block 'wow_so_long' input='longboi_longboi_longboi_l...' output='very_long_very_long_very_...'>" |
| 4525 |
) |
| 4526 |
self.assertEqual(repr(block3), expected_repr_3) |
| 4527 |
|
| 4528 |
def test_Destination_repr(self): |
| 4529 |
c = _make_clinic() |
| 4530 |
|
| 4531 |
destination = Destination( |
| 4532 |
"foo", type="file", clinic=c, args=("eggs",) |
| 4533 |
) |
| 4534 |
self.assertEqual( |
| 4535 |
repr(destination), "<clinic.Destination 'foo' type='file' file='eggs'>" |
| 4536 |
) |
| 4537 |
|
| 4538 |
destination2 = Destination("bar", type="buffer", clinic=c) |
| 4539 |
self.assertEqual(repr(destination2), "<clinic.Destination 'bar' type='buffer'>") |
| 4540 |
|
| 4541 |
def test_Module_repr(self): |
| 4542 |
module = Module("foo", _make_clinic()) |
| 4543 |
self.assertRegex(repr(module), r"<clinic.Module 'foo' at \d+>") |
| 4544 |
|
| 4545 |
def test_Class_repr(self): |
| 4546 |
cls = Class("foo", _make_clinic(), None, 'some_typedef', 'some_type_object') |
| 4547 |
self.assertRegex(repr(cls), r"<clinic.Class 'foo' at \d+>") |
| 4548 |
|
| 4549 |
def test_FunctionKind_repr(self): |
| 4550 |
self.assertEqual( |
| 4551 |
repr(FunctionKind.CLASS_METHOD), "<clinic.FunctionKind.CLASS_METHOD>" |
| 4552 |
) |
| 4553 |
|
| 4554 |
def test_Function_and_Parameter_reprs(self): |
| 4555 |
function = Function( |
| 4556 |
name='foo', |
| 4557 |
module=_make_clinic(), |
| 4558 |
cls=None, |
| 4559 |
c_basename=None, |
| 4560 |
full_name='foofoo', |
| 4561 |
return_converter=int_return_converter(), |
| 4562 |
kind=FunctionKind.METHOD_INIT, |
| 4563 |
coexist=False |
| 4564 |
) |
| 4565 |
self.assertEqual(repr(function), "<clinic.Function 'foo'>") |
| 4566 |
|
| 4567 |
converter = self_converter('bar', 'bar', function) |
| 4568 |
parameter = Parameter( |
| 4569 |
"bar", |
| 4570 |
kind=inspect.Parameter.POSITIONAL_OR_KEYWORD, |
| 4571 |
function=function, |
| 4572 |
converter=converter |
| 4573 |
) |
| 4574 |
self.assertEqual(repr(parameter), "<clinic.Parameter 'bar'>") |
| 4575 |
|
| 4576 |
def test_Monitor_repr(self): |
| 4577 |
monitor = libclinic.cpp.Monitor("test.c") |
| 4578 |
self.assertRegex(repr(monitor), r"<clinic.Monitor \d+ line=0 condition=''>") |
| 4579 |
|
| 4580 |
monitor.line_number = 42 |
| 4581 |
monitor.stack.append(("token1", "condition1")) |
| 4582 |
self.assertRegex( |
| 4583 |
repr(monitor), r"<clinic.Monitor \d+ line=42 condition='condition1'>" |
| 4584 |
) |
| 4585 |
|
| 4586 |
monitor.stack.append(("token2", "condition2")) |
| 4587 |
self.assertRegex( |
| 4588 |
repr(monitor), |
| 4589 |
r"<clinic.Monitor \d+ line=42 condition='condition1 && condition2'>" |
| 4590 |
) |
| 4591 |
|
| 4592 |
|
| 4593 |
if __name__ == "__main__": |
| 4594 |
unittest.main() |