Skip to content

Commit 3c0d40c

Browse files
authored
Implement relative imports, clean up Manifest.in. (#75)
* Fix template lookup logic. This corrects bugs where Mako namespaces would fail to be found. * Implement relative imports, clean up Manifest.in. * Modify actions workflow to run only once per push to an open PR. Rename main branch to main. Skip pypa test publish.
1 parent c3b452c commit 3c0d40c

File tree

11 files changed

+62
-19
lines changed

11 files changed

+62
-19
lines changed

.github/workflows/test.yml

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
name: Test
22

3-
on: [push, pull_request]
4-
3+
on:
4+
pull_request:
5+
types:
6+
- opened
7+
- synchronize
8+
push:
9+
branches:
10+
- main
511
jobs:
612
test:
713
strategy:
@@ -69,13 +75,6 @@ jobs:
6975
- name: Build sdist
7076
run: >-
7177
python setup.py sdist
72-
- name: Publish distribution 📦 to Test PyPI
73-
uses: pypa/gh-action-pypi-publish@master
74-
with:
75-
password: ${{ secrets.TEST_PYPI_API_TOKEN }}
76-
repository_url: https://test.pypi.org/legacy/
77-
verbose: true
78-
skip_existing: true
7978
- name: Publish distribution 📦 to PyPI
8079
if: startsWith(github.ref, 'refs/tags')
8180
uses: pypa/gh-action-pypi-publish@master

MANIFEST.in

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
include README.md
22
include VERSION
3-
recursive-include tests *
3+
recursive-include tests *.py
4+
recursive-include tests *.h
5+
recursive-include tests *.cpp
6+
recursive-include tests *.c

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<a target="_blank" href="https://pypi.org/project/cppimport/" title="PyPI"><img src="https://img.shields.io/pypi/dm/cppimport"></a>
77
<a target="_blank" href="LICENSE" title="License: MIT"><img src="https://img.shields.io/badge/License-MIT-blue.svg"></a>
88
<a target="_blank" href="https://github.com/tbenthompson/cppimport/actions" title="Test Status"><img src="https://github.com/tbenthompson/cppimport/actions/workflows/test.yml/badge.svg"></a>
9-
<a target="_blank" href="https://codecov.io/gh/tbenthompson/cppimport" title="Code coverage"><img src="https://codecov.io/gh/tbenthompson/cppimport/branch/stable/graph/badge.svg?token=GWpX62xMt5"/></a>
9+
<a target="_blank" href="https://codecov.io/gh/tbenthompson/cppimport" title="Code coverage"><img src="https://codecov.io/gh/tbenthompson/cppimport/branch/main/graph/badge.svg?token=GWpX62xMt5"/></a>
1010
</a>
1111

1212
</p>

cppimport/find.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1+
import logging
12
import os
23
import sys
34

45
import cppimport
56
from cppimport.filepaths import make_absolute
67

8+
logger = logging.getLogger(__name__)
9+
710

811
def find_module_cpppath(modulename, opt_in=False):
912
filepath = _find_module_cpppath(modulename, opt_in)
@@ -67,6 +70,10 @@ def _find_file_in_folders(filename, paths, opt_in):
6770
continue
6871
filepath = os.path.join(d, f)
6972
if opt_in and not _check_first_line_contains_cppimport(filepath):
73+
logger.debug(
74+
"Found file but the first line doesn't "
75+
"contain cppimport so it will be skipped: " + filepath
76+
)
7077
continue
7178
return filepath
7279
return None

cppimport/import_hook.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ class Hook(object):
1111
def __init__(self):
1212
self._running = False
1313

14-
def find_module(self, fullname, path=None):
14+
def find_spec(self, fullname, path, target=None):
15+
print("find_spec", fullname, path, target)
1516
# Prevent re-entry by the underlying importer
1617
if self._running:
1718
return

cppimport/templating.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,14 @@ def run_templating(module_data):
2626
ctx = mako.runtime.Context(buf, **module_data)
2727

2828
filepath = module_data["filepath"]
29-
lookup = mako.lookup.TemplateLookup(directories=[os.path.dirname(filepath)])
30-
tmpl = mako.template.Template(filename=filepath, lookup=lookup)
31-
32-
tmpl.render_context(ctx)
29+
try:
30+
template_dirs = [os.path.dirname(filepath)]
31+
lookup = mako.lookup.TemplateLookup(directories=template_dirs)
32+
tmpl = lookup.get_template(module_data["filebasename"])
33+
tmpl.render_context(ctx)
34+
except: # noqa: E722
35+
logger.exception(mako.exceptions.text_error_template().render())
36+
raise
3337

3438
rendered_src_filepath = get_rendered_source_filepath(filepath)
3539

environment.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ dependencies:
55
- pybind11
66
- mako
77
- black
8+
- regex>=2021
89
- flake8
910
- isort
1011
- pytest

release

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
GIT:
22
git commit -m "yy.mm.dd"
33
git tag yy.mm.dd
4-
git push --atomic origin stable yy.mm.dd
4+
git push --atomic origin main yy.mm.dd
55
wait for github action to complete
66
create release on github
77

tests/apackage/mymodule.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/*
1+
/* cppimport
22
<%
33
import pybind11
44
cfg['compiler_args'] = ['-std=c++11']
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from . import mymodule
2+
3+
4+
def f():
5+
return mymodule.add(1, 2)

0 commit comments

Comments
 (0)