Skip to content

Commit

Permalink
Fix #1149 - bytes, bytearray types managed
Browse files Browse the repository at this point in the history
  • Loading branch information
Nelson-numerical-software committed Apr 6, 2024
1 parent 5488490 commit 4a618b6
Show file tree
Hide file tree
Showing 31 changed files with 605 additions and 320 deletions.
8 changes: 6 additions & 2 deletions CHANGELOG.md
Expand Up @@ -9,9 +9,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added

- [#1141](http://github.com/nelson-lang/nelson/issues/1141) Help about Managing Data between Python and Nelson.
- Python interface (part 2):

- [#1141](http://github.com/nelson-lang/nelson/issues/1141) Help about Managing Data between Python and Nelson.
- [#1149](https://github.com/nelson-lang/nelson/issues/1149) python bytes, and bytearray types were not managed.
- `pyenv`: can use environment variables to set values.

- `getenv`: Retrieve the values of several environment variables.
- `pyenv`: can use environment variables to set values.

### Changed

Expand Down
6 changes: 5 additions & 1 deletion modules/python_engine/functions/@py/char.m
Expand Up @@ -8,6 +8,10 @@
% LICENCE_BLOCK_END
%=============================================================================
function r = char(obj)
r = obj.char();
if ismethod(obj, 'char')
r = obj.char();
else
error([_('Wrong value for #2 argument.'), ' ', 'char']);
end
end
%=============================================================================
6 changes: 5 additions & 1 deletion modules/python_engine/functions/@py/double.m
Expand Up @@ -8,6 +8,10 @@
% LICENCE_BLOCK_END
%=============================================================================
function r = double(obj)
r = obj.double();
if ismethod(obj, 'double')
r = obj.double();
else
error([_('Wrong value for #2 argument.'), ' ', 'double']);
end
end
%=============================================================================
6 changes: 5 additions & 1 deletion modules/python_engine/functions/@py/int16.m
Expand Up @@ -8,6 +8,10 @@
% LICENCE_BLOCK_END
%=============================================================================
function r = int16(obj)
r = obj.int16();
if ismethod(obj, 'int16')
r = obj.int16();
else
error([_('Wrong value for #2 argument.'), ' ', 'int16']);
end
end
%=============================================================================
6 changes: 5 additions & 1 deletion modules/python_engine/functions/@py/int32.m
Expand Up @@ -8,6 +8,10 @@
% LICENCE_BLOCK_END
%=============================================================================
function r = int32(obj)
r = obj.int32();
if ismethod(obj, 'int32')
r = obj.int32();
else
error([_('Wrong value for #2 argument.'), ' ', 'int32']);
end
end
%=============================================================================
6 changes: 5 additions & 1 deletion modules/python_engine/functions/@py/int64.m
Expand Up @@ -8,6 +8,10 @@
% LICENCE_BLOCK_END
%=============================================================================
function r = int64(obj)
r = obj.int64();
if ismethod(obj, 'int64')
r = obj.int64();
else
error([_('Wrong value for #2 argument.'), ' ', 'int64']);
end
end
%=============================================================================
6 changes: 5 additions & 1 deletion modules/python_engine/functions/@py/int8.m
Expand Up @@ -8,6 +8,10 @@
% LICENCE_BLOCK_END
%=============================================================================
function r = int8(obj)
r = obj.int8();
if ismethod(obj, 'int8')
r = obj.int8();
else
error([_('Wrong value for #2 argument.'), ' ', 'int8']);
end
end
%=============================================================================
6 changes: 5 additions & 1 deletion modules/python_engine/functions/@py/logical.m
Expand Up @@ -8,6 +8,10 @@
% LICENCE_BLOCK_END
%=============================================================================
function r = logical(obj)
r = obj.logical();
if ismethod(obj, 'logical')
r = obj.logical();
else
error([_('Wrong value for #2 argument.'), ' ', 'logical']);
end
end
%=============================================================================
6 changes: 5 additions & 1 deletion modules/python_engine/functions/@py/single.m
Expand Up @@ -8,6 +8,10 @@
% LICENCE_BLOCK_END
%=============================================================================
function r = single(obj)
r = obj.single();
if ismethod(obj, 'single')
r = obj.single();
else
error([_('Wrong value for #2 argument.'), ' ', 'single']);
end
end
%=============================================================================
6 changes: 5 additions & 1 deletion modules/python_engine/functions/@py/string.m
Expand Up @@ -8,6 +8,10 @@
% LICENCE_BLOCK_END
%=============================================================================
function r = string(obj)
r = obj.string();
if ismethod(obj, 'string')
r = obj.string();
else
error([_('Wrong value for #2 argument.'), ' ', 'string']);
end
end
%=============================================================================
6 changes: 5 additions & 1 deletion modules/python_engine/functions/@py/uint16.m
Expand Up @@ -8,6 +8,10 @@
% LICENCE_BLOCK_END
%=============================================================================
function r = uint16(obj)
r = obj.uint16();
if ismethod(obj, 'uint16')
r = obj.uint16();
else
error([_('Wrong value for #2 argument.'), ' ', 'uint16']);
end
end
%=============================================================================
6 changes: 5 additions & 1 deletion modules/python_engine/functions/@py/uint32.m
Expand Up @@ -8,6 +8,10 @@
% LICENCE_BLOCK_END
%=============================================================================
function r = uint32(obj)
r = obj.uint32();
if ismethod(obj, 'uint32')
r = obj.uint32();
else
error([_('Wrong value for #2 argument.'), ' ', 'uint32']);
end
end
%=============================================================================
6 changes: 5 additions & 1 deletion modules/python_engine/functions/@py/uint64.m
Expand Up @@ -8,6 +8,10 @@
% LICENCE_BLOCK_END
%=============================================================================
function r = uint64(obj)
r = obj.uint64();
if ismethod(obj, 'uint64')
r = obj.uint64();
else
error([_('Wrong value for #2 argument.'), ' ', 'uint64']);
end
end
%=============================================================================
6 changes: 5 additions & 1 deletion modules/python_engine/functions/@py/uint8.m
Expand Up @@ -8,6 +8,10 @@
% LICENCE_BLOCK_END
%=============================================================================
function r = uint8(obj)
r = obj.uint8();
if ismethod(obj, 'uint8')
r = obj.uint8();
else
error([_('Wrong value for #2 argument.'), ' ', 'uint8']);
end
end
%=============================================================================
3 changes: 3 additions & 0 deletions modules/python_engine/help/en_US/xml/pyrun.xml
Expand Up @@ -87,6 +87,9 @@ r = pyrun('d = a + c', 'd')]]>
<see_also_item>
<link linkend="${python_engine}pyenv">pyenv</link>
</see_also_item>
<see_also_item>
<link linkend="${python_engine}python_types">Python types supported</link>
</see_also_item>
</see_also>

<history>
Expand Down
4 changes: 4 additions & 0 deletions modules/python_engine/help/en_US/xml/python_types.xml
Expand Up @@ -55,6 +55,10 @@
<td>py.bytes</td><td
>double, single, int8, uint8, int16, uint16, int32, uint32, int64, uint64, logical</td>
</tr>
<tr>
<td>py.bytearray</td><td
>double, single, int8, uint8, int16, uint16, int32, uint32, int64, uint64, logical</td>
</tr>
<tr>
<td>py.array.array</td><td
>double, single, int8, uint8, int16, uint16, int32, uint32, int64, uint64</td>
Expand Down
28 changes: 27 additions & 1 deletion modules/python_engine/src/cpp/PythonLibraryWrapper.cpp
Expand Up @@ -17,6 +17,8 @@ PyTypeObject* PyBool_TypePtr = nullptr;
PyTypeObject* PyComplex_TypePtr = nullptr;
PyTypeObject* PyFloat_TypePtr = nullptr;
PyTypeObject* PyLong_TypePtr = nullptr;
PyTypeObject* PyBytes_TypePtr = nullptr;
PyTypeObject* PyByteArray_TypePtr = nullptr;
PyTypeObject* PyMemoryView_TypePtr = nullptr;
PyTypeObject* PyUnicode_TypePtr = nullptr;
PyTypeObject* PyDict_TypePtr = nullptr;
Expand Down Expand Up @@ -100,6 +102,8 @@ using PROC_PyEval_GetBuiltins = PyObject* (*)(void);
using PROC_PyDict_GetItemString = PyObject* (*)(PyObject* dp, const char* key);
using PROC_PyDict_SetItemString = int (*)(PyObject* dp, const char* key, PyObject* item);
using PROC_PyDict_Keys = PyObject* (*)(PyObject* mp);
using PROC_PyBytes_AsStringAndSize = int (*)(PyObject* obj, char** s, Py_ssize_t* len);
using PROC_PyByteArray_AsString = char* (*)(PyObject* obj);
//=============================================================================
static std::unordered_map<std::string, void*> pythonSymbols;
//=============================================================================
Expand Down Expand Up @@ -127,15 +131,20 @@ loadPythonSymbols()
LOAD_PYTHON_SYMBOL(PyComplex_Type);
LOAD_PYTHON_SYMBOL(PyFloat_Type);
LOAD_PYTHON_SYMBOL(PyLong_Type);
LOAD_PYTHON_SYMBOL(PyBytes_Type);
LOAD_PYTHON_SYMBOL(PyByteArray_Type);
LOAD_PYTHON_SYMBOL(PyMemoryView_Type);
LOAD_PYTHON_SYMBOL(PyUnicode_Type);
LOAD_PYTHON_SYMBOL(PyDict_Type);
LOAD_PYTHON_SYMBOL(_Py_NoneStruct);

//=============================================================================
PyBool_TypePtr = reinterpret_cast<PyTypeObject*>(pythonSymbols["PyBool_Type"]);
PyComplex_TypePtr = reinterpret_cast<PyTypeObject*>(pythonSymbols["PyComplex_Type"]);
PyFloat_TypePtr = reinterpret_cast<PyTypeObject*>(pythonSymbols["PyFloat_Type"]);
PyLong_TypePtr = reinterpret_cast<PyTypeObject*>(pythonSymbols["PyLong_Type"]);
PyBytes_TypePtr = reinterpret_cast<PyTypeObject*>(pythonSymbols["PyBytes_Type"]);
PyByteArray_TypePtr = reinterpret_cast<PyTypeObject*>(pythonSymbols["PyByteArray_Type"]);

PyMemoryView_TypePtr = reinterpret_cast<PyTypeObject*>(pythonSymbols["PyMemoryView_Type"]);
PyUnicode_TypePtr = reinterpret_cast<PyTypeObject*>(pythonSymbols["PyUnicode_Type"]);
PyDict_TypePtr = reinterpret_cast<PyTypeObject*>(pythonSymbols["PyDict_Type"]);
Expand Down Expand Up @@ -206,6 +215,8 @@ loadPythonSymbols()
LOAD_PYTHON_SYMBOL(PyDict_GetItemString);
LOAD_PYTHON_SYMBOL(PyDict_SetItemString);
LOAD_PYTHON_SYMBOL(PyDict_Keys);
LOAD_PYTHON_SYMBOL(PyBytes_AsStringAndSize);
LOAD_PYTHON_SYMBOL(PyByteArray_AsString);

return true;
}
Expand Down Expand Up @@ -236,6 +247,8 @@ unloadPythonLibrary()
PyComplex_TypePtr = nullptr;
PyFloat_TypePtr = nullptr;
PyLong_TypePtr = nullptr;
PyBytes_TypePtr = nullptr;
PyByteArray_TypePtr = nullptr;
PyMemoryView_TypePtr = nullptr;
PyUnicode_TypePtr = nullptr;
_Py_NoneStructPtr = nullptr;
Expand Down Expand Up @@ -688,3 +701,16 @@ NLSPyDict_Keys(PyObject* mp)
return reinterpret_cast<PROC_PyDict_Keys>(pythonSymbols["PyDict_Keys"])(mp);
}
//=============================================================================
int
NLSPyBytes_AsStringAndSize(PyObject* obj, char** s, Py_ssize_t* len)
{
return reinterpret_cast<PROC_PyBytes_AsStringAndSize>(pythonSymbols["PyBytes_AsStringAndSize"])(
obj, s, len);
}
//=============================================================================
char*
NLSPyByteArray_AsString(PyObject* obj)
{
return reinterpret_cast<PROC_PyByteArray_AsString>(pythonSymbols["PyByteArray_AsString"])(obj);
}
//=============================================================================
10 changes: 10 additions & 0 deletions modules/python_engine/src/cpp/PythonLibraryWrapper.hpp
Expand Up @@ -17,6 +17,8 @@ extern PyTypeObject* PyBool_TypePtr;
extern PyTypeObject* PyComplex_TypePtr;
extern PyTypeObject* PyFloat_TypePtr;
extern PyTypeObject* PyLong_TypePtr;
extern PyTypeObject* PyBytes_TypePtr;
extern PyTypeObject* PyByteArray_TypePtr;
extern PyTypeObject* PyMemoryView_TypePtr;
extern PyTypeObject* PyUnicode_TypePtr;
extern PyTypeObject* PyDict_TypePtr;
Expand All @@ -27,6 +29,8 @@ extern PyObject* _Py_NoneStructPtr;
#define PyComplex_Type *PyComplex_TypePtr
#define PyFloat_Type *PyFloat_TypePtr
#define PyLong_Type *PyLong_TypePtr
#define PyBytes_Type *PyBytes_TypePtr
#define PyByteArray_Type *PyByteArray_TypePtr
#define PyMemoryView_Type *PyMemoryView_TypePtr
#define PyUnicode_Type *PyUnicode_TypePtr
#define PyDict_Type *PyDict_TypePtr
Expand Down Expand Up @@ -251,3 +255,9 @@ NLSPyDict_SetItemString(PyObject* dp, const char* key, PyObject* item);
PyObject*
NLSPyDict_Keys(PyObject* mp);
//=============================================================================
int
NLSPyBytes_AsStringAndSize(PyObject* obj, char** s, Py_ssize_t* len);
//=============================================================================
char*
NLSPyByteArray_AsString(PyObject* obj);
//=============================================================================

0 comments on commit 4a618b6

Please sign in to comment.