[pypy-svn] r75422 - in pypy/trunk/pypy/module/cpyext: . include test
afa at codespeak.net
afa at codespeak.net
Wed Jun 16 15:37:03 CEST 2010
Author: afa
Date: Wed Jun 16 15:37:02 2010
New Revision: 75422
Modified:
pypy/trunk/pypy/module/cpyext/api.py
pypy/trunk/pypy/module/cpyext/cdatetime.py
pypy/trunk/pypy/module/cpyext/include/datetime.h
pypy/trunk/pypy/module/cpyext/state.py
pypy/trunk/pypy/module/cpyext/test/test_cpyext.py
pypy/trunk/pypy/module/cpyext/test/test_datetime.py
Log:
Avoid to prebuild the PyDateTimeAPI structure: this made pypy-c freeze the datetime and time modules.
I'm sure this is not right, specially concerning the call to tzset(),
and this does not work with --cc=mingw32.
Now the PyDateTimeAPI pointer is filled only after the first call to PyDateTime_IMPORT;
Modified: pypy/trunk/pypy/module/cpyext/api.py
==============================================================================
--- pypy/trunk/pypy/module/cpyext/api.py (original)
+++ pypy/trunk/pypy/module/cpyext/api.py Wed Jun 16 15:37:02 2010
@@ -315,7 +315,7 @@
'_Py_TrueStruct#': ('PyObject*', 'space.w_True'),
'_Py_ZeroStruct#': ('PyObject*', 'space.w_False'),
'_Py_NotImplementedStruct#': ('PyObject*', 'space.w_NotImplemented'),
- 'PyDateTimeAPI': ('PyDateTime_CAPI*', 'cpyext.cdatetime.build_datetime_api(space)'),
+ 'PyDateTimeAPI': ('PyDateTime_CAPI*', 'None'),
}
FORWARD_DECLS = []
INIT_FUNCTIONS = []
@@ -617,7 +617,7 @@
if "#" in name:
continue
if typ == 'PyDateTime_CAPI*':
- global_objects.append('%s _%s;' % (typ, name))
+ continue
elif name.startswith('PyExc_'):
global_objects.append('%s _%s;' % (typ[:-1], name))
else:
@@ -817,8 +817,7 @@
structs.append('extern PyTypeObject _%s;' % (name,))
structs.append('PyObject* %s = (PyObject*)&_%s;' % (name, name))
elif typ == 'PyDateTime_CAPI*':
- structs.append('extern %s _%s;' % (typ[:-1], name))
- structs.append('%s %s = &_%s;' % (typ, name, name))
+ structs.append('%s %s = NULL;' % (typ, name))
struct_file.write('\n'.join(structs))
eci = ExternalCompilationInfo(
@@ -871,8 +870,7 @@
if typ in ('PyObject*', 'PyTypeObject*'):
struct_ptr = make_ref(space, w_obj)
elif typ == 'PyDateTime_CAPI*':
- struct_ptr = w_obj
- name = '_' + name
+ continue
else:
assert False, "Unknown static data: %s %s" % (typ, name)
struct = rffi.cast(get_structtype_for_ctype(typ), struct_ptr)._obj
Modified: pypy/trunk/pypy/module/cpyext/cdatetime.py
==============================================================================
--- pypy/trunk/pypy/module/cpyext/cdatetime.py (original)
+++ pypy/trunk/pypy/module/cpyext/cdatetime.py Wed Jun 16 15:37:02 2010
@@ -1,9 +1,11 @@
from pypy.rpython.lltypesystem import rffi, lltype
-from pypy.module.cpyext.pyobject import PyObject, make_ref
+from pypy.rlib.objectmodel import we_are_translated
+from pypy.module.cpyext.pyobject import PyObject, make_ref, Py_DecRef
from pypy.module.cpyext.api import (
cpython_api, CANNOT_FAIL, cpython_struct, PyObjectFields)
from pypy.module.cpyext.import_ import PyImport_Import
from pypy.module.cpyext.typeobject import PyTypeObjectPtr
+from pypy.module.cpyext.state import State
from pypy.interpreter.error import OperationError
from pypy.tool.sourcetools import func_renamer
@@ -17,10 +19,16 @@
('DeltaType', PyTypeObjectPtr),
))
-def build_datetime_api(space):
- w_datetime = PyImport_Import(space, space.wrap("datetime"))
+ at cpython_api([], lltype.Ptr(PyDateTime_CAPI),
+ error=lltype.nullptr(PyDateTime_CAPI))
+def _PyDateTime_Import(space):
datetimeAPI = lltype.malloc(PyDateTime_CAPI, flavor='raw')
+ if not we_are_translated():
+ datetimeAPI_dealloc(space)
+ space.fromcache(State).datetimeAPI = datetimeAPI
+
+ w_datetime = PyImport_Import(space, space.wrap("datetime"))
w_type = space.getattr(w_datetime, space.wrap("date"))
datetimeAPI.c_DateType = rffi.cast(
PyTypeObjectPtr, make_ref(space, w_type))
@@ -36,9 +44,19 @@
return datetimeAPI
- at cpython_api([], PyObject)
-def _PyDateTime_Import(space):
- return
+def datetimeAPI_dealloc(space):
+ "Used in tests only, to please the refcount checker"
+ if we_are_translated():
+ return
+ datetimeAPI = space.fromcache(State).datetimeAPI
+ if datetimeAPI is None:
+ return
+ space.fromcache(State).datetimeAPI = None
+ Py_DecRef(space, rffi.cast(PyObject, datetimeAPI.c_DateType))
+ Py_DecRef(space, rffi.cast(PyObject, datetimeAPI.c_DateTimeType))
+ Py_DecRef(space, rffi.cast(PyObject, datetimeAPI.c_TimeType))
+ Py_DecRef(space, rffi.cast(PyObject, datetimeAPI.c_DeltaType))
+ lltype.free(datetimeAPI, flavor='raw')
PyDateTime_Date = PyObject
PyDateTime_Time = PyObject
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 Wed Jun 16 15:37:02 2010
@@ -14,7 +14,11 @@
} PyDateTime_CAPI;
PyAPI_DATA(PyDateTime_CAPI*) PyDateTimeAPI;
-#define PyDateTime_IMPORT _PyDateTime_Import()
+#define PyDateTime_IMPORT \
+ do { \
+ if(PyDateTimeAPI==NULL) \
+ PyDateTimeAPI = _PyDateTime_Import(); \
+ } while (0)
typedef struct {
PyObject_HEAD
Modified: pypy/trunk/pypy/module/cpyext/state.py
==============================================================================
--- pypy/trunk/pypy/module/cpyext/state.py (original)
+++ pypy/trunk/pypy/module/cpyext/state.py Wed Jun 16 15:37:02 2010
@@ -5,6 +5,8 @@
class State:
+ datetimeAPI = None # used in tests
+
def __init__(self, space):
self.space = space
self.reset()
Modified: pypy/trunk/pypy/module/cpyext/test/test_cpyext.py
==============================================================================
--- pypy/trunk/pypy/module/cpyext/test/test_cpyext.py (original)
+++ pypy/trunk/pypy/module/cpyext/test/test_cpyext.py Wed Jun 16 15:37:02 2010
@@ -271,6 +271,8 @@
Py_DecRef(self.space, w_obj)
state.non_heaptypes_w[:] = []
state.reset_borrowed_references()
+ from pypy.module.cpyext import cdatetime
+ cdatetime.datetimeAPI_dealloc(self.space)
if self.check_and_print_leaks():
assert False, "Test leaks or loses object(s)."
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 Wed Jun 16 15:37:02 2010
@@ -85,6 +85,7 @@
module = self.import_extension('foo', [
("get_types", "METH_NOARGS",
"""
+ PyDateTime_IMPORT;
return PyTuple_Pack(4,
PyDateTimeAPI->DateType,
PyDateTimeAPI->DateTimeType,
More information about the Pypy-commit
mailing list