Skip to content

Commit cc87a03

Browse files
authored
Merge pull request #1251 from Daetalus/new_api_nexedi
Add some helper API to Pyston.
2 parents 4dd4b5b + 3eb3e5f commit cc87a03

File tree

12 files changed

+85
-17
lines changed

12 files changed

+85
-17
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -969,7 +969,7 @@ bench_exceptions:
969969
zsh -c 'ulimit -m $(MAX_MEM_KB); time ./bench_exceptions'
970970
rm bench_exceptions
971971

972-
TEST_EXT_MODULE_NAMES := basic_test descr_test slots_test
972+
TEST_EXT_MODULE_NAMES := basic_test descr_test slots_test type_test api_test
973973
TEST_EXT_MODULE_SRCS := $(TEST_EXT_MODULE_NAMES:%=test/test_extension/%.c)
974974
TEST_EXT_MODULE_OBJS := $(TEST_EXT_MODULE_NAMES:%=test/test_extension/%.pyston.so)
975975

from_cpython/Include/code.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,10 @@ PyAPI_DATA(PyTypeObject*) code_cls;
7474
#define PyCode_Type (*code_cls)
7575

7676
#define PyCode_Check(op) (Py_TYPE(op) == &PyCode_Type)
77-
#define PyCode_GetNumFree(op) (PyTuple_GET_SIZE((op)->co_freevars))
77+
// Pyston change: disable this macro.
78+
// In Pyston the PyCodeObject is just an opaque pointer, get its size
79+
// directly will cause error.
80+
// #define PyCode_GetNumFree(op) (PyTuple_GET_SIZE((op)->co_freevars))
7881

