Skip to content

Commit

Permalink
Merge pull request #18 from Starlink/u/timj/cython3
Browse files Browse the repository at this point in the history
Support cython 3
  • Loading branch information
timj authored Sep 26, 2023
2 parents 2ceaf53 + 1886761 commit 0316c1d
Show file tree
Hide file tree
Showing 7 changed files with 559 additions and 545 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.7", "3.8", "3.9", "3.10"]
python-version: ["3.7", "3.8", "3.9", "3.10", "3.11"]

steps:
- uses: actions/checkout@v3
Expand All @@ -24,7 +24,7 @@ jobs:
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: "3.10"
python-version: ${{ matrix.python-version }}
cache: "pip"
cache-dependency-path: "setup.cfg"

Expand Down
15 changes: 9 additions & 6 deletions pal.pyx.in
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

#cython: language_level=3
#distutils: language=c

cimport cpal

from libc.stdlib cimport malloc, free
Expand Down Expand Up @@ -758,7 +761,7 @@ def dd2tf( int ndp, double days ):

@palDd2tf@
"""
cdef char csign = ' '
cdef char csign = b' '
cdef int ihmsf[4]
cpal.palDd2tf( ndp, days, &csign, ihmsf )
sign = chr(csign)
Expand All @@ -785,7 +788,7 @@ def dd2tfVector( int ndp, np.ndarray[double, ndim=1] days not None):
cdef np.ndarray is_out = np.empty(length, dtype = np.int64)
cdef np.ndarray frac_out = np.empty(length, dtype = np.int64)

cdef char csign = ' '
cdef char csign = b' '
cdef int ihmsf[4]
for row in range(length):
cpal.palDd2tf( ndp, days[row], &csign, ihmsf)
Expand Down Expand Up @@ -1265,7 +1268,7 @@ def dr2af( int ndp, double angle ):
(sign, id, im, is, frac) = dr2af( ndp, angle )
@palDr2af@
"""
cdef char csign = ' '
cdef char csign = b' '
cdef int idmsf[4]
cpal.palDr2af( ndp, angle, &csign, idmsf )
sign = chr(csign)
Expand All @@ -1286,7 +1289,7 @@ def dr2afVector( int ndp, np.ndarray[double, ndim=1] angle not None):
cdef np.ndarray im_out = np.ndarray(length, dtype=int)
cdef np.ndarray isec_out = np.ndarray(length, dtype=int)
cdef np.ndarray frac_out = np.ndarray(length, dtype=int)
cdef char csign = ' '
cdef char csign = b' '
cdef int idmsf[4]

for ii in range(length):
Expand All @@ -1304,7 +1307,7 @@ def dr2tf( int ndp, double angle ):
(sign, ih, im, is, frac) = dr2tf( ndp, angle )
@palDr2tf@
"""
cdef char csign = ' '
cdef char csign = b' '
cdef int ihmsf[4]
cpal.palDr2tf( ndp, angle, &csign, ihmsf )
sign = chr(csign)
Expand All @@ -1325,7 +1328,7 @@ def dr2tfVector( int ndp, np.ndarray[double, ndim=1] angle not None):
cdef np.ndarray im_out = np.ndarray(length, dtype=int)
cdef np.ndarray isec_out = np.ndarray(length, dtype=int)
cdef np.ndarray frac_out = np.ndarray(length, dtype=int)
cdef char csign = ' '
cdef char csign = b' '
cdef int idmsf[4]

for ii in range(length):
Expand Down
10 changes: 9 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,2 +1,10 @@
[build-system]
requires = ["numpy", "cython", "setuptools"]
requires = ["oldest-supported-numpy", "cython", "setuptools"]

[tool.black]
line-length = 110
target-version = ["py37"]

[tool.isort]
profile = "black"
line_length = 110
20 changes: 9 additions & 11 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@
import numpy
import re
import codecs
from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext
from setuptools import Extension, setup
from Cython.Build import cythonize

# Local code
from support import sst2pydoc as sst
Expand Down Expand Up @@ -90,7 +89,7 @@
)

# Build up source file list
sources = ["cpal.pxd", "pal.pyx"]
sources = ["pal.pyx"]

# Sort out path to the C files
for cfile in erfa_c:
Expand Down Expand Up @@ -157,6 +156,11 @@
with open('README.rst') as file:
long_description = file.read()

extensions = [Extension("palpy", sources,
include_dirs=['cextern/erfa/src',
'cextern/pal',
numpy.get_include()])]

setup(
name="palpy",
version=palpy_version,
Expand All @@ -166,13 +170,7 @@
url='https://github.com/Starlink/palpy',
description="PAL -- A Positional Astronomy Library",
long_description=long_description,
cmdclass={'build_ext': build_ext},
ext_modules=[Extension(
name="palpy",
sources=sources,
include_dirs=['cextern/erfa/src', 'cextern/pal', numpy.get_include()],
language="c"
)],
ext_modules=cythonize(extensions),
requires=[
'numpy',
'Cython'
Expand Down
16 changes: 9 additions & 7 deletions support/palvers.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,18 @@ def read_pal_version():
"""
verfile = os.path.join("cextern", "pal", "configure.ac")
verstring = "-1.-1.-1"
for line in open(verfile):
if line.startswith("AC_INIT"):
# Version will be in string [nn.mm.pp]
match = re.search(r"\[(\d+\.\d+\.\d+)\]", line)
if match:
verstring = match.group(1)
break
with open(verfile) as fh:
for line in fh:
if line.startswith("AC_INIT"):
# Version will be in string [nn.mm.pp]
match = re.search(r"\[(\d+\.\d+\.\d+)\]", line)
if match:
verstring = match.group(1)
break
(major, minor, patch) = verstring.split(".")
return (verstring, major, minor, patch)


