Skip to content

Move code from ufunc.c.templ to erfa_generator.py#279

Merged
avalentino merged 1 commit into
liberfa:mainfrom
eerovaher:more-code-generation-in-python
Jun 25, 2026
Merged

Move code from ufunc.c.templ to erfa_generator.py#279
avalentino merged 1 commit into
liberfa:mainfrom
eerovaher:more-code-generation-in-python

Conversation

@eerovaher

Copy link
Copy Markdown
Contributor

The new Python code helps avoid code repetition in the erfa/ufunc.c template. Furthermore, Python code can be checked with tools such as Ruff or Mypy and there is better IDE support. If we keep moving code from the templates to erfa_generator.py then it might eventually become possible to drop jinja2 as a build requirement.

The new Python code helps avoid code repetition in the `erfa/ufunc.c`
template. Furthermore, Python code can be checked with tools such as
Ruff or Mypy and there is better IDE support.
Comment thread erfa/ufunc.c.templ
Comment on lines -236 to +233
if (copy_{{ arg.name }}) {
{{ arg.copy_elements("to") }}
}
{{ arg.cast_pointer_if_needed | indent(8) }}
else {
{{ arg.cast_pointer }}
{{ arg.copy_elements("to") }}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've changed the order of the branches here, which does introduce differences in the generated C code, but there should be no changes in what the code does.

Comment thread erfa_generator.py
Comment on lines +205 to +213
@functools.cached_property
def cast_pointer_if_needed(self) -> str:
return "\n".join(
[
f"if (!copy_{self.name}) {{",
f" {self.cast_pointer}",
"}",
]
)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've been formatting the code I touch (including this function) using the Ruff formatter with its default style. With the preview style this function would look better:

     @functools.cached_property
     def cast_pointer_if_needed(self) -> str:
         return "\n".join([
             f"if (!copy_{self.name}) {{",
             f"    {self.cast_pointer}",
             "}",
         ])

If we can agree that we like the preview style more then I could turn that on.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK for me

@avalentino avalentino merged commit c90fd78 into liberfa:main Jun 25, 2026
27 checks passed
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.

2 participants