File: test_cpylint.py

package info (click to toggle)
python-msgspec 0.19.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 6,356 kB
  • sloc: javascript: 23,944; ansic: 20,540; python: 20,465; makefile: 29; sh: 19
file content (53 lines) | stat: -rw-r--r-- 1,679 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
"""This file contains some simple linters for catching some common but easy to
catch cpython capi bugs. These are naive string-munging checks, if you write
some code that _is_ correct but is failing, add `/* cpylint-ignore */` on the
failing source line and it will be ignored."""

import os

import pytest

MSGSPEC_CORE_PATH = os.path.join(
    os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "msgspec", "_core.c"
)


@pytest.fixture
def source():
    with open(MSGSPEC_CORE_PATH, "r") as f:
        return f.read().splitlines()


def test_recursive_call_blocks(source):
    """Ensure all code that calls `Py_EnterRecursiveCall` doesn't return
    without calling `Py_LeaveRecursiveCall`"""

    in_block = False
    for lineno, line in enumerate(source, 1):
        if "cpylint-ignore" in line:
            continue

        if "Py_EnterRecursiveCall" in line:
            in_block = True
        elif "return " in line and in_block:
            raise ValueError(
                f"return without calling Py_LeaveRecursiveCall on line {lineno}"
            )
        elif "Py_LeaveRecursiveCall" in line:
            in_block = False


def test_recursive_repr_blocks(source):
    """Ensure all code that calls `Py_ReprEnter` doesn't return without
    calling `Py_ReprLeave`"""
    in_block = False
    for lineno, line in enumerate(source, 1):
        if "cpylint-ignore" in line:
            continue

        if "Py_ReprEnter" in line:
            in_block = True
        elif "return " in line and in_block:
            raise ValueError(f"return without calling Py_ReprLeave on line {lineno}")
        elif "Py_ReprLeave" in line:
            in_block = False