if __name__ == "__main__":
v, maj, min, p = read_pal_version()
print(v, maj, min, p)
106 changes: 54 additions & 52 deletions support/sst2pydoc.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,62 +50,64 @@ def read_prologs(filename):
content = ""
counter = 0

for line in open(filename):
line = line.strip()

# Start of a completely new prolog so reset everything
if line.startswith("*+"):
if counter != 0:
raise ValueError("Started prologue without closing previous prologue")
prolog = {}
heading = ""
content = ""
with open(filename) as fh:
for line in fh:
line = line.strip()

# Start of a completely new prolog so reset everything
if line.startswith("*+"):
if counter != 0:
raise ValueError("Started prologue without closing previous prologue")
prolog = {}
heading = ""
content = ""
counter = counter + 1
continue

# End of a prolog. Must store the current dict
if line.startswith("*-"):
counter = 0
if len(heading):
# Flush current heading
prolog[heading] = content
content = ""
name = prolog["name"].strip()
results[name] = prolog
prolog = None
continue

# If we are not in a prologue then nothing further is needed
if counter == 0:
continue

counter = counter + 1
continue

# End of a prolog. Must store the current dict
if line.startswith("*-"):
counter = 0
if len(heading):
# Flush current heading
prolog[heading] = content

# Completely blank lines are ignored
if len(line) == 0:
continue

# Look for a new section heading
match_head = heading_re.search(line)
if match_head is not None:
if len(heading):
# Flush previous heading
prolog[heading] = content
heading = match_head.group(1).lower()
content = ""
name = prolog['name'].strip()
results[name] = prolog
prolog = None
continue

# If we are not in a prologue then nothing further is needed
if counter == 0:
continue

counter = counter + 1

# Completely blank lines are ignored
if len(line) == 0:
continue

# Look for a new section heading
match_head = heading_re.search(line)
if match_head is not None:
if len(heading):
# Flush previous heading
prolog[heading] = content
heading = match_head.group(1).lower()
content = ""
continue

if line.startswith("* "):
content = content + line[6:] + "\n"
continue
elif line == "*":
content = content + "\n"
continue

if counter:
raise ValueError("Error parsing SST prologue line "+str(counter)+":'" + line + "'")
continue

if line.startswith("* "):
content = content + line[6:] + "\n"
continue
elif line == "*":
content = content + "\n"
continue

if counter:
raise ValueError("Error parsing SST prologue line " + str(counter) + ":'" + line + "'")
return results


if __name__ == "__main__":
results = read_prologs("cextern/pal/palAddet.c")
print(results)
Loading

0 comments on commit 0316c1d

Please sign in to comment.