Skip to content

Commit

Permalink
more fixes, seems correct
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewAnnex committed Apr 21, 2020
1 parent 9a145bc commit abe464f
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 28 deletions.
7 changes: 5 additions & 2 deletions spiceypy/spiceypy.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
Ellipse,
Plane,
SpiceCell,
SpiceCellPointer,
SpiceDLADescr,
SpiceDSKDescr,
SpiceEKAttDsc,
Expand Down Expand Up @@ -6272,7 +6273,9 @@ def gfrepf() -> None:


@spice_error_check
def gfrepi(window: SpiceCell, begmss: str, endmss: str) -> None:
def gfrepi(
window: Union[SpiceCell, SpiceCellPointer], begmss: str, endmss: str
) -> None:
"""
This entry point initializes a search progress report.
Expand All @@ -6285,7 +6288,7 @@ def gfrepi(window: SpiceCell, begmss: str, endmss: str) -> None:
begmss = stypes.string_to_char_p(begmss)
endmss = stypes.string_to_char_p(endmss)
# don't do anything if we were given a pointer to a SpiceCell, like if we were in a callback
if not isinstance(window, ctypes.POINTER(stypes.SpiceCell)):
if not isinstance(window, SpiceCellPointer):
assert isinstance(window, stypes.SpiceCell)
assert window.is_double()
window = ctypes.byref(window)
Expand Down
55 changes: 29 additions & 26 deletions spiceypy/utils/callbacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@

import functools
from ctypes import c_int, c_double, c_char_p, POINTER, CFUNCTYPE, byref
from .support_types import SpiceCell
from typing import Callable
from .support_types import SpiceCell, to_python_string
from typing import Callable, Union

UDFUNC = CFUNCTYPE(None, c_double, POINTER(c_double))
UDFUNS = CFUNCTYPE(None, c_double, POINTER(c_double))
Expand Down Expand Up @@ -61,116 +61,119 @@ def SpiceUDFUNS(f: Callable[[float], float]) -> UDFUNS:
"""

@functools.wraps(f)
def wrapping_udfuns(x: float, value: POINTER(c_double)):
def wrapping_udfuns(x: float, value: POINTER(c_double)) -> None:
result = f(x)
value[0] = c_double(result)

return UDFUNS(wrapping_udfuns)


def SpiceUDFUNB(f) -> UDFUNB:
def SpiceUDFUNB(f: Callable[[UDFUNS, float], int]) -> UDFUNB:
"""
Decorator for wrapping python functions in spice udfunb callback type
:param f: function to be wrapped
:type f: builtins.function
:return: wrapped udfunb function
"""

@functools.wraps(f)
def wrapping_udfunb(udf, et, xbool):
result = f(udf, et) # the function takes a udffunc as a argument
def wrapping_udfunb(udf: UDFUNS, et: float, xbool: POINTER(c_int)) -> None:
result = f(udf, et)
xbool[0] = c_int(result) # https://github.com/numpy/numpy/issues/14397

return UDFUNB(wrapping_udfunb)


def SpiceUDSTEP(f) -> UDSTEP:
def SpiceUDSTEP(f: Callable[[float], float]) -> UDSTEP:
"""
Decorator for wrapping python functions in spice udstep callback type
:param f: function to be wrapped
:type f: builtins.function
:return: wrapped udstep function
"""

@functools.wraps(f)
def wrapping_udstep(x, value):
def wrapping_udstep(x: float, value: POINTER(c_double)) -> None:
result = f(x)
value[0] = c_double(result)

return UDSTEP(wrapping_udstep)


def SpiceUDREFN(f) -> UDREFN:
def SpiceUDREFN(
f: Callable[[float, float, Union[bool, int], Union[bool, int]], float]
) -> UDREFN:
"""
Decorator for wrapping python functions in spice udrefn callback type
:param f: function to be wrapped
:type f: builtins.function
:return: wrapped udrefn function
"""

@functools.wraps(f)
def wrapping_udrefn(t1, t2, s1, s2, t):
def wrapping_udrefn(
t1: float,
t2: float,
s1: Union[bool, int],
s2: Union[bool, int],
t: POINTER(c_double),
) -> None:
result = f(t1, t2, s1, s2)
t[0] = c_double(result)

return UDREFN(wrapping_udrefn)


def SpiceUDREPI(f) -> UDREPI:
def SpiceUDREPI(f: Callable[[SpiceCell, str, str], None]) -> UDREPI:
"""
Decorator for wrapping python functions in spice udfrepi callback type
:param f: function to be wrapped
:type f: builtins.function
:return: wrapped udrepi function
"""

@functools.wraps(f)
def wrapping_udrepi(cnfine, srcpre, srcsurf):
f(cnfine, srcpre, srcsurf)
def wrapping_udrepi(
cnfine: POINTER(SpiceCell), srcpre: bytes, srcsurf: bytes
) -> None:
f(cnfine, to_python_string(srcpre), to_python_string(srcsurf))

return UDREPI(wrapping_udrepi)


def SpiceUDREPU(f) -> UDREPU:
def SpiceUDREPU(f: Callable[[float, float, float], None]) -> UDREPU:
"""
Decorator for wrapping python functions in spice udrepu callback type
:param f: function to be wrapped
:type f: builtins.function
:return: wrapped udrepu function
"""

@functools.wraps(f)
def wrapping_udrepu(beg, end, et):
def wrapping_udrepu(beg: float, end: float, et: float) -> None:
f(beg, end, et)

return UDREPU(wrapping_udrepu)


def SpiceUDREPF(f) -> UDREPF:
def SpiceUDREPF(f: Callable) -> UDREPF:
"""
Decorator for wrapping python functions in spice udrepf callback type
:param f: function to be wrapped
:type f: builtins.function
:return: wrapped udrepf function
"""

@functools.wraps(f)
def wrapping_udrepf():
def wrapping_udrepf() -> None:
f()

return UDREPF(wrapping_udrepf)


def SpiceUDBAIL(f: Callable[[], bool]) -> UDBAIL:
def SpiceUDBAIL(f: Callable[[], Union[bool, int]]) -> UDBAIL:
"""
Decorator for wrapping python functions in spice udbail callback type
:param f: function to be wrapped
:type f: builtins.function
:return: wrapped udbail function
"""

@functools.wraps(f)
def wrapping_udbail():
def wrapping_udbail() -> int:
result = f()
return int(result)

Expand Down
3 changes: 3 additions & 0 deletions spiceypy/utils/support_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
cast,
Structure,
string_at,
POINTER,
)

import numpy
Expand Down Expand Up @@ -1028,6 +1029,8 @@ def __eq__(self, other):
return True


SpiceCellPointer = POINTER(SpiceCell)

# Spice Cell classes


Expand Down

0 comments on commit abe464f

Please sign in to comment.