Skip to content

Commit 35d1794

Browse files
committed
Add BaseContext interface
1 parent 37bf546 commit 35d1794

File tree

2 files changed

+107
-3
lines changed

2 files changed

+107
-3
lines changed

sass.c

Lines changed: 98 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,98 @@ static PyTypeObject sass_OptionsType = {
238238
.tp_dictoffset = 0,
239239
.tp_init = (initproc) sass_Options_init,
240240
.tp_alloc = 0,
241-
.tp_new = 0
241+
.tp_new = PyType_GenericNew
242+
};
243+
244+
typedef struct {
245+
PyObject_HEAD
246+
void *context;
247+
} sass_BaseContextObject;
248+
249+
static int
250+
sass_BaseContext_init(sass_BaseContextObject *self, PyObject *args,
251+
PyObject *kwds)
252+
{
253+
PyErr_SetString(PyExc_TypeError,
254+
"the sass.BaseContext type cannot be instantiated "
255+
"because it's an abstract interface. use one of "
256+
"sass.Context, sass.FileContext, or sass.FolderContext "
257+
"instead");
258+
return -1;
259+
}
260+
261+
static PyObject *
262+
sass_BaseContext_compile(sass_BaseContextObject *self) {
263+
PyErr_SetString(PyExc_NotImplementedError,
264+
"the sass.BaseContext type is an abstract interface. "
265+
"use one of sass.Context, sass.FileContext, or sass."
266+
"FolderContext instead");
267+
return NULL;
268+
}
269+
270+
static PyObject *
271+
sass_BaseContext_get_options(sass_BaseContextObject *self, void *closure)
272+
{
273+
PyErr_SetString(PyExc_NotImplementedError,
274+
"the sass.BaseContext type is an abstract interface. "
275+
"use one of sass.Context, sass.FileContext, or sass."
276+
"FolderContext instead");
277+
return NULL;
278+
}
279+
280+
static PyMethodDef sass_BaseContext_methods[] = {
281+
{"compile", (PyCFunction) sass_BaseContext_compile, METH_NOARGS,
282+
"Compiles SASS source."},
283+
{NULL, NULL, 0, NULL}
284+
};
285+
286+
static PyGetSetDef sass_BaseContext_getset[] = {
287+
{"options", (getter) sass_BaseContext_get_options, NULL,
288+
"The compilation options for the context."},
289+
{NULL}
290+
};
291+
292+
static PyTypeObject sass_BaseContextType = {
293+
PyObject_HEAD_INIT(NULL)
294+
.ob_size = 0,
295+
.tp_name = "sass.BaseContext",
296+
.tp_basicsize = sizeof(sass_BaseContextObject),
297+
.tp_itemsize = 0,
298+
.tp_dealloc = 0,
299+
.tp_print = 0,
300+
.tp_getattr = 0,
301+
.tp_setattr = 0,
302+
.tp_compare = 0,
303+
.tp_repr = 0,
304+
.tp_as_number = 0,
305+
.tp_as_sequence = 0,
306+
.tp_as_mapping = 0,
307+
.tp_hash = 0,
308+
.tp_call = 0,
309+
.tp_str = 0,
310+
.tp_getattro = 0,
311+
.tp_setattro = 0,
312+
.tp_as_buffer = 0,
313+
.tp_flags = Py_TPFLAGS_DEFAULT,
314+
.tp_doc = "The common interface between sass.Context, sass.FileContext, "
315+
"and sass.FolderContext.",
316+
.tp_traverse = 0,
317+
.tp_clear = 0,
318+
.tp_richcompare = 0,
319+
.tp_weaklistoffset = 0,
320+
.tp_iter = 0,
321+
.tp_iternext = 0,
322+
.tp_methods = sass_BaseContext_methods,
323+
.tp_members = 0,
324+
.tp_getset = sass_BaseContext_getset,
325+
.tp_base = 0,
326+
.tp_dict = 0,
327+
.tp_descr_get = 0,
328+
.tp_descr_set = 0,
329+
.tp_dictoffset = 0,
330+
.tp_init = (initproc) sass_BaseContext_init,
331+
.tp_alloc = 0,
332+
.tp_new = PyType_GenericNew
242333
};
243334

244335
static PyMethodDef sass_methods[] = {
@@ -250,10 +341,12 @@ initsass()
250341
{
251342
PyObject *module;
252343

253-
sass_OptionsType.tp_new = PyType_GenericNew;
254344
if (PyType_Ready(&sass_OptionsType) < 0) {
255345
return;
256346
}
347+
if (PyType_Ready(&sass_BaseContextType) < 0) {
348+
return;
349+
}
257350

258351
module = Py_InitModule3("sass", sass_methods,
259352
"The thin binding of libsass for Python.");
@@ -262,4 +355,7 @@ initsass()
262355
}
263356
Py_INCREF(&sass_OptionsType);
264357
PyModule_AddObject(module, "Options", (PyObject *) &sass_OptionsType);
358+
Py_INCREF(&sass_BaseContextType);
359+
PyModule_AddObject(module, "BaseContext",
360+
(PyObject *) &sass_BaseContextType);
265361
}

sasstests.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
from attest import Tests, raises
55

6-
from sass import Options
6+
from sass import BaseContext, Options
77

88

99
suite = Tests()
@@ -48,3 +48,11 @@ def options_image_path():
4848
Options('nested', include_paths='a:b', image_path=123)
4949
with raises(TypeError):
5050
Options('nested', include_paths='a:b', image_path=['a/b', 'c/d'])
51+
52+
53+
@suite.test
54+
def base_context_init():
55+
with raises(TypeError):
56+
BaseContext()
57+
assert hasattr(BaseContext, 'options')
58+
assert callable(BaseContext.compile)

0 commit comments

Comments
 (0)