Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement relative imports, clean up Manifest.in. #75

Merged
merged 4 commits into from
Jul 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 8 additions & 9 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
name: Test

on: [push, pull_request]

on:
pull_request:
types:
- opened
- synchronize
push:
branches:
- main
jobs:
test:
strategy:
Expand Down Expand Up @@ -69,13 +75,6 @@ jobs:
- name: Build sdist
run: >-
python setup.py sdist
- name: Publish distribution 📦 to Test PyPI
uses: pypa/gh-action-pypi-publish@master
with:
password: ${{ secrets.TEST_PYPI_API_TOKEN }}
repository_url: https://test.pypi.org/legacy/
verbose: true
skip_existing: true
- name: Publish distribution 📦 to PyPI
if: startsWith(github.ref, 'refs/tags')
uses: pypa/gh-action-pypi-publish@master
Expand Down
5 changes: 4 additions & 1 deletion MANIFEST.in
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
include README.md
include VERSION
recursive-include tests *
recursive-include tests *.py
recursive-include tests *.h
recursive-include tests *.cpp
recursive-include tests *.c
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<a target="_blank" href="https://pypi.org/project/cppimport/" title="PyPI"><img src="https://img.shields.io/pypi/dm/cppimport"></a>
<a target="_blank" href="LICENSE" title="License: MIT"><img src="https://img.shields.io/badge/License-MIT-blue.svg"></a>
<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>
<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>
<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>
</a>

</p>
Expand Down
7 changes: 7 additions & 0 deletions cppimport/find.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import logging
import os
import sys

import cppimport
from cppimport.filepaths import make_absolute

logger = logging.getLogger(__name__)


def find_module_cpppath(modulename, opt_in=False):
filepath = _find_module_cpppath(modulename, opt_in)
Expand Down Expand Up @@ -67,6 +70,10 @@ def _find_file_in_folders(filename, paths, opt_in):
continue
filepath = os.path.join(d, f)
if opt_in and not _check_first_line_contains_cppimport(filepath):
logger.debug(
"Found file but the first line doesn't "
"contain cppimport so it will be skipped: " + filepath
)
continue
return filepath
return None
Expand Down
3 changes: 2 additions & 1 deletion cppimport/import_hook.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ class Hook(object):
def __init__(self):
self._running = False

def find_module(self, fullname, path=None):
def find_spec(self, fullname, path, target=None):
print("find_spec", fullname, path, target)
# Prevent re-entry by the underlying importer
if self._running:
return
Expand Down
12 changes: 8 additions & 4 deletions cppimport/templating.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,14 @@ def run_templating(module_data):
ctx = mako.runtime.Context(buf, **module_data)

filepath = module_data["filepath"]
lookup = mako.lookup.TemplateLookup(directories=[os.path.dirname(filepath)])
tmpl = mako.template.Template(filename=filepath, lookup=lookup)

tmpl.render_context(ctx)
try:
template_dirs = [os.path.dirname(filepath)]
lookup = mako.lookup.TemplateLookup(directories=template_dirs)
tmpl = lookup.get_template(module_data["filebasename"])
tmpl.render_context(ctx)
except: # noqa: E722
logger.exception(mako.exceptions.text_error_template().render())
raise

rendered_src_filepath = get_rendered_source_filepath(filepath)

Expand Down
1 change: 1 addition & 0 deletions environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ dependencies:
- pybind11
- mako
- black
- regex>=2021
- flake8
- isort
- pytest
Expand Down
2 changes: 1 addition & 1 deletion release
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
GIT:
git commit -m "yy.mm.dd"
git tag yy.mm.dd
git push --atomic origin stable yy.mm.dd
git push --atomic origin main yy.mm.dd
wait for github action to complete
create release on github

Expand Down
2 changes: 1 addition & 1 deletion tests/apackage/mymodule.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*
/* cppimport
<%
import pybind11
cfg['compiler_args'] = ['-std=c++11']
Expand Down
5 changes: 5 additions & 0 deletions tests/apackage/rel_import_tester.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from . import mymodule


def f():
return mymodule.add(1, 2)
25 changes: 24 additions & 1 deletion tests/test_cppimport.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,30 @@ def test_import_hook():
import hook_test

cppimport.force_rebuild(False)
hook_test
assert hook_test.sub(3, 1) == 2


def test_submodule_import_hook():
import cppimport.import_hook

# Force rebuild to make sure we're not just reloading the already compiled
# module from disk
cppimport.force_rebuild(True)
import apackage.mymodule

cppimport.force_rebuild(False)
assert apackage.mymodule.add(3, 1) == 4


def test_relative_import():
import cppimport.import_hook

cppimport.force_rebuild(True)
from apackage.rel_import_tester import f

cppimport.force_rebuild(False)
print(f())
assert f() == 3


def test_multiple_processes():
Expand Down