Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
[![license](https://img.shields.io/github/license/Lyncs-API/lyncs.cppyy?logo=github&logoColor=white)](https://github.com/Lyncs-API/lyncs.cppyy/blob/master/LICENSE)
[![build & test](https://img.shields.io/github/workflow/status/Lyncs-API/lyncs.cppyy/build%20&%20test?logo=github&logoColor=white)](https://github.com/Lyncs-API/lyncs.cppyy/actions)
[![codecov](https://img.shields.io/codecov/c/github/Lyncs-API/lyncs.cppyy?logo=codecov&logoColor=white)](https://codecov.io/gh/Lyncs-API/lyncs.cppyy)
[![pylint](https://img.shields.io/badge/pylint%20score-9.4%2F10-green?logo=python&logoColor=white)](http://pylint.pycqa.org/)
[![pylint](https://img.shields.io/badge/pylint%20score-9.3%2F10-green?logo=python&logoColor=white)](http://pylint.pycqa.org/)
[![black](https://img.shields.io/badge/code%20style-black-000000.svg?logo=codefactor&logoColor=white)](https://github.com/ambv/black)


Expand Down
2 changes: 1 addition & 1 deletion lyncs_cppyy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
[cppyy](https://cppyy.readthedocs.io/en/latest/) in the Lyncs API.
"""

__version__ = "0.2.0"
__version__ = "0.2.1"

from cppyy import nullptr, cppdef, gbl, include, c_include, set_debug
from .lib import *
Expand Down
26 changes: 21 additions & 5 deletions lyncs_cppyy/lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,22 @@ def undef(self):
if cpp:
cppyy.cppdef(cpp)

def get_namespace(self, namespace):
"Returns a view of the library at a given namespace"
parts = namespace.replace("::", ".").split(".")
out = cppyy.gbl
for part in parts:
out = getattr(out, part)
return out

def namespace_loop(self):
"Loops over all the given namespace"
for namespace in self.namespace:
try:
yield self.get_namespace(namespace)
except AttributeError:
pass

def __getattr__(self, key):
if not self.loaded:
self.load()
Expand All @@ -235,9 +251,9 @@ def __getattr__(self, key):
if self.defined:
key = self.defined.get(key, key)
if self.namespace:
for namespace in self.namespace:
for namespace in self.namespace_loop():
try:
return getattr(getattr(cppyy.gbl, namespace), key)
return getattr(namespace, key)
except AttributeError:
pass
return getattr(cppyy.gbl, key)
Expand All @@ -260,10 +276,10 @@ def __setattr__(self, key, value):
if self.defined:
key = self.defined.get(key, key)
if self.namespace:
for namespace in self.namespace:
for namespace in self.namespace_loop():
try:
getattr(getattr(cppyy.gbl, namespace), key)
return setattr(getattr(cppyy.gbl, namespace), key, value)
getattr(namespace, key)
return setattr(namespace, key, value)
except AttributeError:
pass
getattr(cppyy.gbl, key)
Expand Down
2 changes: 1 addition & 1 deletion lyncs_cppyy/ll.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def __cppyy__(self):

def to_pointer(ptr: int, ctype: str = "void *", size: int = None):
"Casts integer to void pointer"
ptr = cast[ctype](ptr)
ptr = reinterpret_cast[ctype](ptr)
if size is not None:
ptr.reshape((size,))
return ptr
17 changes: 9 additions & 8 deletions lyncs_cppyy/numpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@
from .ll import to_pointer
from .lib import lib

# NOTE: commented out cases that are not working
dtype_map = (
(np.bool_, "bool"),
(np.byte, "signed char"),
# (np.byte, "signed char"),
(np.ubyte, "unsigned char"),
(np.short, "short"),
(np.ushort, "unsigned short"),
Expand All @@ -22,14 +23,14 @@
(np.uint, "unsigned long"),
(np.longlong, "long long"),
(np.ulonglong, "unsigned long long"),
(np.half, ""),
# (np.half, ""),
(np.single, "float"),
(np.double, "double"),
(np.longdouble, "long double"),
(np.csingle, "float complex"),
(np.cdouble, "double complex"),
(np.clongdouble, "long double complex"),
(np.int8, "int8_t"),
(np.csingle, "std::complex<float>"),
(np.cdouble, "std::complex<double>"),
# (np.clongdouble, "std::complex<long double>"),
# (np.int8, "int8_t"),
(np.int16, "int16_t"),
(np.int32, "int32_t"),
(np.int64, "int64_t"),
Expand All @@ -41,8 +42,8 @@
(np.uintp, "uintptr_t"),
(np.float32, "float"),
(np.float64, "double"),
(np.complex64, "float complex"),
(np.complex128, "double complex"),
(np.complex64, "std::complex<float>"),
(np.complex128, "std::complex<double>"),
)

char_map = {dtype(0).dtype.char: ctype for dtype, ctype in dtype_map}
Expand Down
7 changes: 7 additions & 0 deletions test/cnumbers/numbers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,11 @@ namespace numbers {
T global() {
return (T) gbl;
}

namespace another {
template<typename T>
T two() {
return (T) 2;
}
}
}
3 changes: 2 additions & 1 deletion test/test_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def test_cnumbers():
cppnumbers = Lib(
header="numbers.hpp",
path=path,
namespace="numbers",
namespace=["numbers", "numbers.another"],
include=path + "/include", # Not needed
library=Lib(), # Not needed
defined={"uno": "one", "GBL": "gbl"},
Expand All @@ -76,6 +76,7 @@ def test_cnumbers():
assert cppnumbers.zero["int"]() == 0
assert cppnumbers.one["long"]() == 1
assert cppnumbers.uno["long"]() == 1
assert cppnumbers.two["int"]() == 2

assert cppnumbers.ONE == 1

Expand Down
15 changes: 14 additions & 1 deletion test/test_numpy.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@
import numpy
from lyncs_cppyy import ll, cppdef, include, gbl, lib
from lyncs_cppyy.numpy import array_to_pointers
from lyncs_cppyy.numpy import dtype_map, char_map
from lyncs_cppyy.numpy import *


def test_get_ctype():
for dtype, ctype in char_map.items():
assert get_ctype(dtype) == ctype


def test_get_numpy_pointer():
for dtype in char_map:
print(dtype)
arr = numpy.zeros((10,), dtype=dtype)
assert (numpy.array(get_numpy_pointer(arr)) == arr).all()


def test_flatten_array_to_pointers():
Expand Down