Commit 691e3766 authored by Kevin Modzelewski's avatar Kevin Modzelewski

Add a hodgepodge of CAPI support features

parent dd3c61df
...@@ -277,7 +277,7 @@ STDLIB_OBJS := stdlib.bc.o stdlib.stripped.bc.o ...@@ -277,7 +277,7 @@ STDLIB_OBJS := stdlib.bc.o stdlib.stripped.bc.o
STDLIB_RELEASE_OBJS := stdlib.release.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 $(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 $(EXTRA_STDMODULE_SRCS)
STDOBJECT_SRCS := structseq.c $(EXTRA_STDOBJECT_SRCS) STDOBJECT_SRCS := structseq.c capsule.c $(EXTRA_STDOBJECT_SRCS)
FROM_CPYTHON_SRCS := $(addprefix lib_python/2.7_Modules/,$(STDMODULE_SRCS)) $(addprefix lib_python/2.7_Objects/,$(STDOBJECT_SRCS)) $(wildcard src/capi/*.c) FROM_CPYTHON_SRCS := $(addprefix lib_python/2.7_Modules/,$(STDMODULE_SRCS)) $(addprefix lib_python/2.7_Objects/,$(STDOBJECT_SRCS)) $(wildcard src/capi/*.c)
# The stdlib objects have slightly longer dependency chains, # The stdlib objects have slightly longer dependency chains,
......
...@@ -15,19 +15,30 @@ ...@@ -15,19 +15,30 @@
#ifndef PYSTON_EXTINCLUDE_PYTHON_H #ifndef PYSTON_EXTINCLUDE_PYTHON_H
#define PYSTON_EXTINCLUDE_PYTHON_H #define PYSTON_EXTINCLUDE_PYTHON_H
#include <assert.h>
#include <errno.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
// These include orders come from CPython: // These include orders come from CPython:
#include "patchlevel.h" #include "patchlevel.h"
#include "pyconfig.h" #include "pyconfig.h"
#include <limits.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef HAVE_STDDEF_H
#include <stddef.h>
#endif
#include <assert.h>
// CPython doesn't seem to include this but I'm not sure how they get the definition of 'bool':
#include <stdbool.h>
#include "pyport.h" #include "pyport.h"
#include "pymath.h" #include "pymath.h"
...@@ -47,10 +58,12 @@ ...@@ -47,10 +58,12 @@
#include "complexobject.h" #include "complexobject.h"
#endif #endif
#include "stringobject.h" #include "stringobject.h"
#include "bytesobject.h"
#include "listobject.h" #include "listobject.h"
#include "dictobject.h" #include "dictobject.h"
#include "tupleobject.h" #include "tupleobject.h"
#include "methodobject.h" #include "methodobject.h"
#include "fileobject.h"
#include "pycapsule.h" #include "pycapsule.h"
#include "sliceobject.h" #include "sliceobject.h"
#include "iterobject.h" #include "iterobject.h"
...@@ -65,6 +78,7 @@ ...@@ -65,6 +78,7 @@
#include "modsupport.h" #include "modsupport.h"
#include "pythonrun.h" #include "pythonrun.h"
#include "ceval.h" #include "ceval.h"
#include "intrcheck.h"
#include "import.h" #include "import.h"
#include "abstract.h" #include "abstract.h"
......
// This file is originally from CPython 2.7, with modifications for Pyston
#define PyBytesObject PyStringObject
#define PyBytes_Type PyString_Type
#define PyBytes_Check PyString_Check
#define PyBytes_CheckExact PyString_CheckExact
#define PyBytes_CHECK_INTERNED PyString_CHECK_INTERNED
#define PyBytes_AS_STRING PyString_AS_STRING
#define PyBytes_GET_SIZE PyString_GET_SIZE
#define Py_TPFLAGS_BYTES_SUBCLASS Py_TPFLAGS_STRING_SUBCLASS
#define PyBytes_FromStringAndSize PyString_FromStringAndSize
#define PyBytes_FromString PyString_FromString
#define PyBytes_FromFormatV PyString_FromFormatV
#define PyBytes_FromFormat PyString_FromFormat
#define PyBytes_Size PyString_Size
#define PyBytes_AsString PyString_AsString
#define PyBytes_Repr PyString_Repr
#define PyBytes_Concat PyString_Concat
#define PyBytes_ConcatAndDel PyString_ConcatAndDel
#define _PyBytes_Resize _PyString_Resize
#define _PyBytes_Eq _PyString_Eq
#define PyBytes_Format PyString_Format
#define _PyBytes_FormatLong _PyString_FormatLong
#define PyBytes_DecodeEscape PyString_DecodeEscape
#define _PyBytes_Join _PyString_Join
#define PyBytes_AsStringAndSize PyString_AsStringAndSize
#define _PyBytes_InsertThousandsGrouping _PyString_InsertThousandsGrouping
...@@ -92,7 +92,11 @@ struct _dictobject { ...@@ -92,7 +92,11 @@ struct _dictobject {
PyDictEntry ma_smalltable[PyDict_MINSIZE]; PyDictEntry ma_smalltable[PyDict_MINSIZE];
}; };
#endif #endif
PyAPI_DATA(PyTypeObject*) list_cls;
#define PyList_Type (*list_cls)
// Pyston change: these are no longer static objects:
#if 0
PyAPI_DATA(PyTypeObject) PyDict_Type; PyAPI_DATA(PyTypeObject) PyDict_Type;
PyAPI_DATA(PyTypeObject) PyDictIterKey_Type; PyAPI_DATA(PyTypeObject) PyDictIterKey_Type;
PyAPI_DATA(PyTypeObject) PyDictIterValue_Type; PyAPI_DATA(PyTypeObject) PyDictIterValue_Type;
...@@ -100,6 +104,21 @@ PyAPI_DATA(PyTypeObject) PyDictIterItem_Type; ...@@ -100,6 +104,21 @@ PyAPI_DATA(PyTypeObject) PyDictIterItem_Type;
PyAPI_DATA(PyTypeObject) PyDictKeys_Type; PyAPI_DATA(PyTypeObject) PyDictKeys_Type;
PyAPI_DATA(PyTypeObject) PyDictItems_Type; PyAPI_DATA(PyTypeObject) PyDictItems_Type;
PyAPI_DATA(PyTypeObject) PyDictValues_Type; PyAPI_DATA(PyTypeObject) PyDictValues_Type;
#endif
PyAPI_DATA(PyTypeObject*) dict_cls;
#define PyDict_Type (*dict_cls)
PyAPI_DATA(PyTypeObject*) dictiterkey_cls;
#define PyDictIterKey_Type (*dictiterkey_cls)
PyAPI_DATA(PyTypeObject*) dictitervalue_cls;
#define PyDictIterValue_Type (*dictitervalue_cls)
PyAPI_DATA(PyTypeObject*) dictiteritem_cls;
#define PyDictIterItem_Type (*dictiteritem_cls)
PyAPI_DATA(PyTypeObject*) dictkeys_cls;
#define PyDictKeys_Type (*dictkeys_cls)
PyAPI_DATA(PyTypeObject*) dictitems_cls;
#define PyDictItems_Type (*dictitems_cls)
PyAPI_DATA(PyTypeObject*) dictvalues_cls;
#define PyDictValues_Type (*dictvalues_cls)
// Pyston changes: these aren't direct macros any more [they potentially could be though] // Pyston changes: these aren't direct macros any more [they potentially could be though]
PyAPI_FUNC(bool) PyDict_Check(PyObject*); PyAPI_FUNC(bool) PyDict_Check(PyObject*);
......
// This file is originally from CPython 2.7, with modifications for Pyston
/* File object interface */
#ifndef Py_FILEOBJECT_H
#define Py_FILEOBJECT_H
#ifdef __cplusplus
extern "C" {
#endif
// Pyston change: this is not our format
#if 0
typedef struct {
PyObject_HEAD
FILE *f_fp;
PyObject *f_name;
PyObject *f_mode;
int (*f_close)(FILE *);
int f_softspace; /* Flag used by 'print' command */
int f_binary; /* Flag which indicates whether the file is
open in binary (1) or text (0) mode */
char* f_buf; /* Allocated readahead buffer */
char* f_bufend; /* Points after last occupied position */
char* f_bufptr; /* Current buffer position */
char *f_setbuf; /* Buffer for setbuf(3) and setvbuf(3) */
int f_univ_newline; /* Handle any newline convention */
int f_newlinetypes; /* Types of newlines seen */
int f_skipnextlf; /* Skip next \n */
PyObject *f_encoding;
PyObject *f_errors;
PyObject *weakreflist; /* List of weak references */
int unlocked_count; /* Num. currently running sections of code
using f_fp with the GIL released. */
int readable;
int writable;
} PyFileObject;
#endif
typedef struct _PyFileObject PyFileObject;
// Pyston change: this is no longer a static object
PyAPI_DATA(PyTypeObject*) file_cls;
#define PyFile_Type (*file_cls)
#define PyFile_Check(op) PyObject_TypeCheck(op, &PyFile_Type)
#define PyFile_CheckExact(op) (Py_TYPE(op) == &PyFile_Type)
PyAPI_FUNC(PyObject *) PyFile_FromString(char *, char *);
PyAPI_FUNC(void) PyFile_SetBufSize(PyObject *, int);
PyAPI_FUNC(int) PyFile_SetEncoding(PyObject *, const char *);
PyAPI_FUNC(int) PyFile_SetEncodingAndErrors(PyObject *, const char *, char *errors);
PyAPI_FUNC(PyObject *) PyFile_FromFile(FILE *, char *, char *,
int (*)(FILE *));
PyAPI_FUNC(FILE *) PyFile_AsFile(PyObject *);
PyAPI_FUNC(void) PyFile_IncUseCount(PyFileObject *);
PyAPI_FUNC(void) PyFile_DecUseCount(PyFileObject *);
PyAPI_FUNC(PyObject *) PyFile_Name(PyObject *);
PyAPI_FUNC(PyObject *) PyFile_GetLine(PyObject *, int);
PyAPI_FUNC(int) PyFile_WriteObject(PyObject *, PyObject *, int);
PyAPI_FUNC(int) PyFile_SoftSpace(PyObject *, int);
PyAPI_FUNC(int) PyFile_WriteString(const char *, PyObject *);
PyAPI_FUNC(int) PyObject_AsFileDescriptor(PyObject *);
/* The default encoding used by the platform file system APIs
If non-NULL, this is different than the default encoding for strings
*/
PyAPI_DATA(const char *) Py_FileSystemDefaultEncoding;
/* Routines to replace fread() and fgets() which accept any of \r, \n
or \r\n as line terminators.
*/
#define PY_STDIOTEXTMODE "b"
char *Py_UniversalNewlineFgets(char *, int, FILE*, PyObject *);
size_t Py_UniversalNewlineFread(char *, size_t, FILE *, PyObject *);
/* A routine to do sanity checking on the file mode string. returns
non-zero on if an exception occurred
*/
int _PyFile_SanitizeMode(char *mode);
#if defined _MSC_VER && _MSC_VER >= 1400
/* A routine to check if a file descriptor is valid on Windows. Returns 0
* and sets errno to EBADF if it isn't. This is to avoid Assertions
* from various functions in the Windows CRT beginning with
* Visual Studio 2005
*/
int _PyVerify_fd(int fd);
#elif defined _MSC_VER && _MSC_VER >= 1200
/* fdopen doesn't set errno EBADF and crashes for large fd on debug build */
#define _PyVerify_fd(fd) (_get_osfhandle(fd) >= 0)
#else
#define _PyVerify_fd(A) (1) /* dummy */
#endif
/* A routine to check if a file descriptor can be select()-ed. */
#ifdef HAVE_SELECT
#define _PyIsSelectable_fd(FD) (((FD) >= 0) && ((FD) < FD_SETSIZE))
#else
#define _PyIsSelectable_fd(FD) (1)
#endif /* HAVE_SELECT */
#ifdef __cplusplus
}
#endif
#endif /* !Py_FILEOBJECT_H */
...@@ -63,7 +63,7 @@ PyAPI_FUNC(long) PyInt_GetMax(void); ...@@ -63,7 +63,7 @@ PyAPI_FUNC(long) PyInt_GetMax(void);
/* Macro, trading safety for speed */ /* Macro, trading safety for speed */
// Pyston changes: these aren't direct macros any more [they potentially could be though] // Pyston changes: these aren't direct macros any more [they potentially could be though]
#define PyInt_AS_LONG(op) PyInt_FromLong(op) #define PyInt_AS_LONG(op) PyInt_AsLong((PyObject*)op)
//#define PyInt_AS_LONG(op) (((PyIntObject *)(op))->ob_ival) //#define PyInt_AS_LONG(op) (((PyIntObject *)(op))->ob_ival)
/* These aren't really part of the Int object, but they're handy; the protos /* These aren't really part of the Int object, but they're handy; the protos
......
// This file is originally from CPython 2.7, with modifications for Pyston
#ifndef Py_INTRCHECK_H
#define Py_INTRCHECK_H
#ifdef __cplusplus
extern "C" {
#endif
PyAPI_FUNC(int) PyOS_InterruptOccurred(void);
PyAPI_FUNC(void) PyOS_InitInterrupts(void);
PyAPI_FUNC(void) PyOS_AfterFork(void);
#ifdef __cplusplus
}
#endif
#endif /* !Py_INTRCHECK_H */
...@@ -23,6 +23,8 @@ ...@@ -23,6 +23,8 @@
#define SIZEOF_VOID_P 8 #define SIZEOF_VOID_P 8
#define SIZEOF_SIZE_T 8 #define SIZEOF_SIZE_T 8
#define SIZEOF_INT 4 #define SIZEOF_INT 4
#define SIZEOF_LONG 8
#define SIZEOF_LONG_LONG 8
#define HAVE_COPYSIGN 1 #define HAVE_COPYSIGN 1
#define HAVE_ROUND 1 #define HAVE_ROUND 1
#define HAVE_HYPOT 1 #define HAVE_HYPOT 1
...@@ -35,6 +37,57 @@ ...@@ -35,6 +37,57 @@
#define Py_USING_UNICODE 1 #define Py_USING_UNICODE 1
#define Py_UNICODE_SIZE 4 #define Py_UNICODE_SIZE 4
// Copied from a CPython ./configure run on a Linux machine:
#define HAVE_ALLOCA_H 1
#define HAVE_ASM_TYPES_H 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_GRP_H 1
#define HAVE_INTTYPES_H 1
#define HAVE_LANGINFO_H 1
#define HAVE_LIBINTL_H 1
#define HAVE_LIBUTIL_H 1
#define HAVE_LINUX_NETLINK_H 1
#define HAVE_LINUX_TIPC_H 1
#define HAVE_MEMORY_H 1
#define HAVE_NCURSES_H 1
#define HAVE_NETPACKET_PACKET_H 1
#define HAVE_POLL_H 1
#define HAVE_PTHREAD_H 1
#define HAVE_PTY_H 1
#define HAVE_SHADOW_H 1
#define HAVE_SIGNAL_H 1
#define HAVE_SPAWN_H 1
#define HAVE_STDINT_H 1
#define HAVE_STDLIB_H 1
#define HAVE_STRINGS_H 1
#define HAVE_STRING_H 1
#define HAVE_STROPTS_H 1
#define HAVE_SYSEXITS_H 1
#define HAVE_SYS_EPOLL_H 1
#define HAVE_SYS_FILE_H 1
#define HAVE_SYS_PARAM_H 1
#define HAVE_SYS_POLL_H 1
#define HAVE_SYS_RESOURCE_H 1
#define HAVE_SYS_SELECT_H 1
#define HAVE_SYS_SOCKET_H 1
#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
#define HAVE_SYS_WAIT_H 1
#define HAVE_TERMIOS_H 1
#define HAVE_TERM_H 1
#define HAVE_UNISTD_H 1
#define HAVE_UTIME_H 1
#define HAVE_WCHAR_H 1
// Added this for some Pyston modifications: // Added this for some Pyston modifications:
#define MAX_PYSTRING_SIZE (PY_SSIZE_T_MAX/2 - (1<<20)) #define MAX_PYSTRING_SIZE (PY_SSIZE_T_MAX/2 - (1<<20))
......
// This file is originally from CPython 2.7, with modifications for Pyston
/* Wrap void * pointers to be passed between C modules */
#include "Python.h"
/* Internal structure of PyCapsule */
typedef struct {
PyObject_HEAD
void *pointer;
const char *name;
void *context;
PyCapsule_Destructor destructor;
} PyCapsule;
static int
_is_legal_capsule(PyCapsule *capsule, const char *invalid_capsule)
{
if (!capsule || !PyCapsule_CheckExact(capsule) || capsule->pointer == NULL) {
PyErr_SetString(PyExc_ValueError, invalid_capsule);
return 0;
}
return 1;
}
#define is_legal_capsule(capsule, name) \
(_is_legal_capsule(capsule, \
name " called with invalid PyCapsule object"))
static int
name_matches(const char *name1, const char *name2) {
/* if either is NULL, */
if (!name1 || !name2) {
/* they're only the same if they're both NULL. */
return name1 == name2;
}
return !strcmp(name1, name2);
}
PyObject *
PyCapsule_New(void *pointer, const char *name, PyCapsule_Destructor destructor)
{
PyCapsule *capsule;
if (!pointer) {
PyErr_SetString(PyExc_ValueError, "PyCapsule_New called with null pointer");
return NULL;
}
capsule = PyObject_NEW(PyCapsule, &PyCapsule_Type);
if (capsule == NULL) {
return NULL;
}
capsule->pointer = pointer;
capsule->name = name;
capsule->context = NULL;
capsule->destructor = destructor;
return (PyObject *)capsule;
}
int
PyCapsule_IsValid(PyObject *o, const char *name)
{
PyCapsule *capsule = (PyCapsule *)o;
return (capsule != NULL &&
PyCapsule_CheckExact(capsule) &&
capsule->pointer != NULL &&
name_matches(capsule->name, name));
}
void *
PyCapsule_GetPointer(PyObject *o, const char *name)
{
PyCapsule *capsule = (PyCapsule *)o;
if (!is_legal_capsule(capsule, "PyCapsule_GetPointer")) {
return NULL;
}
if (!name_matches(name, capsule->name)) {
PyErr_SetString(PyExc_ValueError, "PyCapsule_GetPointer called with incorrect name");
return NULL;
}
return capsule->pointer;
}
const char *
PyCapsule_GetName(PyObject *o)
{
PyCapsule *capsule = (PyCapsule *)o;
if (!is_legal_capsule(capsule, "PyCapsule_GetName")) {
return NULL;
}
return capsule->name;
}
PyCapsule_Destructor
PyCapsule_GetDestructor(PyObject *o)
{
PyCapsule *capsule = (PyCapsule *)o;
if (!is_legal_capsule(capsule, "PyCapsule_GetDestructor")) {
return NULL;
}
return capsule->destructor;
}
void *
PyCapsule_GetContext(PyObject *o)
{
PyCapsule *capsule = (PyCapsule *)o;
if (!is_legal_capsule(capsule, "PyCapsule_GetContext")) {
return NULL;
}
return capsule->context;
}
int
PyCapsule_SetPointer(PyObject *o, void *pointer)
{
PyCapsule *capsule = (PyCapsule *)o;
if (!pointer) {
PyErr_SetString(PyExc_ValueError, "PyCapsule_SetPointer called with null pointer");
return -1;
}
if (!is_legal_capsule(capsule, "PyCapsule_SetPointer")) {
return -1;
}
capsule->pointer = pointer;
return 0;
}
int
PyCapsule_SetName(PyObject *o, const char *name)
{
PyCapsule *capsule = (PyCapsule *)o;
if (!is_legal_capsule(capsule, "PyCapsule_SetName")) {
return -1;
}
capsule->name = name;
return 0;
}
int
PyCapsule_SetDestructor(PyObject *o, PyCapsule_Destructor destructor)
{
PyCapsule *capsule = (PyCapsule *)o;
if (!is_legal_capsule(capsule, "PyCapsule_SetDestructor")) {
return -1;
}
capsule->destructor = destructor;
return 0;
}
int
PyCapsule_SetContext(PyObject *o, void *context)
{
PyCapsule *capsule = (PyCapsule *)o;
if (!is_legal_capsule(capsule, "PyCapsule_SetContext")) {
return -1;
}
capsule->context = context;
return 0;
}
void *
PyCapsule_Import(const char *name, int no_block)
{
// Pyston change:
Py_FatalError("Pyston TODO: implement this");
#if 0
PyObject *object = NULL;
void *return_value = NULL;
char *trace;
size_t name_length = (strlen(name) + 1) * sizeof(char);
char *name_dup = (char *)PyMem_MALLOC(name_length);
if (!name_dup) {
return NULL;
}
memcpy(name_dup, name, name_length);
trace = name_dup;
while (trace) {
char *dot = strchr(trace, '.');
if (dot) {
*dot++ = '\0';
}
if (object == NULL) {
if (no_block) {
object = PyImport_ImportModuleNoBlock(trace);
} else {
object = PyImport_ImportModule(trace);
if (!object) {
PyErr_Format(PyExc_ImportError, "PyCapsule_Import could not import module \"%s\"", trace);
}
}
} else {
PyObject *object2 = PyObject_GetAttrString(object, trace);
Py_DECREF(object);
object = object2;
}
if (!object) {
goto EXIT;
}
trace = dot;
}
/* compare attribute name to module.name by hand */
if (PyCapsule_IsValid(object, name)) {
PyCapsule *capsule = (PyCapsule *)object;
return_value = capsule->pointer;
} else {
PyErr_Format(PyExc_AttributeError,
"PyCapsule_Import \"%s\" is not valid",
name);
}
EXIT:
Py_XDECREF(object);
if (name_dup) {
PyMem_FREE(name_dup);
}
return return_value;
#endif
}
static void
capsule_dealloc(PyObject *o)
{
PyCapsule *capsule = (PyCapsule *)o;
if (capsule->destructor) {
capsule->destructor(o);
}
PyObject_DEL(o);
}
static PyObject *
capsule_repr(PyObject *o)
{
PyCapsule *capsule = (PyCapsule *)o;
const char *name;
const char *quote;
if (capsule->name) {
quote = "\"";
name = capsule->name;
} else {
quote = "";
name = "NULL";
}
return PyString_FromFormat("<capsule object %s%s%s at %p>",
quote, name, quote, capsule);
}
PyDoc_STRVAR(PyCapsule_Type__doc__,
"Capsule objects let you wrap a C \"void *\" pointer in a Python\n\
object. They're a way of passing data through the Python interpreter\n\
without creating your own custom type.\n\
\n\
Capsules are used for communication between extension modules.\n\
They provide a way for an extension module to export a C interface\n\
to other extension modules, so that extension modules can use the\n\
Python import mechanism to link to one another.\n\
");
PyTypeObject PyCapsule_Type = {
// Pyston change:
//PyVarObject_HEAD_INIT(&PyType_Type, 0)
PyVarObject_HEAD_INIT(NULL, 0)
"PyCapsule", /*tp_name*/
sizeof(PyCapsule), /*tp_basicsize*/
0, /*tp_itemsize*/
/* methods */
capsule_dealloc, /*tp_dealloc*/
0, /*tp_print*/
0, /*tp_getattr*/
0, /*tp_setattr*/
0, /*tp_reserved*/
capsule_repr, /*tp_repr*/
0, /*tp_as_number*/
0, /*tp_as_sequence*/
0, /*tp_as_mapping*/
0, /*tp_hash*/
0, /*tp_call*/
0, /*tp_str*/
0, /*tp_getattro*/
0, /*tp_setattro*/
0, /*tp_as_buffer*/
0, /*tp_flags*/
PyCapsule_Type__doc__ /*tp_doc*/
};
// This file is originally from CPython 2.7, with modifications for Pyston
/* Implementation helper: a struct that looks like a tuple. See timemodule /* Implementation helper: a struct that looks like a tuple. See timemodule
and posixmodule for example uses. */ and posixmodule for example uses. */
......
...@@ -686,7 +686,7 @@ public: ...@@ -686,7 +686,7 @@ public:
for (; i < arg_names.args->size(); i++) { for (; i < arg_names.args->size(); i++) {
AST_expr* arg = (*arg_names.args)[i]; AST_expr* arg = (*arg_names.args)[i];
assert(arg->type == AST_TYPE::Name); RELEASE_ASSERT(arg->type == AST_TYPE::Name, "");
AST_Name* arg_name = ast_cast<AST_Name>(arg); AST_Name* arg_name = ast_cast<AST_Name>(arg);
initial_types[arg_name->id] = unboxedType(arg_types[i]); initial_types[arg_name->id] = unboxedType(arg_types[i]);
} }
......
...@@ -144,6 +144,16 @@ extern "C" int PyArg_VaParse(PyObject* _tuple, const char* fmt, va_list ap) { ...@@ -144,6 +144,16 @@ extern "C" int PyArg_VaParse(PyObject* _tuple, const char* fmt, va_list ap) {
return vgetargs1(_tuple, fmt, &lva, 0); return vgetargs1(_tuple, fmt, &lva, 0);
} }
extern "C" int _PyArg_Parse_SizeT(PyObject* args, char* format, ...) {
int retval;
va_list va;
va_start(va, format);
retval = vgetargs1(args, format, &va, FLAG_COMPAT | FLAG_SIZE_T);
va_end(va);
return retval;
}
extern "C" int PyArg_ParseTuple(PyObject* _tuple, const char* fmt, ...) { extern "C" int PyArg_ParseTuple(PyObject* _tuple, const char* fmt, ...) {
va_list ap; va_list ap;
va_start(ap, fmt); va_start(ap, fmt);
......
...@@ -38,6 +38,17 @@ ...@@ -38,6 +38,17 @@
namespace pyston { namespace pyston {
extern "C" {
// Copied from CPython:
#if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T)
const char* Py_FileSystemDefaultEncoding = "mbcs";
#elif defined(__APPLE__)
const char* Py_FileSystemDefaultEncoding = "utf-8";
#else
const char* Py_FileSystemDefaultEncoding = NULL; /* use default */
#endif
}
extern "C" Box* trap() { extern "C" Box* trap() {
raise(SIGTRAP); raise(SIGTRAP);
...@@ -453,7 +464,7 @@ BoxedClass* BaseException, *Exception, *StandardError, *AssertionError, *Attribu ...@@ -453,7 +464,7 @@ BoxedClass* BaseException, *Exception, *StandardError, *AssertionError, *Attribu
*NameError, *KeyError, *IndexError, *IOError, *OSError, *ZeroDivisionError, *ValueError, *UnboundLocalError, *NameError, *KeyError, *IndexError, *IOError, *OSError, *ZeroDivisionError, *ValueError, *UnboundLocalError,
*RuntimeError, *ImportError, *StopIteration, *Warning, *SyntaxError, *OverflowError, *DeprecationWarning, *RuntimeError, *ImportError, *StopIteration, *Warning, *SyntaxError, *OverflowError, *DeprecationWarning,
*MemoryError, *LookupError, *EnvironmentError, *ArithmeticError, *BufferError, *KeyboardInterrupt, *SystemExit, *MemoryError, *LookupError, *EnvironmentError, *ArithmeticError, *BufferError, *KeyboardInterrupt, *SystemExit,
*SystemError; *SystemError, *NotImplementedError;
} }
Box* exceptionNew1(BoxedClass* cls) { Box* exceptionNew1(BoxedClass* cls) {
...@@ -745,10 +756,10 @@ void setupBuiltins() { ...@@ -745,10 +756,10 @@ void setupBuiltins() {
/*BytesWarning =*/makeBuiltinException(Warning, "BytesWarning"); /*BytesWarning =*/makeBuiltinException(Warning, "BytesWarning");
MemoryError = makeBuiltinException(StandardError, "MemoryError"); MemoryError = makeBuiltinException(StandardError, "MemoryError");
BufferError = makeBuiltinException(StandardError, "BufferError"); BufferError = makeBuiltinException(StandardError, "BufferError");
/*NotImplementedError=*/makeBuiltinException(RuntimeError, "NotImplementedError");
KeyboardInterrupt = makeBuiltinException(BaseException, "KeyboardInterrupt"); KeyboardInterrupt = makeBuiltinException(BaseException, "KeyboardInterrupt");
SystemExit = makeBuiltinException(BaseException, "SystemExit"); SystemExit = makeBuiltinException(BaseException, "SystemExit");
SystemError = makeBuiltinException(StandardError, "SystemError"); SystemError = makeBuiltinException(StandardError, "SystemError");
NotImplementedError = makeBuiltinException(RuntimeError, "NotImplementedError");
repr_obj = new BoxedFunction(boxRTFunction((void*)repr, UNKNOWN, 1)); repr_obj = new BoxedFunction(boxRTFunction((void*)repr, UNKNOWN, 1));
builtins_module->giveAttr("repr", repr_obj); builtins_module->giveAttr("repr", repr_obj);
......
...@@ -13,6 +13,7 @@ ...@@ -13,6 +13,7 @@
// limitations under the License. // limitations under the License.
#include <dlfcn.h> #include <dlfcn.h>
#include <fcntl.h>
#include <stdarg.h> #include <stdarg.h>
#include <string.h> #include <string.h>
...@@ -777,6 +778,220 @@ extern "C" const char* PyUnicode_AS_DATA(PyObject*) { ...@@ -777,6 +778,220 @@ extern "C" const char* PyUnicode_AS_DATA(PyObject*) {
Py_FatalError("unimplemented"); Py_FatalError("unimplemented");
} }
extern "C" const char* PyUnicode_GetDefaultEncoding(void) {
Py_FatalError("unimplemented");
}
extern "C" int PyBuffer_IsContiguous(Py_buffer* view, char fort) {
Py_FatalError("unimplemented");
}
extern "C" PyObject* PyErr_SetFromErrnoWithFilename(PyObject* exc, const char* filename) {
PyObject* name = filename ? PyString_FromString(filename) : NULL;
PyObject* result = PyErr_SetFromErrnoWithFilenameObject(exc, name);
Py_XDECREF(name);
return result;
}
extern "C" PyObject* PyErr_SetFromErrnoWithFilenameObject(PyObject* exc, PyObject* filenameObject) {
PyObject* v;
const char* s;
int i = errno;
#ifdef PLAN9
char errbuf[ERRMAX];
#endif
#ifdef MS_WINDOWS
char* s_buf = NULL;
char s_small_buf[28]; /* Room for "Windows Error 0xFFFFFFFF" */
#endif
#ifdef EINTR
if (i == EINTR && PyErr_CheckSignals())
return NULL;
#endif
#ifdef PLAN9
rerrstr(errbuf, sizeof errbuf);
s = errbuf;
#else
if (i == 0)
s = "Error"; /* Sometimes errno didn't get set */
else
#ifndef MS_WINDOWS
s = strerror(i);
#else
{
/* Note that the Win32 errors do not lineup with the
errno error. So if the error is in the MSVC error
table, we use it, otherwise we assume it really _is_
a Win32 error code
*/
if (i > 0 && i < _sys_nerr) {
s = _sys_errlist[i];
} else {
int len = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM
| FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, /* no message source */
i, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
/* Default language */
(LPTSTR)&s_buf, 0, /* size not used */
NULL); /* no args */
if (len == 0) {
/* Only ever seen this in out-of-mem
situations */
sprintf(s_small_buf, "Windows Error 0x%X", i);
s = s_small_buf;
s_buf = NULL;
} else {
s = s_buf;
/* remove trailing cr/lf and dots */
while (len > 0 && (s[len - 1] <= ' ' || s[len - 1] == '.'))
s[--len] = '\0';
}
}
}
#endif /* Unix/Windows */
#endif /* PLAN 9*/
if (filenameObject != NULL)
v = Py_BuildValue("(isO)", i, s, filenameObject);
else
v = Py_BuildValue("(is)", i, s);
if (v != NULL) {
PyErr_SetObject(exc, v);
Py_DECREF(v);
}
#ifdef MS_WINDOWS
LocalFree(s_buf);
#endif
return NULL;
}
extern "C" int PyOS_snprintf(char* str, size_t size, const char* format, ...) {
int rc;
va_list va;
va_start(va, format);
rc = PyOS_vsnprintf(str, size, format, va);
va_end(va);
return rc;
}
extern "C" int PyOS_vsnprintf(char* str, size_t size, const char* format, va_list va) {
int len; /* # bytes written, excluding \0 */
#ifdef HAVE_SNPRINTF
#define _PyOS_vsnprintf_EXTRA_SPACE 1
#else
#define _PyOS_vsnprintf_EXTRA_SPACE 512
char* buffer;
#endif
assert(str != NULL);
assert(size > 0);
assert(format != NULL);
/* We take a size_t as input but return an int. Sanity check
* our input so that it won't cause an overflow in the
* vsnprintf return value or the buffer malloc size. */
if (size > INT_MAX - _PyOS_vsnprintf_EXTRA_SPACE) {
len = -666;
goto Done;
}
#ifdef HAVE_SNPRINTF
len = vsnprintf(str, size, format, va);
#else
/* Emulate it. */
buffer = (char*)PyMem_MALLOC(size + _PyOS_vsnprintf_EXTRA_SPACE);
if (buffer == NULL) {
len = -666;
goto Done;
}
len = vsprintf(buffer, format, va);
if (len < 0)
/* ignore the error */;
else if ((size_t)len >= size + _PyOS_vsnprintf_EXTRA_SPACE)
Py_FatalError("Buffer overflow in PyOS_snprintf/PyOS_vsnprintf");
else {
const size_t to_copy = (size_t)len < size ? (size_t)len : size - 1;
assert(to_copy < size);
memcpy(str, buffer, to_copy);
str[to_copy] = '\0';
}
PyMem_FREE(buffer);
#endif
Done:
if (size > 0)
str[size - 1] = '\0';
return len;
#undef _PyOS_vsnprintf_EXTRA_SPACE
}
extern "C" void PyOS_AfterFork(void) {
Py_FatalError("unimplemented");
}
extern "C" {
static int dev_urandom_python(char* buffer, Py_ssize_t size) noexcept {
int fd;
Py_ssize_t n;
if (size <= 0)
return 0;
Py_BEGIN_ALLOW_THREADS fd = ::open("/dev/urandom", O_RDONLY);
Py_END_ALLOW_THREADS if (fd < 0) {
if (errno == ENOENT || errno == ENXIO || errno == ENODEV || errno == EACCES)
PyErr_SetString(PyExc_NotImplementedError, "/dev/urandom (or equivalent) not found");
else
PyErr_SetFromErrno(PyExc_OSError);
return -1;
}
Py_BEGIN_ALLOW_THREADS do {
do {
n = read(fd, buffer, (size_t)size);
} while (n < 0 && errno == EINTR);
if (n <= 0)
break;
buffer += n;
size -= (Py_ssize_t)n;
}
while (0 < size)
;
Py_END_ALLOW_THREADS
if (n <= 0) {
/* stop on error or if read(size) returned 0 */
if (n < 0)
PyErr_SetFromErrno(PyExc_OSError);
else
PyErr_Format(PyExc_RuntimeError, "Failed to read %zi bytes from /dev/urandom", size);
close(fd);
return -1;
}
close(fd);
return 0;
}
}
extern "C" int _PyOS_URandom(void* buffer, Py_ssize_t size) {
if (size < 0) {
PyErr_Format(PyExc_ValueError, "negative argument not allowed");
return -1;
}
if (size == 0)
return 0;
#ifdef MS_WINDOWS
return win32_urandom((unsigned char*)buffer, size, 1);
#else
#ifdef __VMS
return vms_urandom((unsigned char*)buffer, size, 1);
#else
return dev_urandom_python((char*)buffer, size);
#endif
#endif
}
BoxedModule* importTestExtension(const std::string& name) { BoxedModule* importTestExtension(const std::string& name) {
std::string pathname_name = "test/test_extension/" + name + ".pyston.so"; std::string pathname_name = "test/test_extension/" + name + ".pyston.so";
const char* pathname = pathname_name.c_str(); const char* pathname = pathname_name.c_str();
......
...@@ -30,6 +30,10 @@ extern "C" Box* createPureImaginary(double i) { ...@@ -30,6 +30,10 @@ extern "C" Box* createPureImaginary(double i) {
return new BoxedComplex(0.0, i); return new BoxedComplex(0.0, i);
} }
extern "C" Py_complex PyComplex_AsCComplex(PyObject* op) {
Py_FatalError("unimplemented");
}
// addition // addition
extern "C" Box* complexAddComplex(BoxedComplex* lhs, BoxedComplex* rhs) { extern "C" Box* complexAddComplex(BoxedComplex* lhs, BoxedComplex* rhs) {
......
...@@ -159,10 +159,16 @@ extern "C" PyObject* PyDict_GetItem(PyObject* dict, PyObject* key) { ...@@ -159,10 +159,16 @@ extern "C" PyObject* PyDict_GetItem(PyObject* dict, PyObject* key) {
try { try {
return getitem(dict, key); return getitem(dict, key);
} catch (Box* b) { } catch (Box* b) {
if (isSubclass(b->cls, KeyError))
return NULL;
abort(); abort();
} }
} }
extern "C" int PyDict_Next(PyObject* op, Py_ssize_t* ppos, PyObject** pkey, PyObject** pvalue) {
Py_FatalError("unimplemented");
}
extern "C" PyObject* PyDict_GetItemString(PyObject* dict, const char* key) { extern "C" PyObject* PyDict_GetItemString(PyObject* dict, const char* key) {
Box* key_s; Box* key_s;
try { try {
......
...@@ -178,6 +178,18 @@ Box* fileIterHasNext(Box* s) { ...@@ -178,6 +178,18 @@ Box* fileIterHasNext(Box* s) {
return boxBool(!fileEof(self)); return boxBool(!fileEof(self));
} }
extern "C" PyObject* PyFile_FromFile(FILE* fp, char* name, char* mode, int (*close)(FILE*)) {
Py_FatalError("unimplemented");
}
extern "C" void PyFile_SetBufSize(PyObject* f, int bufsize) {
Py_FatalError("unimplemented");
}
extern "C" int _PyFile_SanitizeMode(char* mode) {
Py_FatalError("unimplemented");
}
void setupFile() { void setupFile() {
file_cls->giveAttr("__name__", boxStrConstant("file")); file_cls->giveAttr("__name__", boxStrConstant("file"));
......
...@@ -31,6 +31,10 @@ ...@@ -31,6 +31,10 @@
namespace pyston { namespace pyston {
extern "C" unsigned long PyInt_AsUnsignedLongMask(PyObject* op) {
Py_FatalError("unimplemented");
}
extern "C" long PyInt_AsLong(PyObject* op) { extern "C" long PyInt_AsLong(PyObject* op) {
RELEASE_ASSERT(op->cls == int_cls, ""); RELEASE_ASSERT(op->cls == int_cls, "");
return static_cast<BoxedInt*>(op)->n; return static_cast<BoxedInt*>(op)->n;
......
...@@ -32,6 +32,18 @@ namespace pyston { ...@@ -32,6 +32,18 @@ namespace pyston {
BoxedClass* long_cls; BoxedClass* long_cls;
extern "C" unsigned long PyLong_AsUnsignedLongMask(PyObject* op) {
Py_FatalError("unimplemented");
}
extern "C" unsigned PY_LONG_LONG PyLong_AsUnsignedLongLongMask(PyObject* vv) {
Py_FatalError("unimplemented");
}
extern "C" PY_LONG_LONG PyLong_AsLongLong(PyObject* vv) {
Py_FatalError("unimplemented");
}
static int64_t asSignedLong(BoxedLong* self) { static int64_t asSignedLong(BoxedLong* self) {
assert(self->cls == long_cls); assert(self->cls == long_cls);
if (!mpz_fits_slong_p(self->n)) if (!mpz_fits_slong_p(self->n))
......
...@@ -399,8 +399,10 @@ BoxedHeapClass::BoxedHeapClass(BoxedClass* metaclass, BoxedClass* base, gcvisit_ ...@@ -399,8 +399,10 @@ BoxedHeapClass::BoxedHeapClass(BoxedClass* metaclass, BoxedClass* base, gcvisit_
tp_as_sequence = &as_sequence; tp_as_sequence = &as_sequence;
tp_as_buffer = &as_buffer; tp_as_buffer = &as_buffer;
// just make sure these get zero-initialized: memset(&as_number, 0, sizeof(as_number));
assert(as_sequence.sq_item == NULL); memset(&as_mapping, 0, sizeof(as_mapping));
memset(&as_sequence, 0, sizeof(as_sequence));
memset(&as_buffer, 0, sizeof(as_buffer));
} }
std::string getFullNameOfClass(BoxedClass* cls) { std::string getFullNameOfClass(BoxedClass* cls) {
......
...@@ -31,6 +31,10 @@ ...@@ -31,6 +31,10 @@
namespace pyston { namespace pyston {
extern "C" PyObject* PyString_FromFormat(const char* format, ...) {
Py_FatalError("unimplemented");
}
extern "C" BoxedString* strAdd(BoxedString* lhs, Box* _rhs) { extern "C" BoxedString* strAdd(BoxedString* lhs, Box* _rhs) {
assert(lhs->cls == str_cls); assert(lhs->cls == str_cls);
......
...@@ -953,6 +953,8 @@ void setupRuntime() { ...@@ -953,6 +953,8 @@ void setupRuntime() {
setupCAPI(); setupCAPI();
PyType_Ready(&PyCapsule_Type);
initerrno(); initerrno();
init_sha(); init_sha();
init_sha256(); init_sha256();
......
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