Commit 518f6ec7 authored by Xavier Thompson's avatar Xavier Thompson

Add helloworld.pyx

parent 2ddd8aaa
%.so: %.pyx
python3 setup.py build_ext --inplace $* && mv $*.*.so $@
%: %.so
@echo ">>> import $@" && python3 -c "import $@"
clean:
rm -rf build
rm -rf *.cpp *.c
rm -rf *.so
.PHONY: clean
.PRECIOUS: %.so
.SUFFIXES:
from stdlib.string cimport Str
from libc.stdio cimport puts
def main():
with nogil:
hello = Str("hello")
world = Str("world")
hello_world = hello + world
puts(Str.to_c_str(hello_world))
hello_again = hello_world.slice(0, 5)
puts(Str.to_c_str(hello_again))
world_again = hello_world.slice(5, 10)
puts(Str.to_c_str(world_again))
main()
from distutils.core import setup
from distutils.extension import Extension
from Cython.Build import cythonize
import sys
src = sys.argv[-1]
sys.argv[:] = sys.argv[:-1]
extensions = [
Extension(
src,
language='c++',
sources=[src + '.pyx'],
extra_compile_args=['-std=c++17'],
)
]
setup(
ext_modules = cythonize(extensions)
)
......@@ -77,8 +77,11 @@ cdef cypclass Str:
cat._s = string_view(s, l1 + l2)
return cat
const char * to_c_str(self):
return self._s.data()
@staticmethod
const char * to_c_str(Str s):
if s is NULL:
return NULL
return s._s.data()
@staticmethod
Str steal_c_str(const char *s, size_t size):
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment