API Reference¶
autopxd2's own Python API is small: a translate() function and a Click command.
Everything else — the intermediate representation, the parser backends, and the
Cython writer — lives in headerkit, which
autopxd2 builds on. The pages below document the headerkit APIs that autopxd2 uses,
so that callers of translate() can work with the objects it produces.
autopxd¶
autopxd
¶
translate(code, hdrname, backend='auto', extra_args=None, allowlist=None, debug=False, use_default_includes=True, project_prefixes=None, recursive_includes=True, max_depth=None, denylist=None, whitelist=None)
¶
Generate Cython .pxd from C/C++ header code.
Args:
code: C/C++ header source code.
hdrname: Header filename (used in cdef extern from).
backend: Backend name ("auto", "libclang").
extra_args: Extra arguments passed to backend (e.g., ["-I/usr/include"]).
allowlist: Only include declarations from files matching these patterns.
Supports fnmatch patterns like ".h", "include/.h". With recursion
on (the default) and no allowlist, declarations from every
non-system included header are emitted; an allowlist narrows that,
in both recursion modes.
debug: Print debug info to stderr.
use_default_includes: If True (default), automatically detect and add
system include directories when using the libclang backend.
project_prefixes: [libclang] Tuple of path prefixes to treat as project
headers (not system). Use this for umbrella headers of libraries
installed in system locations (e.g., ("/opt/homebrew/include/sodium",)).
recursive_includes: [libclang] If True (default), detect umbrella headers
and recursively parse included project headers.
max_depth: [libclang] Recursion depth limit for umbrella header
processing. None (default) means unspecified and resolves to
10. 0 disables umbrella expansion. Passing a value to a
backend that does not follow includes raises :class:UserWarning,
because such a caller expects declarations that backend will
never produce.
denylist: Exclude declarations from files matching these patterns.
Same pattern syntax as allowlist. A file matching both lists is
excluded: deny wins over allow.
whitelist: Deprecated alias for allowlist. Emits
:class:DeprecationWarning. Passing both raises :class:ValueError.
Returns: Cython .pxd file contents.
Source code in autopxd/__init__.py
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 | |
cli(version, infile, outfile, include_dir, defines, defines_deprecated, debug, list_backends, json_output, backend, quiet, cpp, std, clang_arg, allowlist, denylist, whitelist, no_default_includes, project_prefixes, no_recursive, max_depth)
¶
Source code in autopxd/__init__.py
408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 | |
headerkit Modules¶
IR Module¶
The Intermediate Representation (IR) module defines data structures for representing C/C++ constructs:
Header- Top-level container for parsed declarationsStruct,Enum,Function,Typedef,Variable- Declaration typesCType,Pointer,Array,FunctionPointer- Type representations
Backends¶
Parser backend implementations:
LibclangBackend- LLVM clang-based parser with full C/C++ support- The
tree-sitterbackend, registered byheaderkit[treesitter], is selected by name throughget_backend("tree-sitter")
Quick Example¶
from headerkit.backends import get_backend
from headerkit.writers.cython import write_pxd
# Parse a header
backend = get_backend() # Uses default (libclang)
with open("myheader.h") as f:
code = f.read()
header = backend.parse(code, "myheader.h")
# Generate pxd output
pxd = write_pxd(header)
print(pxd)
Or go straight from source text to .pxd text:
Common Patterns¶
Inspecting Parsed Declarations¶
from headerkit.ir import Struct, Function
header = backend.parse(code, "header.h")
for decl in header.declarations:
if isinstance(decl, Struct):
print(f"Struct: {decl.name}")
for field in decl.fields:
print(f" {field.name}: {field.type}")
elif isinstance(decl, Function):
print(f"Function: {decl.name}")
print(f" Returns: {decl.return_type}")