Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | # |
| 3 | # Argument Clinic |
| 4 | # Copyright 2012-2013 by Larry Hastings. |
| 5 | # Licensed to the PSF under a contributor agreement. |
| 6 | # |
| 7 | |
| 8 | import abc |
| 9 | import ast |
| 10 | import atexit |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 11 | import collections |
| 12 | import contextlib |
| 13 | import functools |
| 14 | import hashlib |
| 15 | import inspect |
| 16 | import io |
| 17 | import itertools |
| 18 | import os |
Larry Hastings | bebf735 | 2014-01-17 17:47:17 -0800 | [diff] [blame] | 19 | import pprint |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 20 | import re |
| 21 | import shlex |
| 22 | import sys |
| 23 | import tempfile |
| 24 | import textwrap |
Georg Brandl | aabebde | 2014-01-16 06:53:54 +0100 | [diff] [blame] | 25 | import traceback |
Larry Hastings | bebf735 | 2014-01-17 17:47:17 -0800 | [diff] [blame] | 26 | import uuid |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 27 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 28 | # TODO: |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 29 | # |
| 30 | # soon: |
| 31 | # |
| 32 | # * allow mixing any two of {positional-only, positional-or-keyword, |
| 33 | # keyword-only} |
| 34 | # * dict constructor uses positional-only and keyword-only |
| 35 | # * max and min use positional only with an optional group |
| 36 | # and keyword-only |
| 37 | # |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 38 | |
Larry Hastings | ebdcb50 | 2013-11-23 14:54:00 -0800 | [diff] [blame] | 39 | version = '1' |
| 40 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 41 | _empty = inspect._empty |
| 42 | _void = inspect._void |
| 43 | |
Larry Hastings | 4a55fc5 | 2014-01-12 11:09:57 -0800 | [diff] [blame] | 44 | NoneType = type(None) |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 45 | |
| 46 | class Unspecified: |
| 47 | def __repr__(self): |
| 48 | return '<Unspecified>' |
| 49 | |
| 50 | unspecified = Unspecified() |
| 51 | |
| 52 | |
| 53 | class Null: |
| 54 | def __repr__(self): |
| 55 | return '<Null>' |
| 56 | |
| 57 | NULL = Null() |
| 58 | |
| 59 | |
Larry Hastings | 2a72791 | 2014-01-16 11:32:01 -0800 | [diff] [blame] | 60 | class Unknown: |
| 61 | def __repr__(self): |
| 62 | return '<Unknown>' |
| 63 | |
| 64 | unknown = Unknown() |
| 65 | |
| 66 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 67 | def _text_accumulator(): |
| 68 | text = [] |
| 69 | def output(): |
| 70 | s = ''.join(text) |
| 71 | text.clear() |
| 72 | return s |
| 73 | return text, text.append, output |
| 74 | |
| 75 | |
| 76 | def text_accumulator(): |
| 77 | """ |
| 78 | Creates a simple text accumulator / joiner. |
| 79 | |
| 80 | Returns a pair of callables: |
| 81 | append, output |
| 82 | "append" appends a string to the accumulator. |
| 83 | "output" returns the contents of the accumulator |
| 84 | joined together (''.join(accumulator)) and |
| 85 | empties the accumulator. |
| 86 | """ |
| 87 | text, append, output = _text_accumulator() |
| 88 | return append, output |
| 89 | |
| 90 | |
Larry Hastings | bebf735 | 2014-01-17 17:47:17 -0800 | [diff] [blame] | 91 | def warn_or_fail(fail=False, *args, filename=None, line_number=None): |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 92 | joined = " ".join([str(a) for a in args]) |
| 93 | add, output = text_accumulator() |
Larry Hastings | bebf735 | 2014-01-17 17:47:17 -0800 | [diff] [blame] | 94 | if fail: |
| 95 | add("Error") |
| 96 | else: |
| 97 | add("Warning") |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 98 | if clinic: |
| 99 | if filename is None: |
| 100 | filename = clinic.filename |
| 101 | if clinic.block_parser and (line_number is None): |
| 102 | line_number = clinic.block_parser.line_number |
| 103 | if filename is not None: |
| 104 | add(' in file "' + filename + '"') |
| 105 | if line_number is not None: |
| 106 | add(" on line " + str(line_number)) |
| 107 | add(':\n') |
| 108 | add(joined) |
| 109 | print(output()) |
Larry Hastings | bebf735 | 2014-01-17 17:47:17 -0800 | [diff] [blame] | 110 | if fail: |
| 111 | sys.exit(-1) |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 112 | |
| 113 | |
Larry Hastings | bebf735 | 2014-01-17 17:47:17 -0800 | [diff] [blame] | 114 | def warn(*args, filename=None, line_number=None): |
| 115 | return warn_or_fail(False, *args, filename=filename, line_number=line_number) |
| 116 | |
| 117 | def fail(*args, filename=None, line_number=None): |
| 118 | return warn_or_fail(True, *args, filename=filename, line_number=line_number) |
| 119 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 120 | |
| 121 | def quoted_for_c_string(s): |
| 122 | for old, new in ( |
| 123 | ('"', '\\"'), |
| 124 | ("'", "\\'"), |
| 125 | ): |
| 126 | s = s.replace(old, new) |
| 127 | return s |
| 128 | |
Larry Hastings | 4903e00 | 2014-01-18 00:26:16 -0800 | [diff] [blame] | 129 | def c_repr(s): |
| 130 | return '"' + s + '"' |
| 131 | |
| 132 | |
Larry Hastings | dfcd467 | 2013-10-27 02:49:39 -0700 | [diff] [blame] | 133 | is_legal_c_identifier = re.compile('^[A-Za-z_][A-Za-z0-9_]*$').match |
| 134 | |
| 135 | def is_legal_py_identifier(s): |
| 136 | return all(is_legal_c_identifier(field) for field in s.split('.')) |
| 137 | |
Larry Hastings | bebf735 | 2014-01-17 17:47:17 -0800 | [diff] [blame] | 138 | # identifiers that are okay in Python but aren't a good idea in C. |
| 139 | # so if they're used Argument Clinic will add "_value" to the end |
| 140 | # of the name in C. |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 141 | c_keywords = set(""" |
Larry Hastings | bebf735 | 2014-01-17 17:47:17 -0800 | [diff] [blame] | 142 | asm auto break case char cls const continue default do double |
Larry Hastings | ed4a1c5 | 2013-11-18 09:32:13 -0800 | [diff] [blame] | 143 | else enum extern float for goto if inline int long module null |
| 144 | register return self short signed sizeof static struct switch |
Larry Hastings | bebf735 | 2014-01-17 17:47:17 -0800 | [diff] [blame] | 145 | typedef typeof union unsigned void volatile while |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 146 | """.strip().split()) |
| 147 | |
Larry Hastings | dfcd467 | 2013-10-27 02:49:39 -0700 | [diff] [blame] | 148 | def ensure_legal_c_identifier(s): |
| 149 | # for now, just complain if what we're given isn't legal |
| 150 | if not is_legal_c_identifier(s): |
| 151 | fail("Illegal C identifier: {}".format(s)) |
| 152 | # but if we picked a C keyword, pick something else |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 153 | if s in c_keywords: |
| 154 | return s + "_value" |
| 155 | return s |
| 156 | |
| 157 | def rstrip_lines(s): |
| 158 | text, add, output = _text_accumulator() |
| 159 | for line in s.split('\n'): |
| 160 | add(line.rstrip()) |
| 161 | add('\n') |
| 162 | text.pop() |
| 163 | return output() |
| 164 | |
| 165 | def linear_format(s, **kwargs): |
| 166 | """ |
| 167 | Perform str.format-like substitution, except: |
| 168 | * The strings substituted must be on lines by |
| 169 | themselves. (This line is the "source line".) |
| 170 | * If the substitution text is empty, the source line |
| 171 | is removed in the output. |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 172 | * If the field is not recognized, the original line |
| 173 | is passed unmodified through to the output. |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 174 | * If the substitution text is not empty: |
| 175 | * Each line of the substituted text is indented |
| 176 | by the indent of the source line. |
| 177 | * A newline will be added to the end. |
| 178 | """ |
| 179 | |
| 180 | add, output = text_accumulator() |
| 181 | for line in s.split('\n'): |
| 182 | indent, curly, trailing = line.partition('{') |
| 183 | if not curly: |
| 184 | add(line) |
| 185 | add('\n') |
| 186 | continue |
| 187 | |
| 188 | name, curl, trailing = trailing.partition('}') |
| 189 | if not curly or name not in kwargs: |
| 190 | add(line) |
| 191 | add('\n') |
| 192 | continue |
| 193 | |
| 194 | if trailing: |
| 195 | fail("Text found after {" + name + "} block marker! It must be on a line by itself.") |
| 196 | if indent.strip(): |
| 197 | fail("Non-whitespace characters found before {" + name + "} block marker! It must be on a line by itself.") |
| 198 | |
| 199 | value = kwargs[name] |
| 200 | if not value: |
| 201 | continue |
| 202 | |
| 203 | value = textwrap.indent(rstrip_lines(value), indent) |
| 204 | add(value) |
| 205 | add('\n') |
| 206 | |
| 207 | return output()[:-1] |
| 208 | |
Larry Hastings | bebf735 | 2014-01-17 17:47:17 -0800 | [diff] [blame] | 209 | def indent_all_lines(s, prefix): |
| 210 | """ |
| 211 | Returns 's', with 'prefix' prepended to all lines. |
| 212 | |
| 213 | If the last line is empty, prefix is not prepended |
| 214 | to it. (If s is blank, returns s unchanged.) |
| 215 | |
| 216 | (textwrap.indent only adds to non-blank lines.) |
| 217 | """ |
| 218 | split = s.split('\n') |
| 219 | last = split.pop() |
| 220 | final = [] |
| 221 | for line in split: |
| 222 | final.append(prefix) |
| 223 | final.append(line) |
| 224 | final.append('\n') |
| 225 | if last: |
| 226 | final.append(prefix) |
| 227 | final.append(last) |
| 228 | return ''.join(final) |
| 229 | |
| 230 | def suffix_all_lines(s, suffix): |
| 231 | """ |
| 232 | Returns 's', with 'suffix' appended to all lines. |
| 233 | |
| 234 | If the last line is empty, suffix is not appended |
| 235 | to it. (If s is blank, returns s unchanged.) |
| 236 | """ |
| 237 | split = s.split('\n') |
| 238 | last = split.pop() |
| 239 | final = [] |
| 240 | for line in split: |
| 241 | final.append(line) |
| 242 | final.append(suffix) |
| 243 | final.append('\n') |
| 244 | if last: |
| 245 | final.append(last) |
| 246 | final.append(suffix) |
| 247 | return ''.join(final) |
| 248 | |
| 249 | |
Larry Hastings | ebdcb50 | 2013-11-23 14:54:00 -0800 | [diff] [blame] | 250 | def version_splitter(s): |
| 251 | """Splits a version string into a tuple of integers. |
| 252 | |
| 253 | The following ASCII characters are allowed, and employ |
| 254 | the following conversions: |
| 255 | a -> -3 |
| 256 | b -> -2 |
| 257 | c -> -1 |
| 258 | (This permits Python-style version strings such as "1.4b3".) |
| 259 | """ |
| 260 | version = [] |
| 261 | accumulator = [] |
| 262 | def flush(): |
| 263 | if not accumulator: |
Larry Hastings | 2a72791 | 2014-01-16 11:32:01 -0800 | [diff] [blame] | 264 | raise ValueError('Unsupported version string: ' + repr(s)) |
Larry Hastings | ebdcb50 | 2013-11-23 14:54:00 -0800 | [diff] [blame] | 265 | version.append(int(''.join(accumulator))) |
| 266 | accumulator.clear() |
| 267 | |
| 268 | for c in s: |
| 269 | if c.isdigit(): |
| 270 | accumulator.append(c) |
| 271 | elif c == '.': |
| 272 | flush() |
| 273 | elif c in 'abc': |
| 274 | flush() |
| 275 | version.append('abc'.index(c) - 3) |
| 276 | else: |
| 277 | raise ValueError('Illegal character ' + repr(c) + ' in version string ' + repr(s)) |
| 278 | flush() |
| 279 | return tuple(version) |
| 280 | |
| 281 | def version_comparitor(version1, version2): |
| 282 | iterator = itertools.zip_longest(version_splitter(version1), version_splitter(version2), fillvalue=0) |
| 283 | for i, (a, b) in enumerate(iterator): |
| 284 | if a < b: |
| 285 | return -1 |
| 286 | if a > b: |
| 287 | return 1 |
| 288 | return 0 |
| 289 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 290 | |
| 291 | class CRenderData: |
| 292 | def __init__(self): |
| 293 | |
| 294 | # The C statements to declare variables. |
| 295 | # Should be full lines with \n eol characters. |
| 296 | self.declarations = [] |
| 297 | |
| 298 | # The C statements required to initialize the variables before the parse call. |
| 299 | # Should be full lines with \n eol characters. |
| 300 | self.initializers = [] |
| 301 | |
| 302 | # The entries for the "keywords" array for PyArg_ParseTuple. |
| 303 | # Should be individual strings representing the names. |
| 304 | self.keywords = [] |
| 305 | |
| 306 | # The "format units" for PyArg_ParseTuple. |
| 307 | # Should be individual strings that will get |
| 308 | self.format_units = [] |
| 309 | |
| 310 | # The varargs arguments for PyArg_ParseTuple. |
| 311 | self.parse_arguments = [] |
| 312 | |
| 313 | # The parameter declarations for the impl function. |
| 314 | self.impl_parameters = [] |
| 315 | |
| 316 | # The arguments to the impl function at the time it's called. |
| 317 | self.impl_arguments = [] |
| 318 | |
| 319 | # For return converters: the name of the variable that |
| 320 | # should receive the value returned by the impl. |
| 321 | self.return_value = "return_value" |
| 322 | |
| 323 | # For return converters: the code to convert the return |
| 324 | # value from the parse function. This is also where |
| 325 | # you should check the _return_value for errors, and |
| 326 | # "goto exit" if there are any. |
| 327 | self.return_conversion = [] |
| 328 | |
| 329 | # The C statements required to clean up after the impl call. |
| 330 | self.cleanup = [] |
| 331 | |
| 332 | |
| 333 | class Language(metaclass=abc.ABCMeta): |
| 334 | |
| 335 | start_line = "" |
| 336 | body_prefix = "" |
| 337 | stop_line = "" |
| 338 | checksum_line = "" |
| 339 | |
| 340 | @abc.abstractmethod |
Larry Hastings | bebf735 | 2014-01-17 17:47:17 -0800 | [diff] [blame] | 341 | def render(self, clinic, signatures): |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 342 | pass |
| 343 | |
| 344 | def validate(self): |
| 345 | def assert_only_one(field, token='dsl_name'): |
| 346 | line = getattr(self, field) |
| 347 | token = '{' + token + '}' |
| 348 | if len(line.split(token)) != 2: |
| 349 | fail(self.__class__.__name__ + " " + field + " must contain " + token + " exactly once!") |
| 350 | assert_only_one('start_line') |
| 351 | assert_only_one('stop_line') |
| 352 | assert_only_one('checksum_line') |
| 353 | assert_only_one('checksum_line', 'checksum') |
| 354 | |
| 355 | if len(self.body_prefix.split('{dsl_name}')) >= 3: |
| 356 | fail(self.__class__.__name__ + " body_prefix may contain " + token + " once at most!") |
| 357 | |
| 358 | |
| 359 | |
| 360 | class PythonLanguage(Language): |
| 361 | |
| 362 | language = 'Python' |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 363 | start_line = "#/*[{dsl_name} input]" |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 364 | body_prefix = "#" |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 365 | stop_line = "#[{dsl_name} start generated code]*/" |
| 366 | checksum_line = "#/*[{dsl_name} end generated code: checksum={checksum}]*/" |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 367 | |
| 368 | |
| 369 | def permute_left_option_groups(l): |
| 370 | """ |
| 371 | Given [1, 2, 3], should yield: |
| 372 | () |
| 373 | (3,) |
| 374 | (2, 3) |
| 375 | (1, 2, 3) |
| 376 | """ |
| 377 | yield tuple() |
| 378 | accumulator = [] |
| 379 | for group in reversed(l): |
| 380 | accumulator = list(group) + accumulator |
| 381 | yield tuple(accumulator) |
| 382 | |
| 383 | |
| 384 | def permute_right_option_groups(l): |
| 385 | """ |
| 386 | Given [1, 2, 3], should yield: |
| 387 | () |
| 388 | (1,) |
| 389 | (1, 2) |
| 390 | (1, 2, 3) |
| 391 | """ |
| 392 | yield tuple() |
| 393 | accumulator = [] |
| 394 | for group in l: |
| 395 | accumulator.extend(group) |
| 396 | yield tuple(accumulator) |
| 397 | |
| 398 | |
| 399 | def permute_optional_groups(left, required, right): |
| 400 | """ |
| 401 | Generator function that computes the set of acceptable |
| 402 | argument lists for the provided iterables of |
| 403 | argument groups. (Actually it generates a tuple of tuples.) |
| 404 | |
| 405 | Algorithm: prefer left options over right options. |
| 406 | |
| 407 | If required is empty, left must also be empty. |
| 408 | """ |
| 409 | required = tuple(required) |
| 410 | result = [] |
| 411 | |
| 412 | if not required: |
| 413 | assert not left |
| 414 | |
| 415 | accumulator = [] |
| 416 | counts = set() |
| 417 | for r in permute_right_option_groups(right): |
| 418 | for l in permute_left_option_groups(left): |
| 419 | t = l + required + r |
| 420 | if len(t) in counts: |
| 421 | continue |
| 422 | counts.add(len(t)) |
| 423 | accumulator.append(t) |
| 424 | |
| 425 | accumulator.sort(key=len) |
| 426 | return tuple(accumulator) |
| 427 | |
| 428 | |
| 429 | class CLanguage(Language): |
| 430 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 431 | body_prefix = "#" |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 432 | language = 'C' |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 433 | start_line = "/*[{dsl_name} input]" |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 434 | body_prefix = "" |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 435 | stop_line = "[{dsl_name} start generated code]*/" |
| 436 | checksum_line = "/*[{dsl_name} end generated code: checksum={checksum}]*/" |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 437 | |
Larry Hastings | bebf735 | 2014-01-17 17:47:17 -0800 | [diff] [blame] | 438 | def render(self, clinic, signatures): |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 439 | function = None |
| 440 | for o in signatures: |
| 441 | if isinstance(o, Function): |
| 442 | if function: |
| 443 | fail("You may specify at most one function per block.\nFound a block containing at least two:\n\t" + repr(function) + " and " + repr(o)) |
| 444 | function = o |
Larry Hastings | bebf735 | 2014-01-17 17:47:17 -0800 | [diff] [blame] | 445 | return self.render_function(clinic, function) |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 446 | |
| 447 | def docstring_for_c_string(self, f): |
| 448 | text, add, output = _text_accumulator() |
| 449 | # turn docstring into a properly quoted C string |
| 450 | for line in f.docstring.split('\n'): |
| 451 | add('"') |
| 452 | add(quoted_for_c_string(line)) |
| 453 | add('\\n"\n') |
| 454 | |
| 455 | text.pop() |
| 456 | add('"') |
| 457 | return ''.join(text) |
| 458 | |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 459 | _templates = {} |
| 460 | # the templates will be run through str.format(), |
| 461 | # so actual curly-braces need to be doubled up. |
| 462 | templates_source = """ |
| 463 | __________________________________________________ |
| 464 | |
| 465 | docstring_prototype |
| 466 | |
| 467 | PyDoc_VAR({c_basename}__doc__); |
| 468 | __________________________________________________ |
| 469 | |
| 470 | docstring_definition |
| 471 | |
| 472 | PyDoc_STRVAR({c_basename}__doc__, |
| 473 | {docstring}); |
| 474 | __________________________________________________ |
| 475 | |
| 476 | impl_definition |
| 477 | |
| 478 | static {impl_return_type} |
| 479 | {c_basename}_impl({impl_parameters}) |
| 480 | __________________________________________________ |
| 481 | |
| 482 | parser_prototype_noargs |
| 483 | |
| 484 | static PyObject * |
| 485 | {c_basename}({self_type}{self_name}, PyObject *Py_UNUSED(ignored)) |
| 486 | __________________________________________________ |
| 487 | |
| 488 | parser_prototype_meth_o |
| 489 | |
| 490 | # SLIGHT HACK |
| 491 | # METH_O uses {impl_parameters} for the parser! |
| 492 | |
| 493 | static PyObject * |
| 494 | {c_basename}({impl_parameters}) |
| 495 | __________________________________________________ |
| 496 | |
| 497 | parser_prototype_varargs |
| 498 | |
| 499 | static PyObject * |
| 500 | {c_basename}({self_type}{self_name}, PyObject *args) |
| 501 | __________________________________________________ |
| 502 | |
| 503 | parser_prototype_keyword |
| 504 | |
| 505 | static PyObject * |
| 506 | {c_basename}({self_type}{self_name}, PyObject *args, PyObject *kwargs) |
| 507 | __________________________________________________ |
| 508 | |
| 509 | parser_prototype_init |
| 510 | |
| 511 | static int |
| 512 | {c_basename}({self_type}{self_name}, PyObject *args, PyObject *kwargs) |
| 513 | __________________________________________________ |
| 514 | |
| 515 | parser_definition_simple_no_parsing |
| 516 | |
| 517 | {{ |
| 518 | return {c_basename}_impl({impl_arguments}); |
| 519 | }} |
| 520 | __________________________________________________ |
| 521 | |
| 522 | parser_definition_start |
| 523 | |
| 524 | {{ |
| 525 | {return_value_declaration} |
| 526 | {declarations} |
| 527 | {initializers} |
| 528 | {empty line} |
| 529 | __________________________________________________ |
| 530 | |
| 531 | parser_definition_end |
| 532 | |
| 533 | {return_conversion} |
| 534 | |
| 535 | {exit_label} |
| 536 | {cleanup} |
| 537 | return return_value; |
| 538 | }} |
| 539 | __________________________________________________ |
| 540 | |
| 541 | parser_definition_impl_call |
| 542 | |
| 543 | {return_value} = {c_basename}_impl({impl_arguments}); |
| 544 | __________________________________________________ |
| 545 | |
| 546 | parser_definition_unpack_tuple |
| 547 | |
| 548 | if (!PyArg_UnpackTuple(args, "{name}", |
| 549 | {unpack_min}, {unpack_max}, |
| 550 | {parse_arguments})) |
| 551 | goto exit; |
| 552 | __________________________________________________ |
| 553 | |
| 554 | parser_definition_parse_tuple |
| 555 | |
| 556 | if (!PyArg_ParseTuple(args, |
| 557 | "{format_units}:{name}", |
| 558 | {parse_arguments})) |
| 559 | goto exit; |
| 560 | __________________________________________________ |
| 561 | |
| 562 | parser_definition_option_groups |
| 563 | {option_group_parsing} |
| 564 | |
| 565 | __________________________________________________ |
| 566 | |
| 567 | parser_definition_parse_tuple_and_keywords |
| 568 | |
| 569 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, |
| 570 | "{format_units}:{name}", _keywords, |
| 571 | {parse_arguments})) |
| 572 | goto exit; |
| 573 | __________________________________________________ |
| 574 | |
| 575 | parser_definition_no_positional |
| 576 | |
| 577 | if (!_PyArg_NoPositional("{name}", args)) |
| 578 | goto exit; |
| 579 | |
| 580 | __________________________________________________ |
| 581 | |
| 582 | parser_definition_no_keywords |
| 583 | |
| 584 | if (!_PyArg_NoKeywords("{name}", kwargs)) |
| 585 | goto exit; |
| 586 | |
| 587 | __________________________________________________ |
| 588 | |
| 589 | methoddef_define |
| 590 | |
| 591 | #define {methoddef_name} \\ |
| 592 | {{"{name}", (PyCFunction){c_basename}, {methoddef_flags}, {c_basename}__doc__}}, |
| 593 | __________________________________________________ |
| 594 | """.rstrip() |
| 595 | |
| 596 | title = '' |
| 597 | buffer = [] |
| 598 | line = None |
| 599 | for line in templates_source.split('\n'): |
| 600 | line = line.rstrip() |
| 601 | if line.startswith('# '): |
| 602 | # comment |
| 603 | continue |
| 604 | if line.startswith("_____"): |
| 605 | if not buffer: |
| 606 | continue |
| 607 | assert title not in _templates, "defined template twice: " + repr(title) |
| 608 | buffer = '\n'.join(buffer).rstrip() |
| 609 | buffer = buffer.replace('{empty line}', '') |
| 610 | _templates[title] = buffer |
| 611 | buffer = [] |
| 612 | title = '' |
| 613 | continue |
| 614 | if not title: |
| 615 | if not line: |
| 616 | continue |
| 617 | title = line |
| 618 | continue |
| 619 | if not (line or buffer): |
| 620 | # throw away leading blank lines |
| 621 | continue |
| 622 | buffer.append(line) |
| 623 | |
| 624 | assert not title, 'ensure templates_source ends with ______ (still adding to ' + repr(title) + ")" |
| 625 | |
| 626 | del templates_source |
| 627 | del title |
| 628 | del buffer |
| 629 | del line |
| 630 | |
| 631 | # for name, value in _templates.items(): |
| 632 | # print(name + ":") |
| 633 | # pprint.pprint(value) |
| 634 | # print() |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 635 | |
Larry Hastings | bebf735 | 2014-01-17 17:47:17 -0800 | [diff] [blame] | 636 | def output_templates(self, f): |
| 637 | parameters = list(f.parameters.values()) |
| 638 | converters = [p.converter for p in parameters] |
| 639 | |
| 640 | has_option_groups = parameters and (parameters[0].group or parameters[-1].group) |
| 641 | default_return_converter = (not f.return_converter or |
| 642 | f.return_converter.type == 'PyObject *') |
| 643 | |
| 644 | positional = parameters and (parameters[-1].kind == inspect.Parameter.POSITIONAL_ONLY) |
| 645 | all_boring_objects = False # yes, this will be false if there are 0 parameters, it's fine |
| 646 | first_optional = len(parameters) |
| 647 | for i, p in enumerate(parameters): |
| 648 | c = p.converter |
| 649 | if type(c) != object_converter: |
| 650 | break |
| 651 | if c.format_unit != 'O': |
| 652 | break |
| 653 | if p.default is not unspecified: |
| 654 | first_optional = min(first_optional, i) |
| 655 | else: |
| 656 | all_boring_objects = True |
| 657 | |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 658 | new_or_init = f.kind in (METHOD_NEW, METHOD_INIT) |
| 659 | |
Larry Hastings | bebf735 | 2014-01-17 17:47:17 -0800 | [diff] [blame] | 660 | meth_o = (len(parameters) == 1 and |
| 661 | parameters[0].kind == inspect.Parameter.POSITIONAL_ONLY and |
| 662 | not converters[0].is_optional() and |
| 663 | isinstance(converters[0], object_converter) and |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 664 | converters[0].format_unit == 'O' and |
| 665 | not new_or_init) |
Larry Hastings | bebf735 | 2014-01-17 17:47:17 -0800 | [diff] [blame] | 666 | |
| 667 | # we have to set seven things before we're done: |
| 668 | # |
| 669 | # docstring_prototype |
| 670 | # docstring_definition |
| 671 | # impl_prototype |
| 672 | # methoddef_define |
| 673 | # parser_prototype |
| 674 | # parser_definition |
| 675 | # impl_definition |
Larry Hastings | bebf735 | 2014-01-17 17:47:17 -0800 | [diff] [blame] | 676 | |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 677 | templates = self._templates |
Larry Hastings | bebf735 | 2014-01-17 17:47:17 -0800 | [diff] [blame] | 678 | |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 679 | return_value_declaration = "PyObject *return_value = NULL;" |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 680 | |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 681 | methoddef_define = templates['methoddef_define'] |
| 682 | docstring_prototype = templates['docstring_prototype'] |
| 683 | docstring_definition = templates['docstring_definition'] |
| 684 | impl_definition = templates['impl_definition'] |
Larry Hastings | bebf735 | 2014-01-17 17:47:17 -0800 | [diff] [blame] | 685 | impl_prototype = parser_prototype = parser_definition = None |
| 686 | |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 687 | parser_body_fields = None |
| 688 | def parser_body(prototype, *fields): |
| 689 | nonlocal parser_body_fields |
| 690 | add, output = text_accumulator() |
| 691 | add(prototype) |
| 692 | parser_body_fields = fields |
| 693 | fields = list(fields) |
| 694 | fields.insert(0, 'parser_definition_start') |
| 695 | fields.append('parser_definition_impl_call') |
| 696 | fields.append('parser_definition_end') |
| 697 | for field in fields: |
| 698 | add('\n') |
| 699 | add(templates[field]) |
| 700 | return output() |
Larry Hastings | bebf735 | 2014-01-17 17:47:17 -0800 | [diff] [blame] | 701 | |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 702 | def insert_keywords(s): |
| 703 | return linear_format(s, declarations="static char *_keywords[] = {{{keywords}, NULL}};\n{declarations}") |
Larry Hastings | bebf735 | 2014-01-17 17:47:17 -0800 | [diff] [blame] | 704 | |
| 705 | if not parameters: |
| 706 | # no parameters, METH_NOARGS |
| 707 | |
| 708 | flags = "METH_NOARGS" |
| 709 | |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 710 | parser_prototype = templates['parser_prototype_noargs'] |
| 711 | parser_definition = parser_prototype |
Larry Hastings | bebf735 | 2014-01-17 17:47:17 -0800 | [diff] [blame] | 712 | |
| 713 | if default_return_converter: |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 714 | parser_definition = parser_prototype + '\n' + templates['parser_definition_simple_no_parsing'] |
Larry Hastings | bebf735 | 2014-01-17 17:47:17 -0800 | [diff] [blame] | 715 | else: |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 716 | parser_definition = parser_body(parser_prototype) |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 717 | |
Larry Hastings | bebf735 | 2014-01-17 17:47:17 -0800 | [diff] [blame] | 718 | elif meth_o: |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 719 | flags = "METH_O" |
| 720 | # impl_definition = templates['parser_prototype_meth_o'] |
| 721 | |
Larry Hastings | bebf735 | 2014-01-17 17:47:17 -0800 | [diff] [blame] | 722 | if default_return_converter: |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 723 | # maps perfectly to METH_O, doesn't need a return converter. |
| 724 | # so we skip making a parse function |
| 725 | # and call directly into the impl function. |
Larry Hastings | bebf735 | 2014-01-17 17:47:17 -0800 | [diff] [blame] | 726 | impl_prototype = parser_prototype = parser_definition = '' |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 727 | impl_definition = templates['parser_prototype_meth_o'] |
Larry Hastings | bebf735 | 2014-01-17 17:47:17 -0800 | [diff] [blame] | 728 | else: |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 729 | parser_prototype = templates['parser_prototype_meth_o'] |
| 730 | parser_definition = parser_body(parser_prototype) |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 731 | |
Larry Hastings | bebf735 | 2014-01-17 17:47:17 -0800 | [diff] [blame] | 732 | elif has_option_groups: |
| 733 | # positional parameters with option groups |
| 734 | # (we have to generate lots of PyArg_ParseTuple calls |
| 735 | # in a big switch statement) |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 736 | |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 737 | flags = "METH_VARARGS" |
| 738 | parser_prototype = templates['parser_prototype_varargs'] |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 739 | |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 740 | parser_definition = parser_body(parser_prototype, 'parser_definition_option_groups') |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 741 | |
Larry Hastings | bebf735 | 2014-01-17 17:47:17 -0800 | [diff] [blame] | 742 | elif positional and all_boring_objects: |
| 743 | # positional-only, but no option groups, |
| 744 | # and nothing but normal objects: |
| 745 | # PyArg_UnpackTuple! |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 746 | |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 747 | flags = "METH_VARARGS" |
| 748 | parser_prototype = templates['parser_prototype_varargs'] |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 749 | |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 750 | parser_definition = parser_body(parser_prototype, 'parser_definition_unpack_tuple') |
Larry Hastings | bebf735 | 2014-01-17 17:47:17 -0800 | [diff] [blame] | 751 | |
| 752 | elif positional: |
| 753 | # positional-only, but no option groups |
| 754 | # we only need one call to PyArg_ParseTuple |
| 755 | |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 756 | flags = "METH_VARARGS" |
| 757 | parser_prototype = templates['parser_prototype_varargs'] |
Larry Hastings | bebf735 | 2014-01-17 17:47:17 -0800 | [diff] [blame] | 758 | |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 759 | parser_definition = parser_body(parser_prototype, 'parser_definition_parse_tuple') |
Larry Hastings | bebf735 | 2014-01-17 17:47:17 -0800 | [diff] [blame] | 760 | |
| 761 | else: |
| 762 | # positional-or-keyword arguments |
| 763 | flags = "METH_VARARGS|METH_KEYWORDS" |
| 764 | |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 765 | parser_prototype = templates['parser_prototype_keyword'] |
Larry Hastings | bebf735 | 2014-01-17 17:47:17 -0800 | [diff] [blame] | 766 | |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 767 | parser_definition = parser_body(parser_prototype, 'parser_definition_parse_tuple_and_keywords') |
| 768 | parser_definition = insert_keywords(parser_definition) |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 769 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 770 | |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 771 | if new_or_init: |
| 772 | methoddef_define = '' |
| 773 | |
| 774 | if f.kind == METHOD_NEW: |
| 775 | parser_prototype = templates['parser_prototype_keyword'] |
| 776 | else: |
| 777 | return_value_declaration = "int return_value = -1;" |
| 778 | parser_prototype = templates['parser_prototype_init'] |
| 779 | |
| 780 | fields = list(parser_body_fields) |
| 781 | parses_positional = 'METH_NOARGS' not in flags |
| 782 | parses_keywords = 'METH_KEYWORDS' in flags |
| 783 | if parses_keywords: |
| 784 | assert parses_positional |
| 785 | |
| 786 | if not parses_keywords: |
| 787 | fields.insert(0, 'parser_definition_no_keywords') |
| 788 | if not parses_positional: |
| 789 | fields.insert(0, 'parser_definition_no_positional') |
| 790 | |
| 791 | parser_definition = parser_body(parser_prototype, *fields) |
| 792 | if parses_keywords: |
| 793 | parser_definition = insert_keywords(parser_definition) |
| 794 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 795 | |
Larry Hastings | bebf735 | 2014-01-17 17:47:17 -0800 | [diff] [blame] | 796 | if f.methoddef_flags: |
Larry Hastings | bebf735 | 2014-01-17 17:47:17 -0800 | [diff] [blame] | 797 | flags += '|' + f.methoddef_flags |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 798 | |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 799 | methoddef_define = methoddef_define.replace('{methoddef_flags}', flags) |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 800 | |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 801 | # add ';' to the end of parser_prototype and impl_prototype |
| 802 | # (they mustn't be None, but they could be an empty string.) |
Larry Hastings | bebf735 | 2014-01-17 17:47:17 -0800 | [diff] [blame] | 803 | assert parser_prototype is not None |
Larry Hastings | bebf735 | 2014-01-17 17:47:17 -0800 | [diff] [blame] | 804 | if parser_prototype: |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 805 | assert not parser_prototype.endswith(';') |
Larry Hastings | bebf735 | 2014-01-17 17:47:17 -0800 | [diff] [blame] | 806 | parser_prototype += ';' |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 807 | |
Larry Hastings | bebf735 | 2014-01-17 17:47:17 -0800 | [diff] [blame] | 808 | if impl_prototype is None: |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 809 | impl_prototype = impl_definition |
| 810 | if impl_prototype: |
| 811 | impl_prototype += ";" |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 812 | |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 813 | parser_definition = parser_definition.replace("{return_value_declaration}", return_value_declaration) |
Larry Hastings | bebf735 | 2014-01-17 17:47:17 -0800 | [diff] [blame] | 814 | |
| 815 | d = { |
| 816 | "docstring_prototype" : docstring_prototype, |
| 817 | "docstring_definition" : docstring_definition, |
| 818 | "impl_prototype" : impl_prototype, |
| 819 | "methoddef_define" : methoddef_define, |
| 820 | "parser_prototype" : parser_prototype, |
| 821 | "parser_definition" : parser_definition, |
| 822 | "impl_definition" : impl_definition, |
| 823 | } |
| 824 | |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 825 | # make sure we didn't forget to assign something, |
| 826 | # and wrap each non-empty value in \n's |
Larry Hastings | bebf735 | 2014-01-17 17:47:17 -0800 | [diff] [blame] | 827 | d2 = {} |
| 828 | for name, value in d.items(): |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 829 | assert value is not None, "got a None value for template " + repr(name) |
Larry Hastings | bebf735 | 2014-01-17 17:47:17 -0800 | [diff] [blame] | 830 | if value: |
| 831 | value = '\n' + value + '\n' |
| 832 | d2[name] = value |
| 833 | return d2 |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 834 | |
| 835 | @staticmethod |
| 836 | def group_to_variable_name(group): |
| 837 | adjective = "left_" if group < 0 else "right_" |
| 838 | return "group_" + adjective + str(abs(group)) |
| 839 | |
| 840 | def render_option_group_parsing(self, f, template_dict): |
| 841 | # positional only, grouped, optional arguments! |
| 842 | # can be optional on the left or right. |
| 843 | # here's an example: |
| 844 | # |
| 845 | # [ [ [ A1 A2 ] B1 B2 B3 ] C1 C2 ] D1 D2 D3 [ E1 E2 E3 [ F1 F2 F3 ] ] |
| 846 | # |
| 847 | # Here group D are required, and all other groups are optional. |
| 848 | # (Group D's "group" is actually None.) |
| 849 | # We can figure out which sets of arguments we have based on |
| 850 | # how many arguments are in the tuple. |
| 851 | # |
| 852 | # Note that you need to count up on both sides. For example, |
| 853 | # you could have groups C+D, or C+D+E, or C+D+E+F. |
| 854 | # |
| 855 | # What if the number of arguments leads us to an ambiguous result? |
| 856 | # Clinic prefers groups on the left. So in the above example, |
| 857 | # five arguments would map to B+C, not C+D. |
| 858 | |
| 859 | add, output = text_accumulator() |
| 860 | parameters = list(f.parameters.values()) |
| 861 | |
| 862 | groups = [] |
| 863 | group = None |
| 864 | left = [] |
| 865 | right = [] |
| 866 | required = [] |
| 867 | last = unspecified |
| 868 | |
| 869 | for p in parameters: |
| 870 | group_id = p.group |
| 871 | if group_id != last: |
| 872 | last = group_id |
| 873 | group = [] |
| 874 | if group_id < 0: |
| 875 | left.append(group) |
| 876 | elif group_id == 0: |
| 877 | group = required |
| 878 | else: |
| 879 | right.append(group) |
| 880 | group.append(p) |
| 881 | |
| 882 | count_min = sys.maxsize |
| 883 | count_max = -1 |
| 884 | |
Larry Hastings | 2a72791 | 2014-01-16 11:32:01 -0800 | [diff] [blame] | 885 | add("switch (PyTuple_GET_SIZE(args)) {{\n") |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 886 | for subset in permute_optional_groups(left, required, right): |
| 887 | count = len(subset) |
| 888 | count_min = min(count_min, count) |
| 889 | count_max = max(count_max, count) |
| 890 | |
Larry Hastings | 583baa8 | 2014-01-12 08:49:30 -0800 | [diff] [blame] | 891 | if count == 0: |
| 892 | add(""" case 0: |
| 893 | break; |
| 894 | """) |
| 895 | continue |
| 896 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 897 | group_ids = {p.group for p in subset} # eliminate duplicates |
| 898 | d = {} |
| 899 | d['count'] = count |
| 900 | d['name'] = f.name |
| 901 | d['groups'] = sorted(group_ids) |
| 902 | d['format_units'] = "".join(p.converter.format_unit for p in subset) |
| 903 | |
| 904 | parse_arguments = [] |
| 905 | for p in subset: |
| 906 | p.converter.parse_argument(parse_arguments) |
| 907 | d['parse_arguments'] = ", ".join(parse_arguments) |
| 908 | |
| 909 | group_ids.discard(0) |
| 910 | lines = [self.group_to_variable_name(g) + " = 1;" for g in group_ids] |
| 911 | lines = "\n".join(lines) |
| 912 | |
| 913 | s = """ |
| 914 | case {count}: |
| 915 | if (!PyArg_ParseTuple(args, "{format_units}:{name}", {parse_arguments})) |
| 916 | return NULL; |
| 917 | {group_booleans} |
| 918 | break; |
| 919 | """[1:] |
| 920 | s = linear_format(s, group_booleans=lines) |
| 921 | s = s.format_map(d) |
| 922 | add(s) |
| 923 | |
| 924 | add(" default:\n") |
| 925 | s = ' PyErr_SetString(PyExc_TypeError, "{} requires {} to {} arguments");\n' |
| 926 | add(s.format(f.full_name, count_min, count_max)) |
| 927 | add(' return NULL;\n') |
| 928 | add("}}") |
| 929 | template_dict['option_group_parsing'] = output() |
| 930 | |
Larry Hastings | bebf735 | 2014-01-17 17:47:17 -0800 | [diff] [blame] | 931 | def render_function(self, clinic, f): |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 932 | if not f: |
| 933 | return "" |
| 934 | |
| 935 | add, output = text_accumulator() |
| 936 | data = CRenderData() |
| 937 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 938 | parameters = list(f.parameters.values()) |
| 939 | converters = [p.converter for p in parameters] |
| 940 | |
| 941 | template_dict = {} |
| 942 | |
| 943 | full_name = f.full_name |
| 944 | template_dict['full_name'] = full_name |
| 945 | |
| 946 | name = full_name.rpartition('.')[2] |
| 947 | template_dict['name'] = name |
| 948 | |
Larry Hastings | 8666e65 | 2014-01-12 14:12:59 -0800 | [diff] [blame] | 949 | if f.c_basename: |
| 950 | c_basename = f.c_basename |
| 951 | else: |
| 952 | fields = full_name.split(".") |
| 953 | if fields[-1] == '__new__': |
| 954 | fields.pop() |
| 955 | c_basename = "_".join(fields) |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 956 | template_dict['c_basename'] = c_basename |
| 957 | |
| 958 | methoddef_name = "{}_METHODDEF".format(c_basename.upper()) |
| 959 | template_dict['methoddef_name'] = methoddef_name |
| 960 | |
| 961 | template_dict['docstring'] = self.docstring_for_c_string(f) |
| 962 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 963 | positional = has_option_groups = False |
| 964 | |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 965 | first_optional = len(parameters) |
| 966 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 967 | if parameters: |
| 968 | last_group = 0 |
| 969 | |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 970 | for i, p in enumerate(parameters): |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 971 | c = p.converter |
| 972 | |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 973 | if p.default is not unspecified: |
| 974 | first_optional = min(first_optional, i) |
| 975 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 976 | # insert group variable |
| 977 | group = p.group |
| 978 | if last_group != group: |
| 979 | last_group = group |
| 980 | if group: |
| 981 | group_name = self.group_to_variable_name(group) |
| 982 | data.impl_arguments.append(group_name) |
| 983 | data.declarations.append("int " + group_name + " = 0;") |
| 984 | data.impl_parameters.append("int " + group_name) |
| 985 | has_option_groups = True |
| 986 | c.render(p, data) |
| 987 | |
| 988 | positional = parameters[-1].kind == inspect.Parameter.POSITIONAL_ONLY |
Larry Hastings | 4a55fc5 | 2014-01-12 11:09:57 -0800 | [diff] [blame] | 989 | if has_option_groups and (not positional): |
| 990 | fail("You cannot use optional groups ('[' and ']')\nunless all parameters are positional-only ('/').") |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 991 | |
Larry Hastings | bebf735 | 2014-01-17 17:47:17 -0800 | [diff] [blame] | 992 | # HACK |
| 993 | # when we're METH_O, but have a custom |
| 994 | # return converter, we use |
| 995 | # "impl_parameters" for the parsing |
| 996 | # function because that works better. |
| 997 | # but that means we must supress actually |
| 998 | # declaring the impl's parameters as variables |
| 999 | # in the parsing function. but since it's |
| 1000 | # METH_O, we only have one anyway, so we don't |
| 1001 | # have any problem finding it. |
| 1002 | default_return_converter = (not f.return_converter or |
| 1003 | f.return_converter.type == 'PyObject *') |
| 1004 | if (len(parameters) == 1 and |
| 1005 | parameters[0].kind == inspect.Parameter.POSITIONAL_ONLY and |
| 1006 | not converters[0].is_optional() and |
| 1007 | isinstance(converters[0], object_converter) and |
| 1008 | converters[0].format_unit == 'O' and |
| 1009 | not default_return_converter): |
| 1010 | |
| 1011 | data.declarations.pop(0) |
| 1012 | |
Larry Hastings | ebdcb50 | 2013-11-23 14:54:00 -0800 | [diff] [blame] | 1013 | # now insert our "self" (or whatever) parameters |
| 1014 | # (we deliberately don't call render on self converters) |
| 1015 | stock_self = self_converter('self', f) |
| 1016 | template_dict['self_name'] = stock_self.name |
| 1017 | template_dict['self_type'] = stock_self.type |
| 1018 | data.impl_parameters.insert(0, f.self_converter.type + ("" if f.self_converter.type.endswith('*') else " ") + f.self_converter.name) |
| 1019 | if f.self_converter.type != stock_self.type: |
| 1020 | self_cast = '(' + f.self_converter.type + ')' |
| 1021 | else: |
| 1022 | self_cast = '' |
| 1023 | data.impl_arguments.insert(0, self_cast + stock_self.name) |
| 1024 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 1025 | f.return_converter.render(f, data) |
| 1026 | template_dict['impl_return_type'] = f.return_converter.type |
| 1027 | |
| 1028 | template_dict['declarations'] = "\n".join(data.declarations) |
| 1029 | template_dict['initializers'] = "\n\n".join(data.initializers) |
| 1030 | template_dict['keywords'] = '"' + '", "'.join(data.keywords) + '"' |
| 1031 | template_dict['format_units'] = ''.join(data.format_units) |
| 1032 | template_dict['parse_arguments'] = ', '.join(data.parse_arguments) |
| 1033 | template_dict['impl_parameters'] = ", ".join(data.impl_parameters) |
| 1034 | template_dict['impl_arguments'] = ", ".join(data.impl_arguments) |
| 1035 | template_dict['return_conversion'] = "".join(data.return_conversion).rstrip() |
| 1036 | template_dict['cleanup'] = "".join(data.cleanup) |
| 1037 | template_dict['return_value'] = data.return_value |
| 1038 | |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 1039 | # used by unpack tuple |
| 1040 | template_dict['unpack_min'] = str(first_optional) |
| 1041 | template_dict['unpack_max'] = str(len(parameters)) |
| 1042 | |
Larry Hastings | bebf735 | 2014-01-17 17:47:17 -0800 | [diff] [blame] | 1043 | if has_option_groups: |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 1044 | self.render_option_group_parsing(f, template_dict) |
Larry Hastings | bebf735 | 2014-01-17 17:47:17 -0800 | [diff] [blame] | 1045 | |
| 1046 | templates = self.output_templates(f) |
| 1047 | |
| 1048 | for name, destination in clinic.field_destinations.items(): |
| 1049 | template = templates[name] |
| 1050 | if has_option_groups: |
| 1051 | template = linear_format(template, |
| 1052 | option_group_parsing=template_dict['option_group_parsing']) |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 1053 | template = linear_format(template, |
Larry Hastings | bebf735 | 2014-01-17 17:47:17 -0800 | [diff] [blame] | 1054 | declarations=template_dict['declarations'], |
| 1055 | return_conversion=template_dict['return_conversion'], |
| 1056 | initializers=template_dict['initializers'], |
| 1057 | cleanup=template_dict['cleanup'], |
| 1058 | ) |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 1059 | |
Larry Hastings | bebf735 | 2014-01-17 17:47:17 -0800 | [diff] [blame] | 1060 | # Only generate the "exit:" label |
| 1061 | # if we have any gotos |
| 1062 | need_exit_label = "goto exit;" in template |
| 1063 | template = linear_format(template, |
| 1064 | exit_label="exit:" if need_exit_label else '' |
| 1065 | ) |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 1066 | |
Larry Hastings | bebf735 | 2014-01-17 17:47:17 -0800 | [diff] [blame] | 1067 | s = template.format_map(template_dict) |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 1068 | |
Larry Hastings | bebf735 | 2014-01-17 17:47:17 -0800 | [diff] [blame] | 1069 | if clinic.line_prefix: |
| 1070 | s = indent_all_lines(s, clinic.line_prefix) |
| 1071 | if clinic.line_suffix: |
| 1072 | s = suffix_all_lines(s, clinic.line_suffix) |
| 1073 | |
| 1074 | destination.append(s) |
| 1075 | |
| 1076 | return clinic.get_destination('block').dump() |
| 1077 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 1078 | |
| 1079 | |
| 1080 | @contextlib.contextmanager |
| 1081 | def OverrideStdioWith(stdout): |
| 1082 | saved_stdout = sys.stdout |
| 1083 | sys.stdout = stdout |
| 1084 | try: |
| 1085 | yield |
| 1086 | finally: |
| 1087 | assert sys.stdout is stdout |
| 1088 | sys.stdout = saved_stdout |
| 1089 | |
| 1090 | |
| 1091 | def create_regex(before, after): |
| 1092 | """Create an re object for matching marker lines.""" |
| 1093 | pattern = r'^{}(\w+){}$' |
| 1094 | return re.compile(pattern.format(re.escape(before), re.escape(after))) |
| 1095 | |
| 1096 | |
| 1097 | class Block: |
| 1098 | r""" |
| 1099 | Represents a single block of text embedded in |
| 1100 | another file. If dsl_name is None, the block represents |
| 1101 | verbatim text, raw original text from the file, in |
| 1102 | which case "input" will be the only non-false member. |
| 1103 | If dsl_name is not None, the block represents a Clinic |
| 1104 | block. |
| 1105 | |
| 1106 | input is always str, with embedded \n characters. |
| 1107 | input represents the original text from the file; |
| 1108 | if it's a Clinic block, it is the original text with |
| 1109 | the body_prefix and redundant leading whitespace removed. |
| 1110 | |
| 1111 | dsl_name is either str or None. If str, it's the text |
| 1112 | found on the start line of the block between the square |
| 1113 | brackets. |
| 1114 | |
| 1115 | signatures is either list or None. If it's a list, |
| 1116 | it may only contain clinic.Module, clinic.Class, and |
| 1117 | clinic.Function objects. At the moment it should |
| 1118 | contain at most one of each. |
| 1119 | |
| 1120 | output is either str or None. If str, it's the output |
| 1121 | from this block, with embedded '\n' characters. |
| 1122 | |
| 1123 | indent is either str or None. It's the leading whitespace |
| 1124 | that was found on every line of input. (If body_prefix is |
| 1125 | not empty, this is the indent *after* removing the |
| 1126 | body_prefix.) |
| 1127 | |
| 1128 | preindent is either str or None. It's the whitespace that |
| 1129 | was found in front of every line of input *before* the |
| 1130 | "body_prefix" (see the Language object). If body_prefix |
| 1131 | is empty, preindent must always be empty too. |
| 1132 | |
| 1133 | To illustrate indent and preindent: Assume that '_' |
| 1134 | represents whitespace. If the block processed was in a |
| 1135 | Python file, and looked like this: |
| 1136 | ____#/*[python] |
| 1137 | ____#__for a in range(20): |
| 1138 | ____#____print(a) |
| 1139 | ____#[python]*/ |
| 1140 | "preindent" would be "____" and "indent" would be "__". |
| 1141 | |
| 1142 | """ |
| 1143 | def __init__(self, input, dsl_name=None, signatures=None, output=None, indent='', preindent=''): |
| 1144 | assert isinstance(input, str) |
| 1145 | self.input = input |
| 1146 | self.dsl_name = dsl_name |
| 1147 | self.signatures = signatures or [] |
| 1148 | self.output = output |
| 1149 | self.indent = indent |
| 1150 | self.preindent = preindent |
| 1151 | |
| 1152 | |
| 1153 | class BlockParser: |
| 1154 | """ |
| 1155 | Block-oriented parser for Argument Clinic. |
| 1156 | Iterator, yields Block objects. |
| 1157 | """ |
| 1158 | |
| 1159 | def __init__(self, input, language, *, verify=True): |
| 1160 | """ |
| 1161 | "input" should be a str object |
| 1162 | with embedded \n characters. |
| 1163 | |
| 1164 | "language" should be a Language object. |
| 1165 | """ |
| 1166 | language.validate() |
| 1167 | |
| 1168 | self.input = collections.deque(reversed(input.splitlines(keepends=True))) |
| 1169 | self.block_start_line_number = self.line_number = 0 |
| 1170 | |
| 1171 | self.language = language |
| 1172 | before, _, after = language.start_line.partition('{dsl_name}') |
| 1173 | assert _ == '{dsl_name}' |
| 1174 | self.start_re = create_regex(before, after) |
| 1175 | self.verify = verify |
| 1176 | self.last_checksum_re = None |
| 1177 | self.last_dsl_name = None |
| 1178 | self.dsl_name = None |
Larry Hastings | bebf735 | 2014-01-17 17:47:17 -0800 | [diff] [blame] | 1179 | self.first_block = True |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 1180 | |
| 1181 | def __iter__(self): |
| 1182 | return self |
| 1183 | |
| 1184 | def __next__(self): |
Larry Hastings | bebf735 | 2014-01-17 17:47:17 -0800 | [diff] [blame] | 1185 | while True: |
| 1186 | if not self.input: |
| 1187 | raise StopIteration |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 1188 | |
Larry Hastings | bebf735 | 2014-01-17 17:47:17 -0800 | [diff] [blame] | 1189 | if self.dsl_name: |
| 1190 | return_value = self.parse_clinic_block(self.dsl_name) |
| 1191 | self.dsl_name = None |
| 1192 | self.first_block = False |
| 1193 | return return_value |
| 1194 | block = self.parse_verbatim_block() |
| 1195 | if self.first_block and not block.input: |
| 1196 | continue |
| 1197 | self.first_block = False |
| 1198 | return block |
| 1199 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 1200 | |
| 1201 | def is_start_line(self, line): |
| 1202 | match = self.start_re.match(line.lstrip()) |
| 1203 | return match.group(1) if match else None |
| 1204 | |
| 1205 | def _line(self): |
| 1206 | self.line_number += 1 |
| 1207 | return self.input.pop() |
| 1208 | |
| 1209 | def parse_verbatim_block(self): |
| 1210 | add, output = text_accumulator() |
| 1211 | self.block_start_line_number = self.line_number |
| 1212 | |
| 1213 | while self.input: |
| 1214 | line = self._line() |
| 1215 | dsl_name = self.is_start_line(line) |
| 1216 | if dsl_name: |
| 1217 | self.dsl_name = dsl_name |
| 1218 | break |
| 1219 | add(line) |
| 1220 | |
| 1221 | return Block(output()) |
| 1222 | |
| 1223 | def parse_clinic_block(self, dsl_name): |
| 1224 | input_add, input_output = text_accumulator() |
| 1225 | self.block_start_line_number = self.line_number + 1 |
Larry Hastings | 9026113 | 2014-01-07 12:21:08 -0800 | [diff] [blame] | 1226 | stop_line = self.language.stop_line.format(dsl_name=dsl_name) |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 1227 | body_prefix = self.language.body_prefix.format(dsl_name=dsl_name) |
| 1228 | |
Larry Hastings | 9026113 | 2014-01-07 12:21:08 -0800 | [diff] [blame] | 1229 | def is_stop_line(line): |
| 1230 | # make sure to recognize stop line even if it |
| 1231 | # doesn't end with EOL (it could be the very end of the file) |
| 1232 | if not line.startswith(stop_line): |
| 1233 | return False |
| 1234 | remainder = line[len(stop_line):] |
| 1235 | return (not remainder) or remainder.isspace() |
| 1236 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 1237 | # consume body of program |
| 1238 | while self.input: |
| 1239 | line = self._line() |
Larry Hastings | 9026113 | 2014-01-07 12:21:08 -0800 | [diff] [blame] | 1240 | if is_stop_line(line) or self.is_start_line(line): |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 1241 | break |
| 1242 | if body_prefix: |
| 1243 | line = line.lstrip() |
| 1244 | assert line.startswith(body_prefix) |
| 1245 | line = line[len(body_prefix):] |
| 1246 | input_add(line) |
| 1247 | |
| 1248 | # consume output and checksum line, if present. |
| 1249 | if self.last_dsl_name == dsl_name: |
| 1250 | checksum_re = self.last_checksum_re |
| 1251 | else: |
| 1252 | before, _, after = self.language.checksum_line.format(dsl_name=dsl_name, checksum='{checksum}').partition('{checksum}') |
| 1253 | assert _ == '{checksum}' |
| 1254 | checksum_re = create_regex(before, after) |
| 1255 | self.last_dsl_name = dsl_name |
| 1256 | self.last_checksum_re = checksum_re |
| 1257 | |
| 1258 | # scan forward for checksum line |
| 1259 | output_add, output_output = text_accumulator() |
| 1260 | checksum = None |
| 1261 | while self.input: |
| 1262 | line = self._line() |
| 1263 | match = checksum_re.match(line.lstrip()) |
| 1264 | checksum = match.group(1) if match else None |
| 1265 | if checksum: |
| 1266 | break |
| 1267 | output_add(line) |
| 1268 | if self.is_start_line(line): |
| 1269 | break |
| 1270 | |
Larry Hastings | ef3b1fb | 2013-10-22 23:26:23 -0700 | [diff] [blame] | 1271 | output = output_output() |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 1272 | if checksum: |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 1273 | if self.verify: |
| 1274 | computed = compute_checksum(output) |
| 1275 | if checksum != computed: |
Antoine Pitrou | cc1d31e | 2014-01-14 20:52:01 +0100 | [diff] [blame] | 1276 | fail("Checksum mismatch!\nExpected: {}\nComputed: {}\n" |
| 1277 | "Suggested fix: remove all generated code including " |
Larry Hastings | bebf735 | 2014-01-17 17:47:17 -0800 | [diff] [blame] | 1278 | "the end marker,\n" |
| 1279 | "or use the '-f' option." |
Antoine Pitrou | cc1d31e | 2014-01-14 20:52:01 +0100 | [diff] [blame] | 1280 | .format(checksum, computed)) |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 1281 | else: |
| 1282 | # put back output |
Larry Hastings | eb31e9d | 2014-01-06 11:10:08 -0800 | [diff] [blame] | 1283 | output_lines = output.splitlines(keepends=True) |
| 1284 | self.line_number -= len(output_lines) |
| 1285 | self.input.extend(reversed(output_lines)) |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 1286 | output = None |
| 1287 | |
| 1288 | return Block(input_output(), dsl_name, output=output) |
| 1289 | |
| 1290 | |
| 1291 | class BlockPrinter: |
| 1292 | |
| 1293 | def __init__(self, language, f=None): |
| 1294 | self.language = language |
| 1295 | self.f = f or io.StringIO() |
| 1296 | |
| 1297 | def print_block(self, block): |
| 1298 | input = block.input |
| 1299 | output = block.output |
| 1300 | dsl_name = block.dsl_name |
| 1301 | write = self.f.write |
| 1302 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 1303 | assert not ((dsl_name == None) ^ (output == None)), "you must specify dsl_name and output together, dsl_name " + repr(dsl_name) |
| 1304 | |
| 1305 | if not dsl_name: |
| 1306 | write(input) |
| 1307 | return |
| 1308 | |
| 1309 | write(self.language.start_line.format(dsl_name=dsl_name)) |
| 1310 | write("\n") |
| 1311 | |
| 1312 | body_prefix = self.language.body_prefix.format(dsl_name=dsl_name) |
| 1313 | if not body_prefix: |
| 1314 | write(input) |
| 1315 | else: |
| 1316 | for line in input.split('\n'): |
| 1317 | write(body_prefix) |
| 1318 | write(line) |
| 1319 | write("\n") |
| 1320 | |
| 1321 | write(self.language.stop_line.format(dsl_name=dsl_name)) |
| 1322 | write("\n") |
| 1323 | |
Larry Hastings | bebf735 | 2014-01-17 17:47:17 -0800 | [diff] [blame] | 1324 | output = ''.join(block.output) |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 1325 | if output: |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 1326 | if not output.endswith('\n'): |
Larry Hastings | bebf735 | 2014-01-17 17:47:17 -0800 | [diff] [blame] | 1327 | output += '\n' |
| 1328 | write(output) |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 1329 | |
| 1330 | write(self.language.checksum_line.format(dsl_name=dsl_name, checksum=compute_checksum(output))) |
| 1331 | write("\n") |
| 1332 | |
Larry Hastings | bebf735 | 2014-01-17 17:47:17 -0800 | [diff] [blame] | 1333 | def write(self, text): |
| 1334 | self.f.write(text) |
| 1335 | |
| 1336 | |
| 1337 | class Destination: |
| 1338 | def __init__(self, name, type, clinic, *args): |
| 1339 | self.name = name |
| 1340 | self.type = type |
| 1341 | self.clinic = clinic |
| 1342 | valid_types = ('buffer', 'file', 'suppress', 'two-pass') |
| 1343 | if type not in valid_types: |
| 1344 | fail("Invalid destination type " + repr(type) + " for " + name + " , must be " + ', '.join(valid_types)) |
| 1345 | extra_arguments = 1 if type == "file" else 0 |
| 1346 | if len(args) < extra_arguments: |
| 1347 | fail("Not enough arguments for destination " + name + " new " + type) |
| 1348 | if len(args) > extra_arguments: |
| 1349 | fail("Too many arguments for destination " + name + " new " + type) |
| 1350 | if type =='file': |
| 1351 | d = {} |
| 1352 | d['filename'] = filename = clinic.filename |
| 1353 | d['basename'], d['extension'] = os.path.splitext(filename) |
| 1354 | self.filename = args[0].format_map(d) |
| 1355 | if type == 'two-pass': |
| 1356 | self.id = None |
| 1357 | |
| 1358 | self.text, self.append, self._dump = _text_accumulator() |
| 1359 | |
| 1360 | def __repr__(self): |
| 1361 | if self.type == 'file': |
| 1362 | file_repr = " " + repr(self.filename) |
| 1363 | else: |
| 1364 | file_repr = '' |
| 1365 | return "".join(("<Destination ", self.name, " ", self.type, file_repr, ">")) |
| 1366 | |
| 1367 | def clear(self): |
| 1368 | if self.type != 'buffer': |
| 1369 | fail("Can't clear destination" + self.name + " , it's not of type buffer") |
| 1370 | self.text.clear() |
| 1371 | |
| 1372 | def dump(self): |
| 1373 | if self.type == 'two-pass': |
| 1374 | if self.id is None: |
| 1375 | self.id = str(uuid.uuid4()) |
| 1376 | return self.id |
| 1377 | fail("You can only dump a two-pass buffer exactly once!") |
| 1378 | return self._dump() |
| 1379 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 1380 | |
| 1381 | # maps strings to Language objects. |
| 1382 | # "languages" maps the name of the language ("C", "Python"). |
| 1383 | # "extensions" maps the file extension ("c", "py"). |
| 1384 | languages = { 'C': CLanguage, 'Python': PythonLanguage } |
Larry Hastings | 6d2ea21 | 2014-01-05 02:50:45 -0800 | [diff] [blame] | 1385 | extensions = { name: CLanguage for name in "c cc cpp cxx h hh hpp hxx".split() } |
| 1386 | extensions['py'] = PythonLanguage |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 1387 | |
| 1388 | |
| 1389 | # maps strings to callables. |
| 1390 | # these callables must be of the form: |
| 1391 | # def foo(name, default, *, ...) |
| 1392 | # The callable may have any number of keyword-only parameters. |
| 1393 | # The callable must return a CConverter object. |
| 1394 | # The callable should not call builtins.print. |
| 1395 | converters = {} |
| 1396 | |
| 1397 | # maps strings to callables. |
| 1398 | # these callables follow the same rules as those for "converters" above. |
| 1399 | # note however that they will never be called with keyword-only parameters. |
| 1400 | legacy_converters = {} |
| 1401 | |
| 1402 | |
| 1403 | # maps strings to callables. |
| 1404 | # these callables must be of the form: |
| 1405 | # def foo(*, ...) |
| 1406 | # The callable may have any number of keyword-only parameters. |
| 1407 | # The callable must return a CConverter object. |
| 1408 | # The callable should not call builtins.print. |
| 1409 | return_converters = {} |
| 1410 | |
| 1411 | class Clinic: |
Larry Hastings | bebf735 | 2014-01-17 17:47:17 -0800 | [diff] [blame] | 1412 | |
| 1413 | presets_text = """ |
| 1414 | preset original |
| 1415 | everything block |
| 1416 | docstring_prototype suppress |
| 1417 | parser_prototype suppress |
| 1418 | |
| 1419 | preset file |
| 1420 | everything file |
| 1421 | docstring_prototype suppress |
| 1422 | parser_prototype suppress |
| 1423 | impl_definition block |
| 1424 | |
| 1425 | preset buffer |
| 1426 | everything buffer |
| 1427 | docstring_prototype suppress |
| 1428 | impl_prototype suppress |
| 1429 | parser_prototype suppress |
| 1430 | impl_definition block |
| 1431 | |
| 1432 | preset partial-buffer |
| 1433 | everything buffer |
| 1434 | docstring_prototype block |
| 1435 | impl_prototype suppress |
| 1436 | methoddef_define block |
| 1437 | parser_prototype block |
| 1438 | impl_definition block |
| 1439 | |
| 1440 | preset two-pass |
| 1441 | everything buffer |
| 1442 | docstring_prototype two-pass |
| 1443 | impl_prototype suppress |
| 1444 | methoddef_define two-pass |
| 1445 | parser_prototype two-pass |
| 1446 | impl_definition block |
| 1447 | |
| 1448 | """ |
| 1449 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 1450 | def __init__(self, language, printer=None, *, verify=True, filename=None): |
| 1451 | # maps strings to Parser objects. |
| 1452 | # (instantiated from the "parsers" global.) |
| 1453 | self.parsers = {} |
| 1454 | self.language = language |
Larry Hastings | bebf735 | 2014-01-17 17:47:17 -0800 | [diff] [blame] | 1455 | if printer: |
| 1456 | fail("Custom printers are broken right now") |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 1457 | self.printer = printer or BlockPrinter(language) |
| 1458 | self.verify = verify |
| 1459 | self.filename = filename |
| 1460 | self.modules = collections.OrderedDict() |
Larry Hastings | ed4a1c5 | 2013-11-18 09:32:13 -0800 | [diff] [blame] | 1461 | self.classes = collections.OrderedDict() |
Larry Hastings | 2a72791 | 2014-01-16 11:32:01 -0800 | [diff] [blame] | 1462 | self.functions = [] |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 1463 | |
Larry Hastings | bebf735 | 2014-01-17 17:47:17 -0800 | [diff] [blame] | 1464 | self.line_prefix = self.line_suffix = '' |
| 1465 | |
| 1466 | self.destinations = {} |
| 1467 | self.add_destination("block", "buffer") |
| 1468 | self.add_destination("suppress", "suppress") |
| 1469 | self.add_destination("buffer", "buffer") |
| 1470 | self.add_destination("two-pass", "two-pass") |
| 1471 | if filename: |
| 1472 | self.add_destination("file", "file", "{basename}.clinic{extension}") |
| 1473 | |
| 1474 | d = self.destinations.get |
| 1475 | self.field_destinations = collections.OrderedDict(( |
| 1476 | ('docstring_prototype', d('suppress')), |
| 1477 | ('docstring_definition', d('block')), |
| 1478 | ('methoddef_define', d('block')), |
| 1479 | ('impl_prototype', d('block')), |
| 1480 | ('parser_prototype', d('suppress')), |
| 1481 | ('parser_definition', d('block')), |
| 1482 | ('impl_definition', d('block')), |
| 1483 | )) |
| 1484 | |
| 1485 | self.field_destinations_stack = [] |
| 1486 | |
| 1487 | self.presets = {} |
| 1488 | preset = None |
| 1489 | for line in self.presets_text.strip().split('\n'): |
| 1490 | line = line.strip() |
| 1491 | if not line: |
| 1492 | continue |
| 1493 | name, value = line.split() |
| 1494 | if name == 'preset': |
| 1495 | self.presets[value] = preset = collections.OrderedDict() |
| 1496 | continue |
| 1497 | |
| 1498 | destination = self.get_destination(value) |
| 1499 | |
| 1500 | if name == 'everything': |
| 1501 | for name in self.field_destinations: |
| 1502 | preset[name] = destination |
| 1503 | continue |
| 1504 | |
| 1505 | assert name in self.field_destinations |
| 1506 | preset[name] = destination |
| 1507 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 1508 | global clinic |
| 1509 | clinic = self |
| 1510 | |
Larry Hastings | bebf735 | 2014-01-17 17:47:17 -0800 | [diff] [blame] | 1511 | def get_destination(self, name, default=unspecified): |
| 1512 | d = self.destinations.get(name) |
| 1513 | if not d: |
| 1514 | if default is not unspecified: |
| 1515 | return default |
| 1516 | fail("Destination does not exist: " + repr(name)) |
| 1517 | return d |
| 1518 | |
| 1519 | def add_destination(self, name, type, *args): |
| 1520 | if name in self.destinations: |
| 1521 | fail("Destination already exists: " + repr(name)) |
| 1522 | self.destinations[name] = Destination(name, type, self, *args) |
| 1523 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 1524 | def parse(self, input): |
| 1525 | printer = self.printer |
| 1526 | self.block_parser = BlockParser(input, self.language, verify=self.verify) |
| 1527 | for block in self.block_parser: |
| 1528 | dsl_name = block.dsl_name |
| 1529 | if dsl_name: |
| 1530 | if dsl_name not in self.parsers: |
| 1531 | assert dsl_name in parsers, "No parser to handle {!r} block.".format(dsl_name) |
| 1532 | self.parsers[dsl_name] = parsers[dsl_name](self) |
| 1533 | parser = self.parsers[dsl_name] |
Georg Brandl | aabebde | 2014-01-16 06:53:54 +0100 | [diff] [blame] | 1534 | try: |
| 1535 | parser.parse(block) |
| 1536 | except Exception: |
| 1537 | fail('Exception raised during parsing:\n' + |
| 1538 | traceback.format_exc().rstrip()) |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 1539 | printer.print_block(block) |
Larry Hastings | bebf735 | 2014-01-17 17:47:17 -0800 | [diff] [blame] | 1540 | |
| 1541 | second_pass_replacements = {} |
| 1542 | |
| 1543 | for name, destination in self.destinations.items(): |
| 1544 | if destination.type == 'suppress': |
| 1545 | continue |
| 1546 | output = destination._dump() |
| 1547 | |
| 1548 | if destination.type == 'two-pass': |
| 1549 | if destination.id: |
| 1550 | second_pass_replacements[destination.id] = output |
| 1551 | elif output: |
| 1552 | fail("Two-pass buffer " + repr(name) + " not empty at end of file!") |
| 1553 | continue |
| 1554 | |
| 1555 | if output: |
| 1556 | |
| 1557 | block = Block("", dsl_name="clinic", output=output) |
| 1558 | |
| 1559 | if destination.type == 'buffer': |
| 1560 | block.input = "dump " + name + "\n" |
| 1561 | warn("Destination buffer " + repr(name) + " not empty at end of file, emptying.") |
| 1562 | printer.write("\n") |
| 1563 | printer.print_block(block) |
| 1564 | continue |
| 1565 | |
| 1566 | if destination.type == 'file': |
| 1567 | try: |
| 1568 | with open(destination.filename, "rt") as f: |
| 1569 | parser_2 = BlockParser(f.read(), language=self.language) |
| 1570 | blocks = list(parser_2) |
| 1571 | if (len(blocks) != 1) or (blocks[0].input != 'preserve\n'): |
| 1572 | fail("Modified destination file " + repr(destination.filename) + ", not overwriting!") |
| 1573 | except FileNotFoundError: |
| 1574 | pass |
| 1575 | |
| 1576 | block.input = 'preserve\n' |
| 1577 | printer_2 = BlockPrinter(self.language) |
| 1578 | printer_2.print_block(block) |
| 1579 | with open(destination.filename, "wt") as f: |
| 1580 | f.write(printer_2.f.getvalue()) |
| 1581 | continue |
| 1582 | text = printer.f.getvalue() |
| 1583 | |
| 1584 | if second_pass_replacements: |
| 1585 | printer_2 = BlockPrinter(self.language) |
| 1586 | parser_2 = BlockParser(text, self.language) |
| 1587 | changed = False |
| 1588 | for block in parser_2: |
| 1589 | if block.dsl_name: |
| 1590 | for id, replacement in second_pass_replacements.items(): |
| 1591 | if id in block.output: |
| 1592 | changed = True |
| 1593 | block.output = block.output.replace(id, replacement) |
| 1594 | printer_2.print_block(block) |
| 1595 | if changed: |
| 1596 | text = printer_2.f.getvalue() |
| 1597 | |
| 1598 | return text |
| 1599 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 1600 | |
| 1601 | def _module_and_class(self, fields): |
| 1602 | """ |
| 1603 | fields should be an iterable of field names. |
| 1604 | returns a tuple of (module, class). |
| 1605 | the module object could actually be self (a clinic object). |
| 1606 | this function is only ever used to find the parent of where |
| 1607 | a new class/module should go. |
| 1608 | """ |
| 1609 | in_classes = False |
| 1610 | parent = module = self |
| 1611 | cls = None |
| 1612 | so_far = [] |
| 1613 | |
| 1614 | for field in fields: |
| 1615 | so_far.append(field) |
| 1616 | if not in_classes: |
| 1617 | child = parent.modules.get(field) |
| 1618 | if child: |
Larry Hastings | ed4a1c5 | 2013-11-18 09:32:13 -0800 | [diff] [blame] | 1619 | parent = module = child |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 1620 | continue |
| 1621 | in_classes = True |
| 1622 | if not hasattr(parent, 'classes'): |
| 1623 | return module, cls |
| 1624 | child = parent.classes.get(field) |
| 1625 | if not child: |
| 1626 | fail('Parent class or module ' + '.'.join(so_far) + " does not exist.") |
| 1627 | cls = parent = child |
| 1628 | |
| 1629 | return module, cls |
| 1630 | |
| 1631 | |
| 1632 | def parse_file(filename, *, verify=True, output=None, encoding='utf-8'): |
| 1633 | extension = os.path.splitext(filename)[1][1:] |
| 1634 | if not extension: |
| 1635 | fail("Can't extract file type for file " + repr(filename)) |
| 1636 | |
| 1637 | try: |
| 1638 | language = extensions[extension]() |
| 1639 | except KeyError: |
| 1640 | fail("Can't identify file type for file " + repr(filename)) |
| 1641 | |
| 1642 | clinic = Clinic(language, verify=verify, filename=filename) |
| 1643 | |
| 1644 | with open(filename, 'r', encoding=encoding) as f: |
Larry Hastings | dcd340e | 2013-11-23 14:58:45 -0800 | [diff] [blame] | 1645 | raw = f.read() |
| 1646 | |
| 1647 | cooked = clinic.parse(raw) |
| 1648 | if cooked == raw: |
| 1649 | return |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 1650 | |
| 1651 | directory = os.path.dirname(filename) or '.' |
| 1652 | |
| 1653 | with tempfile.TemporaryDirectory(prefix="clinic", dir=directory) as tmpdir: |
Larry Hastings | dcd340e | 2013-11-23 14:58:45 -0800 | [diff] [blame] | 1654 | bytes = cooked.encode(encoding) |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 1655 | tmpfilename = os.path.join(tmpdir, os.path.basename(filename)) |
| 1656 | with open(tmpfilename, "wb") as f: |
| 1657 | f.write(bytes) |
| 1658 | os.replace(tmpfilename, output or filename) |
| 1659 | |
| 1660 | |
| 1661 | def compute_checksum(input): |
| 1662 | input = input or '' |
| 1663 | return hashlib.sha1(input.encode('utf-8')).hexdigest() |
| 1664 | |
| 1665 | |
| 1666 | |
| 1667 | |
| 1668 | class PythonParser: |
| 1669 | def __init__(self, clinic): |
| 1670 | pass |
| 1671 | |
| 1672 | def parse(self, block): |
| 1673 | s = io.StringIO() |
| 1674 | with OverrideStdioWith(s): |
| 1675 | exec(block.input) |
| 1676 | block.output = s.getvalue() |
| 1677 | |
| 1678 | |
| 1679 | class Module: |
| 1680 | def __init__(self, name, module=None): |
| 1681 | self.name = name |
| 1682 | self.module = self.parent = module |
| 1683 | |
| 1684 | self.modules = collections.OrderedDict() |
| 1685 | self.classes = collections.OrderedDict() |
| 1686 | self.functions = [] |
| 1687 | |
Larry Hastings | ed4a1c5 | 2013-11-18 09:32:13 -0800 | [diff] [blame] | 1688 | def __repr__(self): |
| 1689 | return "<clinic.Module " + repr(self.name) + " at " + str(id(self)) + ">" |
| 1690 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 1691 | class Class: |
| 1692 | def __init__(self, name, module=None, cls=None): |
| 1693 | self.name = name |
| 1694 | self.module = module |
| 1695 | self.cls = cls |
| 1696 | self.parent = cls or module |
| 1697 | |
| 1698 | self.classes = collections.OrderedDict() |
| 1699 | self.functions = [] |
| 1700 | |
Larry Hastings | ed4a1c5 | 2013-11-18 09:32:13 -0800 | [diff] [blame] | 1701 | def __repr__(self): |
| 1702 | return "<clinic.Class " + repr(self.name) + " at " + str(id(self)) + ">" |
| 1703 | |
Larry Hastings | 8666e65 | 2014-01-12 14:12:59 -0800 | [diff] [blame] | 1704 | unsupported_special_methods = set(""" |
Larry Hastings | ed4a1c5 | 2013-11-18 09:32:13 -0800 | [diff] [blame] | 1705 | |
Larry Hastings | 8666e65 | 2014-01-12 14:12:59 -0800 | [diff] [blame] | 1706 | __abs__ |
| 1707 | __add__ |
| 1708 | __and__ |
| 1709 | __bytes__ |
| 1710 | __call__ |
| 1711 | __complex__ |
| 1712 | __delitem__ |
| 1713 | __divmod__ |
| 1714 | __eq__ |
| 1715 | __float__ |
| 1716 | __floordiv__ |
| 1717 | __ge__ |
| 1718 | __getattr__ |
| 1719 | __getattribute__ |
| 1720 | __getitem__ |
| 1721 | __gt__ |
| 1722 | __hash__ |
| 1723 | __iadd__ |
| 1724 | __iand__ |
| 1725 | __idivmod__ |
| 1726 | __ifloordiv__ |
| 1727 | __ilshift__ |
| 1728 | __imod__ |
| 1729 | __imul__ |
| 1730 | __index__ |
| 1731 | __int__ |
| 1732 | __invert__ |
| 1733 | __ior__ |
| 1734 | __ipow__ |
| 1735 | __irshift__ |
| 1736 | __isub__ |
| 1737 | __iter__ |
| 1738 | __itruediv__ |
| 1739 | __ixor__ |
| 1740 | __le__ |
| 1741 | __len__ |
| 1742 | __lshift__ |
| 1743 | __lt__ |
| 1744 | __mod__ |
| 1745 | __mul__ |
| 1746 | __neg__ |
| 1747 | __new__ |
| 1748 | __next__ |
| 1749 | __or__ |
| 1750 | __pos__ |
| 1751 | __pow__ |
| 1752 | __radd__ |
| 1753 | __rand__ |
| 1754 | __rdivmod__ |
| 1755 | __repr__ |
| 1756 | __rfloordiv__ |
| 1757 | __rlshift__ |
| 1758 | __rmod__ |
| 1759 | __rmul__ |
| 1760 | __ror__ |
| 1761 | __round__ |
| 1762 | __rpow__ |
| 1763 | __rrshift__ |
| 1764 | __rshift__ |
| 1765 | __rsub__ |
| 1766 | __rtruediv__ |
| 1767 | __rxor__ |
| 1768 | __setattr__ |
| 1769 | __setitem__ |
| 1770 | __str__ |
| 1771 | __sub__ |
| 1772 | __truediv__ |
| 1773 | __xor__ |
| 1774 | |
| 1775 | """.strip().split()) |
| 1776 | |
| 1777 | |
| 1778 | INVALID, CALLABLE, STATIC_METHOD, CLASS_METHOD, METHOD_INIT, METHOD_NEW = range(6) |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 1779 | |
| 1780 | class Function: |
| 1781 | """ |
| 1782 | Mutable duck type for inspect.Function. |
| 1783 | |
| 1784 | docstring - a str containing |
| 1785 | * embedded line breaks |
| 1786 | * text outdented to the left margin |
| 1787 | * no trailing whitespace. |
| 1788 | It will always be true that |
| 1789 | (not docstring) or ((not docstring[0].isspace()) and (docstring.rstrip() == docstring)) |
| 1790 | """ |
| 1791 | |
| 1792 | def __init__(self, parameters=None, *, name, |
| 1793 | module, cls=None, c_basename=None, |
| 1794 | full_name=None, |
| 1795 | return_converter, return_annotation=_empty, |
| 1796 | docstring=None, kind=CALLABLE, coexist=False): |
| 1797 | self.parameters = parameters or collections.OrderedDict() |
| 1798 | self.return_annotation = return_annotation |
| 1799 | self.name = name |
| 1800 | self.full_name = full_name |
| 1801 | self.module = module |
| 1802 | self.cls = cls |
| 1803 | self.parent = cls or module |
| 1804 | self.c_basename = c_basename |
| 1805 | self.return_converter = return_converter |
| 1806 | self.docstring = docstring or '' |
| 1807 | self.kind = kind |
| 1808 | self.coexist = coexist |
Larry Hastings | ebdcb50 | 2013-11-23 14:54:00 -0800 | [diff] [blame] | 1809 | self.self_converter = None |
| 1810 | |
| 1811 | @property |
| 1812 | def methoddef_flags(self): |
Larry Hastings | 8666e65 | 2014-01-12 14:12:59 -0800 | [diff] [blame] | 1813 | if self.kind in (METHOD_INIT, METHOD_NEW): |
| 1814 | return None |
Larry Hastings | ebdcb50 | 2013-11-23 14:54:00 -0800 | [diff] [blame] | 1815 | flags = [] |
| 1816 | if self.kind == CLASS_METHOD: |
| 1817 | flags.append('METH_CLASS') |
| 1818 | elif self.kind == STATIC_METHOD: |
| 1819 | flags.append('METH_STATIC') |
| 1820 | else: |
| 1821 | assert self.kind == CALLABLE, "unknown kind: " + repr(self.kind) |
| 1822 | if self.coexist: |
| 1823 | flags.append('METH_COEXIST') |
| 1824 | return '|'.join(flags) |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 1825 | |
| 1826 | def __repr__(self): |
| 1827 | return '<clinic.Function ' + self.name + '>' |
| 1828 | |
| 1829 | |
| 1830 | class Parameter: |
| 1831 | """ |
| 1832 | Mutable duck type of inspect.Parameter. |
| 1833 | """ |
| 1834 | |
| 1835 | def __init__(self, name, kind, *, default=_empty, |
| 1836 | function, converter, annotation=_empty, |
| 1837 | docstring=None, group=0): |
| 1838 | self.name = name |
| 1839 | self.kind = kind |
| 1840 | self.default = default |
| 1841 | self.function = function |
| 1842 | self.converter = converter |
| 1843 | self.annotation = annotation |
| 1844 | self.docstring = docstring or '' |
| 1845 | self.group = group |
| 1846 | |
| 1847 | def __repr__(self): |
| 1848 | return '<clinic.Parameter ' + self.name + '>' |
| 1849 | |
| 1850 | def is_keyword_only(self): |
| 1851 | return self.kind == inspect.Parameter.KEYWORD_ONLY |
| 1852 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 1853 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 1854 | |
| 1855 | def add_c_converter(f, name=None): |
| 1856 | if not name: |
| 1857 | name = f.__name__ |
| 1858 | if not name.endswith('_converter'): |
| 1859 | return f |
| 1860 | name = name[:-len('_converter')] |
| 1861 | converters[name] = f |
| 1862 | return f |
| 1863 | |
| 1864 | def add_default_legacy_c_converter(cls): |
| 1865 | # automatically add converter for default format unit |
| 1866 | # (but without stomping on the existing one if it's already |
| 1867 | # set, in case you subclass) |
| 1868 | if ((cls.format_unit != 'O&') and |
| 1869 | (cls.format_unit not in legacy_converters)): |
| 1870 | legacy_converters[cls.format_unit] = cls |
| 1871 | return cls |
| 1872 | |
| 1873 | def add_legacy_c_converter(format_unit, **kwargs): |
| 1874 | """ |
| 1875 | Adds a legacy converter. |
| 1876 | """ |
| 1877 | def closure(f): |
| 1878 | if not kwargs: |
| 1879 | added_f = f |
| 1880 | else: |
| 1881 | added_f = functools.partial(f, **kwargs) |
| 1882 | legacy_converters[format_unit] = added_f |
| 1883 | return f |
| 1884 | return closure |
| 1885 | |
| 1886 | class CConverterAutoRegister(type): |
| 1887 | def __init__(cls, name, bases, classdict): |
| 1888 | add_c_converter(cls) |
| 1889 | add_default_legacy_c_converter(cls) |
| 1890 | |
| 1891 | class CConverter(metaclass=CConverterAutoRegister): |
| 1892 | """ |
| 1893 | For the init function, self, name, function, and default |
| 1894 | must be keyword-or-positional parameters. All other |
Larry Hastings | 2a72791 | 2014-01-16 11:32:01 -0800 | [diff] [blame] | 1895 | parameters must be keyword-only. |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 1896 | """ |
| 1897 | |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 1898 | # The C type to use for this variable. |
| 1899 | # 'type' should be a Python string specifying the type, e.g. "int". |
| 1900 | # If this is a pointer type, the type string should end with ' *'. |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 1901 | type = None |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 1902 | |
| 1903 | # The Python default value for this parameter, as a Python value. |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 1904 | # Or the magic value "unspecified" if there is no default. |
Larry Hastings | 2a72791 | 2014-01-16 11:32:01 -0800 | [diff] [blame] | 1905 | # Or the magic value "unknown" if this value is a cannot be evaluated |
| 1906 | # at Argument-Clinic-preprocessing time (but is presumed to be valid |
| 1907 | # at runtime). |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 1908 | default = unspecified |
| 1909 | |
Larry Hastings | 4a55fc5 | 2014-01-12 11:09:57 -0800 | [diff] [blame] | 1910 | # If not None, default must be isinstance() of this type. |
| 1911 | # (You can also specify a tuple of types.) |
| 1912 | default_type = None |
| 1913 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 1914 | # "default" converted into a C value, as a string. |
| 1915 | # Or None if there is no default. |
| 1916 | c_default = None |
| 1917 | |
Larry Hastings | 2a72791 | 2014-01-16 11:32:01 -0800 | [diff] [blame] | 1918 | # "default" converted into a Python value, as a string. |
| 1919 | # Or None if there is no default. |
| 1920 | py_default = None |
| 1921 | |
Larry Hastings | abc716b | 2013-11-20 09:13:52 -0800 | [diff] [blame] | 1922 | # The default value used to initialize the C variable when |
| 1923 | # there is no default, but not specifying a default may |
| 1924 | # result in an "uninitialized variable" warning. This can |
| 1925 | # easily happen when using option groups--although |
| 1926 | # properly-written code won't actually use the variable, |
| 1927 | # the variable does get passed in to the _impl. (Ah, if |
| 1928 | # only dataflow analysis could inline the static function!) |
| 1929 | # |
| 1930 | # This value is specified as a string. |
| 1931 | # Every non-abstract subclass should supply a valid value. |
| 1932 | c_ignored_default = 'NULL' |
| 1933 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 1934 | # The C converter *function* to be used, if any. |
| 1935 | # (If this is not None, format_unit must be 'O&'.) |
| 1936 | converter = None |
Larry Hastings | ebdcb50 | 2013-11-23 14:54:00 -0800 | [diff] [blame] | 1937 | |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 1938 | # Should Argument Clinic add a '&' before the name of |
| 1939 | # the variable when passing it into the _impl function? |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 1940 | impl_by_reference = False |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 1941 | |
| 1942 | # Should Argument Clinic add a '&' before the name of |
| 1943 | # the variable when passing it into PyArg_ParseTuple (AndKeywords)? |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 1944 | parse_by_reference = True |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 1945 | |
| 1946 | ############################################################# |
| 1947 | ############################################################# |
| 1948 | ## You shouldn't need to read anything below this point to ## |
| 1949 | ## write your own converter functions. ## |
| 1950 | ############################################################# |
| 1951 | ############################################################# |
| 1952 | |
| 1953 | # The "format unit" to specify for this variable when |
| 1954 | # parsing arguments using PyArg_ParseTuple (AndKeywords). |
| 1955 | # Custom converters should always use the default value of 'O&'. |
| 1956 | format_unit = 'O&' |
| 1957 | |
| 1958 | # What encoding do we want for this variable? Only used |
| 1959 | # by format units starting with 'e'. |
| 1960 | encoding = None |
| 1961 | |
Larry Hastings | 77561cc | 2014-01-07 12:13:13 -0800 | [diff] [blame] | 1962 | # Should this object be required to be a subclass of a specific type? |
| 1963 | # If not None, should be a string representing a pointer to a |
| 1964 | # PyTypeObject (e.g. "&PyUnicode_Type"). |
| 1965 | # Only used by the 'O!' format unit (and the "object" converter). |
| 1966 | subclass_of = None |
| 1967 | |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 1968 | # Do we want an adjacent '_length' variable for this variable? |
| 1969 | # Only used by format units ending with '#'. |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 1970 | length = False |
| 1971 | |
Larry Hastings | 2a72791 | 2014-01-16 11:32:01 -0800 | [diff] [blame] | 1972 | def __init__(self, name, function, default=unspecified, *, c_default=None, py_default=None, annotation=unspecified, **kwargs): |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 1973 | self.function = function |
| 1974 | self.name = name |
| 1975 | |
| 1976 | if default is not unspecified: |
Larry Hastings | 2a72791 | 2014-01-16 11:32:01 -0800 | [diff] [blame] | 1977 | if self.default_type and not isinstance(default, (self.default_type, Unknown)): |
Larry Hastings | 4a55fc5 | 2014-01-12 11:09:57 -0800 | [diff] [blame] | 1978 | if isinstance(self.default_type, type): |
| 1979 | types_str = self.default_type.__name__ |
| 1980 | else: |
| 1981 | types_str = ', '.join((cls.__name__ for cls in self.default_type)) |
| 1982 | fail("{}: default value {!r} for field {} is not of type {}".format( |
| 1983 | self.__class__.__name__, default, name, types_str)) |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 1984 | self.default = default |
Larry Hastings | 2a72791 | 2014-01-16 11:32:01 -0800 | [diff] [blame] | 1985 | |
Larry Hastings | b470575 | 2014-01-18 21:54:15 -0800 | [diff] [blame] | 1986 | if c_default: |
| 1987 | self.c_default = c_default |
| 1988 | if py_default: |
| 1989 | self.py_default = py_default |
Larry Hastings | 2a72791 | 2014-01-16 11:32:01 -0800 | [diff] [blame] | 1990 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 1991 | if annotation != unspecified: |
| 1992 | fail("The 'annotation' parameter is not currently permitted.") |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 1993 | self.converter_init(**kwargs) |
| 1994 | |
| 1995 | def converter_init(self): |
| 1996 | pass |
| 1997 | |
| 1998 | def is_optional(self): |
Larry Hastings | 2a72791 | 2014-01-16 11:32:01 -0800 | [diff] [blame] | 1999 | return (self.default is not unspecified) |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2000 | |
| 2001 | def render(self, parameter, data): |
| 2002 | """ |
| 2003 | parameter is a clinic.Parameter instance. |
| 2004 | data is a CRenderData instance. |
| 2005 | """ |
Larry Hastings | abc716b | 2013-11-20 09:13:52 -0800 | [diff] [blame] | 2006 | self.parameter = parameter |
Larry Hastings | 9026113 | 2014-01-07 12:21:08 -0800 | [diff] [blame] | 2007 | original_name = self.name |
| 2008 | name = ensure_legal_c_identifier(original_name) |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2009 | |
| 2010 | # declarations |
| 2011 | d = self.declaration() |
| 2012 | data.declarations.append(d) |
| 2013 | |
| 2014 | # initializers |
| 2015 | initializers = self.initialize() |
| 2016 | if initializers: |
| 2017 | data.initializers.append('/* initializers for ' + name + ' */\n' + initializers.rstrip()) |
| 2018 | |
| 2019 | # impl_arguments |
| 2020 | s = ("&" if self.impl_by_reference else "") + name |
| 2021 | data.impl_arguments.append(s) |
Larry Hastings | ebdcb50 | 2013-11-23 14:54:00 -0800 | [diff] [blame] | 2022 | if self.length: |
| 2023 | data.impl_arguments.append(self.length_name()) |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2024 | |
| 2025 | # keywords |
Larry Hastings | 9026113 | 2014-01-07 12:21:08 -0800 | [diff] [blame] | 2026 | data.keywords.append(original_name) |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2027 | |
| 2028 | # format_units |
| 2029 | if self.is_optional() and '|' not in data.format_units: |
| 2030 | data.format_units.append('|') |
| 2031 | if parameter.is_keyword_only() and '$' not in data.format_units: |
| 2032 | data.format_units.append('$') |
| 2033 | data.format_units.append(self.format_unit) |
| 2034 | |
| 2035 | # parse_arguments |
| 2036 | self.parse_argument(data.parse_arguments) |
| 2037 | |
| 2038 | # impl_parameters |
| 2039 | data.impl_parameters.append(self.simple_declaration(by_reference=self.impl_by_reference)) |
Larry Hastings | ebdcb50 | 2013-11-23 14:54:00 -0800 | [diff] [blame] | 2040 | if self.length: |
| 2041 | data.impl_parameters.append("Py_ssize_clean_t " + self.length_name()) |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2042 | |
| 2043 | # cleanup |
| 2044 | cleanup = self.cleanup() |
| 2045 | if cleanup: |
| 2046 | data.cleanup.append('/* Cleanup for ' + name + ' */\n' + cleanup.rstrip() + "\n") |
| 2047 | |
Larry Hastings | ebdcb50 | 2013-11-23 14:54:00 -0800 | [diff] [blame] | 2048 | def length_name(self): |
| 2049 | """Computes the name of the associated "length" variable.""" |
| 2050 | if not self.length: |
| 2051 | return None |
| 2052 | return ensure_legal_c_identifier(self.name) + "_length" |
| 2053 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2054 | # Why is this one broken out separately? |
| 2055 | # For "positional-only" function parsing, |
| 2056 | # which generates a bunch of PyArg_ParseTuple calls. |
| 2057 | def parse_argument(self, list): |
| 2058 | assert not (self.converter and self.encoding) |
| 2059 | if self.format_unit == 'O&': |
| 2060 | assert self.converter |
| 2061 | list.append(self.converter) |
| 2062 | |
| 2063 | if self.encoding: |
Larry Hastings | 77561cc | 2014-01-07 12:13:13 -0800 | [diff] [blame] | 2064 | list.append(c_repr(self.encoding)) |
| 2065 | elif self.subclass_of: |
| 2066 | list.append(self.subclass_of) |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2067 | |
Larry Hastings | ebdcb50 | 2013-11-23 14:54:00 -0800 | [diff] [blame] | 2068 | legal_name = ensure_legal_c_identifier(self.name) |
| 2069 | s = ("&" if self.parse_by_reference else "") + legal_name |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2070 | list.append(s) |
| 2071 | |
Larry Hastings | ebdcb50 | 2013-11-23 14:54:00 -0800 | [diff] [blame] | 2072 | if self.length: |
| 2073 | list.append("&" + self.length_name()) |
| 2074 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2075 | # |
| 2076 | # All the functions after here are intended as extension points. |
| 2077 | # |
| 2078 | |
| 2079 | def simple_declaration(self, by_reference=False): |
| 2080 | """ |
| 2081 | Computes the basic declaration of the variable. |
| 2082 | Used in computing the prototype declaration and the |
| 2083 | variable declaration. |
| 2084 | """ |
| 2085 | prototype = [self.type] |
| 2086 | if by_reference or not self.type.endswith('*'): |
| 2087 | prototype.append(" ") |
| 2088 | if by_reference: |
| 2089 | prototype.append('*') |
Larry Hastings | dfcd467 | 2013-10-27 02:49:39 -0700 | [diff] [blame] | 2090 | prototype.append(ensure_legal_c_identifier(self.name)) |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2091 | return "".join(prototype) |
| 2092 | |
| 2093 | def declaration(self): |
| 2094 | """ |
| 2095 | The C statement to declare this variable. |
| 2096 | """ |
| 2097 | declaration = [self.simple_declaration()] |
Larry Hastings | abc716b | 2013-11-20 09:13:52 -0800 | [diff] [blame] | 2098 | default = self.c_default |
| 2099 | if not default and self.parameter.group: |
| 2100 | default = self.c_ignored_default |
| 2101 | if default: |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2102 | declaration.append(" = ") |
Larry Hastings | abc716b | 2013-11-20 09:13:52 -0800 | [diff] [blame] | 2103 | declaration.append(default) |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2104 | declaration.append(";") |
Larry Hastings | ebdcb50 | 2013-11-23 14:54:00 -0800 | [diff] [blame] | 2105 | if self.length: |
| 2106 | declaration.append('\nPy_ssize_clean_t ') |
| 2107 | declaration.append(self.length_name()) |
| 2108 | declaration.append(';') |
Larry Hastings | 3f144c2 | 2014-01-06 10:34:00 -0800 | [diff] [blame] | 2109 | s = "".join(declaration) |
| 2110 | # double up curly-braces, this string will be used |
| 2111 | # as part of a format_map() template later |
| 2112 | s = s.replace("{", "{{") |
| 2113 | s = s.replace("}", "}}") |
| 2114 | return s |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2115 | |
| 2116 | def initialize(self): |
| 2117 | """ |
| 2118 | The C statements required to set up this variable before parsing. |
| 2119 | Returns a string containing this code indented at column 0. |
| 2120 | If no initialization is necessary, returns an empty string. |
| 2121 | """ |
| 2122 | return "" |
| 2123 | |
| 2124 | def cleanup(self): |
| 2125 | """ |
| 2126 | The C statements required to clean up after this variable. |
| 2127 | Returns a string containing this code indented at column 0. |
| 2128 | If no cleanup is necessary, returns an empty string. |
| 2129 | """ |
| 2130 | return "" |
| 2131 | |
| 2132 | |
| 2133 | class bool_converter(CConverter): |
| 2134 | type = 'int' |
Larry Hastings | 4a55fc5 | 2014-01-12 11:09:57 -0800 | [diff] [blame] | 2135 | default_type = bool |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2136 | format_unit = 'p' |
Larry Hastings | abc716b | 2013-11-20 09:13:52 -0800 | [diff] [blame] | 2137 | c_ignored_default = '0' |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2138 | |
| 2139 | def converter_init(self): |
Larry Hastings | 2a72791 | 2014-01-16 11:32:01 -0800 | [diff] [blame] | 2140 | if self.default is not unspecified: |
| 2141 | self.default = bool(self.default) |
| 2142 | self.c_default = str(int(self.default)) |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2143 | |
| 2144 | class char_converter(CConverter): |
| 2145 | type = 'char' |
Larry Hastings | 4a55fc5 | 2014-01-12 11:09:57 -0800 | [diff] [blame] | 2146 | default_type = str |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2147 | format_unit = 'c' |
Larry Hastings | abc716b | 2013-11-20 09:13:52 -0800 | [diff] [blame] | 2148 | c_ignored_default = "'\0'" |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2149 | |
Larry Hastings | 4a55fc5 | 2014-01-12 11:09:57 -0800 | [diff] [blame] | 2150 | def converter_init(self): |
Larry Hastings | 2a72791 | 2014-01-16 11:32:01 -0800 | [diff] [blame] | 2151 | if isinstance(self.default, str) and (len(self.default) != 1): |
Larry Hastings | 4a55fc5 | 2014-01-12 11:09:57 -0800 | [diff] [blame] | 2152 | fail("char_converter: illegal default value " + repr(self.default)) |
| 2153 | |
| 2154 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2155 | @add_legacy_c_converter('B', bitwise=True) |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 2156 | class unsigned_char_converter(CConverter): |
Serhiy Storchaka | 49776ef | 2014-01-19 00:38:36 +0200 | [diff] [blame] | 2157 | type = 'unsigned char' |
Larry Hastings | 4a55fc5 | 2014-01-12 11:09:57 -0800 | [diff] [blame] | 2158 | default_type = int |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2159 | format_unit = 'b' |
Larry Hastings | abc716b | 2013-11-20 09:13:52 -0800 | [diff] [blame] | 2160 | c_ignored_default = "'\0'" |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2161 | |
| 2162 | def converter_init(self, *, bitwise=False): |
| 2163 | if bitwise: |
Larry Hastings | ebdcb50 | 2013-11-23 14:54:00 -0800 | [diff] [blame] | 2164 | self.format_unit = 'B' |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2165 | |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 2166 | class byte_converter(unsigned_char_converter): pass |
| 2167 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2168 | class short_converter(CConverter): |
| 2169 | type = 'short' |
Larry Hastings | 4a55fc5 | 2014-01-12 11:09:57 -0800 | [diff] [blame] | 2170 | default_type = int |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2171 | format_unit = 'h' |
Larry Hastings | abc716b | 2013-11-20 09:13:52 -0800 | [diff] [blame] | 2172 | c_ignored_default = "0" |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2173 | |
| 2174 | class unsigned_short_converter(CConverter): |
| 2175 | type = 'unsigned short' |
Larry Hastings | 4a55fc5 | 2014-01-12 11:09:57 -0800 | [diff] [blame] | 2176 | default_type = int |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2177 | format_unit = 'H' |
Larry Hastings | abc716b | 2013-11-20 09:13:52 -0800 | [diff] [blame] | 2178 | c_ignored_default = "0" |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2179 | |
| 2180 | def converter_init(self, *, bitwise=False): |
| 2181 | if not bitwise: |
| 2182 | fail("Unsigned shorts must be bitwise (for now).") |
| 2183 | |
Larry Hastings | ebdcb50 | 2013-11-23 14:54:00 -0800 | [diff] [blame] | 2184 | @add_legacy_c_converter('C', types='str') |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2185 | class int_converter(CConverter): |
| 2186 | type = 'int' |
Larry Hastings | 4a55fc5 | 2014-01-12 11:09:57 -0800 | [diff] [blame] | 2187 | default_type = int |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2188 | format_unit = 'i' |
Larry Hastings | abc716b | 2013-11-20 09:13:52 -0800 | [diff] [blame] | 2189 | c_ignored_default = "0" |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2190 | |
Larry Hastings | ebdcb50 | 2013-11-23 14:54:00 -0800 | [diff] [blame] | 2191 | def converter_init(self, *, types='int'): |
| 2192 | if types == 'str': |
| 2193 | self.format_unit = 'C' |
| 2194 | elif types != 'int': |
| 2195 | fail("int_converter: illegal 'types' argument") |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2196 | |
| 2197 | class unsigned_int_converter(CConverter): |
| 2198 | type = 'unsigned int' |
Larry Hastings | 4a55fc5 | 2014-01-12 11:09:57 -0800 | [diff] [blame] | 2199 | default_type = int |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2200 | format_unit = 'I' |
Larry Hastings | abc716b | 2013-11-20 09:13:52 -0800 | [diff] [blame] | 2201 | c_ignored_default = "0" |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2202 | |
| 2203 | def converter_init(self, *, bitwise=False): |
| 2204 | if not bitwise: |
| 2205 | fail("Unsigned ints must be bitwise (for now).") |
| 2206 | |
| 2207 | class long_converter(CConverter): |
| 2208 | type = 'long' |
Larry Hastings | 4a55fc5 | 2014-01-12 11:09:57 -0800 | [diff] [blame] | 2209 | default_type = int |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2210 | format_unit = 'l' |
Larry Hastings | abc716b | 2013-11-20 09:13:52 -0800 | [diff] [blame] | 2211 | c_ignored_default = "0" |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2212 | |
| 2213 | class unsigned_long_converter(CConverter): |
| 2214 | type = 'unsigned long' |
Larry Hastings | 4a55fc5 | 2014-01-12 11:09:57 -0800 | [diff] [blame] | 2215 | default_type = int |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2216 | format_unit = 'k' |
Larry Hastings | abc716b | 2013-11-20 09:13:52 -0800 | [diff] [blame] | 2217 | c_ignored_default = "0" |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2218 | |
| 2219 | def converter_init(self, *, bitwise=False): |
| 2220 | if not bitwise: |
| 2221 | fail("Unsigned longs must be bitwise (for now).") |
| 2222 | |
| 2223 | class PY_LONG_LONG_converter(CConverter): |
| 2224 | type = 'PY_LONG_LONG' |
Larry Hastings | 4a55fc5 | 2014-01-12 11:09:57 -0800 | [diff] [blame] | 2225 | default_type = int |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2226 | format_unit = 'L' |
Larry Hastings | abc716b | 2013-11-20 09:13:52 -0800 | [diff] [blame] | 2227 | c_ignored_default = "0" |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2228 | |
| 2229 | class unsigned_PY_LONG_LONG_converter(CConverter): |
| 2230 | type = 'unsigned PY_LONG_LONG' |
Larry Hastings | 4a55fc5 | 2014-01-12 11:09:57 -0800 | [diff] [blame] | 2231 | default_type = int |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2232 | format_unit = 'K' |
Larry Hastings | abc716b | 2013-11-20 09:13:52 -0800 | [diff] [blame] | 2233 | c_ignored_default = "0" |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2234 | |
| 2235 | def converter_init(self, *, bitwise=False): |
| 2236 | if not bitwise: |
| 2237 | fail("Unsigned PY_LONG_LONGs must be bitwise (for now).") |
| 2238 | |
| 2239 | class Py_ssize_t_converter(CConverter): |
| 2240 | type = 'Py_ssize_t' |
Larry Hastings | 4a55fc5 | 2014-01-12 11:09:57 -0800 | [diff] [blame] | 2241 | default_type = int |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2242 | format_unit = 'n' |
Larry Hastings | abc716b | 2013-11-20 09:13:52 -0800 | [diff] [blame] | 2243 | c_ignored_default = "0" |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2244 | |
| 2245 | |
| 2246 | class float_converter(CConverter): |
| 2247 | type = 'float' |
Larry Hastings | 4a55fc5 | 2014-01-12 11:09:57 -0800 | [diff] [blame] | 2248 | default_type = float |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2249 | format_unit = 'f' |
Larry Hastings | abc716b | 2013-11-20 09:13:52 -0800 | [diff] [blame] | 2250 | c_ignored_default = "0.0" |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2251 | |
| 2252 | class double_converter(CConverter): |
| 2253 | type = 'double' |
Larry Hastings | 4a55fc5 | 2014-01-12 11:09:57 -0800 | [diff] [blame] | 2254 | default_type = float |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2255 | format_unit = 'd' |
Larry Hastings | abc716b | 2013-11-20 09:13:52 -0800 | [diff] [blame] | 2256 | c_ignored_default = "0.0" |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2257 | |
| 2258 | |
| 2259 | class Py_complex_converter(CConverter): |
| 2260 | type = 'Py_complex' |
Larry Hastings | 4a55fc5 | 2014-01-12 11:09:57 -0800 | [diff] [blame] | 2261 | default_type = complex |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2262 | format_unit = 'D' |
Larry Hastings | abc716b | 2013-11-20 09:13:52 -0800 | [diff] [blame] | 2263 | c_ignored_default = "{0.0, 0.0}" |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2264 | |
| 2265 | |
| 2266 | class object_converter(CConverter): |
| 2267 | type = 'PyObject *' |
| 2268 | format_unit = 'O' |
| 2269 | |
Larry Hastings | 4a55fc5 | 2014-01-12 11:09:57 -0800 | [diff] [blame] | 2270 | def converter_init(self, *, converter=None, type=None, subclass_of=None): |
| 2271 | if converter: |
| 2272 | if subclass_of: |
| 2273 | fail("object: Cannot pass in both 'converter' and 'subclass_of'") |
| 2274 | self.format_unit = 'O&' |
| 2275 | self.converter = converter |
| 2276 | elif subclass_of: |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2277 | self.format_unit = 'O!' |
Larry Hastings | 77561cc | 2014-01-07 12:13:13 -0800 | [diff] [blame] | 2278 | self.subclass_of = subclass_of |
Larry Hastings | 4a55fc5 | 2014-01-12 11:09:57 -0800 | [diff] [blame] | 2279 | |
Larry Hastings | 77561cc | 2014-01-07 12:13:13 -0800 | [diff] [blame] | 2280 | if type is not None: |
| 2281 | self.type = type |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2282 | |
| 2283 | |
Larry Hastings | ebdcb50 | 2013-11-23 14:54:00 -0800 | [diff] [blame] | 2284 | @add_legacy_c_converter('s#', length=True) |
Larry Hastings | 2a72791 | 2014-01-16 11:32:01 -0800 | [diff] [blame] | 2285 | @add_legacy_c_converter('y', types="bytes") |
| 2286 | @add_legacy_c_converter('y#', types="bytes", length=True) |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2287 | @add_legacy_c_converter('z', nullable=True) |
Larry Hastings | ebdcb50 | 2013-11-23 14:54:00 -0800 | [diff] [blame] | 2288 | @add_legacy_c_converter('z#', nullable=True, length=True) |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2289 | class str_converter(CConverter): |
| 2290 | type = 'const char *' |
Larry Hastings | 4a55fc5 | 2014-01-12 11:09:57 -0800 | [diff] [blame] | 2291 | default_type = (str, Null, NoneType) |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2292 | format_unit = 's' |
| 2293 | |
Larry Hastings | ebdcb50 | 2013-11-23 14:54:00 -0800 | [diff] [blame] | 2294 | def converter_init(self, *, encoding=None, types="str", |
| 2295 | length=False, nullable=False, zeroes=False): |
| 2296 | |
| 2297 | types = set(types.strip().split()) |
| 2298 | bytes_type = set(("bytes",)) |
| 2299 | str_type = set(("str",)) |
| 2300 | all_3_type = set(("bytearray",)) | bytes_type | str_type |
| 2301 | is_bytes = types == bytes_type |
| 2302 | is_str = types == str_type |
| 2303 | is_all_3 = types == all_3_type |
| 2304 | |
| 2305 | self.length = bool(length) |
| 2306 | format_unit = None |
| 2307 | |
| 2308 | if encoding: |
| 2309 | self.encoding = encoding |
| 2310 | |
| 2311 | if is_str and not (length or zeroes or nullable): |
| 2312 | format_unit = 'es' |
| 2313 | elif is_all_3 and not (length or zeroes or nullable): |
| 2314 | format_unit = 'et' |
| 2315 | elif is_str and length and zeroes and not nullable: |
| 2316 | format_unit = 'es#' |
| 2317 | elif is_all_3 and length and not (nullable or zeroes): |
| 2318 | format_unit = 'et#' |
| 2319 | |
| 2320 | if format_unit.endswith('#'): |
Larry Hastings | 2f9a9aa | 2013-11-24 04:23:35 -0800 | [diff] [blame] | 2321 | print("Warning: code using format unit ", repr(format_unit), "probably doesn't work properly.") |
Larry Hastings | ebdcb50 | 2013-11-23 14:54:00 -0800 | [diff] [blame] | 2322 | # TODO set pointer to NULL |
| 2323 | # TODO add cleanup for buffer |
| 2324 | pass |
| 2325 | |
| 2326 | else: |
| 2327 | if zeroes: |
| 2328 | fail("str_converter: illegal combination of arguments (zeroes is only legal with an encoding)") |
| 2329 | |
| 2330 | if is_bytes and not (nullable or length): |
| 2331 | format_unit = 'y' |
| 2332 | elif is_bytes and length and not nullable: |
| 2333 | format_unit = 'y#' |
| 2334 | elif is_str and not (nullable or length): |
| 2335 | format_unit = 's' |
| 2336 | elif is_str and length and not nullable: |
| 2337 | format_unit = 's#' |
| 2338 | elif is_str and nullable and not length: |
| 2339 | format_unit = 'z' |
| 2340 | elif is_str and nullable and length: |
| 2341 | format_unit = 'z#' |
| 2342 | |
| 2343 | if not format_unit: |
| 2344 | fail("str_converter: illegal combination of arguments") |
| 2345 | self.format_unit = format_unit |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2346 | |
| 2347 | |
| 2348 | class PyBytesObject_converter(CConverter): |
| 2349 | type = 'PyBytesObject *' |
| 2350 | format_unit = 'S' |
| 2351 | |
| 2352 | class PyByteArrayObject_converter(CConverter): |
| 2353 | type = 'PyByteArrayObject *' |
| 2354 | format_unit = 'Y' |
| 2355 | |
| 2356 | class unicode_converter(CConverter): |
| 2357 | type = 'PyObject *' |
Larry Hastings | 4a55fc5 | 2014-01-12 11:09:57 -0800 | [diff] [blame] | 2358 | default_type = (str, Null, NoneType) |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2359 | format_unit = 'U' |
| 2360 | |
Larry Hastings | ebdcb50 | 2013-11-23 14:54:00 -0800 | [diff] [blame] | 2361 | @add_legacy_c_converter('u#', length=True) |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2362 | @add_legacy_c_converter('Z', nullable=True) |
Larry Hastings | ebdcb50 | 2013-11-23 14:54:00 -0800 | [diff] [blame] | 2363 | @add_legacy_c_converter('Z#', nullable=True, length=True) |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2364 | class Py_UNICODE_converter(CConverter): |
| 2365 | type = 'Py_UNICODE *' |
Larry Hastings | 4a55fc5 | 2014-01-12 11:09:57 -0800 | [diff] [blame] | 2366 | default_type = (str, Null, NoneType) |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2367 | format_unit = 'u' |
| 2368 | |
Larry Hastings | ebdcb50 | 2013-11-23 14:54:00 -0800 | [diff] [blame] | 2369 | def converter_init(self, *, nullable=False, length=False): |
| 2370 | format_unit = 'Z' if nullable else 'u' |
| 2371 | if length: |
| 2372 | format_unit += '#' |
| 2373 | self.length = True |
| 2374 | self.format_unit = format_unit |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2375 | |
Larry Hastings | ebdcb50 | 2013-11-23 14:54:00 -0800 | [diff] [blame] | 2376 | # |
| 2377 | # We define three string conventions for buffer types in the 'types' argument: |
| 2378 | # 'buffer' : any object supporting the buffer interface |
| 2379 | # 'rwbuffer': any object supporting the buffer interface, but must be writeable |
| 2380 | # 'robuffer': any object supporting the buffer interface, but must not be writeable |
| 2381 | # |
| 2382 | @add_legacy_c_converter('s*', types='str bytes bytearray buffer') |
| 2383 | @add_legacy_c_converter('z*', types='str bytes bytearray buffer', nullable=True) |
| 2384 | @add_legacy_c_converter('w*', types='bytearray rwbuffer') |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2385 | class Py_buffer_converter(CConverter): |
| 2386 | type = 'Py_buffer' |
| 2387 | format_unit = 'y*' |
| 2388 | impl_by_reference = True |
Larry Hastings | 4a55fc5 | 2014-01-12 11:09:57 -0800 | [diff] [blame] | 2389 | c_ignored_default = "{NULL, NULL}" |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2390 | |
Larry Hastings | ebdcb50 | 2013-11-23 14:54:00 -0800 | [diff] [blame] | 2391 | def converter_init(self, *, types='bytes bytearray buffer', nullable=False): |
Larry Hastings | 4a55fc5 | 2014-01-12 11:09:57 -0800 | [diff] [blame] | 2392 | if self.default not in (unspecified, None): |
| 2393 | fail("The only legal default value for Py_buffer is None.") |
Larry Hastings | 3f144c2 | 2014-01-06 10:34:00 -0800 | [diff] [blame] | 2394 | self.c_default = self.c_ignored_default |
Larry Hastings | ebdcb50 | 2013-11-23 14:54:00 -0800 | [diff] [blame] | 2395 | types = set(types.strip().split()) |
| 2396 | bytes_type = set(('bytes',)) |
| 2397 | bytearray_type = set(('bytearray',)) |
| 2398 | buffer_type = set(('buffer',)) |
| 2399 | rwbuffer_type = set(('rwbuffer',)) |
| 2400 | robuffer_type = set(('robuffer',)) |
| 2401 | str_type = set(('str',)) |
| 2402 | bytes_bytearray_buffer_type = bytes_type | bytearray_type | buffer_type |
| 2403 | |
| 2404 | format_unit = None |
| 2405 | if types == (str_type | bytes_bytearray_buffer_type): |
| 2406 | format_unit = 's*' if not nullable else 'z*' |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2407 | else: |
Larry Hastings | ebdcb50 | 2013-11-23 14:54:00 -0800 | [diff] [blame] | 2408 | if nullable: |
| 2409 | fail('Py_buffer_converter: illegal combination of arguments (nullable=True)') |
| 2410 | elif types == (bytes_bytearray_buffer_type): |
| 2411 | format_unit = 'y*' |
Larry Hastings | 4a55fc5 | 2014-01-12 11:09:57 -0800 | [diff] [blame] | 2412 | elif types == (bytearray_type | rwbuffer_type): |
Larry Hastings | ebdcb50 | 2013-11-23 14:54:00 -0800 | [diff] [blame] | 2413 | format_unit = 'w*' |
| 2414 | if not format_unit: |
| 2415 | fail("Py_buffer_converter: illegal combination of arguments") |
| 2416 | |
| 2417 | self.format_unit = format_unit |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2418 | |
| 2419 | def cleanup(self): |
Larry Hastings | ebdcb50 | 2013-11-23 14:54:00 -0800 | [diff] [blame] | 2420 | name = ensure_legal_c_identifier(self.name) |
Larry Hastings | 4a55fc5 | 2014-01-12 11:09:57 -0800 | [diff] [blame] | 2421 | return "".join(["if (", name, ".obj)\n PyBuffer_Release(&", name, ");\n"]) |
Larry Hastings | ebdcb50 | 2013-11-23 14:54:00 -0800 | [diff] [blame] | 2422 | |
| 2423 | |
| 2424 | class self_converter(CConverter): |
| 2425 | """ |
| 2426 | A special-case converter: |
| 2427 | this is the default converter used for "self". |
| 2428 | """ |
| 2429 | type = "PyObject *" |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 2430 | def converter_init(self, *, type=None): |
Larry Hastings | ebdcb50 | 2013-11-23 14:54:00 -0800 | [diff] [blame] | 2431 | f = self.function |
Larry Hastings | 8666e65 | 2014-01-12 14:12:59 -0800 | [diff] [blame] | 2432 | if f.kind in (CALLABLE, METHOD_INIT): |
Larry Hastings | ebdcb50 | 2013-11-23 14:54:00 -0800 | [diff] [blame] | 2433 | if f.cls: |
| 2434 | self.name = "self" |
| 2435 | else: |
| 2436 | self.name = "module" |
| 2437 | self.type = "PyModuleDef *" |
| 2438 | elif f.kind == STATIC_METHOD: |
| 2439 | self.name = "null" |
| 2440 | self.type = "void *" |
| 2441 | elif f.kind == CLASS_METHOD: |
| 2442 | self.name = "cls" |
| 2443 | self.type = "PyTypeObject *" |
Larry Hastings | 8666e65 | 2014-01-12 14:12:59 -0800 | [diff] [blame] | 2444 | elif f.kind == METHOD_NEW: |
| 2445 | self.name = "type" |
| 2446 | self.type = "PyTypeObject *" |
Larry Hastings | ebdcb50 | 2013-11-23 14:54:00 -0800 | [diff] [blame] | 2447 | |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 2448 | if type: |
| 2449 | self.type = type |
| 2450 | |
Larry Hastings | ebdcb50 | 2013-11-23 14:54:00 -0800 | [diff] [blame] | 2451 | def render(self, parameter, data): |
| 2452 | fail("render() should never be called on self_converter instances") |
| 2453 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2454 | |
| 2455 | |
| 2456 | def add_c_return_converter(f, name=None): |
| 2457 | if not name: |
| 2458 | name = f.__name__ |
| 2459 | if not name.endswith('_return_converter'): |
| 2460 | return f |
| 2461 | name = name[:-len('_return_converter')] |
| 2462 | return_converters[name] = f |
| 2463 | return f |
| 2464 | |
| 2465 | |
| 2466 | class CReturnConverterAutoRegister(type): |
| 2467 | def __init__(cls, name, bases, classdict): |
| 2468 | add_c_return_converter(cls) |
| 2469 | |
| 2470 | class CReturnConverter(metaclass=CReturnConverterAutoRegister): |
| 2471 | |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 2472 | # The C type to use for this variable. |
| 2473 | # 'type' should be a Python string specifying the type, e.g. "int". |
| 2474 | # If this is a pointer type, the type string should end with ' *'. |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2475 | type = 'PyObject *' |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 2476 | |
| 2477 | # The Python default value for this parameter, as a Python value. |
| 2478 | # Or the magic value "unspecified" if there is no default. |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2479 | default = None |
| 2480 | |
Larry Hastings | 2a72791 | 2014-01-16 11:32:01 -0800 | [diff] [blame] | 2481 | def __init__(self, *, py_default=None, **kwargs): |
| 2482 | self.py_default = py_default |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2483 | try: |
| 2484 | self.return_converter_init(**kwargs) |
| 2485 | except TypeError as e: |
| 2486 | s = ', '.join(name + '=' + repr(value) for name, value in kwargs.items()) |
| 2487 | sys.exit(self.__class__.__name__ + '(' + s + ')\n' + str(e)) |
| 2488 | |
| 2489 | def return_converter_init(self): |
| 2490 | pass |
| 2491 | |
| 2492 | def declare(self, data, name="_return_value"): |
| 2493 | line = [] |
| 2494 | add = line.append |
| 2495 | add(self.type) |
| 2496 | if not self.type.endswith('*'): |
| 2497 | add(' ') |
| 2498 | add(name + ';') |
| 2499 | data.declarations.append(''.join(line)) |
| 2500 | data.return_value = name |
| 2501 | |
| 2502 | def err_occurred_if(self, expr, data): |
| 2503 | data.return_conversion.append('if (({}) && PyErr_Occurred())\n goto exit;\n'.format(expr)) |
| 2504 | |
| 2505 | def err_occurred_if_null_pointer(self, variable, data): |
| 2506 | data.return_conversion.append('if ({} == NULL)\n goto exit;\n'.format(variable)) |
| 2507 | |
| 2508 | def render(self, function, data): |
| 2509 | """ |
| 2510 | function is a clinic.Function instance. |
| 2511 | data is a CRenderData instance. |
| 2512 | """ |
| 2513 | pass |
| 2514 | |
| 2515 | add_c_return_converter(CReturnConverter, 'object') |
| 2516 | |
Larry Hastings | 78cf85c | 2014-01-04 12:44:57 -0800 | [diff] [blame] | 2517 | class NoneType_return_converter(CReturnConverter): |
| 2518 | def render(self, function, data): |
| 2519 | self.declare(data) |
| 2520 | data.return_conversion.append(''' |
| 2521 | if (_return_value != Py_None) |
| 2522 | goto exit; |
| 2523 | return_value = Py_None; |
| 2524 | Py_INCREF(Py_None); |
| 2525 | '''.strip()) |
| 2526 | |
Larry Hastings | 4a55fc5 | 2014-01-12 11:09:57 -0800 | [diff] [blame] | 2527 | class bool_return_converter(CReturnConverter): |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2528 | type = 'int' |
| 2529 | |
| 2530 | def render(self, function, data): |
| 2531 | self.declare(data) |
| 2532 | self.err_occurred_if("_return_value == -1", data) |
Larry Hastings | 4a55fc5 | 2014-01-12 11:09:57 -0800 | [diff] [blame] | 2533 | data.return_conversion.append('return_value = PyBool_FromLong((long)_return_value);\n') |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2534 | |
| 2535 | class long_return_converter(CReturnConverter): |
| 2536 | type = 'long' |
Larry Hastings | 4a55fc5 | 2014-01-12 11:09:57 -0800 | [diff] [blame] | 2537 | conversion_fn = 'PyLong_FromLong' |
| 2538 | cast = '' |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2539 | |
| 2540 | def render(self, function, data): |
| 2541 | self.declare(data) |
| 2542 | self.err_occurred_if("_return_value == -1", data) |
| 2543 | data.return_conversion.append( |
Larry Hastings | 4a55fc5 | 2014-01-12 11:09:57 -0800 | [diff] [blame] | 2544 | ''.join(('return_value = ', self.conversion_fn, '(', self.cast, '_return_value);\n'))) |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2545 | |
Larry Hastings | 4a55fc5 | 2014-01-12 11:09:57 -0800 | [diff] [blame] | 2546 | class int_return_converter(long_return_converter): |
| 2547 | type = 'int' |
| 2548 | cast = '(long)' |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2549 | |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 2550 | class init_return_converter(long_return_converter): |
| 2551 | """ |
| 2552 | Special return converter for __init__ functions. |
| 2553 | """ |
| 2554 | type = 'int' |
| 2555 | cast = '(long)' |
| 2556 | |
| 2557 | def render(self, function, data): |
| 2558 | pass |
| 2559 | |
Larry Hastings | 4a55fc5 | 2014-01-12 11:09:57 -0800 | [diff] [blame] | 2560 | class unsigned_long_return_converter(long_return_converter): |
| 2561 | type = 'unsigned long' |
| 2562 | conversion_fn = 'PyLong_FromUnsignedLong' |
| 2563 | |
| 2564 | class unsigned_int_return_converter(unsigned_long_return_converter): |
| 2565 | type = 'unsigned int' |
| 2566 | cast = '(unsigned long)' |
| 2567 | |
| 2568 | class Py_ssize_t_return_converter(long_return_converter): |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2569 | type = 'Py_ssize_t' |
Larry Hastings | 4a55fc5 | 2014-01-12 11:09:57 -0800 | [diff] [blame] | 2570 | conversion_fn = 'PyLong_FromSsize_t' |
| 2571 | |
| 2572 | class size_t_return_converter(long_return_converter): |
| 2573 | type = 'size_t' |
| 2574 | conversion_fn = 'PyLong_FromSize_t' |
| 2575 | |
| 2576 | |
| 2577 | class double_return_converter(CReturnConverter): |
| 2578 | type = 'double' |
| 2579 | cast = '' |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2580 | |
| 2581 | def render(self, function, data): |
| 2582 | self.declare(data) |
Larry Hastings | 4a55fc5 | 2014-01-12 11:09:57 -0800 | [diff] [blame] | 2583 | self.err_occurred_if("_return_value == -1.0", data) |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2584 | data.return_conversion.append( |
Larry Hastings | 4a55fc5 | 2014-01-12 11:09:57 -0800 | [diff] [blame] | 2585 | 'return_value = PyFloat_FromDouble(' + self.cast + '_return_value);\n') |
| 2586 | |
| 2587 | class float_return_converter(double_return_converter): |
| 2588 | type = 'float' |
| 2589 | cast = '(double)' |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2590 | |
| 2591 | |
| 2592 | class DecodeFSDefault_return_converter(CReturnConverter): |
| 2593 | type = 'char *' |
| 2594 | |
| 2595 | def render(self, function, data): |
| 2596 | self.declare(data) |
| 2597 | self.err_occurred_if_null_pointer("_return_value", data) |
| 2598 | data.return_conversion.append( |
| 2599 | 'return_value = PyUnicode_DecodeFSDefault(_return_value);\n') |
| 2600 | |
| 2601 | |
| 2602 | class IndentStack: |
| 2603 | def __init__(self): |
| 2604 | self.indents = [] |
| 2605 | self.margin = None |
| 2606 | |
| 2607 | def _ensure(self): |
| 2608 | if not self.indents: |
| 2609 | fail('IndentStack expected indents, but none are defined.') |
| 2610 | |
| 2611 | def measure(self, line): |
| 2612 | """ |
| 2613 | Returns the length of the line's margin. |
| 2614 | """ |
| 2615 | if '\t' in line: |
| 2616 | fail('Tab characters are illegal in the Clinic DSL.') |
| 2617 | stripped = line.lstrip() |
| 2618 | if not len(stripped): |
| 2619 | # we can't tell anything from an empty line |
| 2620 | # so just pretend it's indented like our current indent |
| 2621 | self._ensure() |
| 2622 | return self.indents[-1] |
| 2623 | return len(line) - len(stripped) |
| 2624 | |
| 2625 | def infer(self, line): |
| 2626 | """ |
| 2627 | Infer what is now the current margin based on this line. |
| 2628 | Returns: |
| 2629 | 1 if we have indented (or this is the first margin) |
| 2630 | 0 if the margin has not changed |
| 2631 | -N if we have dedented N times |
| 2632 | """ |
| 2633 | indent = self.measure(line) |
| 2634 | margin = ' ' * indent |
| 2635 | if not self.indents: |
| 2636 | self.indents.append(indent) |
| 2637 | self.margin = margin |
| 2638 | return 1 |
| 2639 | current = self.indents[-1] |
| 2640 | if indent == current: |
| 2641 | return 0 |
| 2642 | if indent > current: |
| 2643 | self.indents.append(indent) |
| 2644 | self.margin = margin |
| 2645 | return 1 |
| 2646 | # indent < current |
| 2647 | if indent not in self.indents: |
| 2648 | fail("Illegal outdent.") |
| 2649 | outdent_count = 0 |
| 2650 | while indent != current: |
| 2651 | self.indents.pop() |
| 2652 | current = self.indents[-1] |
| 2653 | outdent_count -= 1 |
| 2654 | self.margin = margin |
| 2655 | return outdent_count |
| 2656 | |
| 2657 | @property |
| 2658 | def depth(self): |
| 2659 | """ |
| 2660 | Returns how many margins are currently defined. |
| 2661 | """ |
| 2662 | return len(self.indents) |
| 2663 | |
| 2664 | def indent(self, line): |
| 2665 | """ |
| 2666 | Indents a line by the currently defined margin. |
| 2667 | """ |
| 2668 | return self.margin + line |
| 2669 | |
| 2670 | def dedent(self, line): |
| 2671 | """ |
| 2672 | Dedents a line by the currently defined margin. |
| 2673 | (The inverse of 'indent'.) |
| 2674 | """ |
| 2675 | margin = self.margin |
| 2676 | indent = self.indents[-1] |
| 2677 | if not line.startswith(margin): |
| 2678 | fail('Cannot dedent, line does not start with the previous margin:') |
| 2679 | return line[indent:] |
| 2680 | |
| 2681 | |
| 2682 | class DSLParser: |
| 2683 | def __init__(self, clinic): |
| 2684 | self.clinic = clinic |
| 2685 | |
| 2686 | self.directives = {} |
| 2687 | for name in dir(self): |
| 2688 | # functions that start with directive_ are added to directives |
| 2689 | _, s, key = name.partition("directive_") |
| 2690 | if s: |
| 2691 | self.directives[key] = getattr(self, name) |
| 2692 | |
| 2693 | # functions that start with at_ are too, with an @ in front |
| 2694 | _, s, key = name.partition("at_") |
| 2695 | if s: |
| 2696 | self.directives['@' + key] = getattr(self, name) |
| 2697 | |
| 2698 | self.reset() |
| 2699 | |
| 2700 | def reset(self): |
| 2701 | self.function = None |
| 2702 | self.state = self.state_dsl_start |
| 2703 | self.parameter_indent = None |
| 2704 | self.keyword_only = False |
| 2705 | self.group = 0 |
| 2706 | self.parameter_state = self.ps_start |
| 2707 | self.indent = IndentStack() |
| 2708 | self.kind = CALLABLE |
| 2709 | self.coexist = False |
Larry Hastings | 2a72791 | 2014-01-16 11:32:01 -0800 | [diff] [blame] | 2710 | self.parameter_continuation = '' |
Larry Hastings | bebf735 | 2014-01-17 17:47:17 -0800 | [diff] [blame] | 2711 | self.preserve_output = False |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2712 | |
Larry Hastings | ebdcb50 | 2013-11-23 14:54:00 -0800 | [diff] [blame] | 2713 | def directive_version(self, required): |
| 2714 | global version |
| 2715 | if version_comparitor(version, required) < 0: |
| 2716 | fail("Insufficient Clinic version!\n Version: " + version + "\n Required: " + required) |
| 2717 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2718 | def directive_module(self, name): |
| 2719 | fields = name.split('.') |
| 2720 | new = fields.pop() |
| 2721 | module, cls = self.clinic._module_and_class(fields) |
| 2722 | if cls: |
| 2723 | fail("Can't nest a module inside a class!") |
| 2724 | m = Module(name, module) |
| 2725 | module.modules[name] = m |
| 2726 | self.block.signatures.append(m) |
| 2727 | |
| 2728 | def directive_class(self, name): |
| 2729 | fields = name.split('.') |
| 2730 | in_classes = False |
| 2731 | parent = self |
| 2732 | name = fields.pop() |
| 2733 | so_far = [] |
| 2734 | module, cls = self.clinic._module_and_class(fields) |
| 2735 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2736 | c = Class(name, module, cls) |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2737 | if cls: |
| 2738 | cls.classes[name] = c |
Larry Hastings | ed4a1c5 | 2013-11-18 09:32:13 -0800 | [diff] [blame] | 2739 | else: |
| 2740 | module.classes[name] = c |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2741 | self.block.signatures.append(c) |
| 2742 | |
Larry Hastings | bebf735 | 2014-01-17 17:47:17 -0800 | [diff] [blame] | 2743 | def directive_set(self, name, value): |
| 2744 | if name not in ("line_prefix", "line_suffix"): |
| 2745 | fail("unknown variable", repr(name)) |
| 2746 | |
| 2747 | value = value.format_map({ |
| 2748 | 'block comment start': '/*', |
| 2749 | 'block comment end': '*/', |
| 2750 | }) |
| 2751 | |
| 2752 | self.clinic.__dict__[name] = value |
| 2753 | |
| 2754 | def directive_destination(self, name, command, *args): |
| 2755 | if command is 'new': |
| 2756 | self.clinic.add_destination(name, command, *args) |
| 2757 | return |
| 2758 | |
| 2759 | if command is 'clear': |
| 2760 | self.clinic.get_destination(name).clear() |
| 2761 | fail("unknown destination command", repr(command)) |
| 2762 | |
| 2763 | |
| 2764 | def directive_output(self, field, destination=''): |
| 2765 | fd = self.clinic.field_destinations |
| 2766 | |
| 2767 | if field == "preset": |
| 2768 | preset = self.clinic.presets.get(destination) |
| 2769 | if not preset: |
| 2770 | fail("Unknown preset " + repr(destination) + "!") |
| 2771 | fd.update(preset) |
| 2772 | return |
| 2773 | |
| 2774 | if field == "push": |
| 2775 | self.clinic.field_destinations_stack.append(fd.copy()) |
| 2776 | return |
| 2777 | |
| 2778 | if field == "pop": |
| 2779 | if not self.clinic.field_destinations_stack: |
| 2780 | fail("Can't 'output pop', stack is empty!") |
| 2781 | previous_fd = self.clinic.field_destinations_stack.pop() |
| 2782 | fd.update(previous_fd) |
| 2783 | return |
| 2784 | |
| 2785 | # secret command for debugging! |
| 2786 | if field == "print": |
| 2787 | self.block.output.append(pprint.pformat(fd)) |
| 2788 | self.block.output.append('\n') |
| 2789 | return |
| 2790 | |
| 2791 | d = self.clinic.get_destination(destination) |
| 2792 | |
| 2793 | if field == "everything": |
| 2794 | for name in list(fd): |
| 2795 | fd[name] = d |
| 2796 | return |
| 2797 | |
| 2798 | if field not in fd: |
| 2799 | fail("Invalid field " + repr(field) + ", must be one of:\n " + ", ".join(valid_fields)) |
| 2800 | fd[field] = d |
| 2801 | |
| 2802 | def directive_dump(self, name): |
| 2803 | self.block.output.append(self.clinic.get_destination(name).dump()) |
| 2804 | |
| 2805 | def directive_print(self, *args): |
| 2806 | self.block.output.append(' '.join(args)) |
| 2807 | self.block.output.append('\n') |
| 2808 | |
| 2809 | def directive_preserve(self): |
| 2810 | if self.preserve_output: |
| 2811 | fail("Can't have preserve twice in one block!") |
| 2812 | self.preserve_output = True |
| 2813 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2814 | def at_classmethod(self): |
Larry Hastings | 2a72791 | 2014-01-16 11:32:01 -0800 | [diff] [blame] | 2815 | if self.kind is not CALLABLE: |
| 2816 | fail("Can't set @classmethod, function is not a normal callable") |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2817 | self.kind = CLASS_METHOD |
| 2818 | |
| 2819 | def at_staticmethod(self): |
Larry Hastings | 2a72791 | 2014-01-16 11:32:01 -0800 | [diff] [blame] | 2820 | if self.kind is not CALLABLE: |
| 2821 | fail("Can't set @staticmethod, function is not a normal callable") |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2822 | self.kind = STATIC_METHOD |
| 2823 | |
| 2824 | def at_coexist(self): |
Larry Hastings | 2a72791 | 2014-01-16 11:32:01 -0800 | [diff] [blame] | 2825 | if self.coexist: |
| 2826 | fail("Called @coexist twice!") |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2827 | self.coexist = True |
| 2828 | |
| 2829 | def parse(self, block): |
| 2830 | self.reset() |
| 2831 | self.block = block |
Larry Hastings | bebf735 | 2014-01-17 17:47:17 -0800 | [diff] [blame] | 2832 | self.saved_output = self.block.output |
| 2833 | block.output = [] |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2834 | block_start = self.clinic.block_parser.line_number |
| 2835 | lines = block.input.split('\n') |
| 2836 | for line_number, line in enumerate(lines, self.clinic.block_parser.block_start_line_number): |
| 2837 | if '\t' in line: |
| 2838 | fail('Tab characters are illegal in the Clinic DSL.\n\t' + repr(line), line_number=block_start) |
| 2839 | self.state(line) |
| 2840 | |
| 2841 | self.next(self.state_terminal) |
| 2842 | self.state(None) |
| 2843 | |
Larry Hastings | bebf735 | 2014-01-17 17:47:17 -0800 | [diff] [blame] | 2844 | block.output.extend(self.clinic.language.render(clinic, block.signatures)) |
| 2845 | |
| 2846 | if self.preserve_output: |
| 2847 | if block.output: |
| 2848 | fail("'preserve' only works for blocks that don't produce any output!") |
| 2849 | block.output = self.saved_output |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2850 | |
| 2851 | @staticmethod |
| 2852 | def ignore_line(line): |
| 2853 | # ignore comment-only lines |
| 2854 | if line.lstrip().startswith('#'): |
| 2855 | return True |
| 2856 | |
| 2857 | # Ignore empty lines too |
| 2858 | # (but not in docstring sections!) |
| 2859 | if not line.strip(): |
| 2860 | return True |
| 2861 | |
| 2862 | return False |
| 2863 | |
| 2864 | @staticmethod |
| 2865 | def calculate_indent(line): |
| 2866 | return len(line) - len(line.strip()) |
| 2867 | |
| 2868 | def next(self, state, line=None): |
| 2869 | # real_print(self.state.__name__, "->", state.__name__, ", line=", line) |
| 2870 | self.state = state |
| 2871 | if line is not None: |
| 2872 | self.state(line) |
| 2873 | |
| 2874 | def state_dsl_start(self, line): |
| 2875 | # self.block = self.ClinicOutputBlock(self) |
| 2876 | if self.ignore_line(line): |
| 2877 | return |
| 2878 | self.next(self.state_modulename_name, line) |
| 2879 | |
| 2880 | def state_modulename_name(self, line): |
| 2881 | # looking for declaration, which establishes the leftmost column |
| 2882 | # line should be |
| 2883 | # modulename.fnname [as c_basename] [-> return annotation] |
| 2884 | # square brackets denote optional syntax. |
| 2885 | # |
Larry Hastings | 4a714d4 | 2014-01-14 22:22:41 -0800 | [diff] [blame] | 2886 | # alternatively: |
| 2887 | # modulename.fnname [as c_basename] = modulename.existing_fn_name |
| 2888 | # clones the parameters and return converter from that |
| 2889 | # function. you can't modify them. you must enter a |
| 2890 | # new docstring. |
| 2891 | # |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2892 | # (but we might find a directive first!) |
| 2893 | # |
| 2894 | # this line is permitted to start with whitespace. |
| 2895 | # we'll call this number of spaces F (for "function"). |
| 2896 | |
| 2897 | if not line.strip(): |
| 2898 | return |
| 2899 | |
| 2900 | self.indent.infer(line) |
| 2901 | |
| 2902 | # is it a directive? |
| 2903 | fields = shlex.split(line) |
| 2904 | directive_name = fields[0] |
| 2905 | directive = self.directives.get(directive_name, None) |
| 2906 | if directive: |
Larry Hastings | bebf735 | 2014-01-17 17:47:17 -0800 | [diff] [blame] | 2907 | try: |
| 2908 | directive(*fields[1:]) |
| 2909 | except TypeError as e: |
| 2910 | fail(str(e)) |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2911 | return |
| 2912 | |
Larry Hastings | 4a714d4 | 2014-01-14 22:22:41 -0800 | [diff] [blame] | 2913 | # are we cloning? |
| 2914 | before, equals, existing = line.rpartition('=') |
| 2915 | if equals: |
| 2916 | full_name, _, c_basename = before.partition(' as ') |
| 2917 | full_name = full_name.strip() |
| 2918 | c_basename = c_basename.strip() |
| 2919 | existing = existing.strip() |
| 2920 | if (is_legal_py_identifier(full_name) and |
| 2921 | (not c_basename or is_legal_c_identifier(c_basename)) and |
| 2922 | is_legal_py_identifier(existing)): |
| 2923 | # we're cloning! |
| 2924 | fields = [x.strip() for x in existing.split('.')] |
| 2925 | function_name = fields.pop() |
| 2926 | module, cls = self.clinic._module_and_class(fields) |
| 2927 | |
| 2928 | for existing_function in (cls or module).functions: |
| 2929 | if existing_function.name == function_name: |
| 2930 | break |
| 2931 | else: |
| 2932 | existing_function = None |
| 2933 | if not existing_function: |
| 2934 | fail("Couldn't find existing function " + repr(existing) + "!") |
| 2935 | |
| 2936 | fields = [x.strip() for x in full_name.split('.')] |
| 2937 | function_name = fields.pop() |
| 2938 | module, cls = self.clinic._module_and_class(fields) |
| 2939 | |
| 2940 | if not (existing_function.kind == self.kind and existing_function.coexist == self.coexist): |
| 2941 | fail("'kind' of function and cloned function don't match! (@classmethod/@staticmethod/@coexist)") |
| 2942 | self.function = Function(name=function_name, full_name=full_name, module=module, cls=cls, c_basename=c_basename, |
| 2943 | return_converter=existing_function.return_converter, kind=existing_function.kind, coexist=existing_function.coexist) |
| 2944 | |
| 2945 | self.function.parameters = existing_function.parameters.copy() |
| 2946 | |
| 2947 | self.block.signatures.append(self.function) |
| 2948 | (cls or module).functions.append(self.function) |
| 2949 | self.next(self.state_function_docstring) |
| 2950 | return |
| 2951 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2952 | line, _, returns = line.partition('->') |
| 2953 | |
| 2954 | full_name, _, c_basename = line.partition(' as ') |
| 2955 | full_name = full_name.strip() |
| 2956 | c_basename = c_basename.strip() or None |
| 2957 | |
Larry Hastings | dfcd467 | 2013-10-27 02:49:39 -0700 | [diff] [blame] | 2958 | if not is_legal_py_identifier(full_name): |
| 2959 | fail("Illegal function name: {}".format(full_name)) |
| 2960 | if c_basename and not is_legal_c_identifier(c_basename): |
| 2961 | fail("Illegal C basename: {}".format(c_basename)) |
| 2962 | |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 2963 | return_converter = None |
| 2964 | if returns: |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2965 | ast_input = "def x() -> {}: pass".format(returns) |
| 2966 | module = None |
| 2967 | try: |
| 2968 | module = ast.parse(ast_input) |
| 2969 | except SyntaxError: |
| 2970 | pass |
| 2971 | if not module: |
| 2972 | fail("Badly-formed annotation for " + full_name + ": " + returns) |
| 2973 | try: |
| 2974 | name, legacy, kwargs = self.parse_converter(module.body[0].returns) |
Antoine Pitrou | d7fb791 | 2014-01-14 21:02:43 +0100 | [diff] [blame] | 2975 | if legacy: |
| 2976 | fail("Legacy converter {!r} not allowed as a return converter" |
| 2977 | .format(name)) |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2978 | if name not in return_converters: |
Antoine Pitrou | d7fb791 | 2014-01-14 21:02:43 +0100 | [diff] [blame] | 2979 | fail("No available return converter called " + repr(name)) |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2980 | return_converter = return_converters[name](**kwargs) |
| 2981 | except ValueError: |
| 2982 | fail("Badly-formed annotation for " + full_name + ": " + returns) |
| 2983 | |
| 2984 | fields = [x.strip() for x in full_name.split('.')] |
| 2985 | function_name = fields.pop() |
| 2986 | module, cls = self.clinic._module_and_class(fields) |
| 2987 | |
Larry Hastings | 8666e65 | 2014-01-12 14:12:59 -0800 | [diff] [blame] | 2988 | fields = full_name.split('.') |
| 2989 | if fields[-1] == '__new__': |
| 2990 | if (self.kind != CLASS_METHOD) or (not cls): |
| 2991 | fail("__new__ must be a class method!") |
| 2992 | self.kind = METHOD_NEW |
| 2993 | elif fields[-1] == '__init__': |
| 2994 | if (self.kind != CALLABLE) or (not cls): |
| 2995 | fail("__init__ must be a normal method, not a class or static method!") |
| 2996 | self.kind = METHOD_INIT |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 2997 | if not return_converter: |
| 2998 | return_converter = init_return_converter() |
Larry Hastings | 8666e65 | 2014-01-12 14:12:59 -0800 | [diff] [blame] | 2999 | elif fields[-1] in unsupported_special_methods: |
| 3000 | fail(fields[-1] + " should not be converted to Argument Clinic! (Yet.)") |
| 3001 | |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 3002 | if not return_converter: |
| 3003 | return_converter = CReturnConverter() |
| 3004 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 3005 | if not module: |
| 3006 | fail("Undefined module used in declaration of " + repr(full_name.strip()) + ".") |
| 3007 | self.function = Function(name=function_name, full_name=full_name, module=module, cls=cls, c_basename=c_basename, |
| 3008 | return_converter=return_converter, kind=self.kind, coexist=self.coexist) |
| 3009 | self.block.signatures.append(self.function) |
Larry Hastings | 4a714d4 | 2014-01-14 22:22:41 -0800 | [diff] [blame] | 3010 | (cls or module).functions.append(self.function) |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 3011 | self.next(self.state_parameters_start) |
| 3012 | |
| 3013 | # Now entering the parameters section. The rules, formally stated: |
| 3014 | # |
| 3015 | # * All lines must be indented with spaces only. |
| 3016 | # * The first line must be a parameter declaration. |
| 3017 | # * The first line must be indented. |
| 3018 | # * This first line establishes the indent for parameters. |
| 3019 | # * We'll call this number of spaces P (for "parameter"). |
| 3020 | # * Thenceforth: |
| 3021 | # * Lines indented with P spaces specify a parameter. |
| 3022 | # * Lines indented with > P spaces are docstrings for the previous |
| 3023 | # parameter. |
| 3024 | # * We'll call this number of spaces D (for "docstring"). |
| 3025 | # * All subsequent lines indented with >= D spaces are stored as |
| 3026 | # part of the per-parameter docstring. |
| 3027 | # * All lines will have the first D spaces of the indent stripped |
| 3028 | # before they are stored. |
| 3029 | # * It's illegal to have a line starting with a number of spaces X |
| 3030 | # such that P < X < D. |
| 3031 | # * A line with < P spaces is the first line of the function |
| 3032 | # docstring, which ends processing for parameters and per-parameter |
| 3033 | # docstrings. |
| 3034 | # * The first line of the function docstring must be at the same |
| 3035 | # indent as the function declaration. |
| 3036 | # * It's illegal to have any line in the parameters section starting |
| 3037 | # with X spaces such that F < X < P. (As before, F is the indent |
| 3038 | # of the function declaration.) |
| 3039 | # |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 3040 | # Also, currently Argument Clinic places the following restrictions on groups: |
| 3041 | # * Each group must contain at least one parameter. |
| 3042 | # * Each group may contain at most one group, which must be the furthest |
| 3043 | # thing in the group from the required parameters. (The nested group |
| 3044 | # must be the first in the group when it's before the required |
| 3045 | # parameters, and the last thing in the group when after the required |
| 3046 | # parameters.) |
| 3047 | # * There may be at most one (top-level) group to the left or right of |
| 3048 | # the required parameters. |
| 3049 | # * You must specify a slash, and it must be after all parameters. |
| 3050 | # (In other words: either all parameters are positional-only, |
| 3051 | # or none are.) |
| 3052 | # |
| 3053 | # Said another way: |
| 3054 | # * Each group must contain at least one parameter. |
| 3055 | # * All left square brackets before the required parameters must be |
| 3056 | # consecutive. (You can't have a left square bracket followed |
| 3057 | # by a parameter, then another left square bracket. You can't |
| 3058 | # have a left square bracket, a parameter, a right square bracket, |
| 3059 | # and then a left square bracket.) |
| 3060 | # * All right square brackets after the required parameters must be |
| 3061 | # consecutive. |
| 3062 | # |
| 3063 | # These rules are enforced with a single state variable: |
| 3064 | # "parameter_state". (Previously the code was a miasma of ifs and |
| 3065 | # separate boolean state variables.) The states are: |
| 3066 | # |
| 3067 | # [ [ a, b, ] c, ] d, e, f, [ g, h, [ i ] ] / <- line |
| 3068 | # 01 2 3 4 5 6 <- state transitions |
| 3069 | # |
| 3070 | # 0: ps_start. before we've seen anything. legal transitions are to 1 or 3. |
| 3071 | # 1: ps_left_square_before. left square brackets before required parameters. |
| 3072 | # 2: ps_group_before. in a group, before required parameters. |
| 3073 | # 3: ps_required. required parameters. (renumber left groups!) |
| 3074 | # 4: ps_group_after. in a group, after required parameters. |
| 3075 | # 5: ps_right_square_after. right square brackets after required parameters. |
| 3076 | # 6: ps_seen_slash. seen slash. |
| 3077 | ps_start, ps_left_square_before, ps_group_before, ps_required, \ |
| 3078 | ps_group_after, ps_right_square_after, ps_seen_slash = range(7) |
| 3079 | |
| 3080 | def state_parameters_start(self, line): |
| 3081 | if self.ignore_line(line): |
| 3082 | return |
| 3083 | |
| 3084 | # if this line is not indented, we have no parameters |
| 3085 | if not self.indent.infer(line): |
| 3086 | return self.next(self.state_function_docstring, line) |
| 3087 | |
Larry Hastings | 2a72791 | 2014-01-16 11:32:01 -0800 | [diff] [blame] | 3088 | self.parameter_continuation = '' |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 3089 | return self.next(self.state_parameter, line) |
| 3090 | |
| 3091 | |
| 3092 | def to_required(self): |
| 3093 | """ |
| 3094 | Transition to the "required" parameter state. |
| 3095 | """ |
| 3096 | if self.parameter_state != self.ps_required: |
| 3097 | self.parameter_state = self.ps_required |
| 3098 | for p in self.function.parameters.values(): |
| 3099 | p.group = -p.group |
| 3100 | |
| 3101 | def state_parameter(self, line): |
Larry Hastings | 2a72791 | 2014-01-16 11:32:01 -0800 | [diff] [blame] | 3102 | if self.parameter_continuation: |
| 3103 | line = self.parameter_continuation + ' ' + line.lstrip() |
| 3104 | self.parameter_continuation = '' |
| 3105 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 3106 | if self.ignore_line(line): |
| 3107 | return |
| 3108 | |
| 3109 | assert self.indent.depth == 2 |
| 3110 | indent = self.indent.infer(line) |
| 3111 | if indent == -1: |
| 3112 | # we outdented, must be to definition column |
| 3113 | return self.next(self.state_function_docstring, line) |
| 3114 | |
| 3115 | if indent == 1: |
| 3116 | # we indented, must be to new parameter docstring column |
| 3117 | return self.next(self.state_parameter_docstring_start, line) |
| 3118 | |
Larry Hastings | 2a72791 | 2014-01-16 11:32:01 -0800 | [diff] [blame] | 3119 | line = line.rstrip() |
| 3120 | if line.endswith('\\'): |
| 3121 | self.parameter_continuation = line[:-1] |
| 3122 | return |
| 3123 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 3124 | line = line.lstrip() |
| 3125 | |
| 3126 | if line in ('*', '/', '[', ']'): |
| 3127 | self.parse_special_symbol(line) |
| 3128 | return |
| 3129 | |
| 3130 | if self.parameter_state in (self.ps_start, self.ps_required): |
| 3131 | self.to_required() |
| 3132 | elif self.parameter_state == self.ps_left_square_before: |
| 3133 | self.parameter_state = self.ps_group_before |
| 3134 | elif self.parameter_state == self.ps_group_before: |
| 3135 | if not self.group: |
| 3136 | self.to_required() |
| 3137 | elif self.parameter_state == self.ps_group_after: |
| 3138 | pass |
| 3139 | else: |
| 3140 | fail("Function " + self.function.name + " has an unsupported group configuration. (Unexpected state " + str(self.parameter_state) + ")") |
| 3141 | |
Larry Hastings | 2a72791 | 2014-01-16 11:32:01 -0800 | [diff] [blame] | 3142 | base, equals, default = line.rpartition('=') |
| 3143 | if not equals: |
| 3144 | base = default |
| 3145 | default = None |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 3146 | module = None |
| 3147 | try: |
Larry Hastings | 2a72791 | 2014-01-16 11:32:01 -0800 | [diff] [blame] | 3148 | ast_input = "def x({}): pass".format(base) |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 3149 | module = ast.parse(ast_input) |
| 3150 | except SyntaxError: |
Larry Hastings | 2a72791 | 2014-01-16 11:32:01 -0800 | [diff] [blame] | 3151 | try: |
| 3152 | default = None |
| 3153 | ast_input = "def x({}): pass".format(line) |
| 3154 | module = ast.parse(ast_input) |
| 3155 | except SyntaxError: |
| 3156 | pass |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 3157 | if not module: |
Larry Hastings | ef3b1fb | 2013-10-22 23:26:23 -0700 | [diff] [blame] | 3158 | fail("Function " + self.function.name + " has an invalid parameter declaration:\n\t" + line) |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 3159 | |
| 3160 | function_args = module.body[0].args |
| 3161 | parameter = function_args.args[0] |
| 3162 | |
Larry Hastings | 16c5191 | 2014-01-07 11:53:01 -0800 | [diff] [blame] | 3163 | parameter_name = parameter.arg |
| 3164 | name, legacy, kwargs = self.parse_converter(parameter.annotation) |
| 3165 | |
Larry Hastings | 2a72791 | 2014-01-16 11:32:01 -0800 | [diff] [blame] | 3166 | if not default: |
| 3167 | value = unspecified |
| 3168 | if 'py_default' in kwargs: |
| 3169 | fail("You can't specify py_default without specifying a default value!") |
| 3170 | else: |
| 3171 | default = default.strip() |
| 3172 | ast_input = "x = {}".format(default) |
| 3173 | try: |
| 3174 | module = ast.parse(ast_input) |
| 3175 | |
| 3176 | # blacklist of disallowed ast nodes |
| 3177 | class DetectBadNodes(ast.NodeVisitor): |
| 3178 | bad = False |
| 3179 | def bad_node(self, node): |
| 3180 | self.bad = True |
| 3181 | |
| 3182 | # inline function call |
| 3183 | visit_Call = bad_node |
| 3184 | # inline if statement ("x = 3 if y else z") |
| 3185 | visit_IfExp = bad_node |
| 3186 | |
| 3187 | # comprehensions and generator expressions |
| 3188 | visit_ListComp = visit_SetComp = bad_node |
| 3189 | visit_DictComp = visit_GeneratorExp = bad_node |
| 3190 | |
| 3191 | # literals for advanced types |
| 3192 | visit_Dict = visit_Set = bad_node |
| 3193 | visit_List = visit_Tuple = bad_node |
| 3194 | |
| 3195 | # "starred": "a = [1, 2, 3]; *a" |
| 3196 | visit_Starred = bad_node |
| 3197 | |
| 3198 | # allow ellipsis, for now |
| 3199 | # visit_Ellipsis = bad_node |
| 3200 | |
| 3201 | blacklist = DetectBadNodes() |
| 3202 | blacklist.visit(module) |
| 3203 | if blacklist.bad: |
| 3204 | fail("Unsupported expression as default value: " + repr(default)) |
| 3205 | |
| 3206 | expr = module.body[0].value |
| 3207 | # mild hack: explicitly support NULL as a default value |
| 3208 | if isinstance(expr, ast.Name) and expr.id == 'NULL': |
| 3209 | value = NULL |
| 3210 | py_default = 'None' |
| 3211 | c_default = "NULL" |
| 3212 | elif (isinstance(expr, ast.BinOp) or |
| 3213 | (isinstance(expr, ast.UnaryOp) and not isinstance(expr.operand, ast.Num))): |
| 3214 | c_default = kwargs.get("c_default") |
| 3215 | if not (isinstance(c_default, str) and c_default): |
| 3216 | fail("When you specify an expression (" + repr(default) + ") as your default value,\nyou MUST specify a valid c_default.") |
| 3217 | py_default = default |
| 3218 | value = unknown |
| 3219 | elif isinstance(expr, ast.Attribute): |
| 3220 | a = [] |
| 3221 | n = expr |
| 3222 | while isinstance(n, ast.Attribute): |
| 3223 | a.append(n.attr) |
| 3224 | n = n.value |
| 3225 | if not isinstance(n, ast.Name): |
| 3226 | fail("Unsupported default value " + repr(default) + " (looked like a Python constant)") |
| 3227 | a.append(n.id) |
| 3228 | py_default = ".".join(reversed(a)) |
| 3229 | |
| 3230 | c_default = kwargs.get("c_default") |
| 3231 | if not (isinstance(c_default, str) and c_default): |
| 3232 | fail("When you specify a named constant (" + repr(py_default) + ") as your default value,\nyou MUST specify a valid c_default.") |
| 3233 | |
| 3234 | try: |
| 3235 | value = eval(py_default) |
| 3236 | except NameError: |
| 3237 | value = unknown |
| 3238 | else: |
| 3239 | value = ast.literal_eval(expr) |
| 3240 | py_default = repr(value) |
| 3241 | if isinstance(value, (bool, None.__class__)): |
| 3242 | c_default = "Py_" + py_default |
| 3243 | elif isinstance(value, str): |
Larry Hastings | 4903e00 | 2014-01-18 00:26:16 -0800 | [diff] [blame] | 3244 | c_default = c_repr(value) |
Larry Hastings | 2a72791 | 2014-01-16 11:32:01 -0800 | [diff] [blame] | 3245 | else: |
| 3246 | c_default = py_default |
| 3247 | |
| 3248 | except SyntaxError as e: |
| 3249 | fail("Syntax error: " + repr(e.text)) |
| 3250 | except (ValueError, AttributeError): |
| 3251 | value = unknown |
Larry Hastings | 4a55fc5 | 2014-01-12 11:09:57 -0800 | [diff] [blame] | 3252 | c_default = kwargs.get("c_default") |
Larry Hastings | 2a72791 | 2014-01-16 11:32:01 -0800 | [diff] [blame] | 3253 | py_default = default |
Larry Hastings | 4a55fc5 | 2014-01-12 11:09:57 -0800 | [diff] [blame] | 3254 | if not (isinstance(c_default, str) and c_default): |
| 3255 | fail("When you specify a named constant (" + repr(py_default) + ") as your default value,\nyou MUST specify a valid c_default.") |
| 3256 | |
Larry Hastings | 2a72791 | 2014-01-16 11:32:01 -0800 | [diff] [blame] | 3257 | kwargs.setdefault('c_default', c_default) |
| 3258 | kwargs.setdefault('py_default', py_default) |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 3259 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 3260 | dict = legacy_converters if legacy else converters |
| 3261 | legacy_str = "legacy " if legacy else "" |
| 3262 | if name not in dict: |
| 3263 | fail('{} is not a valid {}converter'.format(name, legacy_str)) |
| 3264 | converter = dict[name](parameter_name, self.function, value, **kwargs) |
| 3265 | |
Larry Hastings | ebdcb50 | 2013-11-23 14:54:00 -0800 | [diff] [blame] | 3266 | # special case: if it's the self converter, |
| 3267 | # don't actually add it to the parameter list |
| 3268 | if isinstance(converter, self_converter): |
| 3269 | if self.function.parameters or (self.parameter_state != self.ps_required): |
| 3270 | fail("The 'self' parameter, if specified, must be the very first thing in the parameter block.") |
| 3271 | if self.function.self_converter: |
| 3272 | fail("You can't specify the 'self' parameter more than once.") |
| 3273 | self.function.self_converter = converter |
| 3274 | self.parameter_state = self.ps_start |
| 3275 | return |
| 3276 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 3277 | kind = inspect.Parameter.KEYWORD_ONLY if self.keyword_only else inspect.Parameter.POSITIONAL_OR_KEYWORD |
| 3278 | p = Parameter(parameter_name, kind, function=self.function, converter=converter, default=value, group=self.group) |
Larry Hastings | bebf735 | 2014-01-17 17:47:17 -0800 | [diff] [blame] | 3279 | |
| 3280 | if parameter_name in self.function.parameters: |
| 3281 | fail("You can't have two parameters named " + repr(parameter_name) + "!") |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 3282 | self.function.parameters[parameter_name] = p |
| 3283 | |
| 3284 | def parse_converter(self, annotation): |
| 3285 | if isinstance(annotation, ast.Str): |
| 3286 | return annotation.s, True, {} |
| 3287 | |
| 3288 | if isinstance(annotation, ast.Name): |
| 3289 | return annotation.id, False, {} |
| 3290 | |
Larry Hastings | 4a55fc5 | 2014-01-12 11:09:57 -0800 | [diff] [blame] | 3291 | if not isinstance(annotation, ast.Call): |
| 3292 | fail("Annotations must be either a name, a function call, or a string.") |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 3293 | |
| 3294 | name = annotation.func.id |
| 3295 | kwargs = {node.arg: ast.literal_eval(node.value) for node in annotation.keywords} |
| 3296 | return name, False, kwargs |
| 3297 | |
| 3298 | def parse_special_symbol(self, symbol): |
| 3299 | if self.parameter_state == self.ps_seen_slash: |
| 3300 | fail("Function " + self.function.name + " specifies " + symbol + " after /, which is unsupported.") |
| 3301 | |
| 3302 | if symbol == '*': |
| 3303 | if self.keyword_only: |
| 3304 | fail("Function " + self.function.name + " uses '*' more than once.") |
| 3305 | self.keyword_only = True |
| 3306 | elif symbol == '[': |
| 3307 | if self.parameter_state in (self.ps_start, self.ps_left_square_before): |
| 3308 | self.parameter_state = self.ps_left_square_before |
| 3309 | elif self.parameter_state in (self.ps_required, self.ps_group_after): |
| 3310 | self.parameter_state = self.ps_group_after |
| 3311 | else: |
| 3312 | fail("Function " + self.function.name + " has an unsupported group configuration. (Unexpected state " + str(self.parameter_state) + ")") |
| 3313 | self.group += 1 |
| 3314 | elif symbol == ']': |
| 3315 | if not self.group: |
| 3316 | fail("Function " + self.function.name + " has a ] without a matching [.") |
| 3317 | if not any(p.group == self.group for p in self.function.parameters.values()): |
| 3318 | fail("Function " + self.function.name + " has an empty group.\nAll groups must contain at least one parameter.") |
| 3319 | self.group -= 1 |
| 3320 | if self.parameter_state in (self.ps_left_square_before, self.ps_group_before): |
| 3321 | self.parameter_state = self.ps_group_before |
| 3322 | elif self.parameter_state in (self.ps_group_after, self.ps_right_square_after): |
| 3323 | self.parameter_state = self.ps_right_square_after |
| 3324 | else: |
| 3325 | fail("Function " + self.function.name + " has an unsupported group configuration. (Unexpected state " + str(self.parameter_state) + ")") |
| 3326 | elif symbol == '/': |
| 3327 | # ps_required is allowed here, that allows positional-only without option groups |
| 3328 | # to work (and have default values!) |
| 3329 | if (self.parameter_state not in (self.ps_required, self.ps_right_square_after, self.ps_group_before)) or self.group: |
| 3330 | fail("Function " + self.function.name + " has an unsupported group configuration. (Unexpected state " + str(self.parameter_state) + ")") |
| 3331 | if self.keyword_only: |
| 3332 | fail("Function " + self.function.name + " mixes keyword-only and positional-only parameters, which is unsupported.") |
| 3333 | self.parameter_state = self.ps_seen_slash |
| 3334 | # fixup preceeding parameters |
| 3335 | for p in self.function.parameters.values(): |
| 3336 | if p.kind != inspect.Parameter.POSITIONAL_OR_KEYWORD: |
| 3337 | fail("Function " + self.function.name + " mixes keyword-only and positional-only parameters, which is unsupported.") |
| 3338 | p.kind = inspect.Parameter.POSITIONAL_ONLY |
| 3339 | |
| 3340 | def state_parameter_docstring_start(self, line): |
| 3341 | self.parameter_docstring_indent = len(self.indent.margin) |
| 3342 | assert self.indent.depth == 3 |
| 3343 | return self.next(self.state_parameter_docstring, line) |
| 3344 | |
| 3345 | # every line of the docstring must start with at least F spaces, |
| 3346 | # where F > P. |
| 3347 | # these F spaces will be stripped. |
| 3348 | def state_parameter_docstring(self, line): |
| 3349 | stripped = line.strip() |
| 3350 | if stripped.startswith('#'): |
| 3351 | return |
| 3352 | |
| 3353 | indent = self.indent.measure(line) |
| 3354 | if indent < self.parameter_docstring_indent: |
| 3355 | self.indent.infer(line) |
| 3356 | assert self.indent.depth < 3 |
| 3357 | if self.indent.depth == 2: |
| 3358 | # back to a parameter |
| 3359 | return self.next(self.state_parameter, line) |
| 3360 | assert self.indent.depth == 1 |
| 3361 | return self.next(self.state_function_docstring, line) |
| 3362 | |
| 3363 | assert self.function.parameters |
| 3364 | last_parameter = next(reversed(list(self.function.parameters.values()))) |
| 3365 | |
| 3366 | new_docstring = last_parameter.docstring |
| 3367 | |
| 3368 | if new_docstring: |
| 3369 | new_docstring += '\n' |
| 3370 | if stripped: |
| 3371 | new_docstring += self.indent.dedent(line) |
| 3372 | |
| 3373 | last_parameter.docstring = new_docstring |
| 3374 | |
| 3375 | # the final stanza of the DSL is the docstring. |
| 3376 | def state_function_docstring(self, line): |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 3377 | if self.group: |
| 3378 | fail("Function " + self.function.name + " has a ] without a matching [.") |
| 3379 | |
| 3380 | stripped = line.strip() |
| 3381 | if stripped.startswith('#'): |
| 3382 | return |
| 3383 | |
| 3384 | new_docstring = self.function.docstring |
| 3385 | if new_docstring: |
| 3386 | new_docstring += "\n" |
| 3387 | if stripped: |
| 3388 | line = self.indent.dedent(line).rstrip() |
| 3389 | else: |
| 3390 | line = '' |
| 3391 | new_docstring += line |
| 3392 | self.function.docstring = new_docstring |
| 3393 | |
| 3394 | def format_docstring(self): |
| 3395 | f = self.function |
| 3396 | |
| 3397 | add, output = text_accumulator() |
| 3398 | parameters = list(f.parameters.values()) |
| 3399 | |
| 3400 | ## |
| 3401 | ## docstring first line |
| 3402 | ## |
| 3403 | |
Larry Hastings | 44e2eaa | 2013-11-23 15:37:55 -0800 | [diff] [blame] | 3404 | add(f.name) |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 3405 | add('(') |
| 3406 | |
| 3407 | # populate "right_bracket_count" field for every parameter |
| 3408 | if parameters: |
| 3409 | # for now, the only way Clinic supports positional-only parameters |
| 3410 | # is if all of them are positional-only. |
| 3411 | positional_only_parameters = [p.kind == inspect.Parameter.POSITIONAL_ONLY for p in parameters] |
| 3412 | if parameters[0].kind == inspect.Parameter.POSITIONAL_ONLY: |
| 3413 | assert all(positional_only_parameters) |
| 3414 | for p in parameters: |
| 3415 | p.right_bracket_count = abs(p.group) |
| 3416 | else: |
| 3417 | # don't put any right brackets around non-positional-only parameters, ever. |
| 3418 | for p in parameters: |
| 3419 | p.right_bracket_count = 0 |
| 3420 | |
| 3421 | right_bracket_count = 0 |
| 3422 | |
| 3423 | def fix_right_bracket_count(desired): |
| 3424 | nonlocal right_bracket_count |
| 3425 | s = '' |
| 3426 | while right_bracket_count < desired: |
| 3427 | s += '[' |
| 3428 | right_bracket_count += 1 |
| 3429 | while right_bracket_count > desired: |
| 3430 | s += ']' |
| 3431 | right_bracket_count -= 1 |
| 3432 | return s |
| 3433 | |
| 3434 | added_star = False |
| 3435 | add_comma = False |
| 3436 | |
| 3437 | for p in parameters: |
| 3438 | assert p.name |
| 3439 | |
| 3440 | if p.is_keyword_only() and not added_star: |
| 3441 | added_star = True |
| 3442 | if add_comma: |
| 3443 | add(', ') |
| 3444 | add('*') |
| 3445 | |
| 3446 | a = [p.name] |
| 3447 | if p.converter.is_optional(): |
| 3448 | a.append('=') |
Larry Hastings | c4fe092 | 2014-01-19 02:27:34 -0800 | [diff] [blame^] | 3449 | value = p.converter.py_default |
| 3450 | if not value: |
| 3451 | value = str(p.converter.default) |
| 3452 | a.append(value) |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 3453 | s = fix_right_bracket_count(p.right_bracket_count) |
| 3454 | s += "".join(a) |
| 3455 | if add_comma: |
| 3456 | add(', ') |
| 3457 | add(s) |
| 3458 | add_comma = True |
| 3459 | |
| 3460 | add(fix_right_bracket_count(0)) |
| 3461 | add(')') |
| 3462 | |
Larry Hastings | 2a72791 | 2014-01-16 11:32:01 -0800 | [diff] [blame] | 3463 | # PEP 8 says: |
| 3464 | # |
| 3465 | # The Python standard library will not use function annotations |
| 3466 | # as that would result in a premature commitment to a particular |
| 3467 | # annotation style. Instead, the annotations are left for users |
| 3468 | # to discover and experiment with useful annotation styles. |
| 3469 | # |
| 3470 | # therefore this is commented out: |
| 3471 | # |
| 3472 | # if f.return_converter.py_default: |
Larry Hastings | 44e2eaa | 2013-11-23 15:37:55 -0800 | [diff] [blame] | 3473 | # add(' -> ') |
Larry Hastings | 2a72791 | 2014-01-16 11:32:01 -0800 | [diff] [blame] | 3474 | # add(f.return_converter.py_default) |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 3475 | |
| 3476 | docstring_first_line = output() |
| 3477 | |
| 3478 | # now fix up the places where the brackets look wrong |
| 3479 | docstring_first_line = docstring_first_line.replace(', ]', ',] ') |
| 3480 | |
Larry Hastings | 44e2eaa | 2013-11-23 15:37:55 -0800 | [diff] [blame] | 3481 | # okay. now we're officially building the "parameters" section. |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 3482 | # create substitution text for {parameters} |
Larry Hastings | 44e2eaa | 2013-11-23 15:37:55 -0800 | [diff] [blame] | 3483 | spacer_line = False |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 3484 | for p in parameters: |
| 3485 | if not p.docstring.strip(): |
| 3486 | continue |
Larry Hastings | 44e2eaa | 2013-11-23 15:37:55 -0800 | [diff] [blame] | 3487 | if spacer_line: |
| 3488 | add('\n') |
| 3489 | else: |
| 3490 | spacer_line = True |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 3491 | add(" ") |
| 3492 | add(p.name) |
| 3493 | add('\n') |
| 3494 | add(textwrap.indent(rstrip_lines(p.docstring.rstrip()), " ")) |
Larry Hastings | 44e2eaa | 2013-11-23 15:37:55 -0800 | [diff] [blame] | 3495 | parameters = output() |
| 3496 | if parameters: |
| 3497 | parameters += '\n' |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 3498 | |
| 3499 | ## |
| 3500 | ## docstring body |
| 3501 | ## |
| 3502 | |
| 3503 | docstring = f.docstring.rstrip() |
| 3504 | lines = [line.rstrip() for line in docstring.split('\n')] |
| 3505 | |
| 3506 | # Enforce the summary line! |
| 3507 | # The first line of a docstring should be a summary of the function. |
| 3508 | # It should fit on one line (80 columns? 79 maybe?) and be a paragraph |
| 3509 | # by itself. |
| 3510 | # |
| 3511 | # Argument Clinic enforces the following rule: |
| 3512 | # * either the docstring is empty, |
| 3513 | # * or it must have a summary line. |
| 3514 | # |
| 3515 | # Guido said Clinic should enforce this: |
| 3516 | # http://mail.python.org/pipermail/python-dev/2013-June/127110.html |
| 3517 | |
| 3518 | if len(lines) >= 2: |
| 3519 | if lines[1]: |
| 3520 | fail("Docstring for " + f.full_name + " does not have a summary line!\n" + |
| 3521 | "Every non-blank function docstring must start with\n" + |
| 3522 | "a single line summary followed by an empty line.") |
| 3523 | elif len(lines) == 1: |
| 3524 | # the docstring is only one line right now--the summary line. |
| 3525 | # add an empty line after the summary line so we have space |
Larry Hastings | 44e2eaa | 2013-11-23 15:37:55 -0800 | [diff] [blame] | 3526 | # between it and the {parameters} we're about to add. |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 3527 | lines.append('') |
| 3528 | |
Larry Hastings | 44e2eaa | 2013-11-23 15:37:55 -0800 | [diff] [blame] | 3529 | parameters_marker_count = len(docstring.split('{parameters}')) - 1 |
| 3530 | if parameters_marker_count > 1: |
| 3531 | fail('You may not specify {parameters} more than once in a docstring!') |
| 3532 | |
| 3533 | if not parameters_marker_count: |
| 3534 | # insert after summary line |
| 3535 | lines.insert(2, '{parameters}') |
| 3536 | |
| 3537 | # insert at front of docstring |
| 3538 | lines.insert(0, docstring_first_line) |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 3539 | |
| 3540 | docstring = "\n".join(lines) |
| 3541 | |
| 3542 | add(docstring) |
| 3543 | docstring = output() |
| 3544 | |
Larry Hastings | 44e2eaa | 2013-11-23 15:37:55 -0800 | [diff] [blame] | 3545 | docstring = linear_format(docstring, parameters=parameters) |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 3546 | docstring = docstring.rstrip() |
| 3547 | |
| 3548 | return docstring |
| 3549 | |
| 3550 | def state_terminal(self, line): |
| 3551 | """ |
| 3552 | Called when processing the block is done. |
| 3553 | """ |
| 3554 | assert not line |
| 3555 | |
| 3556 | if not self.function: |
| 3557 | return |
| 3558 | |
Larry Hastings | 6d2ea21 | 2014-01-05 02:50:45 -0800 | [diff] [blame] | 3559 | if not self.function.self_converter: |
| 3560 | self.function.self_converter = self_converter("self", self.function) |
| 3561 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 3562 | if self.keyword_only: |
| 3563 | values = self.function.parameters.values() |
| 3564 | if not values: |
| 3565 | no_parameter_after_star = True |
| 3566 | else: |
| 3567 | last_parameter = next(reversed(list(values))) |
| 3568 | no_parameter_after_star = last_parameter.kind != inspect.Parameter.KEYWORD_ONLY |
| 3569 | if no_parameter_after_star: |
| 3570 | fail("Function " + self.function.name + " specifies '*' without any parameters afterwards.") |
| 3571 | |
| 3572 | # remove trailing whitespace from all parameter docstrings |
| 3573 | for name, value in self.function.parameters.items(): |
| 3574 | if not value: |
| 3575 | continue |
| 3576 | value.docstring = value.docstring.rstrip() |
| 3577 | |
| 3578 | self.function.docstring = self.format_docstring() |
| 3579 | |
| 3580 | |
| 3581 | # maps strings to callables. |
| 3582 | # the callable should return an object |
| 3583 | # that implements the clinic parser |
| 3584 | # interface (__init__ and parse). |
| 3585 | # |
| 3586 | # example parsers: |
| 3587 | # "clinic", handles the Clinic DSL |
| 3588 | # "python", handles running Python code |
| 3589 | # |
| 3590 | parsers = {'clinic' : DSLParser, 'python': PythonParser} |
| 3591 | |
| 3592 | |
| 3593 | clinic = None |
| 3594 | |
| 3595 | |
| 3596 | def main(argv): |
| 3597 | import sys |
| 3598 | |
| 3599 | if sys.version_info.major < 3 or sys.version_info.minor < 3: |
| 3600 | sys.exit("Error: clinic.py requires Python 3.3 or greater.") |
| 3601 | |
| 3602 | import argparse |
| 3603 | cmdline = argparse.ArgumentParser() |
| 3604 | cmdline.add_argument("-f", "--force", action='store_true') |
| 3605 | cmdline.add_argument("-o", "--output", type=str) |
| 3606 | cmdline.add_argument("--converters", action='store_true') |
Larry Hastings | dcd340e | 2013-11-23 14:58:45 -0800 | [diff] [blame] | 3607 | cmdline.add_argument("--make", action='store_true') |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 3608 | cmdline.add_argument("filename", type=str, nargs="*") |
| 3609 | ns = cmdline.parse_args(argv) |
| 3610 | |
| 3611 | if ns.converters: |
| 3612 | if ns.filename: |
| 3613 | print("Usage error: can't specify --converters and a filename at the same time.") |
| 3614 | print() |
| 3615 | cmdline.print_usage() |
| 3616 | sys.exit(-1) |
| 3617 | converters = [] |
| 3618 | return_converters = [] |
| 3619 | ignored = set(""" |
| 3620 | add_c_converter |
| 3621 | add_c_return_converter |
| 3622 | add_default_legacy_c_converter |
| 3623 | add_legacy_c_converter |
| 3624 | """.strip().split()) |
| 3625 | module = globals() |
| 3626 | for name in module: |
| 3627 | for suffix, ids in ( |
| 3628 | ("_return_converter", return_converters), |
| 3629 | ("_converter", converters), |
| 3630 | ): |
| 3631 | if name in ignored: |
| 3632 | continue |
| 3633 | if name.endswith(suffix): |
| 3634 | ids.append((name, name[:-len(suffix)])) |
| 3635 | break |
| 3636 | print() |
| 3637 | |
| 3638 | print("Legacy converters:") |
| 3639 | legacy = sorted(legacy_converters) |
| 3640 | print(' ' + ' '.join(c for c in legacy if c[0].isupper())) |
| 3641 | print(' ' + ' '.join(c for c in legacy if c[0].islower())) |
| 3642 | print() |
| 3643 | |
| 3644 | for title, attribute, ids in ( |
| 3645 | ("Converters", 'converter_init', converters), |
| 3646 | ("Return converters", 'return_converter_init', return_converters), |
| 3647 | ): |
| 3648 | print(title + ":") |
| 3649 | longest = -1 |
| 3650 | for name, short_name in ids: |
| 3651 | longest = max(longest, len(short_name)) |
| 3652 | for name, short_name in sorted(ids, key=lambda x: x[1].lower()): |
| 3653 | cls = module[name] |
| 3654 | callable = getattr(cls, attribute, None) |
| 3655 | if not callable: |
| 3656 | continue |
| 3657 | signature = inspect.signature(callable) |
| 3658 | parameters = [] |
| 3659 | for parameter_name, parameter in signature.parameters.items(): |
| 3660 | if parameter.kind == inspect.Parameter.KEYWORD_ONLY: |
| 3661 | if parameter.default != inspect.Parameter.empty: |
| 3662 | s = '{}={!r}'.format(parameter_name, parameter.default) |
| 3663 | else: |
| 3664 | s = parameter_name |
| 3665 | parameters.append(s) |
| 3666 | print(' {}({})'.format(short_name, ', '.join(parameters))) |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 3667 | print() |
Larry Hastings | 2a72791 | 2014-01-16 11:32:01 -0800 | [diff] [blame] | 3668 | print("All converters also accept (c_default=None, py_default=None, annotation=None).") |
| 3669 | print("All return converters also accept (py_default=None).") |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 3670 | sys.exit(0) |
| 3671 | |
Larry Hastings | dcd340e | 2013-11-23 14:58:45 -0800 | [diff] [blame] | 3672 | if ns.make: |
| 3673 | if ns.output or ns.filename: |
| 3674 | print("Usage error: can't use -o or filenames with --make.") |
| 3675 | print() |
| 3676 | cmdline.print_usage() |
| 3677 | sys.exit(-1) |
| 3678 | for root, dirs, files in os.walk('.'): |
| 3679 | for rcs_dir in ('.svn', '.git', '.hg'): |
| 3680 | if rcs_dir in dirs: |
| 3681 | dirs.remove(rcs_dir) |
| 3682 | for filename in files: |
| 3683 | if not filename.endswith('.c'): |
| 3684 | continue |
| 3685 | path = os.path.join(root, filename) |
| 3686 | parse_file(path, verify=not ns.force) |
| 3687 | return |
| 3688 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 3689 | if not ns.filename: |
| 3690 | cmdline.print_usage() |
| 3691 | sys.exit(-1) |
| 3692 | |
| 3693 | if ns.output and len(ns.filename) > 1: |
| 3694 | print("Usage error: can't use -o with multiple filenames.") |
| 3695 | print() |
| 3696 | cmdline.print_usage() |
| 3697 | sys.exit(-1) |
| 3698 | |
| 3699 | for filename in ns.filename: |
| 3700 | parse_file(filename, output=ns.output, verify=not ns.force) |
| 3701 | |
| 3702 | |
| 3703 | if __name__ == "__main__": |
| 3704 | sys.exit(main(sys.argv[1:])) |