Commit cc87a036 authored by Kevin Modzelewski's avatar Kevin Modzelewski Committed by GitHub

Merge pull request #1251 from Daetalus/new_api_nexedi

Add some helper API to Pyston.
parents 4dd4b5b3 3eb3e5f9
...@@ -969,7 +969,7 @@ bench_exceptions: ...@@ -969,7 +969,7 @@ bench_exceptions:
zsh -c 'ulimit -m $(MAX_MEM_KB); time ./bench_exceptions' zsh -c 'ulimit -m $(MAX_MEM_KB); time ./bench_exceptions'
rm bench_exceptions rm bench_exceptions
TEST_EXT_MODULE_NAMES := basic_test descr_test slots_test TEST_EXT_MODULE_NAMES := basic_test descr_test slots_test type_test api_test
TEST_EXT_MODULE_SRCS := $(TEST_EXT_MODULE_NAMES:%=test/test_extension/%.c) TEST_EXT_MODULE_SRCS := $(TEST_EXT_MODULE_NAMES:%=test/test_extension/%.c)
TEST_EXT_MODULE_OBJS := $(TEST_EXT_MODULE_NAMES:%=test/test_extension/%.pyston.so) TEST_EXT_MODULE_OBJS := $(TEST_EXT_MODULE_NAMES:%=test/test_extension/%.pyston.so)
......
...@@ -74,7 +74,10 @@ PyAPI_DATA(PyTypeObject*) code_cls; ...@@ -74,7 +74,10 @@ PyAPI_DATA(PyTypeObject*) code_cls;
#define PyCode_Type (*code_cls) #define PyCode_Type (*code_cls)
#define PyCode_Check(op) (Py_TYPE(op) == &PyCode_Type) #define PyCode_Check(op) (Py_TYPE(op) == &PyCode_Type)
#define PyCode_GetNumFree(op) (PyTuple_GET_SIZE((op)->co_freevars)) // Pyston change: disable this macro.
// In Pyston the PyCodeObject is just an opaque pointer, get its size
// directly will cause error.
// #define PyCode_GetNumFree(op) (PyTuple_GET_SIZE((op)->co_freevars))
/* Public interface */ /* Public interface */
PyAPI_FUNC(PyCodeObject *) PyCode_New( PyAPI_FUNC(PyCodeObject *) PyCode_New(
...@@ -95,6 +98,7 @@ PyAPI_FUNC(int) PyCode_Addr2Line(PyCodeObject *, int) PYSTON_NOEXCEPT; ...@@ -95,6 +98,7 @@ PyAPI_FUNC(int) PyCode_Addr2Line(PyCodeObject *, int) PYSTON_NOEXCEPT;
PyAPI_FUNC(int) PyCode_GetArgCount(PyCodeObject *) PYSTON_NOEXCEPT; PyAPI_FUNC(int) PyCode_GetArgCount(PyCodeObject *) PYSTON_NOEXCEPT;
PyAPI_FUNC(BORROWED(PyObject*)) PyCode_GetFilename(PyCodeObject *) PYSTON_NOEXCEPT; PyAPI_FUNC(BORROWED(PyObject*)) PyCode_GetFilename(PyCodeObject *) PYSTON_NOEXCEPT;
PyAPI_FUNC(BORROWED(PyObject*)) PyCode_GetName(PyCodeObject *) PYSTON_NOEXCEPT; PyAPI_FUNC(BORROWED(PyObject*)) PyCode_GetName(PyCodeObject *) PYSTON_NOEXCEPT;
PyAPI_FUNC(int) PyCode_HasFreeVars(PyCodeObject* ) PYSTON_NOEXCEPT;
/* for internal use only */ /* for internal use only */
#define _PyCode_GETCODEPTR(co, pp) \ #define _PyCode_GETCODEPTR(co, pp) \
......
...@@ -702,15 +702,17 @@ static long slot_tp_hash(PyObject* self) noexcept { ...@@ -702,15 +702,17 @@ static long slot_tp_hash(PyObject* self) noexcept {
PyObject* slot_tp_call(PyObject* self, PyObject* args, PyObject* kwds) noexcept { PyObject* slot_tp_call(PyObject* self, PyObject* args, PyObject* kwds) noexcept {
STAT_TIMER(t0, "us_timer_slot_tpcall", SLOT_AVOIDABILITY(self)); STAT_TIMER(t0, "us_timer_slot_tpcall", SLOT_AVOIDABILITY(self));
try { static PyObject* call_str;
Py_FatalError("this function is untested"); PyObject* meth = lookup_method(self, "__call__", &call_str);
PyObject* res;
// TODO: runtime ICs? if (meth == NULL)
return runtimeCall(self, ArgPassSpec(0, 0, true, true), args, kwds, NULL, NULL, NULL);
} catch (ExcInfo e) {
setCAPIException(e);
return NULL; return NULL;
}
res = PyObject_Call(meth, args, kwds);
Py_DECREF(meth);
return res;
} }
static const char* name_op[] = { static const char* name_op[] = {
......
...@@ -200,11 +200,17 @@ extern "C" BORROWED(PyObject*) PyCode_GetFilename(PyCodeObject* op) noexcept { ...@@ -200,11 +200,17 @@ extern "C" BORROWED(PyObject*) PyCode_GetFilename(PyCodeObject* op) noexcept {
RELEASE_ASSERT(PyCode_Check((Box*)op), ""); RELEASE_ASSERT(PyCode_Check((Box*)op), "");
return BoxedCode::filename((Box*)op, NULL); return BoxedCode::filename((Box*)op, NULL);
} }
extern "C" BORROWED(PyObject*) PyCode_GetName(PyCodeObject* op) noexcept { extern "C" BORROWED(PyObject*) PyCode_GetName(PyCodeObject* op) noexcept {
RELEASE_ASSERT(PyCode_Check((Box*)op), ""); RELEASE_ASSERT(PyCode_Check((Box*)op), "");
return BoxedCode::name((Box*)op, NULL); return BoxedCode::name((Box*)op, NULL);
} }
extern "C" int PyCode_HasFreeVars(PyCodeObject* _code) noexcept {
BoxedCode* code = (BoxedCode*)_code;
return code->f->source->getScopeInfo()->takesClosure() ? 1 : 0;
}
void setupCode() { void setupCode() {
code_cls code_cls
= BoxedClass::create(type_cls, object_cls, 0, 0, sizeof(BoxedCode), false, "code", false, = BoxedClass::create(type_cls, object_cls, 0, 0, sizeof(BoxedCode), false, "code", false,
......
...@@ -351,6 +351,7 @@ extern "C" PyFrameObject* PyFrame_New(PyThreadState* tstate, PyCodeObject* code, ...@@ -351,6 +351,7 @@ extern "C" PyFrameObject* PyFrame_New(PyThreadState* tstate, PyCodeObject* code,
extern "C" BORROWED(PyObject*) PyFrame_GetGlobals(PyFrameObject* f) noexcept { extern "C" BORROWED(PyObject*) PyFrame_GetGlobals(PyFrameObject* f) noexcept {
return BoxedFrame::globals((Box*)f, NULL); return BoxedFrame::globals((Box*)f, NULL);
} }
extern "C" BORROWED(PyObject*) PyFrame_GetCode(PyFrameObject* f) noexcept { extern "C" BORROWED(PyObject*) PyFrame_GetCode(PyFrameObject* f) noexcept {
return BoxedFrame::code((Box*)f, NULL); return BoxedFrame::code((Box*)f, NULL);
} }
......
...@@ -457,6 +457,14 @@ extern "C" int PySet_Add(PyObject* set, PyObject* key) noexcept { ...@@ -457,6 +457,14 @@ extern "C" int PySet_Add(PyObject* set, PyObject* key) noexcept {
} }
} }
extern "C" Py_ssize_t PySet_Size(PyObject* anyset) noexcept {
if (!PyAnySet_Check(anyset)) {
PyErr_BadInternalCall();
return -1;
}
BoxedSet* self = (BoxedSet*)anyset;
return self->s.size();
}
Box* setClear(BoxedSet* self) { Box* setClear(BoxedSet* self) {
RELEASE_ASSERT(isSubclass(self->cls, set_cls), ""); RELEASE_ASSERT(isSubclass(self->cls, set_cls), "");
......
import os import os
import sys import sys
import subprocess import subprocess
import shutil
""" """
Using this test file. Using this test file.
...@@ -64,6 +63,7 @@ SRC_DIR = ENV_NAME ...@@ -64,6 +63,7 @@ SRC_DIR = ENV_NAME
PYTHON_EXE = os.path.abspath(ENV_NAME + "/bin/python") PYTHON_EXE = os.path.abspath(ENV_NAME + "/bin/python")
CYTHON_DIR = os.path.abspath(os.path.join(SRC_DIR, "cython-0.22")) CYTHON_DIR = os.path.abspath(os.path.join(SRC_DIR, "cython-0.22"))
NUMPY_DIR = os.path.abspath(os.path.join(SRC_DIR, "numpy")) NUMPY_DIR = os.path.abspath(os.path.join(SRC_DIR, "numpy"))
SCIPY_DIR = os.path.abspath(os.path.join(SRC_DIR, "scipy"))
print_progress_header("Setting up Cython...") print_progress_header("Setting up Cython...")
if not os.path.exists(CYTHON_DIR): if not os.path.exists(CYTHON_DIR):
...@@ -77,7 +77,6 @@ if not os.path.exists(CYTHON_DIR): ...@@ -77,7 +77,6 @@ if not os.path.exists(CYTHON_DIR):
subprocess.check_call(["patch", "-p1", "--input=" + PATCH_FILE], cwd=CYTHON_DIR) subprocess.check_call(["patch", "-p1", "--input=" + PATCH_FILE], cwd=CYTHON_DIR)
print ">>> Applied Cython patch" print ">>> Applied Cython patch"
try: try:
subprocess.check_call([PYTHON_EXE, "setup.py", "install"], cwd=CYTHON_DIR) subprocess.check_call([PYTHON_EXE, "setup.py", "install"], cwd=CYTHON_DIR)
subprocess.check_call([PYTHON_EXE, "-c", "import Cython"], cwd=CYTHON_DIR) subprocess.check_call([PYTHON_EXE, "-c", "import Cython"], cwd=CYTHON_DIR)
...@@ -86,6 +85,10 @@ if not os.path.exists(CYTHON_DIR): ...@@ -86,6 +85,10 @@ if not os.path.exists(CYTHON_DIR):
else: else:
print ">>> Cython already installed." print ">>> Cython already installed."
env = os.environ
CYTHON_BIN_DIR = os.path.abspath(os.path.join(ENV_NAME + "/bin"))
env["PATH"] = CYTHON_BIN_DIR + ":" + env["PATH"]
print_progress_header("Cloning up NumPy...") print_progress_header("Cloning up NumPy...")
if not os.path.exists(NUMPY_DIR): if not os.path.exists(NUMPY_DIR):
url = "https://github.com/numpy/numpy" url = "https://github.com/numpy/numpy"
...@@ -94,10 +97,6 @@ else: ...@@ -94,10 +97,6 @@ else:
print ">>> NumPy already installed." print ">>> NumPy already installed."
try: try:
env = os.environ
CYTHON_BIN_DIR = os.path.abspath(os.path.join(ENV_NAME + "/bin"))
env["PATH"] = CYTHON_BIN_DIR + ":" + env["PATH"]
print_progress_header("Setting up NumPy...") print_progress_header("Setting up NumPy...")
subprocess.check_call([PYTHON_EXE, "setup.py", "build"], cwd=NUMPY_DIR, env=env) subprocess.check_call([PYTHON_EXE, "setup.py", "build"], cwd=NUMPY_DIR, env=env)
...@@ -109,6 +108,25 @@ except: ...@@ -109,6 +108,25 @@ except:
raise raise
print_progress_header("Cloning up SciPy...")
if not os.path.exists(SCIPY_DIR):
url = "https://github.com/scipy/scipy"
# subprocess.check_call(["git", "clone", "--depth", "1", "--branch", "v0.17.1", url], cwd=SRC_DIR)
else:
print ">>> SciPy already installed."
try:
print_progress_header("Setting up SciPy...")
# subprocess.check_call([PYTHON_EXE, "setup.py", "build"], cwd=SCIPY_DIR, env=env)
print_progress_header("Installing SciPy...")
# subprocess.check_call([PYTHON_EXE, "setup.py", "install"], cwd=SCIPY_DIR, env=env)
except:
subprocess.check_call(["rm", "-rf", SCIPY_DIR + "/build"])
subprocess.check_call(["rm", "-rf", SCIPY_DIR + "/dist"])
raise
# From Wikipedia # From Wikipedia
script = """ script = """
import numpy as np import numpy as np
......
add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/build/lib.linux-x86_64-2.7/basic_test.so add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/build/lib.linux-x86_64-2.7/basic_test.so
COMMAND python setup.py build --build-lib ${CMAKE_CURRENT_BINARY_DIR}/build/lib.linux-x86_64-2.7 COMMAND python setup.py build --build-lib ${CMAKE_CURRENT_BINARY_DIR}/build/lib.linux-x86_64-2.7
DEPENDS basic_test.c descr_test.c slots_test.c type_test.c DEPENDS basic_test.c descr_test.c slots_test.c type_test.c api_test.c
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}) WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/basic_test.pyston.so add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/basic_test.pyston.so
COMMAND ${CMAKE_BINARY_DIR}/pyston setup.py build --build-lib ${CMAKE_CURRENT_BINARY_DIR} COMMAND ${CMAKE_BINARY_DIR}/pyston setup.py build --build-lib ${CMAKE_CURRENT_BINARY_DIR}
DEPENDS pyston copy_stdlib copy_libpyston basic_test.c descr_test.c slots_test.c type_test.c DEPENDS pyston copy_stdlib copy_libpyston basic_test.c descr_test.c slots_test.c type_test.c api_test.c
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}) WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
add_custom_target(ext_cpython DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/build/lib.linux-x86_64-2.7/basic_test.so) add_custom_target(ext_cpython DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/build/lib.linux-x86_64-2.7/basic_test.so)
......
#include <Python.h>
static PyObject *
set_size(PyObject *self, PyObject *so)
{
return Py_BuildValue("i", PySet_Size(so));
}
static PyMethodDef TestMethods[] = {
{"set_size", set_size, METH_O, "Get set size by PySet_Size." },
{NULL, NULL, 0, NULL} /* Sentinel */
};
PyMODINIT_FUNC
initapi_test(void)
{
PyObject *m;
m = Py_InitModule("api_test", TestMethods);
if (m == NULL)
return;
}
...@@ -7,6 +7,7 @@ extensions = [ ...@@ -7,6 +7,7 @@ extensions = [
Extension("descr_test", sources = ["descr_test.c"]), Extension("descr_test", sources = ["descr_test.c"]),
Extension("slots_test", sources = ["slots_test.c"]), Extension("slots_test", sources = ["slots_test.c"]),
Extension("type_test", sources = ["type_test.c"]), Extension("type_test", sources = ["type_test.c"]),
Extension("api_test", sources = ["api_test.c"]),
] ]
def relpath(fn): def relpath(fn):
......
...@@ -539,3 +539,4 @@ calling __hash__ ...@@ -539,3 +539,4 @@ calling __hash__
False False
set([1L]) set([1]) set([1]) set([1L]) set([1L]) set([1]) set([1]) set([1L])
set([1]) set([1])
4
...@@ -269,3 +269,8 @@ print {1, 1L}, {1L, 1}, set([1, 1L]), set([1L, 1]) ...@@ -269,3 +269,8 @@ print {1, 1L}, {1L, 1}, set([1, 1L]), set([1L, 1])
s = {1} s = {1}
s.add(1L) s.add(1L)
print s print s
from api_test import set_size
s = set([1, 2, 3, 4])
print(set_size(s))
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