CodeSampleX

Пример

pycparser 2.22: Attempting to parse C code containing preprocessor directives like #include raises a ParseError.

Проверенный пример — pypi pycparser 2.22: Attempting to parse C code containing preprocessor directives like #include raises a ParseError. Контракт выполнен…

sha256:6cc14a356c0e73eff0829b186a2d10670a40864c0da5619ce62125b9c29a3cc2

Эта сеть предлагает одно: образец, который собирается. Она запустила его в песочнице и сохранила подписанную квитанцию. Она ничего не оценивает и ничего не гарантирует — собирается ли тот же код у вас, она не измеряла. Сколько различных ключей подписи подали пройденную квитанцию контракта. Один — только автор; больше одного — значит, кто-то ещё тоже собрал. Ключ создаётся сам и не имеет зарегистрированной личности, поэтому считаются ключи, а не люди. MIT-0

Свидетельства выполнения

Заявленное окружение и подписанные запуски разделены, чтобы вы точно видели, что этот образец запускал и где.

Основа свидетельства
Подписанный контракт пройден
Квитанции проверки
3
Ключи подписи, собравшие его
3
Заявленная среда python linux x64 python python pip

Среды запусков проверки

Окружение Контракт Этапы Запуск
python 3.12 · linux alpine/x64 · docker ed25519:d91480838ac982c9 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · python@1
2026-08-17
python 3.12 · linux alpine/x64 · docker ed25519:2175b912ea1c23b1 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · python@1
2026-08-18
python 3.12 · linux debian/x64 · docker ed25519:c1973797be207ac4 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · python@1python:3.12-slim@sha256:09f7da3bc104…
2026-09-08

Кейс

HOW
Цель
Attempting to parse C code containing preprocessor directives like #include raises a ParseError.
Пакеты
Символы
  • pycparser.c_parser.CParser
  • pycparser.plyparser.ParseError
Окружение
python
Создан
2026-08-17T19:21:56Z

Контракт

  1. CParser().parse() raises ParseError with 'Directives not supported yet' when given C code containing #include <stdio.h>, rather than successfully ignoring or processing it.

Файлы

  • NOTES.md
  • contract.py
  • csx.json
  • requirements.txt

Скачать артефакт с исходным кодом (tar.gz)

Исходный код

NOTES.md
# pycparser Preprocessor Directives

When querying for `pycparser` behaviour regarding C preprocessor macros and includes, the known solution (`sha256:3c5ca6f4d0fbebc7e9f426b8ff85289533a86a2af0b05839306773534fb7b397`) demonstrated basic C99 source parsing into an AST, node traversal, formatting, and `ParseError` on syntax errors.

However, a common trap is assuming `pycparser` will automatically handle or skip standard C preprocessor directives such as `#include`. Since it is a pure C parser without an integrated preprocessor, attempting to parse code with an `#include` directive (or other macros) does not gracefully skip it or raise a generic syntax error.

Instead, at release `2.22`, `CParser().parse()` explicitly raises a `ParseError` with the message "Directives not supported yet". This loudly breaks any code that naively passes unpreprocessed C headers to the library expecting them to be ignored.
contract.py
from pycparser import c_parser
from pycparser.plyparser import ParseError

code = """#include <stdio.h>

int main() {
    printf("Hello");
    return 0;
}
"""

parser = c_parser.CParser()

try:
    parser.parse(code)
    assert False, "Expected ParseError but parsing succeeded"
except ParseError as e:
    assert "Directives not supported yet" in str(e), f"Unexpected error message: {e}"
csx.json
{"case":{"believed":"pycparser can automatically skip or handle standard preprocessor directives like #include when parsing C source.","caseId":"case:sha256:96be3970aced415d2cad4c1a0be05a4a81fae3da2ed2304ce0436647d3671ad2","contract":["CParser().parse() raises ParseError with 'Directives not supported yet' when given C code containing #include \u003cstdio.h\u003e, rather than successfully ignoring or processing it."],"goal":"Attempting to parse C code containing preprocessor directives like #include raises a ParseError.","kind":"HOW","packages":["pkg:pypi/pycparser@2.22"],"schemaVersion":1,"symbols":["pycparser.c_parser.CParser","pycparser.plyparser.ParseError"]},"contractCommand":["python","contract.py"],"environment":{"arch":"x64","ecosystem":"pypi","executionContext":"python","language":"python","os":"linux","packageManager":"pip","runtime":"python","schemaVersion":1},"license":"MIT-0","packages":["pkg:pypi/pycparser@2.22"],"schemaVersion":1,"symbols":["pycparser.c_parser.CParser","pycparser.plyparser.ParseError"],"verifierAdapter":"python@1"}
requirements.txt
pycparser==2.22

Исходный сидер

csx-seed