Skip to content

Commit

Permalink
Fixed float-printing in numpy: Added support for compare and richcomp…
Browse files Browse the repository at this point in the history
…are in PyCPeer. Added support for PySeqIter and iter-methods in PyCPeer.
  • Loading branch information
Stewori committed Jul 27, 2016
1 parent 1dd0c6e commit b4ffbd5
Show file tree
Hide file tree
Showing 20 changed files with 1,073 additions and 293 deletions.
9 changes: 9 additions & 0 deletions DemoExtension/DemoExtensionmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,14 @@ nativeDictGet(PyObject* self, PyObject* args)
return PyDict_GetItem(dict, key);
}

PyObject*
nativeDictCreate(PyObject* self, PyObject* args)
{
PyObject* dct = PyDict_New();
Py_ssize_t ds = PyDict_Size(dct);
return PyInt_FromLong(ds);
}

PyObject*
newstyleCheck(PyObject* self, PyObject* args)
{
Expand Down Expand Up @@ -447,6 +455,7 @@ PyMethodDef DemoExtensionMethods[] = {
{"booleanToInt", booleanToInt, METH_O, "Converts True to one, False to zero, everything else to None."},
{"intToBoolean", intToBoolean, METH_O, "Converts one to True, zero to False, everything else to None."},
{"nativeDictGet", nativeDictGet, METH_VARARGS, "Looks up a key in a dict."},
{"nativeDictCreate", nativeDictCreate, METH_NOARGS, "Creates a dict on native site."},
{"newstyleCheck", newstyleCheck, METH_VARARGS, "Checks integrity of new-style instance conversion."},
{"newstyleCheckSubtype", newstyleCheckSubtype, METH_VARARGS, "Checks subtype consistence new-style conversion."},
{"refcount", refcount, METH_VARARGS, "Provides the current native refcount of the given object."},
Expand Down
47 changes: 30 additions & 17 deletions JyNI-C/include/JyNI.h
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,10 @@
#define _PyGC_REFS_EXPLORING (-6)
#define _PyGC_REFS_EXPLORED (-7)

/* Macro to get the tp_richcompare field of a type if defined */
#define RICHCOMPARE(t) (PyType_HasFeature((t), Py_TPFLAGS_HAVE_RICHCOMPARE) \
? (t)->tp_richcompare : NULL)

/* define some method-signatures for sync purposes: */

/* jobject is src, PyObject* is dest. Src must not be modified. */
Expand Down Expand Up @@ -601,7 +605,11 @@ typedef struct {
unsigned short flags;
char* type_name;
} TypeMapEntry;
typedef struct { PyTypeObject* exc_type; jyFactoryMethod exc_factory;} ExceptionMapEntry;

typedef struct {
PyTypeObject* exc_type;
jyFactoryMethod exc_factory;
} ExceptionMapEntry;

#define JyObject_HasJyGCHead(pyObject, jyObject) \
JyNI_HasJyAttribute(jyObject, JyAttributeJyGCHead)
Expand Down Expand Up @@ -713,6 +721,10 @@ jint JyNI_delItem(JNIEnv *env, jclass class, jlong handle, jobject key, jlong ts
jint JyNI_PyObjectLength(JNIEnv *env, jclass class, jlong handle, jlong tstate);
jobject JyNI_descr_get(jlong self, jobject obj, jobject type, jlong tstate);
jint JyNI_descr_set(jlong self, jobject obj, jobject value, jlong tstate);
jint JyNI_PyObject_Compare(jlong handle, jobject o, jlong tstate);
jobject JyNI_PyObject_RichCompare(jlong handle, jobject o, jint op, jlong tstate);
jobject JyNI_PyObject_GetIter(jlong handle, jlong tstate);
jobject JyNI_PyIter_Next(jlong handle, jlong tstate);

// PyCFunction call-ins:
jobject JyNI_PyCFunction_getSelf(jlong handle, jlong tstate);
Expand Down Expand Up @@ -778,7 +790,7 @@ jint JyNI_PyMapping_Length(jlong o, jlong tstate);
//jint JyNI_PyMapping_AssSubscript(jlong o1, jobject o2, jobject o3, jlong tstate);


#define builtinTypeCount 41
#define builtinTypeCount 42
extern TypeMapEntry builtinTypes[builtinTypeCount];
#define TME_INDEX_Type 0
#define TME_INDEX_NotImplemented 1
Expand Down Expand Up @@ -808,34 +820,34 @@ extern TypeMapEntry builtinTypes[builtinTypeCount];
#define TME_INDEX_Weakref_Proxy 25
#define TME_INDEX_Weakref_CallableProxy 26
//#define TME_INDEX_BaseString 26
//#define TME_INDEX_SeqIter 27
#define TME_INDEX_SeqIter 27
//#define TME_INDEX_Range 28
//#define TME_INDEX_rangeiter 29
#define TME_INDEX_Tuple 27
#define TME_INDEX_Tuple 28
//#define TME_INDEX_TupleIter 28
#define TME_INDEX_List 28
#define TME_INDEX_List 29
//#define TME_INDEX_ListIter 30
//#define TME_INDEX_ListRevIter 31
#define TME_INDEX_Dict 29
#define TME_INDEX_StringMap 30
#define TME_INDEX_Set 31
#define TME_INDEX_Dict 30
#define TME_INDEX_StringMap 31
#define TME_INDEX_Set 32
//#define TME_INDEX_SetIter 34
#define TME_INDEX_FrozenSet 32
#define TME_INDEX_FrozenSet 33
//#define TME_INDEX_Enum 36
#define TME_INDEX_Slice 33
#define TME_INDEX_Ellipsis 34
#define TME_INDEX_Slice 34
#define TME_INDEX_Ellipsis 35
//#define TME_INDEX_Gen 39
#define TME_INDEX_Code_Bytecode 35
#define TME_INDEX_Code_Tablecode 36
#define TME_INDEX_Frame 37
#define TME_INDEX_Code_Bytecode 36
#define TME_INDEX_Code_Tablecode 37
#define TME_INDEX_Frame 38
//#define TME_INDEX_Super 43
#define TME_INDEX_Exc_BaseException 38
#define TME_INDEX_TraceBack 39
#define TME_INDEX_Exc_BaseException 39
#define TME_INDEX_TraceBack 40
//#define TME_INDEX_ByteArray 46
//#define TME_INDEX_Buffer 47
//#define TME_INDEX_MemoryView 48
//#define TME_INDEX_CallIter 50
#define TME_INDEX_BaseObject 40 //must be last type in list
#define TME_INDEX_BaseObject 41 //must be last type in list

/* "Hidden" PyTypes: */
extern PyTypeObject PyNone_Type;
Expand Down Expand Up @@ -929,6 +941,7 @@ inline PyObject * _JyObject_New(PyTypeObject *tp, TypeMapEntry* tme);
jobject _PyImport_LoadDynamicModuleJy(char *name, char *pathname, FILE *fp);
inline int PyModule_AddStringConstantJy(jobject m, const char *name, const char *value);
inline int PyModule_AddObjectJy(jobject m, const char *name, jobject o);
inline int _PyObject_Compare(PyObject *v, PyObject *w);

/* JyNI specific:
* Backdoor to reach original alloc-functions, which were renamed with "Raw"-prefix: */
Expand Down
9 changes: 8 additions & 1 deletion JyNI-C/include/JythonObjects.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ extern jmethodID pyObject__contains__;
extern jmethodID pyObject__len__;
extern jmethodID pyObject__getslice__;
extern jmethodID pyObject__nonzero__;
extern jmethodID pyObject__iter__;
extern jmethodID pyObject__iternext__;
extern jmethodID pyObject_cmp;
extern jmethodID pyObjectGetDict;
extern jmethodID pyObjectFastGetDict;
Expand Down Expand Up @@ -121,7 +123,7 @@ extern jmethodID pyAbstractDictMergeFromSeq;

extern jclass pyDictClass;
extern jmethodID pyDictConstructor;
extern jmethodID pyDictByPyObjectArrayConstructor;
//extern jmethodID pyDictByPyObjectArrayConstructor;

extern jclass pyStringMapClass;

Expand Down Expand Up @@ -271,7 +273,12 @@ extern jfieldID pyProperty_docFromGetter;

extern jclass pyBaseStringClass;
extern jclass pyXRangeClass;

extern jclass pySequenceIterClass;
extern jmethodID pySequenceIterConstructor;
extern jfieldID pySequenceIter_seq;
extern jfieldID pySequenceIter_index;

extern jclass pyFastSequenceIterClass;
extern jclass pyReversedIteratorClass;

Expand Down
2 changes: 1 addition & 1 deletion JyNI-C/include/Python_JyNI/Python_JyNI.h
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@
#include "traceback_JyNI.h"
#include "sliceobject_JyNI.h"
#include "cellobject_JyNI.h"
//#include "iterobject_JyNI.h"
#include "iterobject_JyNI.h"
//#include "genobject_JyNI.h"
#include "descrobject_JyNI.h"
#include "warnings_JyNI.h"
Expand Down
55 changes: 55 additions & 0 deletions JyNI-C/include/Python_JyNI/iterobject_JyNI.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/* This File is based on iterobject.h from CPython 2.7.11 release.
* It has been modified to suit JyNI needs.
*
*
* Copyright of JyNI:
* Copyright (c) 2013, 2014, 2015, 2016 Stefan Richthofer.
* All rights reserved.
*
*
* Copyright of Python and Jython:
* Copyright (c) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009,
* 2010, 2011, 2012, 2013, 2014, 2015, 2016 Python Software Foundation.
* All rights reserved.
*
*
* This file is part of JyNI.
*
* JyNI is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of
* the License, or (at your option) any later version.
*
* JyNI is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with JyNI. If not, see <http://www.gnu.org/licenses/>.
*/


#ifndef Py_ITEROBJECT_H
#define Py_ITEROBJECT_H
/* Iterators (the basic kind, over a sequence) */
#ifdef __cplusplus
extern "C" {
#endif

PyAPI_DATA(PyTypeObject) PySeqIter_Type;

#define PySeqIter_Check(op) (Py_TYPE(op) == &PySeqIter_Type)

PyAPI_FUNC(PyObject *) PySeqIter_New(PyObject *);

PyAPI_DATA(PyTypeObject) PyCallIter_Type;

#define PyCallIter_Check(op) (Py_TYPE(op) == &PyCallIter_Type)

PyAPI_FUNC(PyObject *) PyCallIter_New(PyObject *, PyObject *);
#ifdef __cplusplus
}
#endif
#endif /* !Py_ITEROBJECT_H */

65 changes: 52 additions & 13 deletions JyNI-C/src/JyAbstract.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,19 +69,6 @@ jobject JyNI_ ## pref ## _ ## name (jlong o1, jobject o2, jlong tstate) \

#define PyNumberMethod2(name) PyMethod2(PyNumber, name)

//#define PyNumberMethod2(name) \
//jobject JyNI_PyNumber_ ## name (jlong o1, jobject o2, jlong tstate) \
//{ \
// ENTER_JyNI \
// PyObject* arg = JyNI_PyObject_FromJythonPyObject(o2); \
// PyObject* res = PyNumber_ ## name ((PyObject*) o1, arg); \
// jobject jres = JyNI_JythonPyObject_FromPyObject(res); \
// Py_XDECREF(arg); \
// Py_XDECREF(res); \
// LEAVE_JyNI \
// return jres; \
//}

#define PyNumberMethod3(name) \
jobject JyNI_PyNumber_ ## name (jlong o1, jobject o2, jobject o3, jlong tstate) \
{ \
Expand Down Expand Up @@ -118,6 +105,58 @@ jobject JyNI_PySequence_ ## name (jlong o, jint l, jlong tstate) \
}


jint JyNI_PyObject_Compare(jlong handle, jobject o, jlong tstate)
{
// jputs(__FUNCTION__);
RE_ENTER_JyNI
PyObject* obj = JyNI_PyObject_FromJythonPyObject(o);
//Maybe use _PyObject_Compare?
//jint res = _PyObject_Compare((PyObject*) handle, obj);
// PyTypeObject* tp = Py_TYPE((PyObject*) handle);
jint res = Py_TYPE((PyObject*) handle)->tp_compare((PyObject*) handle, obj);
Py_XDECREF(obj);
RE_LEAVE_JyNI
// jputs("JyNI_PyObject_Compare done");
return res;
}

jobject JyNI_PyObject_RichCompare(jlong handle, jobject o, jint op, jlong tstate)
{
// jputs(__FUNCTION__);
RE_ENTER_JyNI
PyObject* obj = JyNI_PyObject_FromJythonPyObject(o);
//Maybe use PyObject_RichCompare?
//PyObject* res = PyObject_RichCompare((PyObject*) handle, obj);
PyObject* res = Py_TYPE((PyObject*) handle)->tp_richcompare((PyObject*) handle, obj, op);
jobject jres = JyNI_JythonPyObject_FromPyObject(res);
Py_XDECREF(obj);
Py_XDECREF(res);
RE_LEAVE_JyNI
// jputs("JyNI_PyObject_RichCompare done");
return jres;
}

jobject JyNI_PyObject_GetIter(jlong handle, jlong tstate)
{
RE_ENTER_JyNI
PyObject* res = PyObject_GetIter((PyObject*) handle);
jobject jres = JyNI_JythonPyObject_FromPyObject(res);
Py_XDECREF(res);
RE_LEAVE_JyNI
return jres;
}

jobject JyNI_PyIter_Next(jlong handle, jlong tstate)
{
RE_ENTER_JyNI
PyObject* res = PyIter_Next((PyObject*) handle);
jobject jres = JyNI_JythonPyObject_FromPyObject(res);
Py_XDECREF(res);
RE_LEAVE_JyNI
return jres;
}


// PyNumber-methods:

PyNumberMethod2(Add)
Expand Down
2 changes: 2 additions & 0 deletions JyNI-C/src/JyAlloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,8 @@ inline PyTypeObject* JyNI_InitPyObjectNativeTypePeer(jobject srctype)
dest = tme->sync->jyInit(src, NULL); \
} else \
{ \
/*jputs("init with new const-lookup:");*/ \
/*jputs(Py_TYPE(src)->tp_name);*/ \
jmethodID cm = (*env)->GetMethodID(env, tme->jy_class, "<init>", "()V"); \
if (cm) \
{ \
Expand Down
Loading

0 comments on commit b4ffbd5

Please sign in to comment.