Skip to content
23 changes: 20 additions & 3 deletions numpy/core/code_generators/generate_numpy_api.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import sys
import genapi

from genapi import \
Expand Down Expand Up @@ -141,6 +142,18 @@
#endif
"""

h_pypy_template = r"""

typedef struct {
PyObject_HEAD
npy_bool obval;
} PyBoolScalarObject;

#define import_array()
extern PyTypeObject PyArray_Type;
#define PyArray_New _PyArray_New

"""

c_template = r"""
/* These pointers will be stored in the C-object for use in other
Expand Down Expand Up @@ -233,14 +246,18 @@ def do_generate_api(targets, sources):

# Write to header
fid = open(header_file, 'w')
s = h_template % ('\n'.join(module_list), '\n'.join(extension_list))
if '__pypy__' in sys.builtin_module_names:
s = h_pypy_template
else:
s = h_template % ('\n'.join(module_list), '\n'.join(extension_list))
fid.write(s)
fid.close()

# Write to c-code
fid = open(c_file, 'w')
s = c_template % ',\n'.join(init_list)
fid.write(s)
if '__pypy__' not in sys.builtin_module_names:
s = c_template % ',\n'.join(init_list)
fid.write(s)
fid.close()

# write to documentation
Expand Down
48 changes: 37 additions & 11 deletions numpy/core/fromnumeric.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,26 @@ def take(a, indices, axis=None, out=None, mode='raise'):
array([4, 3, 6])

"""
try:
take = a.take
except AttributeError:
return _wrapit(a, 'take', indices, axis, out, mode)
return take(indices, axis, out, mode)
# XXX: numpypy doesn't support ndarray.take()
assert mode == 'raise'
if axis is None:
res = a.ravel()[indices]
else:
if axis < 0: axis += len(a.shape)
s0, s1 = a.shape[:axis], a.shape[axis+1:]
l0 = prod(s0) if s0 else 1
l1 = prod(s1) if s1 else 1
res = a.reshape((l0, -1, l1))[:,indices,:].reshape(s0 + (-1,) + s1)
if out is not None:
out[:] = res
return out
else:
return res
#try:
# take = a.take
#except AttributeError:
# return _wrapit(a, 'take', indices, axis, out, mode)
#return take(indices, axis, out, mode)


# not deprecated --- copy if necessary, view otherwise
Expand Down Expand Up @@ -1837,11 +1852,18 @@ def ptp(a, axis=None, out=None):
array([1, 1])

"""
try:
ptp = a.ptp
except AttributeError:
return _wrapit(a, 'ptp', axis, out)
return ptp(axis, out)
#XXX: numpypy does not have ndarray.ptp()
res = amax(a, axis) - amin(a, axis)
if out is not None:
out[:] = res
return out
else:
return res
#try:
# ptp = a.ptp
#except AttributeError:
# return _wrapit(a, 'ptp', axis, out)
#return ptp(axis, out)


def amax(a, axis=None, out=None, keepdims=False):
Expand Down Expand Up @@ -2395,11 +2417,15 @@ def round_(a, decimals=0, out=None):
around : equivalent function

"""
# XXX: numpypy doesn't support 'out' in ndarray.round()
try:
round = a.round
except AttributeError:
return _wrapit(a, 'round', decimals, out)
return round(decimals, out)
if out is None:
return round(decimals)
else:
return round(decimals, out)


def mean(a, axis=None, dtype=None, out=None, keepdims=False):
Expand Down
11 changes: 9 additions & 2 deletions numpy/core/multiarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,18 @@
set_string_function, typeinfo, where, zeros)
'''

def _fastCopyAndTranspose(a):
return a.T.copy()

def copyto(dst, src, casting='same_kind', where=None, preservena=False):
dst.fill(src)


undef = '''CLIP WRAP RAISE MAXDIMS ALLOW_THREADS BUFSIZE nditer nested_iters
broadcast empty_like fromiter fromfile frombuffer newbuffer getbuffer
int_asbuffer _fastCopyAndTranspose set_numeric_ops can_cast promote_types
int_asbuffer set_numeric_ops can_cast promote_types
min_scalar_type result_type lexsort compare_chararrays putmask einsum inner
_vec_string copyto datetime_data format_longfloat
_vec_string datetime_data format_longfloat
datetime_as_string busday_offset busday_count is_busday busdaycalendar
_flagdict flagsobj
'''.split()
Expand Down
3 changes: 3 additions & 0 deletions numpy/core/numeric.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,9 @@ def ones(shape, dtype=None, order='C'):
[ 1., 1.]])

"""
#a = empty(shape, dtype, order)
#multiarray.copyto(a, 1, casting='unsafe')
#return a
return multiarray.ones(shape, dtype=dtype, order=order)

def ones_like(a, dtype=None, order='K', subok=True):
Expand Down
8 changes: 6 additions & 2 deletions numpy/core/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,6 @@ def configuration(parent_package='',top_path=None):
from numpy.distutils.system_info import get_info, default_lib_dirs

config = Configuration('core',parent_package,top_path)
return config
local_dir = config.local_path
codegen_dir = join(local_dir,'code_generators')

Expand Down Expand Up @@ -595,6 +594,9 @@ def generate_api(ext, build_dir):
config.add_include_dirs(join(local_dir))

config.add_data_files('include/numpy/*.h')
if '__pypy__' in sys.builtin_module_names:
# Copy pypy's builting headers
config.add_data_files(('include/numpy', sys.exec_prefix + '/include/numpy/*.h'))
config.add_include_dirs(join('src', 'npymath'))
config.add_include_dirs(join('src', 'multiarray'))
config.add_include_dirs(join('src', 'umath'))
Expand Down Expand Up @@ -627,9 +629,11 @@ def generate_api(ext, build_dir):
sources = [join('src','dummymodule.c'),
generate_config_h,
generate_numpyconfig_h,
generate_numpy_api]
generate_numpy_api],
activate = True,
)

return config
#######################################################################
# npymath library #
#######################################################################
Expand Down
17 changes: 13 additions & 4 deletions numpy/distutils/misc_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
except NameError:
from sets import Set as set

is_pypy = '__pypy__' in sys.builtin_module_names

from numpy.distutils.compat import get_exception

__all__ = ['Configuration', 'get_numpy_include_dirs', 'default_config_dict',
Expand All @@ -27,7 +29,7 @@
'has_f_sources', 'has_cxx_sources', 'filter_sources',
'get_dependencies', 'is_local_src_dir', 'get_ext_source_files',
'get_script_files', 'get_lib_source_files', 'get_data_files',
'dot_join', 'get_frame', 'minrelpath','njoin',
'dot_join', 'get_frame', 'minrelpath','njoin', 'is_pypy',
'is_sequence', 'is_string', 'as_list', 'gpaths', 'get_language',
'quote_args', 'get_build_architecture', 'get_info', 'get_pkg_info']

Expand Down Expand Up @@ -719,7 +721,12 @@ class Configuration(object):
_dict_keys = ['package_dir', 'installed_pkg_config']
_extra_keys = ['name', 'version']

numpy_include_dirs = []
if is_pypy:
import numpypy as np
numpy_include_dirs = [np.get_include()]
del np
else:
numpy_include_dirs = []

def __init__(self,
package_name=None,
Expand Down Expand Up @@ -1442,6 +1449,7 @@ def add_extension(self,name,sources,**kw):
The self.paths(...) method is applied to all lists that may contain
paths.
"""
activate = kw.pop('activate', False)
ext_args = copy.copy(kw)
ext_args['name'] = dot_join(self.name,name)
ext_args['sources'] = sources
Expand Down Expand Up @@ -1487,7 +1495,9 @@ def add_extension(self,name,sources,**kw):

from numpy.distutils.core import Extension
ext = Extension(**ext_args)
#self.ext_modules.append(ext)
#XXX: This was commented out to prevent the building of extension modules
if activate:
self.ext_modules.append(ext)

dist = self.get_distribution()
if dist is not None:
Expand Down Expand Up @@ -2138,7 +2148,6 @@ def get_cmd(cmdname, _cache={}):
def get_numpy_include_dirs():
# numpy_include_dirs are set by numpy/core/setup.py, otherwise []
include_dirs = Configuration.numpy_include_dirs[:]
return include_dirs
if not include_dirs:
import numpy
include_dirs = [ numpy.get_include() ]
Expand Down
2 changes: 1 addition & 1 deletion numpy/f2py/f90mod_rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def findf90modules(m):
deallocate(d)
end if
end if
if ((.not.allocated(d)).and.(s(1).ge.1)) then""" % np.intp().itemsize
if ((.not.allocated(d)).and.(s(1).ge.1)) then""" % np.dtype('intp').itemsize

fgetdims2="""\
end if
Expand Down
2 changes: 1 addition & 1 deletion numpy/f2py/rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -1046,7 +1046,7 @@
\t\tif (!PyErr_Occurred())
\t\t\tPyErr_SetString(#modulename#_error,\"failed in converting #nth# `#varname#\' of #pyname# to C/Fortran array\" );
\t} else {
\t\t#varname# = (#ctype# *)(capi_#varname#_tmp->data);
\t\t#varname# = (#ctype# *)(PyArray_DATA(capi_#varname#_tmp));
""",
{hasinitvalue:[
{isintent_nothide:'\tif (#varname#_capi == Py_None) {'},
Expand Down
Loading