Commit 8a87c682 authored by Marius Wachtler's avatar Marius Wachtler

replace our time module with the cpython implementation

parent e6e72e86
......@@ -290,7 +290,7 @@ SRCS := $(MAIN_SRCS) $(STDLIB_SRCS)
STDLIB_OBJS := stdlib.bc.o stdlib.stripped.bc.o
STDLIB_RELEASE_OBJS := stdlib.release.bc.o
STDMODULE_SRCS := errnomodule.c shamodule.c sha256module.c sha512module.c _math.c mathmodule.c md5.c md5module.c _randommodule.c _sre.c operator.c binascii.c pwdmodule.c posixmodule.c _struct.c datetimemodule.c _functoolsmodule.c _collectionsmodule.c itertoolsmodule.c resource.c signalmodule.c selectmodule.c fcntlmodule.c $(EXTRA_STDMODULE_SRCS)
STDMODULE_SRCS := errnomodule.c shamodule.c sha256module.c sha512module.c _math.c mathmodule.c md5.c md5module.c _randommodule.c _sre.c operator.c binascii.c pwdmodule.c posixmodule.c _struct.c datetimemodule.c _functoolsmodule.c _collectionsmodule.c itertoolsmodule.c resource.c signalmodule.c selectmodule.c fcntlmodule.c timemodule.c $(EXTRA_STDMODULE_SRCS)
STDOBJECT_SRCS := structseq.c capsule.c stringobject.c $(EXTRA_STDOBJECT_SRCS)
STDPYTHON_SRCS := pyctype.c getargs.c formatter_string.c pystrtod.c dtoa.c $(EXTRA_STDPYTHON_SRCS)
FROM_CPYTHON_SRCS := $(addprefix from_cpython/Modules/,$(STDMODULE_SRCS)) $(addprefix from_cpython/Objects/,$(STDOBJECT_SRCS)) $(addprefix from_cpython/Python/,$(STDPYTHON_SRCS))
......
......@@ -15,7 +15,7 @@ endforeach(STDLIB_FILE)
add_custom_target(copy_stdlib ALL DEPENDS ${STDLIB_TARGETS})
# compile specified files in from_cpython/Modules
file(GLOB_RECURSE STDMODULE_SRCS Modules errnomodule.c shamodule.c sha256module.c sha512module.c _math.c mathmodule.c md5.c md5module.c _randommodule.c _sre.c operator.c binascii.c pwdmodule.c posixmodule.c _struct.c datetimemodule.c _functoolsmodule.c _collectionsmodule.c itertoolsmodule.c resource.c signalmodule.c selectmodule.c fcntlmodule.c)
file(GLOB_RECURSE STDMODULE_SRCS Modules errnomodule.c shamodule.c sha256module.c sha512module.c _math.c mathmodule.c md5.c md5module.c _randommodule.c _sre.c operator.c binascii.c pwdmodule.c posixmodule.c _struct.c datetimemodule.c _functoolsmodule.c _collectionsmodule.c itertoolsmodule.c resource.c signalmodule.c selectmodule.c fcntlmodule.c timemodule.c)
# compile specified files in from_cpython/Objects
file(GLOB_RECURSE STDOBJECT_SRCS Objects structseq.c capsule.c stringobject.c)
......
......@@ -62,11 +62,13 @@
#define HAVE_ALLOCA_H 1
#define HAVE_ASM_TYPES_H 1
#define HAVE_CLOCK 1
#define HAVE_CURSES_H 1
#define HAVE_DIRENT_H 1
#define HAVE_DLFCN_H 1
#define HAVE_ERRNO_H 1
#define HAVE_FCNTL_H 1
#define HAVE_FTIME 1
#define HAVE_GRP_H 1
#define HAVE_INTTYPES_H 1
#define HAVE_LANGINFO_H 1
......@@ -99,7 +101,6 @@
#define HAVE_SYS_STATVFS_H 1
#define HAVE_SYS_STAT_H 1
#define HAVE_SYS_TIMES_H 1
#define HAVE_SYS_TIME_H 1
#define HAVE_SYS_TYPES_H 1
#define HAVE_SYS_UN_H 1
#define HAVE_SYS_UTSNAME_H 1
......
......@@ -292,6 +292,20 @@ typedef ssize_t Py_ssize_t;
#define Py_MEMCPY memcpy
#endif
/********************************************
* WRAPPER FOR <time.h> and/or <sys/time.h> *
********************************************/
#ifdef TIME_WITH_SYS_TIME
#include <sys/time.h>
#include <time.h>
#else /* !TIME_WITH_SYS_TIME */
#ifdef HAVE_SYS_TIME_H
#include <sys/time.h>
#else /* !HAVE_SYS_TIME_H */
#include <time.h>
#endif /* !HAVE_SYS_TIME_H */
#endif /* !TIME_WITH_SYS_TIME */
#endif /* Py_PYPORT_H */
......@@ -63,4 +63,6 @@ bool ENABLE_RUNTIME_ICS = 1 && _GLOBAL_ENABLE;
bool ENABLE_FRAME_INTROSPECTION = 1;
bool BOOLS_AS_I64 = ENABLE_FRAME_INTROSPECTION;
extern "C" int Py_IgnoreEnvironmentFlag = 1;
}
// Copyright (c) 2014-2015 Dropbox, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <cmath>
#include <ctime>
#include <err.h>
#include <sys/time.h>
#include "core/threading.h"
#include "core/types.h"
#include "runtime/objmodel.h"
#include "runtime/types.h"
namespace pyston {
BoxedModule* time_module;
/* Exposed in timefuncs.h. */
extern "C" time_t _PyTime_DoubleToTimet(double x) noexcept {
time_t result;
double diff;
result = (time_t)x;
/* How much info did we lose? time_t may be an integral or
* floating type, and we don't know which. If it's integral,
* we don't know whether C truncates, rounds, returns the floor,
* etc. If we lost a second or more, the C rounding is
* unreasonable, or the input just doesn't fit in a time_t;
* call it an error regardless. Note that the original cast to
* time_t can cause a C error too, but nothing we can do to
* worm around that.
*/
diff = x - (double)result;
if (diff <= -1.0 || diff >= 1.0) {
PyErr_SetString(PyExc_ValueError, "timestamp out of range for platform time_t");
result = (time_t)-1;
}
return result;
}
Box* timeTime() {
struct timeval now;
gettimeofday(&now, NULL);
double t = now.tv_sec + .000001 * now.tv_usec;
return boxFloat(t);
}
Box* timeSleep(Box* arg) {
double secs;
if (isSubclass(arg->cls, int_cls))
secs = static_cast<BoxedInt*>(arg)->n;
else if (arg->cls == float_cls)
secs = static_cast<BoxedFloat*>(arg)->d;
else {
raiseExcHelper(TypeError, "a float is required");
}
double fullsecs;
double nanosecs = modf(secs, &fullsecs);
struct timespec req;
req.tv_sec = (int)(fullsecs + 0.01);
req.tv_nsec = (int)(nanosecs * 1000000000);
{
threading::GLAllowThreadsReadRegion _allow_threads;
int code = nanosleep(&req, NULL);
if (code)
err(1, NULL);
}
return None;
}
void setupTime() {
time_module = createModule("time", "__builtin__");
time_module->giveAttr("time", new BoxedFunction(boxRTFunction((void*)timeTime, BOXED_FLOAT, 0)));
time_module->giveAttr("sleep", new BoxedFunction(boxRTFunction((void*)timeSleep, NONE, 1)));
}
}
......@@ -56,6 +56,7 @@ extern "C" void initresource();
extern "C" void initsignal();
extern "C" void initselect();
extern "C" void initfcntl();
extern "C" void inittime();
namespace pyston {
......@@ -1162,7 +1163,6 @@ void setupRuntime() {
setupSys();
setupBuiltins();
setupTime();
setupThread();
setupGC();
......@@ -1191,6 +1191,7 @@ void setupRuntime() {
initsignal();
initselect();
initfcntl();
inittime();
setupSysEnd();
......
......@@ -68,7 +68,6 @@ void teardownDescr();
void setupSys();
void setupBuiltins();
void setupTime();
void setupThread();
void setupSysEnd();
......
......@@ -3,3 +3,4 @@ print type(time)
time.sleep(0)
time.sleep(False)
time.clock()
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