7982
/* Public interface */
8083
PyAPI_FUNC(PyCodeObject *) PyCode_New(
@@ -95,6 +98,7 @@ PyAPI_FUNC(int) PyCode_Addr2Line(PyCodeObject *, int) PYSTON_NOEXCEPT;
9598
PyAPI_FUNC(int) PyCode_GetArgCount(PyCodeObject *) PYSTON_NOEXCEPT;
9699
PyAPI_FUNC(BORROWED(PyObject*)) PyCode_GetFilename(PyCodeObject *) PYSTON_NOEXCEPT;
97100
PyAPI_FUNC(BORROWED(PyObject*)) PyCode_GetName(PyCodeObject *) PYSTON_NOEXCEPT;
101+
PyAPI_FUNC(int) PyCode_HasFreeVars(PyCodeObject* ) PYSTON_NOEXCEPT;
98102

99103
/* for internal use only */
100104
#define _PyCode_GETCODEPTR(co, pp) \

src/capi/typeobject.cpp

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -702,15 +702,17 @@ static long slot_tp_hash(PyObject* self) noexcept {
702702
PyObject* slot_tp_call(PyObject* self, PyObject* args, PyObject* kwds) noexcept {
703703
STAT_TIMER(t0, "us_timer_slot_tpcall", SLOT_AVOIDABILITY(self));
704704

705-
try {
706-
Py_FatalError("this function is untested");
705+
static PyObject* call_str;
706+
PyObject* meth = lookup_method(self, "__call__", &call_str);
707+
PyObject* res;
707708

708-
// TODO: runtime ICs?
709-
return runtimeCall(self, ArgPassSpec(0, 0, true, true), args, kwds, NULL, NULL, NULL);
710-
} catch (ExcInfo e) {
711-
setCAPIException(e);
709+
if (meth == NULL)
712710
return NULL;
713-
}
711+
712+
res = PyObject_Call(meth, args, kwds);
713+
714+
Py_DECREF(meth);
715+
return res;
714716
}
715717

716718
static const char* name_op[] = {

src/runtime/code.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,11 +200,17 @@ extern "C" BORROWED(PyObject*) PyCode_GetFilename(PyCodeObject* op) noexcept {
200200
RELEASE_ASSERT(PyCode_Check((Box*)op), "");
201201
return BoxedCode::filename((Box*)op, NULL);
202202
}
203+
203204
extern "C" BORROWED(PyObject*) PyCode_GetName(PyCodeObject* op) noexcept {
204205
RELEASE_ASSERT(PyCode_Check((Box*)op), "");
205206
return BoxedCode::name((Box*)op, NULL);
206207
}
207208

209+
extern "C" int PyCode_HasFreeVars(PyCodeObject* _code) noexcept {
210+
BoxedCode* code = (BoxedCode*)_code;
211+
return code->f->source->getScopeInfo()->takesClosure() ? 1 : 0;
212+
}
213+
208214
void setupCode() {
209215
code_cls
210216
= BoxedClass::create(type_cls, object_cls, 0, 0, sizeof(BoxedCode), false, "code", false,

src/runtime/frame.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,7 @@ extern "C" PyFrameObject* PyFrame_New(PyThreadState* tstate, PyCodeObject* code,
351351
extern "C" BORROWED(PyObject*) PyFrame_GetGlobals(PyFrameObject* f) noexcept {
352352
return BoxedFrame::globals((Box*)f, NULL);
353353
}
354+
354355
extern "C" BORROWED(PyObject*) PyFrame_GetCode(PyFrameObject* f) noexcept {
355356
return BoxedFrame::code((Box*)f, NULL);
356357
}

src/runtime/set.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,14 @@ extern "C" int PySet_Add(PyObject* set, PyObject* key) noexcept {
457457
}
458458
}
459459

460+
extern "C" Py_ssize_t PySet_Size(PyObject* anyset) noexcept {
461+
if (!PyAnySet_Check(anyset)) {
462+
PyErr_BadInternalCall();
463+
return -1;
464+
}
465+
BoxedSet* self = (BoxedSet*)anyset;
466+
return self->s.size();
467+
}
460468

461469
Box* setClear(BoxedSet* self) {
462470
RELEASE_ASSERT(isSubclass(self->cls, set_cls), "");

test/integration/numpy_test.py

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import os
22
import sys
33
import subprocess
4-
import shutil
54

65
"""
76
Using this test file.
@@ -64,6 +63,7 @@ def print_progress_header(text):
6463
PYTHON_EXE = os.path.abspath(ENV_NAME + "/bin/python")
6564
CYTHON_DIR = os.path.abspath(os.path.join(SRC_DIR, "cython-0.22"))
6665
NUMPY_DIR = os.path.abspath(os.path.join(SRC_DIR, "numpy"))
66+
SCIPY_DIR = os.path.abspath(os.path.join(SRC_DIR, "scipy"))
6767

6868
print_progress_header("Setting up Cython...")
6969
if not os.path.exists(CYTHON_DIR):
@@ -77,7 +77,6 @@ def print_progress_header(text):
7777
subprocess.check_call(["patch", "-p1", "--input=" + PATCH_FILE], cwd=CYTHON_DIR)
7878
print ">>> Applied Cython patch"
7979

80-
8180
try:
8281
subprocess.check_call([PYTHON_EXE, "setup.py", "install"], cwd=CYTHON_DIR)
8382
subprocess.check_call([PYTHON_EXE, "-c", "import Cython"], cwd=CYTHON_DIR)
@@ -86,6 +85,10 @@ def print_progress_header(text):
8685
else:
8786
print ">>> Cython already installed."
8887

88+
env = os.environ
89+
CYTHON_BIN_DIR = os.path.abspath(os.path.join(ENV_NAME + "/bin"))
90+
env["PATH"] = CYTHON_BIN_DIR + ":" + env["PATH"]
91+
8992
print_progress_header("Cloning up NumPy...")
9093
if not os.path.exists(NUMPY_DIR):
9194
url = "https://github.com/numpy/numpy"
@@ -94,10 +97,6 @@ def print_progress_header(text):
9497
print ">>> NumPy already installed."
9598

9699
try:
97-
env = os.environ
98-
CYTHON_BIN_DIR = os.path.abspath(os.path.join(ENV_NAME + "/bin"))
99-
env["PATH"] = CYTHON_BIN_DIR + ":" + env["PATH"]
100-
101100
print_progress_header("Setting up NumPy...")
102101
subprocess.check_call([PYTHON_EXE, "setup.py", "build"], cwd=NUMPY_DIR, env=env)
103102

@@ -109,6 +108,25 @@ def print_progress_header(text):
109108

110109
raise
111110

111+
print_progress_header("Cloning up SciPy...")
112+
if not os.path.exists(SCIPY_DIR):
113+
url = "https://github.com/scipy/scipy"
114+
# subprocess.check_call(["git", "clone", "--depth", "1", "--branch", "v0.17.1", url], cwd=SRC_DIR)
115+
else:
116+
print ">>> SciPy already installed."
117+
118+
try:
119+
print_progress_header("Setting up SciPy...")
120+
# subprocess.check_call([PYTHON_EXE, "setup.py", "build"], cwd=SCIPY_DIR, env=env)
121+
122+
print_progress_header("Installing SciPy...")
123+
# subprocess.check_call([PYTHON_EXE, "setup.py", "install"], cwd=SCIPY_DIR, env=env)
124+
except:
125+
subprocess.check_call(["rm", "-rf", SCIPY_DIR + "/build"])
126+
subprocess.check_call(["rm", "-rf", SCIPY_DIR + "/dist"])
127+
128+
raise
129+
112130
# From Wikipedia
113131
script = """
114132
import numpy as np

test/test_extension/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/build/lib.linux-x86_64-2.7/basic_test.so
22
COMMAND python setup.py build --build-lib ${CMAKE_CURRENT_BINARY_DIR}/build/lib.linux-x86_64-2.7
3-
DEPENDS basic_test.c descr_test.c slots_test.c type_test.c
3+
DEPENDS basic_test.c descr_test.c slots_test.c type_test.c api_test.c
44
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
55

66
add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/basic_test.pyston.so
77
COMMAND ${CMAKE_BINARY_DIR}/pyston setup.py build --build-lib ${CMAKE_CURRENT_BINARY_DIR}
8-
DEPENDS pyston copy_stdlib copy_libpyston basic_test.c descr_test.c slots_test.c type_test.c
8+
DEPENDS pyston copy_stdlib copy_libpyston basic_test.c descr_test.c slots_test.c type_test.c api_test.c
99
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
1010

1111
add_custom_target(ext_cpython DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/build/lib.linux-x86_64-2.7/basic_test.so)

test/test_extension/api_test.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#include <Python.h>
2+
3+
static PyObject *
4+
set_size(PyObject *self, PyObject *so)
5+
{
6+
return Py_BuildValue("i", PySet_Size(so));
7+
}
8+
9+
static PyMethodDef TestMethods[] = {
10+
{"set_size", set_size, METH_O, "Get set size by PySet_Size." },
11+
{NULL, NULL, 0, NULL} /* Sentinel */
12+
};
13+
14+
PyMODINIT_FUNC
15+
initapi_test(void)
16+
{
17+
PyObject *m;
18+
19+
m = Py_InitModule("api_test", TestMethods);
20+
if (m == NULL)
21+
return;
22+
}

test/test_extension/setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
Extension("descr_test", sources = ["descr_test.c"]),
88
Extension("slots_test", sources = ["slots_test.c"]),
99
Extension("type_test", sources = ["type_test.c"]),
10+
Extension("api_test", sources = ["api_test.c"]),
1011
]
1112

1213
def relpath(fn):

0 commit comments

Comments
 (0)