From 68b5dedfee38d46f3ae81ae10504cf4cfdcca1fa Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Piotr=20Ma=C5=9Blanka?= <piotr.maslanka@henrietta.com.pl>
Date: Sun, 15 Mar 2020 22:00:17 +0100
Subject: [PATCH] proper handling of nested packages

---
 cython_multibuild/multibuild.py    | 11 ++++++++---
 example/example_module/__init__.py |  3 +++
 2 files changed, 11 insertions(+), 3 deletions(-)

diff --git a/cython_multibuild/multibuild.py b/cython_multibuild/multibuild.py
index 1d08de8..fd9557c 100644
--- a/cython_multibuild/multibuild.py
+++ b/cython_multibuild/multibuild.py
@@ -44,7 +44,7 @@ class Multibuild:
                 with open(os.path.join(path, h_name), 'w') as f_out:
                     f_out.write(data)
             else:
-                with open(os.path.join(path, name.replace('.pyx', '.h')), 'w') as f_out:
+                with open(os.path.join(path, h_name), 'w') as f_out:
                     f_out.write('#include "Python.h"\n')
                     f_out.write('\n'+exported_line+'\n')
 
@@ -64,7 +64,7 @@ cdef extern from "Python.h":
             path = path.replace(self.bootstrap_directory, '')
             module_name = name.replace('.pyx', '')
             if path:
-                h_path_name = os.path.join(path, name.replace('.pyx', '.h'))
+                h_path_name = os.path.join(path[1:], name.replace('.pyx', '.h')).replace('\\', '\\\\')
             else:
                 h_path_name = name.replace('.pyx', '.h')
             bootstrap_contents.append('cdef extern from "%s":\n' % (h_path_name, ))
@@ -72,7 +72,12 @@ cdef extern from "Python.h":
 
             module_py_name = '.'.join([self.extension_name] + h_path_name.split(os.path.sep))
 
-            self.modules.add((self.extension_name+'.'+module_name, 'PyInit_%s()' % (module_name, )))
+            if path:
+                complete_module_name = self.extension_name+'.'+'.'.join(path[1:].split(os.path.sep))+'.'+module_name
+            else:
+                complete_module_name = self.extension_name + '.'+module_name
+
+            self.modules.add((complete_module_name, 'PyInit_%s()' % (module_name, )))
 
         bootstrap_contents.append('''cdef object get_definition_by_name(str name):\n''')
         modules = iter(self.modules)
diff --git a/example/example_module/__init__.py b/example/example_module/__init__.py
index e69de29..a29ecf4 100644
--- a/example/example_module/__init__.py
+++ b/example/example_module/__init__.py
@@ -0,0 +1,3 @@
+
+from example_module.__bootstrap__ import bootstrap_cython_submodules
+bootstrap_cython_submodules()
-- 
GitLab