Commit b9975566 authored by scoder's avatar scoder Committed by GitHub

Merge pull request #2408 from gabrieldemarmiesse/test_sharing_declarations__2

Adding tests for "sharing declarations" part 2
parents e4913479 82f95375
cdef extern from "lunch.h":
void eject_tomato(float)
cimport shrubbing
import shrubbing
def main():
cdef shrubbing.Shrubbery sh
sh = shrubbing.standard_shrubbery()
print("Shrubbery size is", sh.width, 'x', sh.length)
cimport c_lunch
def eject_tomato(float speed):
c_lunch.eject_tomato(speed)
from distutils.core import setup
from Cython.Build import cythonize
setup(ext_modules=cythonize(["landscaping.pyx", "shrubbing.pyx"]))
cdef class Shrubbery:
cdef int width
cdef int length
cdef class Shrubbery:
def __cinit__(self, int w, int l):
self.width = w
self.length = l
def standard_shrubbery():
return Shrubbery(3, 7)
...@@ -147,17 +147,13 @@ for an imaginary module, and :keyword:`cimport` that module. You can then ...@@ -147,17 +147,13 @@ for an imaginary module, and :keyword:`cimport` that module. You can then
refer to the C functions by qualifying them with the name of the module. refer to the C functions by qualifying them with the name of the module.
Here's an example: Here's an example:
:file:`c_lunch.pxd`:: :file:`c_lunch.pxd`:
cdef extern from "lunch.h": .. literalinclude:: ../../examples/userguide/sharing_declarations/c_lunch.pxd
void eject_tomato(float)
:file:`lunch.pyx`:: :file:`lunch.pyx`:
cimport c_lunch .. literalinclude:: ../../examples/userguide/sharing_declarations/lunch.pyx
def eject_tomato(float speed):
c_lunch.eject_tomato(speed)
You don't need any :file:`c_lunch.pyx` file, because the only things defined You don't need any :file:`c_lunch.pyx` file, because the only things defined
in :file:`c_lunch.pxd` are extern C entities. There won't be any actual in :file:`c_lunch.pxd` are extern C entities. There won't be any actual
...@@ -222,38 +218,23 @@ Python methods. ...@@ -222,38 +218,23 @@ Python methods.
Here is an example of a module which defines and exports an extension type, Here is an example of a module which defines and exports an extension type,
and another module which uses it: and another module which uses it:
:file:`Shrubbing.pxd`:: :file:`shrubbing.pxd`:
cdef class Shrubbery:
cdef int width
cdef int length
:file:`Shrubbing.pyx`::
cdef class Shrubbery: .. literalinclude:: ../../examples/userguide/sharing_declarations/shrubbing.pxd
def __cinit__(self, int w, int l):
self.width = w
self.length = l
def standard_shrubbery(): :file:`shrubbing.pyx`:
return Shrubbery(3, 7)
:file:`Landscaping.pyx`:: .. literalinclude:: ../../examples/userguide/sharing_declarations/shrubbing.pyx
cimport Shrubbing :file:`landscaping.pyx`:
import Shrubbing
cdef Shrubbing.Shrubbery sh .. literalinclude:: ../../examples/userguide/sharing_declarations/landscaping.pyx
sh = Shrubbing.standard_shrubbery()
print("Shrubbery size is %d x %d" % (sh.width, sh.length))
One would then need to compile both of these modules, e.g. using One would then need to compile both of these modules, e.g. using
:file:`setup.py`:: :file:`setup.py`:
from distutils.core import setup .. literalinclude:: ../../examples/userguide/sharing_declarations/setup.py
from Cython.Build import cythonize
setup(ext_modules = cythonize(["Landscaping.pyx", "Shrubbing.pyx"]))
Some things to note about this example: Some things to note about this example:
......
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