Skip to content

Commit

Permalink
Allow yast python modules to work via either python2 or python3
Browse files Browse the repository at this point in the history
  • Loading branch information
dmulder committed Mar 8, 2018
1 parent 35890fb commit 7efcc7b
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 4 deletions.
3 changes: 3 additions & 0 deletions src/yast-core.i
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
%module ycp
%begin %{
#define SWIG_PYTHON_2_UNICODE
%}

%feature("autodoc", "3");

Expand Down
18 changes: 15 additions & 3 deletions src/yast.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from __future__ import absolute_import, division, print_function, unicode_literals
import ycpbuiltins
from ycp import Symbol, List, String, Integer, Boolean, Float, Code, Map, Byteblock, Path, Void

Expand Down Expand Up @@ -135,7 +136,11 @@ def meta_func_creator(func, lowercase):
def Term(*args):
from ycp import Term as YCPTerm
from ycp import pyval_to_ycp
name = args[0]
from six import binary_type, text_type, PY2
if PY2 and isinstance(args[0], text_type):
name = binary_type(args[0])
else:
name = args[0]
l = None
if len(args) > 1:
l = List()
Expand All @@ -147,18 +152,25 @@ def Term(*args):

def Opt(*args):
from ycp import Term as YCPTerm
from six import binary_type, text_type, PY2
l = List()
for arg in args:
if PY2 and isinstance(arg, text_type):
arg = binary_type(arg)
l.add(Symbol(arg))
return YCPTerm("opt", l)

# Id can take argument other than string
def Id(arg, dont_force_sym = False):
from ycp import pyval_to_ycp
from ycp import Term as YCPTerm
from six import binary_type, text_type, PY2
l = List()
if isinstance(arg, str) and not dont_force_sym:
l.add(Symbol(arg))
if isinstance(arg, text_type) and not dont_force_sym:
if PY2:
l.add(Symbol(binary_type(arg)))
else:
l.add(Symbol(arg))
else:
l.add(pyval_to_ycp(arg))
return YCPTerm("id", l)
Expand Down
1 change: 1 addition & 0 deletions src/ycpbuiltins.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from __future__ import absolute_import, division, print_function, unicode_literals
from ycp import List, String, Integer, Boolean, Float, Value
from ycp import Term as YCPTerm

Expand Down
6 changes: 5 additions & 1 deletion src/ytypes.i
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,14 @@ YCPValue pyval_to_ycp(PyObject *input)
return YCPFloat(PyFloat_AsDouble(input));
if (PyString_Check(input))
return YCPString(PyString_AsString(input));
if (PyUnicode_Check(input)) {
#if PY_MAJOR_VERSION >= 3
if (PyUnicode_Check(input))
return YCPString(_PyUnicode_AsString(input));
#else
PyObject* encoded = PyUnicode_Encode(PyUnicode_AsUnicode(input), PyUnicode_GetSize(input), "ascii", NULL);
return YCPString(PyBytes_AsString(encoded));
#endif
}
if (PyList_Check(input)) {
auto size = PyList_Size(input);
if (size > 0 && PyFunction_Check(PyList_GetItem(input, 0))) {
Expand Down

0 comments on commit 7efcc7b

Please sign in to comment.