Skip to content
This repository was archived by the owner on Jan 20, 2025. It is now read-only.

Commit bbd2a1e

Browse files
Added support for default heredocs value when creating a Command in Python, to avoid breaking current Command API.
1 parent 65655bf commit bbd2a1e

File tree

2 files changed

+12
-14
lines changed

2 files changed

+12
-14
lines changed

pylib/support.c

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,18 @@ static PyObject* _setup_module(PyObject* module) {
6969

7070
PyObject* collections = PyImport_ImportModule("collections");
7171

72-
PyDockerfile_Command = PyObject_CallMethod(
73-
collections, "namedtuple", "ss",
74-
"Command", "cmd sub_cmd json original start_line end_line flags value heredocs"
75-
);
72+
// Set up a Command namedtuple object, with empty default for heredocs substructure.
73+
PyObject* defaults = PyTuple_New(1);
74+
PyObject* default_heredoc = PyTuple_New(0);
75+
PyTuple_SetItem(defaults, 0, default_heredoc);
76+
PyObject *args = Py_BuildValue("ss", "Command", "cmd sub_cmd json original start_line end_line flags value heredocs");
77+
PyObject *keywords = PyDict_New();
78+
PyDict_SetItemString(keywords, "defaults", defaults);
79+
PyObject *namedtuple_method = PyObject_GetAttrString(collections, "namedtuple");
80+
PyDockerfile_Command = PyObject_Call(namedtuple_method, args, keywords);
81+
Py_DECREF(args);
82+
Py_DECREF(keywords);
83+
Py_DECREF(namedtuple_method);
7684
PyObject_SetAttrString(
7785
PyDockerfile_Command, "__module__",
7886
PyObject_GetAttrString(module, "__name__")

tests/dockerfile_test.py

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,37 +42,31 @@ def test_parse_string_success():
4242
cmd='FROM', sub_cmd=None, json=False, flags=(),
4343
value=('ubuntu:xenial',),
4444
start_line=1, end_line=1, original='FROM ubuntu:xenial',
45-
heredocs=(),
4645
),
4746
dockerfile.Command(
4847
cmd='RUN', sub_cmd=None, json=False, flags=(),
4948
value=('echo hi > /etc/hi.conf',),
5049
start_line=2, end_line=2, original='RUN echo hi > /etc/hi.conf',
51-
heredocs=(),
5250
),
5351
dockerfile.Command(
5452
cmd='CMD', sub_cmd=None, json=True, flags=(), value=('echo',),
5553
start_line=3, end_line=3, original='CMD ["echo"]',
56-
heredocs=(),
5754
),
5855
dockerfile.Command(
5956
cmd='HEALTHCHECK', sub_cmd=None, json=False,
6057
flags=('--retries=5',), value=('CMD', 'echo hi'),
6158
start_line=4, end_line=4,
6259
original='HEALTHCHECK --retries=5 CMD echo hi',
63-
heredocs=(),
6460
),
6561
dockerfile.Command(
6662
cmd='ONBUILD', sub_cmd='ADD', json=False, flags=(),
6763
value=('foo', 'bar'),
6864
start_line=5, end_line=5, original='ONBUILD ADD foo bar',
69-
heredocs=(),
7065
),
7166
dockerfile.Command(
7267
cmd='ONBUILD', sub_cmd='RUN', json=True, flags=(),
7368
value=('cat', 'bar'),
7469
start_line=6, end_line=6, original='ONBUILD RUN ["cat", "bar"]',
75-
heredocs=(),
7670
),
7771
)
7872

@@ -86,12 +80,10 @@ def test_parse_string_text():
8680
dockerfile.Command(
8781
cmd='FROM', sub_cmd=None, json=False, value=('ubuntu:xenial',),
8882
start_line=1, end_line=1, original='FROM ubuntu:xenial', flags=(),
89-
heredocs=(),
9083
),
9184
dockerfile.Command(
9285
cmd='CMD', sub_cmd=None, json=True, value=('echo', '☃'),
9386
start_line=2, end_line=2, original='CMD ["echo", "☃"]', flags=(),
94-
heredocs=(),
9587
),
9688
)
9789

@@ -103,12 +95,10 @@ def test_parse_file_success():
10395
cmd='FROM', sub_cmd=None, json=False, flags=(),
10496
value=('ubuntu:xenial',),
10597
start_line=1, end_line=1, original='FROM ubuntu:xenial',
106-
heredocs=(),
10798
),
10899
dockerfile.Command(
109100
cmd='CMD', sub_cmd=None, json=True, flags=(), value=('echo', 'hi'),
110101
start_line=2, end_line=2, original='CMD ["echo", "hi"]',
111-
heredocs=(),
112102
),
113103
)
114104

0 commit comments

Comments
 (0)