Commit 0d272a51 authored by da-woods's avatar da-woods Committed by GitHub

Allow tempita utility code to be @required (GH-3375)

"@subsitute: tempita" tag ensures that they are loaded in tempita utility code class
parent 61d606ac
...@@ -237,6 +237,15 @@ class UtilityCodeBase(object): ...@@ -237,6 +237,15 @@ class UtilityCodeBase(object):
[definitions] [definitions]
##### MyUtility #####
#@subsitute: tempita
[requires tempita substitution
- context can't be specified here though so only
tempita utility that requires no external context
will benefit from this tag
- only necessary when @required from non-tempita code]
for prototypes and implementation respectively. For non-python or for prototypes and implementation respectively. For non-python or
-cython files backslashes should be used instead. 5 to 30 comment -cython files backslashes should be used instead. 5 to 30 comment
characters may be used on either side. characters may be used on either side.
...@@ -255,8 +264,7 @@ class UtilityCodeBase(object): ...@@ -255,8 +264,7 @@ class UtilityCodeBase(object):
return return
code = '\n'.join(lines) code = '\n'.join(lines)
if tags and 'substitute' in tags and tags['substitute'] == set(['naming']): if tags and 'substitute' in tags and 'naming' in tags['substitute']:
del tags['substitute']
try: try:
code = Template(code).substitute(vars(Naming)) code = Template(code).substitute(vars(Naming))
except (KeyError, ValueError) as e: except (KeyError, ValueError) as e:
...@@ -347,6 +355,7 @@ class UtilityCodeBase(object): ...@@ -347,6 +355,7 @@ class UtilityCodeBase(object):
Load utility code from a file specified by from_file (relative to Load utility code from a file specified by from_file (relative to
Cython/Utility) and name util_code_name. Cython/Utility) and name util_code_name.
""" """
if '::' in util_code_name: if '::' in util_code_name:
from_file, util_code_name = util_code_name.rsplit('::', 1) from_file, util_code_name = util_code_name.rsplit('::', 1)
assert from_file assert from_file
...@@ -354,6 +363,9 @@ class UtilityCodeBase(object): ...@@ -354,6 +363,9 @@ class UtilityCodeBase(object):
proto, impl, tags = utilities[util_code_name] proto, impl, tags = utilities[util_code_name]
if tags: if tags:
if "substitute" in tags and "tempita" in tags["substitute"]:
if not issubclass(cls, TempitaUtilityCode):
return TempitaUtilityCode.load(util_code_name, from_file, **kwargs)
orig_kwargs = kwargs.copy() orig_kwargs = kwargs.copy()
for name, values in tags.items(): for name, values in tags.items():
if name in kwargs: if name in kwargs:
...@@ -367,6 +379,12 @@ class UtilityCodeBase(object): ...@@ -367,6 +379,12 @@ class UtilityCodeBase(object):
# dependencies are rarely unique, so use load_cached() when we can # dependencies are rarely unique, so use load_cached() when we can
values = [cls.load_cached(dep, from_file) values = [cls.load_cached(dep, from_file)
for dep in sorted(values)] for dep in sorted(values)]
elif name == 'substitute':
# don't want to pass "naming" or "tempita" to the constructor
# since these will have been handled
values = values - set(['naming', 'tempita'])
if not values:
continue
elif not values: elif not values:
values = None values = None
elif len(values) == 1: elif len(values) == 1:
......
...@@ -356,6 +356,7 @@ static PyObject *__Pyx_PyDict_GetItem(PyObject *d, PyObject* key) { ...@@ -356,6 +356,7 @@ static PyObject *__Pyx_PyDict_GetItem(PyObject *d, PyObject* key) {
#endif #endif
/////////////// GetItemInt.proto /////////////// /////////////// GetItemInt.proto ///////////////
//@substitute: tempita
#define __Pyx_GetItemInt(o, i, type, is_signed, to_py_func, is_list, wraparound, boundscheck) \ #define __Pyx_GetItemInt(o, i, type, is_signed, to_py_func, is_list, wraparound, boundscheck) \
(__Pyx_fits_Py_ssize_t(i, type, is_signed) ? \ (__Pyx_fits_Py_ssize_t(i, type, is_signed) ? \
...@@ -378,6 +379,7 @@ static CYTHON_INLINE PyObject *__Pyx_GetItemInt_Fast(PyObject *o, Py_ssize_t i, ...@@ -378,6 +379,7 @@ static CYTHON_INLINE PyObject *__Pyx_GetItemInt_Fast(PyObject *o, Py_ssize_t i,
int is_list, int wraparound, int boundscheck); int is_list, int wraparound, int boundscheck);
/////////////// GetItemInt /////////////// /////////////// GetItemInt ///////////////
//@substitute: tempita
static PyObject *__Pyx_GetItemInt_Generic(PyObject *o, PyObject* j) { static PyObject *__Pyx_GetItemInt_Generic(PyObject *o, PyObject* j) {
PyObject *r; PyObject *r;
...@@ -796,6 +798,7 @@ static CYTHON_INLINE PyObject* __Pyx_PyTuple_GetSlice(PyObject* src, Py_ssize_t ...@@ -796,6 +798,7 @@ static CYTHON_INLINE PyObject* __Pyx_PyTuple_GetSlice(PyObject* src, Py_ssize_t
/////////////// SliceTupleAndList /////////////// /////////////// SliceTupleAndList ///////////////
//@requires: TupleAndListFromArray //@requires: TupleAndListFromArray
//@substitute: tempita
#if CYTHON_COMPILING_IN_CPYTHON #if CYTHON_COMPILING_IN_CPYTHON
static CYTHON_INLINE void __Pyx_crop_slice(Py_ssize_t* _start, Py_ssize_t* _stop, Py_ssize_t* _length) { static CYTHON_INLINE void __Pyx_crop_slice(Py_ssize_t* _start, Py_ssize_t* _stop, Py_ssize_t* _length) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment