Skip to content

Commit 96560a8

Browse files
committed
Merge pull request #169 from sparkprime/python
Plumb tla (and ext code) into Python
2 parents 57c2689 + 938f9bc commit 96560a8

File tree

5 files changed

+90
-24
lines changed

5 files changed

+90
-24
lines changed

.travis.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@ before_install:
66
- echo $LANG
77
- echo $LC_ALL
88
before_script:
9-
script: make test
9+
script: |
10+
make test
11+
python setup.py build
12+
sudo python setup.py install
13+
( cd python ; ./run_tests.sh )
1014
branches:
1115
only:
1216
- master

python/_jsonnet.c

Lines changed: 48 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -99,14 +99,14 @@ static PyObject *handle_result(struct JsonnetVm *vm, char *out, int error)
9999
}
100100
}
101101

102-
int handle_ext_vars(struct JsonnetVm *vm, PyObject *ext_vars)
102+
int handle_vars(struct JsonnetVm *vm, PyObject *map, int code, int tla)
103103
{
104-
if (ext_vars == NULL) return 1;
104+
if (map == NULL) return 1;
105105

106106
PyObject *key, *val;
107107
Py_ssize_t pos = 0;
108108

109-
while (PyDict_Next(ext_vars, &pos, &key, &val)) {
109+
while (PyDict_Next(map, &pos, &key, &val)) {
110110
const char *key_ = PyString_AsString(key);
111111
if (key_ == NULL) {
112112
jsonnet_destroy(vm);
@@ -117,7 +117,15 @@ int handle_ext_vars(struct JsonnetVm *vm, PyObject *ext_vars)
117117
jsonnet_destroy(vm);
118118
return 0;
119119
}
120-
jsonnet_ext_var(vm, key_, val_);
120+
if (!tla && !code) {
121+
jsonnet_ext_var(vm, key_, val_);
122+
} else if (!tla && code) {
123+
jsonnet_ext_code(vm, key_, val_);
124+
} else if (tla && !code) {
125+
jsonnet_tla_var(vm, key_, val_);
126+
} else {
127+
jsonnet_tla_code(vm, key_, val_);
128+
}
121129
}
122130
return 1;
123131
}
@@ -145,16 +153,24 @@ static PyObject* evaluate_file(PyObject* self, PyObject* args, PyObject *keywds)
145153
unsigned max_stack = 500, gc_min_objects = 1000, max_trace = 20;
146154
double gc_growth_trigger = 2;
147155
int error;
148-
PyObject *ext_vars = NULL, *import_callback = NULL;
156+
PyObject *ext_vars = NULL, *ext_codes = NULL;
157+
PyObject *tla_vars = NULL, *tla_codes = NULL;
158+
PyObject *import_callback = NULL;
149159
struct JsonnetVm *vm;
150-
static char *kwlist[] = {"filename", "max_stack", "gc_min_objects", "gc_growth_trigger", "ext_vars", "max_trace", "import_callback", NULL};
160+
static char *kwlist[] = {
161+
"filename",
162+
"max_stack", "gc_min_objects", "gc_growth_trigger", "ext_vars",
163+
"ext_codes", "tla_vars", "tla_codes", "max_trace", "import_callback",
164+
NULL
165+
};
151166

152167
(void) self;
153168

154-
if (!PyArg_ParseTupleAndKeywords(args, keywds, "s|IIdOIO", kwlist,
155-
&filename,
156-
&max_stack, &gc_min_objects, &gc_growth_trigger, &ext_vars,
157-
&max_trace, &import_callback)) {
169+
if (!PyArg_ParseTupleAndKeywords(
170+
args, keywds, "s|IIdOOOOIO", kwlist,
171+
&filename,
172+
&max_stack, &gc_min_objects, &gc_growth_trigger, &ext_vars,
173+
&ext_codes, &tla_vars, &tla_codes, &max_trace, &import_callback)) {
158174
return NULL;
159175
}
160176

@@ -163,9 +179,10 @@ static PyObject* evaluate_file(PyObject* self, PyObject* args, PyObject *keywds)
163179
jsonnet_gc_min_objects(vm, gc_min_objects);
164180
jsonnet_max_trace(vm, max_trace);
165181
jsonnet_gc_growth_trigger(vm, gc_growth_trigger);
166-
if (!handle_ext_vars(vm, ext_vars)) {
167-
return NULL;
168-
}
182+
if (!handle_vars(vm, ext_vars, 0, 0)) return NULL;
183+
if (!handle_vars(vm, ext_codes, 1, 0)) return NULL;
184+
if (!handle_vars(vm, tla_vars, 0, 1)) return NULL;
185+
if (!handle_vars(vm, tla_codes, 1, 1)) return NULL;
169186
struct ImportCtx ctx = { vm, import_callback };
170187
if (!handle_import_callback(&ctx, import_callback)) {
171188
return NULL;
@@ -182,16 +199,24 @@ static PyObject* evaluate_snippet(PyObject* self, PyObject* args, PyObject *keyw
182199
unsigned max_stack = 500, gc_min_objects = 1000, max_trace = 20;
183200
double gc_growth_trigger = 2;
184201
int error;
185-
PyObject *ext_vars = NULL, *import_callback = NULL;
202+
PyObject *ext_vars = NULL, *ext_codes = NULL;
203+
PyObject *tla_vars = NULL, *tla_codes = NULL;
204+
PyObject *import_callback = NULL;
186205
struct JsonnetVm *vm;
187-
static char *kwlist[] = {"filename", "src", "max_stack", "gc_min_objects", "gc_growth_trigger", "ext_vars", "max_trace", "import_callback", NULL};
206+
static char *kwlist[] = {
207+
"filename", "src",
208+
"max_stack", "gc_min_objects", "gc_growth_trigger", "ext_vars",
209+
"ext_codes", "tla_vars", "tla_codes", "max_trace", "import_callback",
210+
NULL
211+
};
188212

189213
(void) self;
190214

191-
if (!PyArg_ParseTupleAndKeywords(args, keywds, "ss|IIdOIO", kwlist,
192-
&filename, &src,
193-
&max_stack, &gc_min_objects, &gc_growth_trigger, &ext_vars,
194-
&max_trace, &import_callback)) {
215+
if (!PyArg_ParseTupleAndKeywords(
216+
args, keywds, "ss|IIdOOOOIO", kwlist,
217+
&filename, &src,
218+
&max_stack, &gc_min_objects, &gc_growth_trigger, &ext_vars,
219+
&ext_codes, &tla_vars, &tla_codes, &max_trace, &import_callback)) {
195220
return NULL;
196221
}
197222

@@ -200,9 +225,10 @@ static PyObject* evaluate_snippet(PyObject* self, PyObject* args, PyObject *keyw
200225
jsonnet_gc_min_objects(vm, gc_min_objects);
201226
jsonnet_max_trace(vm, max_trace);
202227
jsonnet_gc_growth_trigger(vm, gc_growth_trigger);
203-
if (!handle_ext_vars(vm, ext_vars)) {
204-
return NULL;
205-
}
228+
if (!handle_vars(vm, ext_vars, 0, 0)) return NULL;
229+
if (!handle_vars(vm, ext_codes, 1, 0)) return NULL;
230+
if (!handle_vars(vm, tla_vars, 0, 1)) return NULL;
231+
if (!handle_vars(vm, tla_codes, 1, 1)) return NULL;
206232
struct ImportCtx ctx = { vm, import_callback };
207233
if (!handle_import_callback(&ctx, import_callback)) {
208234
return NULL;

python/run_tests.sh

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#!/bin/bash
2+
3+
TEST_SNIPPET="$(cat test.jsonnet)"
4+
5+
echo "Python testing Jsonnet snippet..."
6+
OUTPUT="$(python jsonnet_test_snippet.py "${TEST_SNIPPET}")"
7+
if [ "$?" != "0" ] ; then
8+
echo "Jsonnet execution failed:"
9+
echo "$OUTPUT"
10+
exit 1
11+
fi
12+
if [ "$OUTPUT" != "true" ] ; then
13+
echo "Got bad output:"
14+
echo "$OUTPUT"
15+
exit 1
16+
fi
17+
18+
echo "Python testing Jsonnet file..."
19+
OUTPUT="$(python jsonnet_test_file.py "test.jsonnet")"
20+
if [ "$?" != "0" ] ; then
21+
echo "Jsonnet execution failed:"
22+
echo "$OUTPUT"
23+
exit 1
24+
fi
25+
if [ "$OUTPUT" != "true" ] ; then
26+
echo "Got bad output:"
27+
echo "$OUTPUT"
28+
exit 1
29+
fi
30+
31+
echo "Python test passed."

python/test.jsonnet

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
std.assertEqual(({ x: 1, y: self.x } { x: 2 }).y, 2)
2+

setup.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,10 @@ def get_version():
3939
with open(os.path.join(DIR, 'include/libjsonnet.h')) as f:
4040
for line in f:
4141
if '#define' in line and 'LIB_JSONNET_VERSION' in line:
42-
return line.partition('LIB_JSONNET_VERSION')[2].strip('\n "')
42+
v_code = line.partition('LIB_JSONNET_VERSION')[2].strip('\n "')
43+
if v_code[0] == "v":
44+
v_code = v_code[1:]
45+
return v_code
4346

4447
class BuildJsonnetExt(BuildExt):
4548
def run(self):

0 commit comments

Comments
 (0)