[pypy-svn] r75178 - in pypy/trunk/pypy/module/cpyext: . include test

afa at codespeak.net afa at codespeak.net
Mon Jun 7 18:16:06 CEST 2010


Author: afa
Date: Mon Jun  7 18:16:02 2010
New Revision: 75178

Modified:
   pypy/trunk/pypy/module/cpyext/datetime.py
   pypy/trunk/pypy/module/cpyext/include/Python.h
   pypy/trunk/pypy/module/cpyext/include/datetime.h
   pypy/trunk/pypy/module/cpyext/test/test_datetime.py
Log:
Our implmentation of datetime is written in Python.
but it does not seem possible to expose Python types with a customized PyObject structure.

Invent "macros" like PyDateTime_DELTA_GET_DAYS to access data members.
They could be added to CPython IMO.


Modified: pypy/trunk/pypy/module/cpyext/datetime.py
==============================================================================
--- pypy/trunk/pypy/module/cpyext/datetime.py	(original)
+++ pypy/trunk/pypy/module/cpyext/datetime.py	Mon Jun  7 18:16:02 2010
@@ -1,18 +1,25 @@
 from pypy.rpython.lltypesystem import rffi, lltype
 from pypy.module.cpyext.pyobject import PyObject
-from pypy.module.cpyext.api import cpython_api, CANNOT_FAIL
+from pypy.module.cpyext.api import (
+    cpython_api, CANNOT_FAIL, cpython_struct, PyObjectFields)
 from pypy.module.cpyext.import_ import PyImport_Import
 from pypy.interpreter.error import OperationError
 from pypy.tool.sourcetools import func_renamer
 
+# API import function
+
 @cpython_api([], lltype.Void)
-def PyDateTime_IMPORT(space):
+def _PyDateTime_Import(space):
     return
 
 PyDateTime_Date = PyObject
 PyDateTime_Time = PyObject
 PyDateTime_DateTime = PyObject
 
+PyDeltaObjectStruct = lltype.ForwardReference()
+cpython_struct("PyDateTime_Delta", PyObjectFields, PyDeltaObjectStruct)
+PyDateTime_Delta = lltype.Ptr(PyDeltaObjectStruct)
+
 # Check functions
 
 def make_check_function(func_name, type_name):
@@ -195,3 +202,19 @@
     """Return the microsecond, as an int from 0 through 999999.
     """
     return space.getattr(w_obj, space.wrap("microsecond"))
+
+# XXX these functions are not present in the Python API
+# But it does not seem possible to expose a different structure
+# for types defined in a python module like lib/datetime.py.
+
+ at cpython_api([PyDateTime_Delta], rffi.INT_real, error=CANNOT_FAIL)
+def PyDateTime_DELTA_GET_DAYS(space, w_obj):
+    return space.getattr(w_obj, space.wrap("days"))
+
+ at cpython_api([PyDateTime_Delta], rffi.INT_real, error=CANNOT_FAIL)
+def PyDateTime_DELTA_GET_SECONDS(space, w_obj):
+    return space.getattr(w_obj, space.wrap("seconds"))
+
+ at cpython_api([PyDateTime_Delta], rffi.INT_real, error=CANNOT_FAIL)
+def PyDateTime_DELTA_GET_MICROSECONDS(space, w_obj):
+    return space.getattr(w_obj, space.wrap("microseconds"))

Modified: pypy/trunk/pypy/module/cpyext/include/Python.h
==============================================================================
--- pypy/trunk/pypy/module/cpyext/include/Python.h	(original)
+++ pypy/trunk/pypy/module/cpyext/include/Python.h	Mon Jun  7 18:16:02 2010
@@ -96,6 +96,7 @@
 #include "pycobject.h"
 #include "bufferobject.h"
 #include "sliceobject.h"
+#include "datetime.h"
 #include "pystate.h"
 
 // XXX This shouldn't be included here

Modified: pypy/trunk/pypy/module/cpyext/include/datetime.h
==============================================================================
--- pypy/trunk/pypy/module/cpyext/include/datetime.h	(original)
+++ pypy/trunk/pypy/module/cpyext/include/datetime.h	Mon Jun  7 18:16:02 2010
@@ -0,0 +1,16 @@
+#ifndef DATETIME_H
+#define DATETIME_H
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#define PyDateTime_IMPORT _PyDateTime_Import()
+
+typedef struct {
+    PyObject_HEAD
+} PyDateTime_Delta;
+
+#ifdef __cplusplus
+}
+#endif
+#endif

Modified: pypy/trunk/pypy/module/cpyext/test/test_datetime.py
==============================================================================
--- pypy/trunk/pypy/module/cpyext/test/test_datetime.py	(original)
+++ pypy/trunk/pypy/module/cpyext/test/test_datetime.py	Mon Jun  7 18:16:02 2010
@@ -63,6 +63,12 @@
         assert api.PyDelta_Check(w_delta)
         assert api.PyDelta_CheckExact(w_delta)
 
+        assert space.unwrap(space.newtuple([
+            api.PyDateTime_DELTA_GET_DAYS(w_delta),
+            api.PyDateTime_DELTA_GET_SECONDS(w_delta),
+            api.PyDateTime_DELTA_GET_MICROSECONDS(w_delta)])) == (
+            10, 20, 30)
+
     def test_fromtimestamp(self, space, api):
         w_args = space.wrap((0,))
         w_date = api.PyDate_FromTimestamp(w_args)



More information about the Pypy-commit mailing list