Skip to content

Commit

Permalink
Fixes #18
Browse files Browse the repository at this point in the history
  • Loading branch information
tbenthompson committed Sep 15, 2017
1 parent 98ab937 commit 9daaa02
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 45 deletions.
28 changes: 0 additions & 28 deletions cppimport/find.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,31 +40,3 @@ def find_module_cpppath(modulename):
return outfilename

return None


def find_module_path(module_name, search_path=None):
"""
Find the module path (pyd / so), while accounting for platform/arch naming
:param module_name: The name of the module
:param search_path: The path to search in. If None, searches system path.
:return: The full path to the library or None if not found.
"""

# Use importlib if python 3.4+, else imp
if sys.version_info[0] > 3 or (sys.version_info[0] == 3 and sys.version_info[1] >= 4):

from importlib.machinery import FileFinder, ExtensionFileLoader, EXTENSION_SUFFIXES
file_finder = FileFinder(search_path, (ExtensionFileLoader, EXTENSION_SUFFIXES))

# The search caches must be cleared to guaranteed find dynamically created modules
file_finder.invalidate_caches()
result = file_finder.find_spec(module_name)
return None if not result else result.origin
else:
from imp import find_module # Deprecated in 3.4
try:
result = find_module(module_name, [search_path])
except ImportError:
result = None

return None if not result else result[1]
62 changes: 45 additions & 17 deletions cppimport/importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,22 @@ def setup_module_data(fullname, filepath):
)
return module_data

def should_rebuild(module_data):
module_path = cppimport.find.find_module_path(module_data['fullname'], module_data['filedirname'])
return (not module_path or
not os.path.exists(module_path) or
cppimport.config.should_force_rebuild or
not cppimport.checksum.is_checksum_current(module_data))
def load_module(module_data):
module_data['module'] = importlib.import_module(module_data['fullname'])

def checksum_and_try_load(module_data):
if cppimport.config.should_force_rebuild:
return False
if not cppimport.checksum.is_checksum_current(module_data):
return False
quiet_print("Matching checksum for " + module_data['filepath'] + " --> not compiling")
try:
load_module(module_data)
return True
except ImportError:
quiet_print(
"ImportError during import with matching checksum. Trying to rebuild.")
return False

def template_and_build(filepath, module_data):
# Don't import until here to reduce startup time.
Expand All @@ -46,22 +56,40 @@ def template_and_build(filepath, module_data):
build_module.build_module(module_data)
cppimport.checksum.checksum_save(module_data)

def reload_if_loaded(module_data):
if module_data['module'] is None:
return importlib.import_module(module_data['fullname'])
else:
try:
reload
except NameError:
from imp import reload
print("RELOADING")

This comment has been minimized.

Copy link
@sjperkins

sjperkins Sep 16, 2017

You may want to remove the prints?

print("RELOADING")
print("RELOADING")
print("RELOADING")
return reload(module_data['module'])

def imp_from_filepath(filepath, fullname = None):
if fullname is None:
fullname = os.path.splitext(os.path.basename(filepath))[0]
module_data = setup_module_data(fullname, filepath)
if should_rebuild(module_data):
if not checksum_and_try_load(module_data):
template_and_build(filepath, module_data)
return importlib.import_module(fullname)
else:
quiet_print("Matching checksum for " + filepath + " --> not compiling")
try:
return importlib.import_module(fullname)
except ImportError as e:
quiet_print(
"ImportError during import with matching checksum. Trying to rebuild.")
template_and_build(filepath, module_data)
return importlib.import_module(fullname)
load_module(module_data)
return module_data['module']

# return module_data['module']
# return reload_if_loaded(module_data)

This comment has been minimized.

Copy link
@sjperkins

sjperkins Sep 16, 2017

Might want to remove commented out code

This comment has been minimized.

Copy link
@tbenthompson

tbenthompson Sep 18, 2017

Author Owner

Oops. Thanks for the code review!

# else:
# quiet_print("Matching checksum for " + filepath + " --> not compiling")
# try:
# return reload_if_loaded(module_data)
# except ImportError as e:
# quiet_print(
# "ImportError during import with matching checksum. Trying to rebuild.")
# template_and_build(filepath, module_data)
# return reload_if_loaded(module_data)

def imp(fullname):
# Search through sys.path to find a file that matches the module
Expand Down
1 change: 1 addition & 0 deletions tests/mymodule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ PYBIND11_PLUGIN(mymodule) {
pybind11::module m("mymodule", "auto-compiled c++ extension");
m.def("add", &add);
#ifdef THING_DEFINED
#pragma message "stuff"
py::class_<Thing>(m, "Thing")
.def(py::init<>())
.def("cheer", &Thing::cheer);
Expand Down

0 comments on commit 9daaa02

Please sign in to comment.