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

Allow cimport for quicktions #5

Open
wants to merge 25 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
479ad2d
Add pxd to allow cimport from other cython modules
nocarryr Apr 6, 2018
4ab0b49
Add test case for cimporting from another cython module
nocarryr Apr 6, 2018
2a10e91
Rename top-level src directory so it can be imported as a package
nocarryr Apr 6, 2018
0a6e8e2
Modify extension build methods to match layout
nocarryr Nov 12, 2018
cee0127
Merge branch 'master' into pxd-header
nocarryr Nov 12, 2018
b59f3ba
Merge branch 'master' into pxd-restructure
nocarryr Nov 12, 2018
278516c
Add language level directive to pxd and unittest cython module
nocarryr Nov 12, 2018
18f7389
Update paths in Makefile
nocarryr Nov 12, 2018
46daa88
Add language directive to pxd
nocarryr Nov 12, 2018
3be2c4e
Add module-level pxd
nocarryr Nov 12, 2018
13b03b5
Add language level to pyximport, handle extension teardown properly
nocarryr Nov 12, 2018
32fe9ea
Import docstrings from quicktions.pyx at package level
nocarryr Nov 12, 2018
53e1250
Handle py2 imports properly
nocarryr Nov 12, 2018
08ac125
Compatibility fix for py2.6
nocarryr Nov 12, 2018
f628e89
Merge branch 'master' into pxd-header
nocarryr Jan 22, 2019
6de8ea4
Use pre-built wheel to install and run tests against
nocarryr Jan 22, 2019
5cb185a
Move wrapper code and typedefs into pxd
nocarryr Jan 23, 2019
19ea21c
Merge branch 'pxd-header' into pxd-restructure
nocarryr Jan 24, 2019
b7d99f4
Move to src layout with subdirectory. Move test module to project root
nocarryr Jan 25, 2019
f8894e9
Remove unused imports and assertions
nocarryr Jan 25, 2019
fad41af
Correct paths for "make test"
nocarryr Jan 25, 2019
dde7654
Add comment explaining wheel installation step in travis config
nocarryr Jan 25, 2019
efcca15
Make __version__ and __all__ variables available at the package level
nocarryr Jan 25, 2019
0536e1e
Place Cython-related tests into separate module
nocarryr Feb 15, 2019
5a477cc
Use Cythonize module instead of pyximport to compile test modules
nocarryr Feb 15, 2019
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
37 changes: 18 additions & 19 deletions test_quicktions.py
Original file line number Diff line number Diff line change
@@ -1,49 +1,48 @@
import os
import glob
import unittest

from Cython.Build import Cythonize

import quicktions
F = quicktions.Fraction
gcd = quicktions._gcd

class CImportTest(unittest.TestCase):

def setUp(self):
self.build_test_module()
self.module_files = []

def tearDown(self):
self.remove_test_module()
for fn in self.module_files:
if os.path.exists(fn):
os.remove(fn)

def build_test_module(self):
self.module_code = '\n'.join([
module_code = '\n'.join([
'# cython: language_level=3str',
'from quicktions cimport Fraction',
'def get_fraction():',
' return Fraction(1, 2)',
])
self.base_path = os.path.abspath(os.path.dirname(__file__))
self.module_name = 'quicktions_importtest'
self.module_filename = os.path.join(self.base_path, '.'.join([self.module_name, 'pyx']))
with open(self.module_filename, 'w') as f:
f.write(self.module_code)

def remove_test_module(self):
for fn in os.listdir(self.base_path):
if not fn.startswith(self.module_name):
continue
os.remove(os.path.join(self.base_path, fn))
base_path = os.path.abspath(os.path.dirname(__file__))
module_name = 'quicktions_importtest'
module_filename = os.path.join(base_path, '.'.join([module_name, 'pyx']))
with open(module_filename, 'w') as f:
f.write(module_code)

Cythonize.main(['-i', module_filename])
Copy link
Owner

Choose a reason for hiding this comment

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

You can pass a (temporary) build directory here via -s build_dir=.... That should simplify the cleanup.


for fn in glob.glob(os.path.join(base_path, '.'.join([module_name, '*']))):
self.module_files.append(os.path.abspath(fn))

def test_cimport(self):
self.build_test_module()
import pyximport
self.py_importer, self.pyx_importer = pyximport.install(inplace=True, language_level=3)

from quicktions_importtest import get_fraction

self.assertEqual(get_fraction(), F(1,2))

pyximport.uninstall(self.py_importer, self.pyx_importer)



def test_main():
suite = unittest.TestSuite()
Expand Down