Skip to content

Commit

Permalink
Reimplement '%.3f' float formatting in C.
Browse files Browse the repository at this point in the history
This was the only usage of dtoa.c / pystrtod.c.

spec/builtins.test.sh pass again under the OVM build.
  • Loading branch information
Andy Chu committed Oct 13, 2018
1 parent a0a8eb2 commit d8edecd
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 5 deletions.
4 changes: 1 addition & 3 deletions core/cmd_exec.py
Expand Up @@ -1085,9 +1085,7 @@ def _Dispatch(self, node, fork_external):
real = end_t - start_t
user = end_u.ru_utime - start_u.ru_utime
sys_ = end_u.ru_stime - start_u.ru_stime
print('real\t%.3f' % real, file=sys.stderr)
print('user\t%.3f' % user, file=sys.stderr)
print('sys\t%.3f' % sys_, file=sys.stderr)
libc.print_time(real, user, sys_)

else:
raise NotImplementedError(node.__class__.__name__)
Expand Down
21 changes: 19 additions & 2 deletions native/libc.c
Expand Up @@ -338,6 +338,21 @@ func_regex_first_group_match(PyObject *self, PyObject *args) {
return Py_BuildValue("(i,i)", pos + start, pos + end);
}

// We do this in C so we can remove '%f' % 0.1 from the CPython build. That
// involves dtoa.c and pystrod.c, which are thousands of lines of code.
static PyObject *
func_print_time(PyObject *self, PyObject *args) {
double real, user, sys;
int pos;
if (!PyArg_ParseTuple(args, "ddd", &real, &user, &sys)) {
return NULL;
}
fprintf(stderr, "real\t%.3f\n", real);
fprintf(stderr, "user\t%.3f\n", user);
fprintf(stderr, "sys\t%.3f\n", sys);
Py_RETURN_NONE;
}

static PyMethodDef methods[] = {
{"realpath", func_realpath, METH_VARARGS,
"Return the canonical version of a path with symlinks, or None if there is "
Expand All @@ -350,12 +365,14 @@ static PyMethodDef methods[] = {
{"regex_parse", func_regex_parse, METH_VARARGS,
"Compile a regex in ERE syntax, returning whether it is valid"},
{"regex_match", func_regex_match, METH_VARARGS,
"Match regex against a string. Returns a list of matches, None if no match. "
"Raises RuntimeError if the regex is invalid."},
"Match regex against a string. Returns a list of matches, None if no "
"match. Raises RuntimeError if the regex is invalid."},
{"regex_first_group_match", func_regex_first_group_match, METH_VARARGS,
"If the regex matches the string, return the start and end position of the "
"first group. Returns None if there is no match. Raises RuntimeError if "
"the regex is invalid."},
{"print_time", func_print_time, METH_VARARGS,
"Print three floating point values for the 'time' builtin."},
{NULL, NULL},
};

Expand Down
3 changes: 3 additions & 0 deletions native/libc_test.py
Expand Up @@ -134,6 +134,9 @@ def testRealpathFailOnNonexistentDirectory(self):
# Consistent with GNU
self.assertEqual(None, libc.realpath('_tmp/nonexistent/supernonexistent'))

def testPrintTime(self):
libc.print_time(0.1, 0.2, 0.3)


if __name__ == '__main__':
unittest.main()

0 comments on commit d8edecd

Please sign in to comment.