Skip to content

Conversation

@greenc-FNAL
Copy link
Contributor

@greenc-FNAL greenc-FNAL commented Dec 19, 2025

Non-test changes:

1. Python/C++ Interoperability Enhancements

Files: modulewrap.cpp, lifelinewrap.cpp

  • Feature: RAII for Python Objects

    • Change: Introduced PyObjectPtr (a std::shared_ptr alias with a custom PyObjectDeleter) to manage Python object reference counts.
    • Rationalization: Manual reference counting (Py_INCREF/Py_DECREF) is error-prone, especially in the presence of C++ exceptions. If an exception is thrown, manual decrements might be skipped, leading to memory leaks.
    • Resolution: PyObjectPtr ensures that Py_DECREF is called automatically when the pointer goes out of scope, even during stack unwinding.
  • Fix: Robust Annotation Parsing

    • Change: Rewrote the argument parsing logic in parse_args to iterate over the __annotations__ dictionary using PyDict_Next and explicitly skip the "return" key.
    • Root Cause: The previous implementation relied on PyDict_Values, which returns all values including the return type annotation. Depending on dictionary iteration order (which can vary or be insertion-ordered), the return type could be mistakenly interpreted as an input argument type.
    • Diagnosis: Likely diagnosed by observing type mismatch errors when Python functions had return type annotations.
  • Fix: Flexible Input Conversion (List vs. NumPy)

    • Change: Replaced rigid macro-based vector converters with explicit implementations (py_to_vint, py_to_vuint, etc.) that accept both Python list and NumPy ndarray objects.
    • Root Cause: The previous converters strictly expected NumPy arrays. Users passing standard Python lists would cause runtime errors or type mismatches.
    • Resolution: The new converters check the input type (PyList_Check vs PyArray_Check) and handle data extraction accordingly.
  • Fix: Memory Safety in Cyclic GC

    • Change: Added PyObject_GC_UnTrack(pyobj) in ll_dealloc (lifelinewrap.cpp).
    • Root Cause: Python objects that support cyclic garbage collection must be untracked before deallocation to prevent the GC from visiting invalid memory. Missing this can lead to segfaults during interpreter shutdown or garbage collection cycles.
  • Fix: Type String Matching

    • Change: Replaced brittle fixed-offset string comparisons (e.g., inp_type.compare(pos, ...)) with robust substring searching (suffix.find(...)). Corrected a typo where double64]] was checked instead of float64]].
    • Root Cause: The fixed-offset logic assumed a specific string format for type signatures, which could break if the format changed slightly. The typo prevented float64 arrays from being correctly identified.

2. Build System & CI Improvements

Files: CMakeLists.txt, CMakeLists.txt

  • Enhancement: Reduced Build Dependencies

    • Change: Removed the dependency on the external packaging Python module in CMakeLists.txt.
    • Rationalization: The build system previously used packaging.version to check module versions. This required the packaging library to be installed in the build environment.
    • Resolution: Implemented a lightweight, inline version parser (splitting strings by .) to perform the check using only the standard library.
  • Fix: GCC 14+ Warning Suppression

    • Change: Added -Wno-maybe-uninitialized to compile options for GCC 14.1+.
    • Root Cause: Newer GCC versions have more aggressive static analysis that produces false positives for uninitialized variables in complex C++ templates used by the project.

3. Documentation

Files: copilot-instructions.md

  • New Feature: Added a comprehensive instructions file for GitHub Copilot.
  • Rationalization: To standardize the behavior of AI assistants working in the repository, ensuring they follow project-specific coding standards (formatting, error handling) and workflow guidelines.

Test code changes and additions:

1. New Tests: py:vectypes and py:types

Files: vectypes.py, test_types.py, pyvectypes.jsonnet, pytypes.jsonnet, verify_extended.py

  • Rationale:

    • The existing tests primarily covered basic integer and string types.
    • There was a gap in coverage for:
      • Floating point types (float, double).
      • Unsigned integers (unsigned int, unsigned long).
      • 64-bit integers (long, int64_t).
      • NumPy array interoperability (passing vectors from C++ to Python as NumPy arrays).
    • These tests were added to verify the robustness of the new modulewrap.cpp converters.
  • Coverage Improvements:

    • py:types: Validates scalar type conversion between C++ and Python for float, double, and unsigned int.
    • py:vectypes: Validates vector/array conversion. It tests:
      • Creation of NumPy arrays from scalar inputs (collectify_*).
      • Summation of NumPy arrays back to scalars (sum_array_*).
      • Handling of all major numeric types: int32, uint32, int64, uint64, float32, float64.
    • verify_extended.py: Introduces specialized verifiers (VerifierFloat, VerifierUInt, etc.) that handle type-specific assertions (e.g., epsilon comparison for floats).
  • Problems Exposed:

    • Integer Overflow/Underflow: The py:vectypes test exposed a logic error in source.cpp where large 64-bit hashes were being used in arithmetic (100 - id), causing underflow for unsigned types and wrapping for signed types. This was fixed by introducing modulo arithmetic to keep values small and predictable.
    • Type Mismatches: The strict type checking in the new tests likely exposed the need for the robust annotation parsing and explicit type converters implemented in modulewrap.cpp.
  • Regression Detection:

    • Type Conversion Breakages: These tests will fail if future changes to modulewrap.cpp break the mapping between C++ types (like std::vector<int>) and Python types (like numpy.ndarray or list).
    • Precision Loss: The float/double tests will catch regressions where 64-bit precision is accidentally truncated to 32-bit.
    • Sign Errors: The unsigned integer tests will detect if unsigned values are incorrectly cast to signed values (e.g., treating UINT_MAX as -1).

2. Test Infrastructure Enhancements

Files: CMakeLists.txt, source.cpp

  • Rationale:

    • To support the new tests and ensure the test environment is consistent with real-world usage.
    • To fix flaky or incorrect test data generation.
  • Changes:

    • CMakeLists.txt:
      • Added py:vectypes and py:types to the test suite.
      • Enhanced PYTHONPATH setup to explicitly include Python_SITELIB and Python_SITEARCH. This ensures tests running in embedded environments (like Spack) can find installed packages.
      • Replaced the external packaging dependency with a simple inline version parser for the module check.
    • source.cpp:
      • Expanded the C++ data provider to generate all required types (float, double, uint, int64, uint64).
      • Fix: Changed data generation logic from id.number() to id.number() % N to prevent integer overflow and ensure deterministic summation results.

3. Code Quality & Modernization

Files: adder.py, all_config.py, reducer.py, sumit.py, verify.py

  • Rationale:

    • To comply with the project's stricter linting rules (ruff, mypy) introduced in this commit.
  • Changes:

    • Formatting: Applied standard Python formatting (whitespace, indentation).
    • Linting: Fixed issues like:
      • Comparison to False (changed == False to is False or kept as is with # noqa if intentional for testing).
      • Missing docstrings or blank lines.
      • Unused imports.
    • Type Hinting: Added or corrected type hints to satisfy mypy.
  • Regression Detection:

    • Static Analysis: By enforcing these standards, the CI pipeline can now detect syntax errors, undefined variables, and type inconsistencies before tests are even run.

@greenc-FNAL
Copy link
Contributor Author

@phlexbot format

@github-actions
Copy link
Contributor

Automatic cmake-format fixes pushed (commit e60d811).
⚠️ Note: Some issues may require manual review and fixing.

@greenc-FNAL
Copy link
Contributor Author

@phlexbot format

@github-actions
Copy link
Contributor

No automatic cmake-format fixes were necessary.

@github-actions
Copy link
Contributor

Automatic clang-format fixes pushed (commit 2aa7957).
⚠️ Note: Some issues may require manual review and fixing.

@greenc-FNAL
Copy link
Contributor Author

@phlexbot python-fix

@github-actions
Copy link
Contributor

Automatic Python linting fixes pushed (commit 092dec4).
⚠️ Note: Some issues may require manual review and fixing.

@codecov
Copy link

codecov bot commented Dec 19, 2025

Codecov Report

❌ Patch coverage is 65.87537% with 115 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
plugins/python/src/modulewrap.cpp 65.77% 73 Missing and 42 partials ⚠️

❌ Your patch check has failed because the patch coverage (65.87%) is below the target coverage (80.00%). You can increase the patch coverage or adjust the target coverage.

@@            Coverage Diff             @@
##             main     #213      +/-   ##
==========================================
+ Coverage   75.87%   78.70%   +2.82%     
==========================================
  Files         124      124              
  Lines        2898     3156     +258     
  Branches      506      576      +70     
==========================================
+ Hits         2199     2484     +285     
+ Misses        483      429      -54     
- Partials      216      243      +27     
Flag Coverage Δ
unittests 78.70% <65.87%> (+2.82%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
plugins/python/src/lifelinewrap.cpp 53.33% <100.00%> (+3.33%) ⬆️
plugins/python/src/modulewrap.cpp 68.91% <65.77%> (+22.88%) ⬆️

... and 1 file with indirect coverage changes


Continue to review full report in Codecov by Sentry.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 3e74780...90d2593. Read the comment docs.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@greenc-FNAL greenc-FNAL force-pushed the maintenance/improve-test-coverage branch from 38fc738 to 47661fa Compare December 19, 2025 17:19
@greenc-FNAL
Copy link
Contributor Author

@phlexbot format

@github-actions
Copy link
Contributor

No automatic clang-format fixes were necessary.

@github-actions
Copy link
Contributor

Automatic cmake-format fixes pushed (commit bb954c8).
⚠️ Note: Some issues may require manual review and fixing.

@greenc-FNAL
Copy link
Contributor Author

@phlexbot python-fix

@github-actions
Copy link
Contributor

Automatic Python linting fixes pushed (commit cfdb09a).
⚠️ Note: Some issues may require manual review and fixing.

@greenc-FNAL greenc-FNAL force-pushed the maintenance/improve-test-coverage branch 3 times, most recently from c9ccf06 to 1159796 Compare December 21, 2025 17:30
@greenc-FNAL
Copy link
Contributor Author

@phlexbot format

@github-actions
Copy link
Contributor

Automatic clang-format fixes pushed (commit b145a22).
⚠️ Note: Some issues may require manual review and fixing.

@greenc-FNAL
Copy link
Contributor Author

@phlexbot format

@github-actions
Copy link
Contributor

Automatic cmake-format fixes pushed (commit cc928e7).
⚠️ Note: Some issues may require manual review and fixing.

@github-actions
Copy link
Contributor

No automatic clang-format fixes were necessary.

@github-actions
Copy link
Contributor

Automatic cmake-format fixes pushed (commit 38a38fe).
⚠️ Note: Some issues may require manual review and fixing.

@github-actions
Copy link
Contributor

No automatic clang-format fixes were necessary.

@greenc-FNAL
Copy link
Contributor Author

@phlexbot python fix

@greenc-FNAL
Copy link
Contributor Author

@phlexbot format

@github-actions
Copy link
Contributor

No automatic clang-format fixes were necessary.

@github-actions
Copy link
Contributor

Automatic cmake-format fixes pushed (commit 1354192).
⚠️ Note: Some issues may require manual review and fixing.

@greenc-FNAL
Copy link
Contributor Author

@phlexbot python-fix

@github-actions
Copy link
Contributor

Automatic Python linting fixes pushed (commit 6b1f634).
⚠️ Note: Some issues may require manual review and fixing.

@greenc-FNAL greenc-FNAL force-pushed the maintenance/improve-test-coverage branch from d48f0ff to b0b30b1 Compare January 14, 2026 20:32
@greenc-FNAL
Copy link
Contributor Author

@phlexbot python-fix

@greenc-FNAL
Copy link
Contributor Author

@phlexbot format

@github-actions
Copy link
Contributor

Automatic Python linting fixes pushed (commit b1c55bf).
⚠️ Note: Some issues may require manual review and fixing.

@github-actions
Copy link
Contributor

No automatic clang-format fixes were necessary.

@greenc-FNAL
Copy link
Contributor Author

@phlexbot format

@github-actions
Copy link
Contributor

No automatic clang-format fixes were necessary.

@github-actions
Copy link
Contributor

Automatic cmake-format fixes pushed (commit f8a3c8d).
⚠️ Note: Some issues may require manual review and fixing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants