diff --git a/.ci/upload_docs.sh b/.ci/upload_docs.sh index e277b294..aa20838d 100755 --- a/.ci/upload_docs.sh +++ b/.ci/upload_docs.sh @@ -5,11 +5,19 @@ SOURCE_BRANCH="master" TARGET_BRANCH="gh-pages" # Pull requests and commits to other branches shouldn't try to deploy, just build to verify -if [ "$TRAVIS_PULL_REQUEST" != "false" ] || [ "$TRAVIS_BRANCH" != master -a "$TRAVIS_BRANCH" != develop ]; then +if [ "$TRAVIS_PULL_REQUEST" != "false" ] || \ + [ "$TRAVIS_BRANCH" != master -a \ + "$TRAVIS_BRANCH" != develop -a \ + "$TRAVIS_BRANCH" != travis ]; then echo "No docs to upload." exit 0 fi +if [ -z "$GH_TOKEN" ]; then + echo "Error: GH_TOKEN is undefined" + exit 1 +fi + # Save some useful information REPO=`git config remote.origin.url` SHA=`git rev-parse --verify HEAD` @@ -20,19 +28,30 @@ SHA=`git rev-parse --verify HEAD` cd bin.SCons git init git config user.name "Travis CI" -git config user.email "$COMMIT_AUTHOR_EMAIL" +git config user.email "travis-ci" # Make sure 'GH_TOKEN' is set (as 'secure' variable) in .travis.yml git remote add upstream "https://$GH_TOKEN@github.com/boostorg/python.git" git fetch upstream git reset upstream/gh-pages +# Prepare version. +if [ "$TRAVIS_BRANCH" = develop -o "$TRAVIS_BRANCH" = travis ]; then + mkdir -p develop/doc/ + cp ../index.html develop/ + cp ../doc/index.html develop/doc/ + cp -a doc/html develop/doc/ + git add develop/index.html + git add develop/doc/index.html + git add -A develop/doc/html +else + cp ../index.html . + cp ../doc/index.html doc/ + git add index.html + git add doc/index.html + git add -A doc/html +fi # Commit the new version. -cp ../index.html . -cp ../doc/index.html doc/ -git add index.html -git add doc/index.html -git add -A doc/html git commit -m "Deploy to GitHub Pages: ${SHA}" # Now that we're all set up, we can push. diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..06ea5d43 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +bin.SCons +*.pyc +*~ +\#*\# \ No newline at end of file diff --git a/.travis.yml b/.travis.yml index b336967c..97a4fc8a 100644 --- a/.travis.yml +++ b/.travis.yml @@ -11,23 +11,28 @@ dist: trusty language: cpp +env: + global: + - secure: BRNUkxN3p8f+uYKWC3Hr0VPqZA0PxbWr1DJlcI4hbiZtzKhMCWjDmd9UW9CzzexqeOxpd+9s0G87qvOur+wMSVxugDxtTesZrh1czXHeSVxgQrYD783XJtQJ9aYypbChkiboRD6Xpmbq7itwMuHBJMFtCuDxMynpU1jWwkyTf2Y= + matrix: include: - - compiler: gcc - env: CXX=g++ PYTHON=python CXXFLAGS=-std=c++98 - - compiler: gcc - env: CXX=g++ PYTHON=python CXXFLAGS=-std=c++11 - - compiler: gcc - env: CXX=g++ PYTHON=python3 CXXFLAGS=-std=c++98 - - compiler: gcc - env: CXX=g++ PYTHON=python3 CXXFLAGS=-std=c++11 - - compiler: clang - env: CXX=clang++ PYTHON=python3 CXXFLAGS=-std=c++98 - - compiler: clang - env: CXX=clang++ PYTHON=python3 CXXFLAGS=-std=c++11 - - env: PYTHON=python DOC=1 - global: - - secure: mqoxglbUN/At/r8O7nLVccGldnB1jvhLHNyYjfCXrdOD0GNX+TY2TS1+kIEv9Deg/P6X/QvrBa/ZzbDNryn3mDXBfOSy400ebSIUHHP3HtGHJShOGDyXedY3hZ/dqmxdV3p9hIxv4lcx1HPyC96s4wpiR0S9F1JBzD6scIabezM= + - compiler: gcc + env: CXX=g++ PYTHON=python CXXFLAGS=-std=c++98 + - compiler: gcc + env: CXX=g++ PYTHON=python CXXFLAGS=-std=c++11 + - compiler: gcc + env: CXX=g++ PYTHON=python3 CXXFLAGS=-std=c++98 + - compiler: gcc + env: CXX=g++ PYTHON=python3 CXXFLAGS=-std=c++11 + - compiler: clang + # clang generates an 'illegal instruction' error in the NumPy check. + # Perhaps we need to upgrade clang to a newer version ? + env: CXX=clang++ PYTHON=python3 CXXFLAGS=-std=c++98 OPTIONS=--no-numpy + - compiler: clang + env: CXX=clang++ PYTHON=python3 CXXFLAGS=-std=c++11 OPTIONS=--no-numpy + - env: PYTHON=python DOC=1 + addons: apt: @@ -38,28 +43,57 @@ addons: - gcc-4.8 - g++-4.8 - clang - - python-dev python-pip + - python-numpy + - python-sphinx - python3-dev + - python3-numpy - libboost-all-dev - xsltproc + - docbook-xsl - python-docutils + +cache: + directories: + - $HOME/Boost + before_install: -# The Trusty image has several Python versions pre-installed compiled with -# conflicting UCS2 and UCS4 unicode. Modify the PATH to skip the TravisCI python. -# See https://github.com/travis-ci/travis-ci/issues/4948 for details. -- export PATH=$(echo $PATH | tr ':' "\n" | sed '/\/opt\/python/d' | tr "\n" ":" | sed "s|::|:|g") -- sudo pip install future + # The Trusty image has several Python versions pre-installed compiled with + # conflicting UCS2 and UCS4 unicode. Modify the PATH to skip the TravisCI python. + # See https://github.com/travis-ci/travis-ci/issues/4948 for details. + - export PATH=$(echo $PATH | tr ':' "\n" | sed '/\/opt\/python/d' | tr "\n" ":" | sed "s|::|:|g") install: + # Install our own version of Boost (the subset we need) as the system version is + # too old (for C++11 support). + - rm -rf $HOME/Boost + - | + set -e + if [ ! -d $HOME/Boost ]; then + echo "rebuilding Boost prerequisites" + wget https://sourceforge.net/projects/boost/files/boost/1.61.0/boost_1_61_0.tar.gz/download + tar xf download + pushd boost_1_61_0 + ./bootstrap.sh + ./b2 tools/bcp + mkdir -p $HOME/Boost + dist/bin/bcp python tools/boostbook tools/quickbook $HOME/Boost &> /dev/null + popd + fi before_script: - scons --version script: -- scons config --python=$PYTHON +- scons config --python=$PYTHON --boost-include=$HOME/Boost $OPTIONS - if [ "$DOC" ]; then scons doc; else scons && scons test; fi after_success: # Upload docs only when building upstream. -- if [ "$DOC" -a "$TRAVIS_REPO_SLUG" = "boostorg/python" -a "$TRAVIS_PULL_REQUEST" = "false" ]; then .ci/upload_docs.sh; fi +- | + if [ "$DOC" -a \ + "$TRAVIS_REPO_SLUG" = "boostorg/python" -a \ + "$TRAVIS_PULL_REQUEST" = "false" ]; then + export GH_TOKEN + .ci/upload_docs.sh + fi diff --git a/LICENSE_1_0.txt b/LICENSE_1_0.txt new file mode 100644 index 00000000..36b7cd93 --- /dev/null +++ b/LICENSE_1_0.txt @@ -0,0 +1,23 @@ +Boost Software License - Version 1.0 - August 17th, 2003 + +Permission is hereby granted, free of charge, to any person or organization +obtaining a copy of the software and accompanying documentation covered by +this license (the "Software") to use, reproduce, display, distribute, +execute, and transmit the Software, and to prepare derivative works of the +Software, and to permit third-parties to whom the Software is furnished to +do so, all subject to the following: + +The copyright notices in the Software and this entire statement, including +the above license grant, this restriction and the following disclaimer, +must be included in all copies of the Software, in whole or in part, and +all derivative works of the Software, unless such copies or derivative +works are solely in the form of machine-executable object code generated by +a source language processor. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT +SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE +FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, +ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +DEALINGS IN THE SOFTWARE. diff --git a/README.md b/README.md index acad3817..373d4e87 100644 --- a/README.md +++ b/README.md @@ -17,6 +17,8 @@ Welcome to Boost.Python, a C++ library which enables seamless interoperability b See the [Boost.Python](http://boostorg.github.io/python) documentation for details. +**Hint :** Check out the [development version](http://boostorg.github.io/python/develop) of the documentation to see work in progress. + # Building ![Build Status](https://travis-ci.org/boostorg/python.svg?branch=develop) While Boost.Python is part of the Boost C++ Libraries super-project, and thus can be compiled as part of Boost, it can also be compiled and installed stand-alone, i.e. against a pre-installed Boost package. @@ -65,4 +67,4 @@ Run ``` scons doc ``` -to build the documentation. \ No newline at end of file +to build the documentation. diff --git a/SConscript b/SConscript deleted file mode 100644 index a773cde3..00000000 --- a/SConscript +++ /dev/null @@ -1,34 +0,0 @@ -# -*- python -*- - -Import("env") - -env.Append(CPPPATH = "#/include",CPPDEFINES = ["BOOST_ALL_NO_LIB=1"]) - -env.AppendUnique(CPPDEFINES = ["${LINK_DYNAMIC and 'BOOST_PYTHON_DYN_LINK=1' or []}"]) -for variant in env["variant"]: - env["current_variant"] = variant - env.SetProperty(profile = False) - if variant == "release": - env.SetProperty(optimize = "speed", debug = False) - elif variant == "debug": - env.SetProperty(optimize = "no", debug = True) - elif variant == "profile": - env.SetProperty(optimize = "speed", profile = True, debug = True) - for linking in env["link"]: - env["linking"] = linking - if linking == "dynamic": - env["LINK_DYNAMIC"] = True - else: - env["LINK_DYNAMIC"] = False - for threading in env["threading"]: - env["current_threading"] = threading - env.SetProperty(threading = threading) - variant_dir=env.subst("$BOOST_CURRENT_VARIANT_DIR") - - env.SConscript("src/SConscript", variant_dir=variant_dir + '/src', - exports = { "env" : env.Clone(BOOST_LIB = 'python') }) - if GetOption("test"): - test_env = env.Clone(BOOST_LIB = 'python', BOOST_TEST = True) - test_env.BoostUseLib('python') - env.SConscript("test/SConscript", variant_dir=variant_dir + '/test', - exports = { "env" : test_env }) diff --git a/SConstruct b/SConstruct index 24de13dd..3f3dbfd4 100644 --- a/SConstruct +++ b/SConstruct @@ -40,7 +40,7 @@ env_vars = {} if 'CXX' in os.environ: env_vars['CXX'] = os.environ['CXX'] if 'CXXFLAGS' in os.environ: env_vars['CXXFLAGS'] = os.environ['CXXFLAGS'].split() env = Environment(toolpath=['config/tools'], - tools=['default', 'libs', 'tests', 'doc'], + tools=['default', 'libs', 'tests', 'doc', 'sphinx4scons'], variables=vars, TARGET_ARCH=arch, **env_vars) @@ -68,7 +68,7 @@ config_log = '{}/config.log'.format(build_dir) SConsignFile('{}/.sconsign'.format(build_dir)) #env.Decider('MD5-timestamp') env.Decider('timestamp-newer') -checks = config.get_checks() +checks = config.get_checks(env) if 'config' in COMMAND_LINE_TARGETS: conf=env.Configure(custom_tests=checks, log_file=config_log, conf_dir=build_dir) if False in (getattr(conf, c)() for c in checks): diff --git a/build/Jamfile.v2 b/build/Jamfile similarity index 74% rename from build/Jamfile.v2 rename to build/Jamfile index 32bffb0f..313fdab3 100644 --- a/build/Jamfile.v2 +++ b/build/Jamfile @@ -48,27 +48,8 @@ py3-version = [ find-py3-version ] ; project boost/python : source-location ../src - : requirements - -@$(BOOST_JAMROOT_MODULE)%$(BOOST_JAMROOT_MODULE).tag - @$(__name__).tag ; -rule tag ( name : type ? : property-set ) -{ - local result = $(name) ; - if $(type) in STATIC_LIB SHARED_LIB IMPORT_LIB - { - if $(name) = boost_python && $(PYTHON_ID) - { - result = $(result)-$(PYTHON_ID) ; - } - } - - # forward to the boost tagging rule - return [ indirect.call $(BOOST_JAMROOT_MODULE)%$(BOOST_JAMROOT_MODULE).tag - $(result) : $(type) : $(property-set) ] ; -} - rule cond ( test ? : yes * : no * ) { if $(test) { return $(yes) ; } else { return $(no) ; } } rule unless ( test ? : yes * : no * ) { if ! $(test) { return $(yes) ; } else { return $(no) ; } } @@ -132,20 +113,63 @@ rule lib_boost_python ( is-py3 ? ) on:BOOST_DEBUG_PYTHON [ cond $(is-py3) : $(py3-version) ] + + -@$(BOOST_JAMROOT_MODULE)%$(BOOST_JAMROOT_MODULE).tag + @$(BOOST_JAMROOT_MODULE)%$(BOOST_JAMROOT_MODULE).python-tag + : # default build shared : # usage requirements static:BOOST_PYTHON_STATIC_LIB on:BOOST_DEBUG_PYTHON ; +} +rule lib_boost_numpy ( is-py3 ? ) +{ + numpy-include = [ python.numpy-include ] ; + lib [ cond $(is-py3) : boost_numpy3 : boost_numpy ] + : # sources + numpy/dtype.cpp + numpy/matrix.cpp + numpy/ndarray.cpp + numpy/numpy.cpp + numpy/scalars.cpp + numpy/ufunc.cpp + : # requirements + [ cond [ python.numpy ] : /python//python_for_extensions ] + [ unless [ python.numpy ] : no ] + $(numpy-include) + boost_python + on:BOOST_DEBUG_PYTHON + [ cond $(is-py3) : $(py3-version) ] + + -@$(BOOST_JAMROOT_MODULE)%$(BOOST_JAMROOT_MODULE).tag + @$(BOOST_JAMROOT_MODULE)%$(BOOST_JAMROOT_MODULE).python-tag + + : # default build + shared + : # usage requirements + on:BOOST_DEBUG_PYTHON + ; +} + +libraries = boost_python ; +libraries3 = boost_python3 ; +if [ python.numpy ] +{ + libraries += boost_numpy ; + libraries3 += boost_numpy3 ; } lib_boost_python ; -boost-install boost_python ; +lib_boost_numpy ; if $(py3-version) { lib_boost_python yes ; - boost-install boost_python3 ; + lib_boost_numpy yes ; + libraries += $(libraries3) ; } + +boost-install $(libraries) ; diff --git a/build/python_v1.zip b/build/python_v1.zip deleted file mode 100644 index 0377a07b..00000000 Binary files a/build/python_v1.zip and /dev/null differ diff --git a/config/__init__.py b/config/__init__.py index b08eb04b..704124b6 100644 --- a/config/__init__.py +++ b/config/__init__.py @@ -13,11 +13,14 @@ import platform from . import ui from . import cxx from . import python +from . import numpy from . import boost def add_options(vars): ui.add_option('-V', '--verbose', dest='verbose', action='store_true', help='verbose mode: print full commands.') + ui.add_option('--no-numpy', dest='numpy', action='store_false', help='do not attempt to build NumPy bindings.') python.add_options(vars) + numpy.add_options(vars) boost.add_options(vars) vars.Add('CXX') @@ -29,8 +32,10 @@ def add_options(vars): vars.Add('PYTHON') vars.Add('PYTHONLIBS') vars.Add('prefix') - vars.Add('boostbook_prefix', - vars.Add('CXX11')) + vars.Add('boostbook_prefix') + vars.Add('CXX11') + vars.Add('NUMPY') + vars.Add('NUMPY_CPPPATH', converter=lambda v:v.split()) ui.add_variable(vars, ("arch", "target architeture", platform.machine())) ui.add_variable(vars, ("toolchain", "toolchain to use", 'gcc')) @@ -42,10 +47,14 @@ def add_options(vars): ui.add_variable(vars, PathVariable("prefix", "Install prefix", "/usr/local", PathVariable.PathAccept)) -def get_checks(): +def get_checks(env): checks = OrderedDict() checks['cxx'] = cxx.check checks['python'] = python.check + if env.GetOption('numpy') is not False: + checks['numpy'] = numpy.check + else: + env['NUMPY'] = False checks['boost'] = boost.check return checks diff --git a/config/numpy.py b/config/numpy.py new file mode 100644 index 00000000..3380ae56 --- /dev/null +++ b/config/numpy.py @@ -0,0 +1,86 @@ +# +# Copyright (c) 2016 Stefan Seefeld +# All rights reserved. +# +# Distributed under the Boost Software License, Version 1.0. +# (See accompanying file LICENSE_1_0.txt or copy at +# http://www.boost.org/LICENSE_1_0.txt) + +from . import ui +from contextlib import contextmanager + +@contextmanager +def saved(context): + save_cpppath = context.env.get('CPPPATH', []) + save_libs = context.env.get('LIBS', []) + yield context + context.env.Replace(LIBS=save_libs) + context.env.Replace(CPPPATH=save_cpppath) + + +def add_options(vars): + + pass + + +def check(context): + + numpy_source_file = r""" +// If defined, enforces linking againg PythonXXd.lib, which +// is usually not included in Python environments. +#undef _DEBUG +#include "Python.h" +#include "numpy/arrayobject.h" + +#if PY_VERSION_HEX >= 0x03000000 +void *initialize() { import_array();} +#else +void initialize() { import_array();} +#endif + +int main() +{ + int result = 0; + Py_Initialize(); + initialize(); + if (PyErr_Occurred()) + { + result = 1; + } + else + { + npy_intp dims = 2; + PyObject * a = PyArray_SimpleNew(1, &dims, NPY_INT); + if (!a) result = 1; + Py_DECREF(a); + } + Py_Finalize(); + return result; +} +""" + + import platform + import subprocess + import re, os + + def check_python(cmd): + try: + return True, subprocess.check_output([python, '-c', cmd]).strip() + except subprocess.CalledProcessError as e: + return False, e + + context.Message('Checking for NumPy...') + with saved(context): + python = context.env['PYTHON'] + result, numpy_incpath = check_python('import numpy; print(numpy.get_include())') + if result: + context.env.AppendUnique(CPPPATH=numpy_incpath) + context.env.AppendUnique(LIBS=context.env['PYTHONLIBS']) + result, output = context.TryRun(numpy_source_file,'.cpp') + if not result: + context.Result(0) + return False + context.env['NUMPY'] = True + context.env['NUMPY_CPPPATH'] = numpy_incpath + context.Result(1) + return True diff --git a/config/tools/doc.py b/config/tools/doc.py index e96f089a..5e81f379 100644 --- a/config/tools/doc.py +++ b/config/tools/doc.py @@ -58,6 +58,10 @@ def BoostRST(env, target, source, resources=[]): 'rst2html --link-stylesheet --traceback --trim-footnote-reference-space --footnote-references=superscript --stylesheet=rst.css $SOURCE $TARGET') +def BoostSphinx(env, target, source): + env.Sphinx(target, source) + + def exists(env): return True @@ -68,3 +72,4 @@ def generate(env): env.AddMethod(BoostBook) env.AddMethod(BoostHTML) env.AddMethod(BoostRST) + env.AddMethod(BoostSphinx) diff --git a/config/tools/sphinx4scons.py b/config/tools/sphinx4scons.py new file mode 100644 index 00000000..dda88d23 --- /dev/null +++ b/config/tools/sphinx4scons.py @@ -0,0 +1,592 @@ +"""SCons.Tool.spinx4scons + +Tool-specific initialization for the Sphinx document build system. + +There normally shouldn't be any need to import this module directly. +It will usually be imported through the generic SCons.Tool.Tool() +selection method. + +It should be placed in e.g. ~/site_scons/site_tools/sphinx4scons/ +directory. Then it may be loaded by placing + + sphinx = Tool('sphinx4scons') + sphinx(env) + +in your SConstruct file. + +For further details, please see the SCons documentation on how to +install and enable custom tools. +""" + +# +# This package is provided under the Expat license +# +# Copyright (c) 2012 Orlando Wingbrant +# +# Permission is hereby granted, free of charge, to any person obtaining +# a copy of this software and associated documentation files (the +# "Software"), to deal in the Software without restriction, including +# without limitation the rights to use, copy, modify, merge, publish, +# distribute, sublicense, and/or sell copies of the Software, and to +# permit persons to whom the Software is furnished to do so, subject to +# the following conditions: +# +# The above copyright notice and this permission notice shall be included +# in all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY +# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, +# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE +# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +# + +__author__ = "Orlando Wingbrant" +__email__ = "orlando@widesite.org" +__url__ = "https://bitbucket.org/wingbrant/sphinx4scons" +__license__ = "Expat license" + +import SCons.Action +import SCons.Builder +import SCons.Defaults +import SCons.Util +import SCons.Node.FS +import os + +from sphinx.util.matching import patfilter, compile_matchers +from sphinx.util.osutil import make_filename + + +class ToolSphinxWarning(SCons.Warnings.Warning): + pass + + +class SphinxBuilderNotFound(ToolSphinxWarning): + pass + +SCons.Warnings.enableWarningClass(ToolSphinxWarning) + + +def exists(env): + return _detect(env) + + +def _detect(env): + """Try to detect the sphinx-build script.""" + try: + return env['SPHINXBUILD'] + except KeyError: + pass + + sphinx = env.WhereIs('sphinx-build') + if sphinx: + return sphinx + + raise SCons.Errors.StopError( + SphinxBuilderNotFound, + "Could not detect sphinx-build script") + return None + + +def generate(env): + """Add Builders and construction variables to the Environment.""" + + env['SPHINXBUILD'] = _detect(env) + sphinx = _create_sphinx_builder(env) + + env.SetDefault( + # Additional command-line flags + SPHINXFLAGS = '', + + # Tag definitions, each entry will appear on the command line preceded by -t + SPHINXTAGS = [], + + # Directory for doctrees + SPHINXDOCTREE = '', + + # Path to sphinx configuration file + SPHINXCONFIG = '', + + # Config file override settings, each entry will be preceded by -D + SPHINXSETTINGS = {}, + + # Default sphinx builder, + SPHINXBUILDER = 'html', + + # Sphinx command + SPHINXCOM = "$SPHINXBUILD $_SPHINXOPTIONS ${SOURCE.attributes.root} ${TARGET.attributes.root}", + + # Alternate console output when building sphinx documents + SPHINXCOMSTR = "" + ) + + try: + env.AddMethod(Sphinx, "Sphinx") + except AttributeError: + # Looks like we use a pre-0.98 version of SCons... + from SCons.Script.SConscript import SConsEnvironment + SConsEnvironment.Sphinx = Sphinx + + +def Sphinx(env, target, source, **kw): + """A pseudo-builder wrapper for the sphinx builder.""" + builder = env['BUILDERS']['Sphinx4Scons'] + env_kw = env.Override(kw) + options = _get_sphinxoptions(env_kw, target, source) + output = builder(env, target, source, _SPHINXOPTIONS=options, **kw) + return output + + +def _get_sphinxoptions(env, target, source): + """Concatenates all the options for the sphinx command line.""" + options = [] + + builder = _get_sphinxbuilder(env) + options.append("-b %s" % env.subst(builder, target=target, source=source)) + + flags = env.get('options', env.get('SPHINXFLAGS', '')) + options.append(env.subst(flags, target=target, source=source)) + + tags = env.get('tags', env.get('SPHINXTAGS', None)) + if tags is not None: + if not SCons.SCons.Util.is_List(tags): + tags = [tags] + for tag in tags: + if tag != '': + tag = env.subst(tag, target=target, source=source) + options.append("-t %s" % tag) + + settings = env.get('settings', env.get('SPHINXSETTINGS', None)) + if settings is not None: + if not SCons.SCons.Util.is_Dict(settings): + raise TypeError('SPHINXSETTINGS and/or settings argument must be a dictionary') + for key, value in settings.iteritems(): + if value != '': + value = env.subst(value, target=target, source=source) + options.append('-D "%s=%s"' % (key, value)) + + doctree = env.get('doctree', env.get("SPHINXDOCTREE", None)) + if isinstance(doctree, SCons.Node.FS.Dir): + options.append("-d %s" % doctree.get_abspath()) + elif doctree is not None and doctree != '': + doctree = env.subst(doctree, target=target, source=source) + options.append("-d %s" % env.Dir(doctree).get_abspath()) + + config = _get_sphinxconfig_path(env, None) + if config is not None and config != '': + config = env.subst(config, target=target, source=source) + options.append("-c %s" % env.Dir(config).File('conf.py').rfile().dir.get_abspath()) + return " ".join(options) + + +def _create_sphinx_builder(env): + try: + sphinx = env['BUILDERS']['Sphinx4Scons'] + except KeyError: + fs = SCons.Node.FS.get_default_fs() + sphinx_com = SCons.Action.Action('$SPHINXCOM', '$SPHINXCOMSTR') + sphinx = SCons.Builder.Builder(action=sphinx_com, + emitter=sphinx_emitter, + target_factory=fs.Dir, + source_factory=fs.Dir + ) + env['BUILDERS']['Sphinx4Scons'] = sphinx + return sphinx + + +def sphinx_emitter(target, source, env): + target[0].must_be_same(SCons.Node.FS.Dir) + targetnode = target[0] + + source[0].must_be_same(SCons.Node.FS.Dir) + srcnode = source[0] + + configdir = _get_sphinxconfig_path(env, None) + if not configdir: + confignode = srcnode + else: + confignode = env.Dir(configdir) + + srcinfo = SourceInfo(srcnode, confignode, env) + targets, sources = _get_emissions(env, target, srcinfo) + env.Clean(targets, target[0]) + + return targets, sources + + +def sphinx_path(os_path): + """Create sphinx-style path from os-style path.""" + return os_path.replace(os.sep, "/") + + +def os_path(sphinx_path): + """Create os-style path from sphinx-style path.""" + return sphinx_path.replace("/", os.sep) + + +class SourceInfo(object): + """ + Data container for all different kinds of source files used in + a sphinx project. + """ + def __init__(self, srcnode, confignode, env): + self.confignode = confignode + self.config = self._get_config(self.confignode, env) + self.templates = self._get_templates(self.confignode, self.config) + self.statics = self._get_statics(self.confignode, self.config) + self.srcnode = srcnode + self.sources = self._get_sources(self.srcnode, self.config) + + self.srcroot = srcnode + if not srcnode.duplicate: + self.srcroot = srcnode.srcnode().rdir() + + + def _get_config(self, confignode, env): + config = {} + execfile(confignode.File('conf.py').rfile().get_abspath(), config) + return config + + + def _get_templates(self, confignode, config): + """Returns template files defined in the project.""" + templates = [] + for path in config.get('templates_path', []): + # Check if path is dir or file. + # We can't use FS.Entry since that will create nodes, and + # these nodes don't know about the source tree and will + # get disambiguated to files even if they are directories in the + # source tree. + p = confignode.File('conf.py').rfile().dir.srcnode().get_abspath() + p = os.path.join(p, os_path(path)) + if os.path.isfile(p): + templates.append(confignode.File(path)) + elif os.path.isdir(p): + node = confignode.Dir(path) + for root, dirs, files in os.walk(p): + mydir = node.Dir(os.path.relpath(root, p)) + templates += [mydir.File(f) for f in files] + return templates + + + def _get_statics(self, confignode, config): + """Returns static files, filtered through exclude_patterns.""" + statics = [] + matchers = compile_matchers(config.get('exclude_patterns', [])) + + for path in config.get('html_static_path', []): + # Check _get_templates() why we use this construction. + p = confignode.File('conf.py').rfile().dir.srcnode().get_abspath() + p = os.path.join(p, os_path(path)) + if os.path.isfile(p): + statics.append(confignode.File(path)) + elif os.path.isdir(p): + node = confignode.Dir(path) + for root, dirs, files in os.walk(p): + relpath = os.path.relpath(root, p) + for entry in [d for d in dirs if + self._anymatch(matchers, + sphinx_path(os.path.join(relpath, d)))]: + dirs.remove(entry) + statics += [node.File(os_path(f)) for f in + self._exclude(matchers, + [sphinx_path(os.path.join(relpath, name)) + for name in files])] + return statics + + + def _get_sources(self, srcnode, config): + """Returns all source files in the project filtered through exclude_patterns.""" + suffix = config.get('source_suffix', '.rst') + matchers = compile_matchers(config.get('exclude_patterns', [])) + + srcfiles = [] + scannode = srcnode.srcnode().rdir() + + for root, dirs, files in os.walk(scannode.get_abspath()): + relpath = os.path.relpath(root, scannode.get_abspath()) + for entry in [d for d in dirs if + self._anymatch(matchers, + sphinx_path(os.path.join(relpath, d)))]: + dirs.remove(entry) + srcfiles += [srcnode.File(os_path(f)) for f in + self._exclude(matchers, + [sphinx_path(os.path.join(relpath, name)) + for name in files if name.endswith(suffix)])] + return srcfiles + + + def _exclude(self, matchers, items): + result = items + for matcher in matchers: + result = filter(lambda x: not matcher(x), result) + return result + + + def _anymatch(self, matchers, item): + for matcher in matchers: + if matcher(item): + return True + return False + + +def _get_sphinxconfig_path(env, default): + path = env.get('config', env.get('SPHINXCONFIG', None)) + if path is None or path == '': + path = default + return path + + +def _get_emissions(env, target, srcinfo): + targets = [] + sources = [] + builder = _get_sphinxbuilder(env) + if builder == 'changes': + targets, sources = _get_changes_emissions(env, target, srcinfo) + if builder == 'devhelp': + targets, sources = _get_help_emissions(env, target, srcinfo, + ['.devhelp.gz']) + elif builder == 'dirhtml': + targets, sources = _get_dirhtml_emissions(env, target, srcinfo) + elif builder == 'doctest': + targets, sources = _get_doctest_emissions(env, target, srcinfo) + elif builder == 'epub': + targets, sources = _get_epub_emissions(env, target, srcinfo) + elif builder == 'html': + targets, sources = _get_serialize_emissions(env, target, srcinfo) + elif builder == 'htmlhelp': + targets, sources = _get_help_emissions(env, target, srcinfo, + ['.hhp'], 'htmlhelp_basename') + elif builder == 'gettext': + targets, sources = _get_gettext_emissions(env, target, srcinfo) + elif builder == 'json': + targets, sources = _get_serialize_emissions(env, target, srcinfo, + '.fjson', + ['globalcontext.json', + 'searchindex.json', + 'self.environment.pickle']) + elif builder == 'latex': + targets, sources = _get_latex_emissions(env, target, srcinfo) + elif builder == 'linkcheck': + targets, sources = _get_linkcheck_emissions(env, target, srcinfo) + elif builder == 'man': + targets, sources = _get_man_emissions(env, target, srcinfo) + elif builder == 'pickle': + targets, sources = _get_serialize_emissions(env, target, srcinfo, + '.fpickle', + ['globalcontext.pickle', + 'searchindex.pickle', + 'environment.pickle']) + elif builder == 'qthelp': + targets, sources = _get_help_emissions(env, target, srcinfo, + ['.qhp', '.qhcp']) + elif builder == 'singlehtml': + targets, sources = _get_singlehtml_emissions(env, target, srcinfo) + elif builder == 'texinfo': + targets, sources = _get_texinfo_emissions(env, target, srcinfo) + elif builder == 'text': + targets, sources = _get_text_emissions(env, target, srcinfo) + + sources.append(srcinfo.confignode.File('conf.py')) + + for s in sources: + s.attributes.root = srcinfo.srcroot + + for t in targets: + t.attributes.root = target[0] + + return targets, sources + + +def _get_sphinxbuilder(env): + builder = env.get('builder', env["SPHINXBUILDER"]) + if builder is None or builder == '': + raise SCons.Errors.UserError(("Missing construction variable " + + "SPHINXBUILDER or variable is empty.")) + return builder + + +def _get_changes_emissions(env, target, srcinfo): + sources = [] + sources.extend(srcinfo.sources) + targets = [target[0].File("changes.html")] + return targets, sources + + +def _get_dirhtml_emissions(env, target, srcinfo): + suffix = srcinfo.config.get('html_file_suffix', ".html") + + def get_outfilename(pagename): + pagename = os.path.splitext(pagename)[0] + + #Special treatment of files named "index". Don't create directory. + if pagename == 'index' or pagename.endswith(os.sep + 'index'): + outfilename = pagename + suffix + else: + outfilename = os.path.join(pagename, 'index' + suffix) + return outfilename + + sources = [] + sources.extend(srcinfo.sources) + sources.extend(srcinfo.templates) + sources.extend(srcinfo.statics) + + targets = [] + for s in srcinfo.sources: + t = os.path.relpath(str(s), str(srcinfo.srcroot)) + targets.append(target[0].File(get_outfilename(t))) + + for key in srcinfo.config.get('html_additional_pages', {}): + t = target[0].File(get_outfilename(key)) + targets.append(t) + + return targets, sources + + +def _get_doctest_emissions(env, target, srcinfo): + sources = [] + sources.extend(srcinfo.sources) + targets = [target[0].File("output.txt")] + return targets, sources + + +def _get_epub_emissions(env, target, srcinfo): + epubPreFiles = srcinfo.config.get('epub_pre_files', []) + epubPostFiles = srcinfo.config.get('epub_post_files', []) + epubCover = srcinfo.config.get('epub_cover', (None, None)) + + sources = [] + sources.extend(srcinfo.sources) + sources.extend([srcinfo.srcroot.File(os_path(f[0])) for f in epubPreFiles]) + sources.extend([srcinfo.srcroot.File(os_path(f[0])) for f in epubPostFiles]) + if not (epubCover[0] is None or epubCover[0] == ''): + sources.append(srcinfo.srcroot.File(os_path(epubCover[0]))) + if not (epubCover[1] is None or epubCover[1] == ''): + sources.append(srcinfo.srcroot.File(os_path(epubCover[1]))) + + t = srcinfo.config.get('epub_basename', + srcinfo.config.get('project', + 'Python')) + + targets = [target[0].File("%s.epub" % make_filename(t))] + + return targets, sources + + +def _get_gettext_emissions(env, target, srcinfo): + sources = [] + sources.extend(srcinfo.sources) + + targets = [os.path.relpath(str(s), str(srcinfo.srcroot)) for s in sources] + targets = [os.path.splitext(t)[0] for t in targets] + targets = set([t.split(os.sep)[0] for t in targets]) + targets = [target[0].File(t + ".pot") for t in targets] + + return targets, sources + + +def _get_help_emissions(env, target, srcinfo, suffixes, basenameConfigKey='project'): + basename = make_filename( + srcinfo.config.get(basenameConfigKey, srcinfo.config['project'])) + + sources = [] + sources.extend(srcinfo.sources) + sources.extend(srcinfo.templates) + sources.extend(srcinfo.statics) + + targets = [target[0].File(basename + s) for s in suffixes] + + return targets, sources + + +def _get_latex_emissions(env, target, srcinfo): + sources = [] + sources.extend(srcinfo.sources) + + targets = map(lambda x: target[0].File(os_path(x[1])), + srcinfo.config.get('latex_documents')) + + return targets, sources + + +def _get_linkcheck_emissions(env, target, srcinfo): + sources = [] + sources.extend(srcinfo.sources) + targets = [target[0].File("output.txt")] + return targets, sources + + +def _get_man_emissions(env, target, srcinfo): + sources = [] + sources.extend(srcinfo.sources) + targets = map(lambda x: target[0].File(os_path("%s.%s" % (x[1], x[4]))), + srcinfo.config.get('man_pages')) + return targets, sources + + +def _get_serialize_emissions(env, target, srcinfo, suffix=None, extrafiles=[]): + if suffix is None: + suffix = srcinfo.config.get('html_file_suffix', '.html') + + sources = [] + sources.extend(srcinfo.sources) + sources.extend(srcinfo.templates) + sources.extend(srcinfo.statics) + + targets = [] + for s in srcinfo.sources: + t = os.path.splitext(str(s))[0] + suffix + t = os.path.relpath(t, str(srcinfo.srcroot)) + targets.append(t) + + for key in srcinfo.config.get('html_additional_pages', {}): + targets.append(os_path("%s%s" % (key, suffix))) + + targets.extend(extrafiles) + targets = [target[0].File(t) for t in targets] + + return targets, sources + + +def _get_singlehtml_emissions(env, target, srcinfo): + suffix = srcinfo.config.get('html_file_suffix', ".html") + + sources = [] + sources.extend(srcinfo.sources) + sources.extend(srcinfo.templates) + sources.extend(srcinfo.statics) + + t = os.path.relpath(srcinfo.config['master_doc'] + suffix, + str(srcinfo.srcroot)) + targets = [target[0].File(t)] + + return targets, sources + + +def _get_texinfo_emissions(env, target, srcinfo): + suffix = srcinfo.config.get('source_suffix', '.rst') + + sources = [] + sources.extend(srcinfo.sources) + sources.extend(map(lambda x: source[0].File(os_path(x + suffix)), + srcinfo.config.get('texinfo_appendices', []))) + + targets = map(lambda x: target[0].File(os_path("%s.texi" % x[1])), + srcinfo.config.get('texinfo_documents')) + + return targets, sources + + +def _get_text_emissions(env, target, srcinfo): + sources = [] + sources.extend(srcinfo.sources) + + targets = [] + for s in sources: + t = os.path.relpath(str(s), str(srcinfo.srcroot)) + t = os.path.splitext(t)[0] + ".txt" + targets.append(target[0].File(t)) + + return targets, sources diff --git a/doc/Jamfile b/doc/Jamfile index a68555ad..735ef22e 100644 --- a/doc/Jamfile +++ b/doc/Jamfile @@ -4,10 +4,14 @@ # Distributed under the Boost Software License, Version 1.0. (See accompanying # file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +import boostbook ; +import quickbook ; +import docutils ; +import os ; + path-constant here : . ; path-constant images : html/images ; - project python/doc : requirements -boost.defaults=Boost @@ -17,9 +21,16 @@ project python/doc html:chunk.section.depth=1 ; -import boostbook ; -import quickbook ; -import docutils ; +make numpy : numpy/index.rst : @sphinx-build ; + +if [ os.name ] = NT +{ + actions sphinx-build { chdir "$(>:D)" && make clean && make html} +} +else +{ + actions sphinx-build { make -C "$(>:D)" clean html} +} boostbook python : python.qbk : html:$(here)/html @@ -51,5 +62,5 @@ html article : article.rst ############################################################################### alias boostdoc ; explicit boostdoc ; -alias boostrelease : python tutorial reference article ; +alias boostrelease : python tutorial reference numpy article ; explicit boostrelease ; diff --git a/doc/SConscript b/doc/SConscript index e22b3fc2..8eb0fb93 100644 --- a/doc/SConscript +++ b/doc/SConscript @@ -47,3 +47,6 @@ env.BoostHTML('html/reference/', 'reference.dbk', '--stringparam', 'boost.graphics.root', '../images/']) env.BoostRST('html/article.html', 'article.rst', resources=['rst.css']) + +if env['NUMPY']: + env.BoostSphinx('html/numpy', 'numpy/') diff --git a/doc/numpy/Makefile b/doc/numpy/Makefile new file mode 100644 index 00000000..f0ffaaea --- /dev/null +++ b/doc/numpy/Makefile @@ -0,0 +1,133 @@ +# Makefile for Sphinx documentation +# + +# You can set these variables from the command line. +SPHINXOPTS = +SPHINXBUILD = sphinx-build +PAPER = +BUILDDIR = _build +HTMLDIR = ../html/numpy + +# Internal variables. +PAPEROPT_a4 = -D latex_paper_size=a4 +PAPEROPT_letter = -D latex_paper_size=letter +ALLSPHINXOPTS = -d $(BUILDDIR)/doctrees $(PAPEROPT_$(PAPER)) $(SPHINXOPTS) . + +.PHONY: help clean html dirhtml singlehtml pickle json htmlhelp qthelp devhelp epub latex latexpdf text man changes linkcheck doctest + +all: html + +help: + @echo "Please use \`make ' where is one of" + @echo " html to make standalone HTML files" + @echo " dirhtml to make HTML files named index.html in directories" + @echo " singlehtml to make a single large HTML file" + @echo " pickle to make pickle files" + @echo " json to make JSON files" + @echo " htmlhelp to make HTML files and a HTML help project" + @echo " qthelp to make HTML files and a qthelp project" + @echo " devhelp to make HTML files and a Devhelp project" + @echo " epub to make an epub" + @echo " latex to make LaTeX files, you can set PAPER=a4 or PAPER=letter" + @echo " latexpdf to make LaTeX files and run them through pdflatex" + @echo " text to make text files" + @echo " man to make manual pages" + @echo " changes to make an overview of all changed/added/deprecated items" + @echo " linkcheck to check all external links for integrity" + @echo " doctest to run all doctests embedded in the documentation (if enabled)" + +clean: + -rm -rf $(BUILDDIR)/* + +html: + $(SPHINXBUILD) -b html $(ALLSPHINXOPTS) $(HTMLDIR) + @echo + @echo "Build finished. The HTML pages are in $(HTMLDIR)." + +dirhtml: + $(SPHINXBUILD) -b dirhtml $(ALLSPHINXOPTS) $(BUILDDIR)/dirhtml + @echo + @echo "Build finished. The HTML pages are in $(BUILDDIR)/dirhtml." + +singlehtml: + $(SPHINXBUILD) -b singlehtml $(ALLSPHINXOPTS) $(BUILDDIR)/singlehtml + @echo + @echo "Build finished. The HTML page is in $(BUILDDIR)/singlehtml." + +pickle: + $(SPHINXBUILD) -b pickle $(ALLSPHINXOPTS) $(BUILDDIR)/pickle + @echo + @echo "Build finished; now you can process the pickle files." + +json: + $(SPHINXBUILD) -b json $(ALLSPHINXOPTS) $(BUILDDIR)/json + @echo + @echo "Build finished; now you can process the JSON files." + +htmlhelp: + $(SPHINXBUILD) -b htmlhelp $(ALLSPHINXOPTS) $(BUILDDIR)/htmlhelp + @echo + @echo "Build finished; now you can run HTML Help Workshop with the" \ + ".hhp project file in $(BUILDDIR)/htmlhelp." + +qthelp: + $(SPHINXBUILD) -b qthelp $(ALLSPHINXOPTS) $(BUILDDIR)/qthelp + @echo + @echo "Build finished; now you can run "qcollectiongenerator" with the" \ + ".qhcp project file in $(BUILDDIR)/qthelp, like this:" + @echo "# qcollectiongenerator $(BUILDDIR)/qthelp/BoostNumPy.qhcp" + @echo "To view the help file:" + @echo "# assistant -collectionFile $(BUILDDIR)/qthelp/BoostNumPy.qhc" + +devhelp: + $(SPHINXBUILD) -b devhelp $(ALLSPHINXOPTS) $(BUILDDIR)/devhelp + @echo + @echo "Build finished." + @echo "To view the help file:" + @echo "# mkdir -p $$HOME/.local/share/devhelp/BoostNumPy" + @echo "# ln -s $(BUILDDIR)/devhelp $$HOME/.local/share/devhelp/BoostNumPy" + @echo "# devhelp" + +epub: + $(SPHINXBUILD) -b epub $(ALLSPHINXOPTS) $(BUILDDIR)/epub + @echo + @echo "Build finished. The epub file is in $(BUILDDIR)/epub." + +latex: + $(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex + @echo + @echo "Build finished; the LaTeX files are in $(BUILDDIR)/latex." + @echo "Run \`make' in that directory to run these through (pdf)latex" \ + "(use \`make latexpdf' here to do that automatically)." + +latexpdf: + $(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex + @echo "Running LaTeX files through pdflatex..." + make -C $(BUILDDIR)/latex all-pdf + @echo "pdflatex finished; the PDF files are in $(BUILDDIR)/latex." + +text: + $(SPHINXBUILD) -b text $(ALLSPHINXOPTS) $(BUILDDIR)/text + @echo + @echo "Build finished. The text files are in $(BUILDDIR)/text." + +man: + $(SPHINXBUILD) -b man $(ALLSPHINXOPTS) $(BUILDDIR)/man + @echo + @echo "Build finished. The manual pages are in $(BUILDDIR)/man." + +changes: + $(SPHINXBUILD) -b changes $(ALLSPHINXOPTS) $(BUILDDIR)/changes + @echo + @echo "The overview file is in $(BUILDDIR)/changes." + +linkcheck: + $(SPHINXBUILD) -b linkcheck $(ALLSPHINXOPTS) $(BUILDDIR)/linkcheck + @echo + @echo "Link check complete; look for any errors in the above output " \ + "or in $(BUILDDIR)/linkcheck/output.txt." + +doctest: + $(SPHINXBUILD) -b doctest $(ALLSPHINXOPTS) $(BUILDDIR)/doctest + @echo "Testing of doctests in the sources finished, look at the " \ + "results in $(BUILDDIR)/doctest/output.txt." diff --git a/doc/numpy/_static/boost.css b/doc/numpy/_static/boost.css new file mode 100644 index 00000000..28f89359 --- /dev/null +++ b/doc/numpy/_static/boost.css @@ -0,0 +1,716 @@ + +/*============================================================================= +Copyright (c) 2004 Joel de Guzman +http://spirit.sourceforge.net/ + +Copyright 2013 Niall Douglas additions for colors and alignment. +Copyright 2013 Paul A. Bristow additions for more colors and alignments. + +Distributed under the Boost Software License, Version 1.0. (See accompany- +ing file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +=============================================================================*/ + +/*============================================================================= +Body defaults +=============================================================================*/ + + body + { + margin: 1em; + font-family: sans-serif; + } + +/*============================================================================= +Paragraphs +=============================================================================*/ + + p + { + text-align: left; + font-size: 10pt; + line-height: 1.15; + } + +/*============================================================================= +Program listings +=============================================================================*/ + + /* Code on paragraphs */ + p tt.computeroutput + { + font-size: 9pt; + } + + pre.synopsis + { + font-size: 9pt; + margin: 1pc 4% 0pc 4%; + padding: 0.5pc 0.5pc 0.5pc 0.5pc; + } + + .programlisting, + .screen + { + font-size: 9pt; + display: block; + margin: 1pc 4% 0pc 4%; + padding: 0.5pc 0.5pc 0.5pc 0.5pc; + } + + /* Program listings in tables don't get borders */ + td .programlisting, + td .screen + { + margin: 0pc 0pc 0pc 0pc; + padding: 0pc 0pc 0pc 0pc; + } + +/*============================================================================= +Headings +=============================================================================*/ + + h1, h2, h3, h4, h5, h6 + { + text-align: left; + margin: 1em 0em 0.5em 0em; + font-weight: bold; + } + + h1 { font-size: 140%; } + h2 { font-weight: bold; font-size: 140%; } + h3 { font-weight: bold; font-size: 130%; } + h4 { font-weight: bold; font-size: 120%; } + h5 { font-weight: normal; font-style: italic; font-size: 110%; } + h6 { font-weight: normal; font-style: italic; font-size: 100%; } + + /* Top page titles */ + title, + h1.title, + h2.title + h3.title, + h4.title, + h5.title, + h6.title, + .refentrytitle + { + font-weight: bold; + margin-bottom: 1pc; + } + + h1.title { font-size: 140% } + h2.title { font-size: 140% } + h3.title { font-size: 130% } + h4.title { font-size: 120% } + h5.title { font-size: 110% } + h6.title { font-size: 100% } + + .section h1 + { + margin: 0em 0em 0.5em 0em; + font-size: 140%; + } + + .section h2 { font-size: 140% } + .section h3 { font-size: 130% } + .section h4 { font-size: 120% } + .section h5 { font-size: 110% } + .section h6 { font-size: 100% } + + /* Code on titles */ + h1 tt.computeroutput { font-size: 140% } + h2 tt.computeroutput { font-size: 140% } + h3 tt.computeroutput { font-size: 130% } + h4 tt.computeroutput { font-size: 130% } + h5 tt.computeroutput { font-size: 130% } + h6 tt.computeroutput { font-size: 130% } + + +/*============================================================================= +Author +=============================================================================*/ + + h3.author + { + font-size: 100% + } + +/*============================================================================= +Lists +=============================================================================*/ + + li + { + font-size: 10pt; + line-height: 1.3; + } + + /* Unordered lists */ + ul + { + text-align: left; + } + + /* Ordered lists */ + ol + { + text-align: left; + } + +/*============================================================================= +Links +=============================================================================*/ + + a + { + text-decoration: none; /* no underline */ + } + + a:hover + { + text-decoration: underline; + } + +/*============================================================================= +Spirit style navigation +=============================================================================*/ + + .spirit-nav + { + text-align: right; + } + + .spirit-nav a + { + color: white; + padding-left: 0.5em; + } + + .spirit-nav img + { + border-width: 0px; + } + +/*============================================================================= +Copyright footer +=============================================================================*/ + .copyright-footer + { + text-align: right; + font-size: 70%; + } + + .copyright-footer p + { + text-align: right; + font-size: 80%; + } + +/*============================================================================= +Table of contents +=============================================================================*/ + + div.toc + { + margin: 1pc 4% 0pc 4%; + padding: 0.1pc 1pc 0.1pc 1pc; + font-size: 80%; + line-height: 1.15; + } + + .boost-toc + { + float: right; + padding: 0.5pc; + } + + /* Code on toc */ + .toc .computeroutput { font-size: 120% } + + /* No margin on nested menus */ + + .toc dl dl { margin: 0; } + +/*============================================================================= +Tables +=============================================================================*/ + + .table-title, + div.table p.title + { + margin-left: 4%; + padding-right: 0.5em; + padding-left: 0.5em; + } + + .informaltable table, + .table table + { + width: 92%; + margin-left: 4%; + margin-right: 4%; + } + + div.informaltable table, + div.table table + { + padding: 4px; + } + + /* Table Cells */ + div.informaltable table tr td, + div.table table tr td + { + padding: 0.5em; + text-align: left; + font-size: 9pt; + } + + div.informaltable table tr th, + div.table table tr th + { + padding: 0.5em 0.5em 0.5em 0.5em; + border: 1pt solid white; + font-size: 80%; + } + + table.simplelist + { + width: auto !important; + margin: 0em !important; + padding: 0em !important; + border: none !important; + } + table.simplelist td + { + margin: 0em !important; + padding: 0em !important; + text-align: left !important; + font-size: 9pt !important; + border: none !important; + } + +/*============================================================================= +Suppress margins in tables +=============================================================================*/ + + table th > *:first-child, + table td > *:first-child + { + margin-top: 0; + } + + table th > *:last-child, + table td > *:last-child + { + margin-bottom: 0; + } + +/*============================================================================= +Blurbs +=============================================================================*/ + + div.note, + div.tip, + div.important, + div.caution, + div.warning, + p.blurb + { + font-size: 9pt; /* A little bit smaller than the main text */ + line-height: 1.2; + display: block; + margin: 1pc 4% 0pc 4%; + padding: 0.5pc 0.5pc 0.5pc 0.5pc; + } + + p.blurb img + { + padding: 1pt; + } + +/*============================================================================= +Variable Lists +=============================================================================*/ + + div.variablelist + { + margin: 1em 0; + } + + /* Make the terms in definition lists bold */ + div.variablelist dl dt, + span.term + { + font-weight: bold; + font-size: 10pt; + } + + div.variablelist table tbody tr td + { + text-align: left; + vertical-align: top; + padding: 0em 2em 0em 0em; + font-size: 10pt; + margin: 0em 0em 0.5em 0em; + line-height: 1; + } + + div.variablelist dl dt + { + margin-bottom: 0.2em; + } + + div.variablelist dl dd + { + margin: 0em 0em 0.5em 2em; + font-size: 10pt; + } + + div.variablelist table tbody tr td p, + div.variablelist dl dd p + { + margin: 0em 0em 0.5em 0em; + line-height: 1; + } + +/*============================================================================= +Misc +=============================================================================*/ + + /* Title of books and articles in bibliographies */ + span.title + { + font-style: italic; + } + + span.underline + { + text-decoration: underline; + } + + span.strikethrough + { + text-decoration: line-through; + } + + /* Copyright, Legal Notice */ + div div.legalnotice p + { + text-align: left + } + +/*============================================================================= +Colors +=============================================================================*/ + + @media screen + { + body { + background-color: #FFFFFF; + color: #000000; + } + + /* Syntax Highlighting */ + .keyword { color: #0000AA; } + .identifier { color: #000000; } + .special { color: #707070; } + .preprocessor { color: #402080; } + .char { color: teal; } + .comment { color: #800000; } + .string { color: teal; } + .number { color: teal; } + .white_bkd { background-color: #FFFFFF; } + .dk_grey_bkd { background-color: #999999; } + + /* Links */ + a, a .keyword, a .identifier, a .special, a .preprocessor + a .char, a .comment, a .string, a .number + { + color: #005a9c; + } + + a:visited, a:visited .keyword, a:visited .identifier, + a:visited .special, a:visited .preprocessor a:visited .char, + a:visited .comment, a:visited .string, a:visited .number + { + color: #9c5a9c; + } + + h1 a, h2 a, h3 a, h4 a, h5 a, h6 a, + h1 a:hover, h2 a:hover, h3 a:hover, h4 a:hover, h5 a:hover, h6 a:hover, + h1 a:visited, h2 a:visited, h3 a:visited, h4 a:visited, h5 a:visited, h6 a:visited + { + text-decoration: none; /* no underline */ + color: #000000; + } + + /* Copyright, Legal Notice */ + .copyright + { + color: #666666; + font-size: small; + } + + div div.legalnotice p + { + color: #666666; + } + + /* Program listing */ + pre.synopsis + { + border: 1px solid #DCDCDC; + } + + .programlisting, + .screen + { + border: 1px solid #DCDCDC; + } + + td .programlisting, + td .screen + { + border: 0px solid #DCDCDC; + } + + /* Blurbs */ + div.note, + div.tip, + div.important, + div.caution, + div.warning, + p.blurb + { + border: 1px solid #DCDCDC; + } + + /* Table of contents */ + div.toc + { + border: 1px solid #DCDCDC; + } + + /* Tables */ + div.informaltable table tr td, + div.table table tr td + { + border: 1px solid #DCDCDC; + } + + div.informaltable table tr th, + div.table table tr th + { + background-color: #F0F0F0; + border: 1px solid #DCDCDC; + } + + .copyright-footer + { + color: #8F8F8F; + } + + /* Misc */ + span.highlight + { + color: #00A000; + } + } + + @media print + { + /* Links */ + a + { + color: black; + } + + a:visited + { + color: black; + } + + .spirit-nav + { + display: none; + } + + /* Program listing */ + pre.synopsis + { + border: 1px solid gray; + } + + .programlisting, + .screen + { + border: 1px solid gray; + } + + td .programlisting, + td .screen + { + border: 0px solid #DCDCDC; + } + + /* Table of contents */ + div.toc + { + border: 1px solid gray; + } + + .informaltable table, + .table table + { + border: 1px solid gray; + border-collapse: collapse; + } + + /* Tables */ + div.informaltable table tr td, + div.table table tr td + { + border: 1px solid gray; + } + + div.informaltable table tr th, + div.table table tr th + { + border: 1px solid gray; + } + + table.simplelist tr td + { + border: none !important; + } + + /* Misc */ + span.highlight + { + font-weight: bold; + } + } + +/*============================================================================= +Images +=============================================================================*/ + + span.inlinemediaobject img + { + vertical-align: middle; + } + +/*============================================================================== +Super and Subscript: style so that line spacing isn't effected, see +http://www.adobe.com/cfusion/communityengine/index.cfm?event=showdetails&productId=1&postId=5341 +==============================================================================*/ + +sup, +sub { +height: 0; +line-height: 1; +vertical-align: baseline; +position: relative; + +} + +/* For internet explorer: */ + +* html sup, +* html sub { +vertical-align: bottom; +} + +sup { +bottom: 1ex; +} + +sub { +top: .5ex; +} + +/*============================================================================== +Indexes: pretty much the same as the TOC. +==============================================================================*/ + + .index + { + font-size: 80%; + padding-top: 0px; + padding-bottom: 0px; + margin-top: 0px; + margin-bottom: 0px; + margin-left: 0px; + } + + .index ul + { + padding-left: 3em; + } + + .index p + { + padding: 2px; + margin: 2px; + } + + .index-entry-level-0 + { + font-weight: bold; + } + + .index em + { + font-weight: bold; + } + + +/*============================================================================== +Alignment and coloring use 'role' feature, available from Quickbook 1.6 up. +Added from Niall Douglas for role color and alignment. +http://article.gmane.org/gmane.comp.lib.boost.devel/243318 +*/ + +/* Add text alignment (see http://www.w3schools.com/cssref/pr_text_text-align.asp) */ +span.aligncenter +{ + display: inline-block; width: 100%; text-align: center; +} +span.alignright +{ + display: inline-block; width: 100%; text-align: right; +} +/* alignleft is the default. */ +span.alignleft +{ + display: inline-block; width: 100%; text-align: left; +} + +/* alignjustify stretches the word spacing so that each line has equal width +within a chosen fraction of page width (here arbitrarily 20%). +*Not* useful inside table items as the column width remains the total string width. +Nor very useful, except to temporarily restrict the width. +*/ +span.alignjustify +{ + display: inline-block; width: 20%; text-align: justify; +} + +/* Text colors. +Names at http://www.w3.org/TR/2002/WD-css3-color-20020219/ 4.3. X11 color keywords. +Quickbook Usage: [role red Some red text] + +*/ +span.red { inline-block; color: red; } +span.green { color: green; } +span.lime { color: #00FF00; } +span.blue { color: blue; } +span.navy { color: navy; } +span.yellow { color: yellow; } +span.magenta { color: magenta; } +span.indigo { color: #4B0082; } +span.cyan { color: cyan; } +span.purple { color: purple; } +span.gold { color: gold; } +span.silver { color: silver; } /* lighter gray */ +span.gray { color: #808080; } /* light gray */ diff --git a/doc/numpy/_static/boost.png b/doc/numpy/_static/boost.png new file mode 100644 index 00000000..b4d51fcd Binary files /dev/null and b/doc/numpy/_static/boost.png differ diff --git a/doc/numpy/_static/bpl.png b/doc/numpy/_static/bpl.png new file mode 100644 index 00000000..c2d8c69e Binary files /dev/null and b/doc/numpy/_static/bpl.png differ diff --git a/doc/numpy/_static/home.png b/doc/numpy/_static/home.png new file mode 100644 index 00000000..5584aacb Binary files /dev/null and b/doc/numpy/_static/home.png differ diff --git a/doc/numpy/_static/next.png b/doc/numpy/_static/next.png new file mode 100644 index 00000000..59800b4e Binary files /dev/null and b/doc/numpy/_static/next.png differ diff --git a/doc/numpy/_static/prev.png b/doc/numpy/_static/prev.png new file mode 100644 index 00000000..d88a40f9 Binary files /dev/null and b/doc/numpy/_static/prev.png differ diff --git a/doc/numpy/_static/style.css b/doc/numpy/_static/style.css new file mode 100644 index 00000000..65277d0e --- /dev/null +++ b/doc/numpy/_static/style.css @@ -0,0 +1,38 @@ +@import url(boost.css); + +.header h1 a +{ + color: #00507f; + font-size: 200%; + font-style: italic; +} +.header h3 { margin: 1px;} +#contents +{ +/* border-bottom: solid thin black;*/ +} + +.highlight +{ + border: 1px solid #dcdcdc; + background-color: inherit; + padding: 0 1em; + margin: 0 5em; +} +#searchbox +{ + float: right; + width: auto; + margin: 0 2em; +} + +.admonition-title { font-weight: bold;} +.toctree-wrapper +{ + border: 1px solid #dcdcdc; + padding: 1em; + margin: 0 2em; +} +.toctree-wrapper .caption, +.toctree-wrapper .topic-title { font-weight: bold;} + diff --git a/doc/numpy/_static/up.png b/doc/numpy/_static/up.png new file mode 100644 index 00000000..17d9c3ec Binary files /dev/null and b/doc/numpy/_static/up.png differ diff --git a/doc/numpy/_templates/layout.html b/doc/numpy/_templates/layout.html new file mode 100644 index 00000000..1aa68f0e --- /dev/null +++ b/doc/numpy/_templates/layout.html @@ -0,0 +1,120 @@ +{%- macro navbar() %} + +{%- endmacro %} + + + + + + {{ metatags }} + {%- if builder != 'htmlhelp' %} + {%- set titlesuffix = docstitle|e %} + {%- set titlesuffix = " - " + titlesuffix %} + {%- endif %} + {{ title|striptags }}{{ titlesuffix }} + {%- if builder == 'web' %} + + {%- for link, type, title in page_links %} + + {%- endfor %} + {%- else %} + + + + {%- endif %} + {%- if builder != 'htmlhelp' %} + + {%- for scriptfile in script_files %} + + {%- endfor %} + {%- if use_opensearch %} + + {%- endif %} + {%- if favicon %} + + {%- endif %} + {%- endif %} +{%- block linktags %} + {%- if hasdoc('about') %} + + {%- endif %} + + + {%- if hasdoc('copyright') %} + + {%- endif %} + + {%- if parents %} + + {%- endif %} + {%- if next %} + + {%- endif %} + {%- if prev %} + + {%- endif %} +{%- endblock %} +{%- block extrahead %} {% endblock %} + + +
+ + + + + + + +
+

C++ Boost

+
+

(NumPy)

+ +
+ {%- if pagename != "search" %} + + + {%- endif %} +
+
+
+
+ {%- block top_navbar %}{{ navbar() }}{% endblock %} + {% block body %} {% endblock %} + {%- block bottom_navbar %}{{ navbar() }}{% endblock %} +
+ + diff --git a/doc/numpy/conf.py b/doc/numpy/conf.py new file mode 100644 index 00000000..2f5d5e81 --- /dev/null +++ b/doc/numpy/conf.py @@ -0,0 +1,219 @@ +# -*- coding: utf-8 -*- +# +# Boost.Python NumPy documentation build configuration file, created by +# sphinx-quickstart on Thu Oct 27 09:04:58 2011. +# +# This file is execfile()d with the current directory set to its containing dir. +# +# Note that not all possible configuration values are present in this +# autogenerated file. +# +# All configuration values have a default; values that are commented out +# serve to show the default. + +import sys, os + +# If extensions (or modules to document with autodoc) are in another directory, +# add these directories to sys.path here. If the directory is relative to the +# documentation root, use os.path.abspath to make it absolute, like shown here. +#sys.path.insert(0, os.path.abspath('.')) + +# -- General configuration ----------------------------------------------------- + +# If your documentation needs a minimal Sphinx version, state it here. +#needs_sphinx = '1.0' + +# Add any Sphinx extension module names here, as strings. They can be extensions +# coming with Sphinx (named 'sphinx.ext.*') or your custom ones. +extensions = [] + +# Add any paths that contain templates here, relative to this directory. +templates_path = ['_templates'] + +# The suffix of source filenames. +source_suffix = '.rst' + +# The encoding of source files. +#source_encoding = 'utf-8-sig' + +# The master toctree document. +master_doc = 'index' + +# General information about the project. +project = u'Boost.Python NumPy extension' +copyright = u'2011, Stefan Seefeld' + +# The version info for the project you're documenting, acts as replacement for +# |version| and |release|, also used in various other places throughout the +# built documents. +# +# The short X.Y version. +version = '1.0' +# The full version, including alpha/beta/rc tags. +release = '1.0' + +# The language for content autogenerated by Sphinx. Refer to documentation +# for a list of supported languages. +#language = None + +# There are two options for replacing |today|: either, you set today to some +# non-false value, then it is used: +#today = '' +# Else, today_fmt is used as the format for a strftime call. +#today_fmt = '%B %d, %Y' + +# List of patterns, relative to source directory, that match files and +# directories to ignore when looking for source files. +exclude_patterns = ['_build'] + +# The reST default role (used for this markup: `text`) to use for all documents. +#default_role = None + +# If true, '()' will be appended to :func: etc. cross-reference text. +#add_function_parentheses = True + +# If true, the current module name will be prepended to all description +# unit titles (such as .. function::). +#add_module_names = True + +# If true, sectionauthor and moduleauthor directives will be shown in the +# output. They are ignored by default. +show_authors = False + +# The name of the Pygments (syntax highlighting) style to use. +pygments_style = 'default' + +highlight_language = 'c++' + +# A list of ignored prefixes for module index sorting. +#modindex_common_prefix = [] + + +# -- Options for HTML output --------------------------------------------------- + +# The theme to use for HTML and HTML Help pages. See the documentation for +# a list of builtin themes. +html_theme = 'default' + +# Theme options are theme-specific and customize the look and feel of a theme +# further. For a list of options available for each theme, see the +# documentation. +#html_theme_options = {} + +# Add any paths that contain custom themes here, relative to this directory. +#html_theme_path = [] + +# The name for this set of Sphinx documents. If None, it defaults to +# " v documentation". +#html_title = None + +# A shorter title for the navigation bar. Default is the same as html_title. +#html_short_title = None + +# The name of an image file (relative to this directory) to place at the top +# of the sidebar. +html_logo = '_static/bpl.png' + +# The name of an image file (within the static path) to use as favicon of the +# docs. This file should be a Windows icon file (.ico) being 16x16 or 32x32 +# pixels large. +#html_favicon = None + +# Add any paths that contain custom static files (such as style sheets) here, +# relative to this directory. They are copied after the builtin static files, +# so a file named "default.css" will overwrite the builtin "default.css". +html_static_path = ['_static'] + +# If not '', a 'Last updated on:' timestamp is inserted at every page bottom, +# using the given strftime format. +#html_last_updated_fmt = '%b %d, %Y' + +# If true, SmartyPants will be used to convert quotes and dashes to +# typographically correct entities. +#html_use_smartypants = True + +# Custom sidebar templates, maps document names to template names. +#html_sidebars = {} + +# Additional templates that should be rendered to pages, maps page names to +# template names. +#html_additional_pages = {} + +# If false, no module index is generated. +#html_domain_indices = True + +# If false, no index is generated. +html_use_index = True + +# If true, the index is split into individual pages for each letter. +#html_split_index = False + +# If true, links to the reST sources are added to the pages. +#html_show_sourcelink = True + +# If true, "Created using Sphinx" is shown in the HTML footer. Default is True. +#html_show_sphinx = True + +# If true, "(C) Copyright ..." is shown in the HTML footer. Default is True. +#html_show_copyright = True + +# If true, an OpenSearch description file will be output, and all pages will +# contain a tag referring to it. The value of this option must be the +# base URL from which the finished HTML is served. +#html_use_opensearch = '' + +# This is the file name suffix for HTML files (e.g. ".xhtml"). +#html_file_suffix = None + +# Output file base name for HTML help builder. +htmlhelp_basename = 'BoostPythonNumPydoc' + +html_add_permalinks = False + +# -- Options for LaTeX output -------------------------------------------------- + +# The paper size ('letter' or 'a4'). +#latex_paper_size = 'letter' + +# The font size ('10pt', '11pt' or '12pt'). +#latex_font_size = '10pt' + +# Grouping the document tree into LaTeX files. List of tuples +# (source start file, target name, title, author, documentclass [howto/manual]). +latex_documents = [ + ('index', 'BoostPythonNumPy.tex', u'Boost.Python NumPy Documentation', + u'Stefan Seefeld', 'manual'), +] + +# The name of an image file (relative to this directory) to place at the top of +# the title page. +#latex_logo = None + +# For "manual" documents, if this is true, then toplevel headings are parts, +# not chapters. +#latex_use_parts = False + +# If true, show page references after internal links. +#latex_show_pagerefs = False + +# If true, show URL addresses after external links. +#latex_show_urls = False + +# Additional stuff for the LaTeX preamble. +#latex_preamble = '' + +# Documents to append as an appendix to all manuals. +#latex_appendices = [] + +# If false, no module index is generated. +#latex_domain_indices = True + + +# -- Options for manual page output -------------------------------------------- + +# One entry per manual page. List of tuples +# (source start file, name, description, authors, manual section). +man_pages = [ + ('index', 'boostnumpy', u'Boost.Python NumPy Documentation', + [u'Stefan Seefeld'], 1) +] diff --git a/doc/numpy/index.rst b/doc/numpy/index.rst new file mode 100644 index 00000000..bb3b623e --- /dev/null +++ b/doc/numpy/index.rst @@ -0,0 +1,14 @@ +.. Boost.Python NumPy extension documentation master file, created by + sphinx-quickstart on Thu Oct 27 09:04:58 2011. + You can adapt this file completely to your liking, but it should at least + contain the root `toctree` directive. + +Welcome to the documentation of the Boost.Python NumPy extension! +================================================================= + +.. toctree:: + :maxdepth: 2 + + Tutorial + Reference + diff --git a/doc/numpy/make.bat b/doc/numpy/make.bat new file mode 100644 index 00000000..7dc41fc3 --- /dev/null +++ b/doc/numpy/make.bat @@ -0,0 +1,171 @@ +@ECHO OFF + +REM Command file for Sphinx documentation + +if "%SPHINXBUILD%" == "" ( + set SPHINXBUILD=sphinx-build +) +set BUILDDIR=_build +set HTMLDIR=../html/numpy +set ALLSPHINXOPTS=-d %BUILDDIR%/doctrees %SPHINXOPTS% . +if NOT "%PAPER%" == "" ( + set ALLSPHINXOPTS=-D latex_paper_size=%PAPER% %ALLSPHINXOPTS% +) + +if "%1" == "" goto help + +if "%1" == "help" ( + :help + echo.Please use `make ^` where ^ is one of + echo. html to make standalone HTML files + echo. dirhtml to make HTML files named index.html in directories + echo. singlehtml to make a single large HTML file + echo. pickle to make pickle files + echo. json to make JSON files + echo. htmlhelp to make HTML files and a HTML help project + echo. qthelp to make HTML files and a qthelp project + echo. devhelp to make HTML files and a Devhelp project + echo. epub to make an epub + echo. latex to make LaTeX files, you can set PAPER=a4 or PAPER=letter + echo. text to make text files + echo. man to make manual pages + echo. changes to make an overview over all changed/added/deprecated items + echo. linkcheck to check all external links for integrity + echo. doctest to run all doctests embedded in the documentation if enabled + goto end +) + +if "%1" == "clean" ( + for /d %%i in (%BUILDDIR%\*) do rmdir /q /s %%i + del /q /s %BUILDDIR%\* + goto end +) + +if "%1" == "html" ( + %SPHINXBUILD% -b html %ALLSPHINXOPTS% %HTMLDIR% + if errorlevel 1 exit /b 1 + echo. + echo.Build finished. The HTML pages are in %HTMLDIR%. + goto end +) + +if "%1" == "dirhtml" ( + %SPHINXBUILD% -b dirhtml %ALLSPHINXOPTS% %BUILDDIR%/dirhtml + if errorlevel 1 exit /b 1 + echo. + echo.Build finished. The HTML pages are in %BUILDDIR%/dirhtml. + goto end +) + +if "%1" == "singlehtml" ( + %SPHINXBUILD% -b singlehtml %ALLSPHINXOPTS% %BUILDDIR%/singlehtml + if errorlevel 1 exit /b 1 + echo. + echo.Build finished. The HTML pages are in %BUILDDIR%/singlehtml. + goto end +) + +if "%1" == "pickle" ( + %SPHINXBUILD% -b pickle %ALLSPHINXOPTS% %BUILDDIR%/pickle + if errorlevel 1 exit /b 1 + echo. + echo.Build finished; now you can process the pickle files. + goto end +) + +if "%1" == "json" ( + %SPHINXBUILD% -b json %ALLSPHINXOPTS% %BUILDDIR%/json + if errorlevel 1 exit /b 1 + echo. + echo.Build finished; now you can process the JSON files. + goto end +) + +if "%1" == "htmlhelp" ( + %SPHINXBUILD% -b htmlhelp %ALLSPHINXOPTS% %BUILDDIR%/htmlhelp + if errorlevel 1 exit /b 1 + echo. + echo.Build finished; now you can run HTML Help Workshop with the ^ +.hhp project file in %BUILDDIR%/htmlhelp. + goto end +) + +if "%1" == "qthelp" ( + %SPHINXBUILD% -b qthelp %ALLSPHINXOPTS% %BUILDDIR%/qthelp + if errorlevel 1 exit /b 1 + echo. + echo.Build finished; now you can run "qcollectiongenerator" with the ^ +.qhcp project file in %BUILDDIR%/qthelp, like this: + echo.^> qcollectiongenerator %BUILDDIR%\qthelp\BoostNumPy.qhcp + echo.To view the help file: + echo.^> assistant -collectionFile %BUILDDIR%\qthelp\BoostNumPy.ghc + goto end +) + +if "%1" == "devhelp" ( + %SPHINXBUILD% -b devhelp %ALLSPHINXOPTS% %BUILDDIR%/devhelp + if errorlevel 1 exit /b 1 + echo. + echo.Build finished. + goto end +) + +if "%1" == "epub" ( + %SPHINXBUILD% -b epub %ALLSPHINXOPTS% %BUILDDIR%/epub + if errorlevel 1 exit /b 1 + echo. + echo.Build finished. The epub file is in %BUILDDIR%/epub. + goto end +) + +if "%1" == "latex" ( + %SPHINXBUILD% -b latex %ALLSPHINXOPTS% %BUILDDIR%/latex + if errorlevel 1 exit /b 1 + echo. + echo.Build finished; the LaTeX files are in %BUILDDIR%/latex. + goto end +) + +if "%1" == "text" ( + %SPHINXBUILD% -b text %ALLSPHINXOPTS% %BUILDDIR%/text + if errorlevel 1 exit /b 1 + echo. + echo.Build finished. The text files are in %BUILDDIR%/text. + goto end +) + +if "%1" == "man" ( + %SPHINXBUILD% -b man %ALLSPHINXOPTS% %BUILDDIR%/man + if errorlevel 1 exit /b 1 + echo. + echo.Build finished. The manual pages are in %BUILDDIR%/man. + goto end +) + +if "%1" == "changes" ( + %SPHINXBUILD% -b changes %ALLSPHINXOPTS% %BUILDDIR%/changes + if errorlevel 1 exit /b 1 + echo. + echo.The overview file is in %BUILDDIR%/changes. + goto end +) + +if "%1" == "linkcheck" ( + %SPHINXBUILD% -b linkcheck %ALLSPHINXOPTS% %BUILDDIR%/linkcheck + if errorlevel 1 exit /b 1 + echo. + echo.Link check complete; look for any errors in the above output ^ +or in %BUILDDIR%/linkcheck/output.txt. + goto end +) + +if "%1" == "doctest" ( + %SPHINXBUILD% -b doctest %ALLSPHINXOPTS% %BUILDDIR%/doctest + if errorlevel 1 exit /b 1 + echo. + echo.Testing of doctests in the sources finished, look at the ^ +results in %BUILDDIR%/doctest/output.txt. + goto end +) + +:end diff --git a/doc/numpy/reference/binary_ufunc.rst b/doc/numpy/reference/binary_ufunc.rst new file mode 100644 index 00000000..0215e555 --- /dev/null +++ b/doc/numpy/reference/binary_ufunc.rst @@ -0,0 +1,110 @@ +binary_ufunc +============ + +.. contents :: Table of Contents + +A ``binary_ufunc`` is a struct used as an intermediate step to broadcast two arguments so that a C++ function can be converted to a ufunc like function + + ```` contains the ``binary_ufunc`` structure definitions + + +synopsis +-------- + +:: + + namespace boost + { + namespace python + { + namespace numpy + { + + template + + struct binary_ufunc + { + + static object call(TBinaryFunctor & self, + object const & input1, + object const & input2, + object const & output); + + static object make(); + }; + + } + } + } + + +constructors +------------ + +:: + + struct example_binary_ufunc + { + typedef any_valid first_argument_type; + typedef any_valid second_argument_type; + typedef any_valid result_type; + }; + +:Requirements: The ``any_valid`` type must be defined using typedef as a valid C++ type in order to use the struct methods correctly + +:Note: The struct must be exposed as a Python class, and an instance of the class must be created to use the ``call`` method corresponding to the ``__call__`` attribute of the Python object + +accessors +--------- + +:: + + template + static object call(TBinaryFunctor & self, + object const & input, + object const & output); + +:Requires: Typenames ``TBinaryFunctor`` and optionally ``TArgument1`` and ``TArgument2`` for argument type and ``TResult`` for result type + +:Effects: Passes a Python object to the underlying C++ functor after broadcasting its arguments + +:: + + template + static object make(); + +:Requires: Typenames ``TBinaryFunctor`` and optionally ``TArgument1`` and ``TArgument2`` for argument type and ``TResult`` for result type + +:Returns: A Python function object to call the overloaded () operator in the struct (in typical usage) + +Example(s) +---------- + +:: + + namespace p = boost::python; + namespace np = boost::python::numpy; + + struct BinarySquare + { + typedef double first_argument_type; + typedef double second_argument_type; + typedef double result_type; + + double operator()(double a,double b) const { return (a*a + b*b) ; } + }; + + p::object ud = p::class_ >("BinarySquare").def("__call__", np::binary_ufunc::make()); + p::object inst = ud(); + result_array = inst.attr("__call__")(demo_array,demo_array) ; + std::cout << "Square of list with binary ufunc is " << p::extract (p::str(result_array)) << std::endl ; + diff --git a/doc/numpy/reference/dtype.rst b/doc/numpy/reference/dtype.rst new file mode 100644 index 00000000..03227116 --- /dev/null +++ b/doc/numpy/reference/dtype.rst @@ -0,0 +1,92 @@ +dtype +===== + +.. contents :: Table of Contents + +A `dtype`_ is an object describing the type of the elements of an ndarray + +.. _dtype: http://docs.scipy.org/doc/numpy/reference/arrays.dtypes.html#data-type-objects-dtype + + ```` contains the method calls necessary to generate a python object equivalent to a numpy.dtype from builtin C++ objects, as well as to create custom dtypes from user defined types + + +synopsis +-------- + +:: + + namespace boost + { + namespace python + { + namespace numpy + { + + class dtype : public object + { + static python::detail::new_reference convert(object::object_cref arg, bool align); + public: + + // Convert an arbitrary Python object to a data-type descriptor object. + template + explicit dtype(T arg, bool align=false); + + // Get the built-in numpy dtype associated with the given scalar template type. + template static dtype get_builtin(); + + // Return the size of the data type in bytes. + int get_itemsize() const; + }; + + } + } + } + +constructors +------------ + +:: + + template + explicit dtype(T arg, bool align=false) + +:Requirements: ``T`` must be either : + + * a built-in C++ typename convertible to object + * a valid python object or convertible to object + +:Effects: Constructs an object from the supplied python object / convertible + to object / builtin C++ data type + +:Throws: Nothing + +:: + + template static dtype get_builtin(); + +:Requirements: The typename supplied, ``T`` must be a builtin C++ type also supported by numpy + +:Returns: Numpy dtype corresponding to builtin C++ type + +accessors +--------- + +:: + + int get_itemsize() const; + +:Returns: the size of the data type in bytes. + + +Example(s) +---------- + +:: + + namespace p = boost::python; + namespace np = boost::python::numpy; + + np::dtype dtype = np::dtype::get_builtin(); + p::tuple for_custom_dtype = p::make_tuple("ha",dtype); + np::dtype custom_dtype = np::dtype(list_for_dtype); + diff --git a/doc/numpy/reference/index.rst b/doc/numpy/reference/index.rst new file mode 100644 index 00000000..5c0d852d --- /dev/null +++ b/doc/numpy/reference/index.rst @@ -0,0 +1,12 @@ +Boost.Python NumPy extension Reference +====================================== + +.. toctree:: + :maxdepth: 2 + + dtype + ndarray + unary_ufunc + binary_ufunc + multi_iter + diff --git a/doc/numpy/reference/multi_iter.rst b/doc/numpy/reference/multi_iter.rst new file mode 100644 index 00000000..c1d812ef --- /dev/null +++ b/doc/numpy/reference/multi_iter.rst @@ -0,0 +1,94 @@ +multi_iter +========== + +.. contents :: Table of Contents + +A ``multi_iter`` is a Python object, intended to be used as an iterator It should generally only be used in loops. + + ```` contains the class definitions for ``multi_iter`` + + +synopsis +-------- + +:: + + namespace boost + { + namespace python + { + namespace numpy + { + + class multi_iter : public object + { + public: + void next(); + bool not_done() const; + char * get_data(int n) const; + int const get_nd() const; + Py_intptr_t const * get_shape() const; + Py_intptr_t const shape(int n) const; + }; + + + multi_iter make_multi_iter(object const & a1); + multi_iter make_multi_iter(object const & a1, object const & a2); + multi_iter make_multi_iter(object const & a1, object const & a2, object const & a3); + + } + } + } + + +constructors +------------ + +:: + + multi_iter make_multi_iter(object const & a1); + multi_iter make_multi_iter(object const & a1, object const & a2); + multi_iter make_multi_iter(object const & a1, object const & a2, object const & a3); + +:Returns: A Python iterator object broadcasting over one, two or three sequences as supplied + +accessors +--------- + +:: + + void next(); + +:Effects: Increments the iterator + +:: + + bool not_done() const; + +:Returns: boolean value indicating whether the iterator is at its end + +:: + + char * get_data(int n) const; + +:Returns: a pointer to the element of the nth broadcasted array. + +:: + + int const get_nd() const; + +:Returns: the number of dimensions of the broadcasted array expression + +:: + + Py_intptr_t const * get_shape() const; + +:Returns: the shape of the broadcasted array expression as an array of integers. + +:: + + Py_intptr_t const shape(int n) const; + +:Returns: the shape of the broadcasted array expression in the nth dimension. + + diff --git a/doc/numpy/reference/ndarray.rst b/doc/numpy/reference/ndarray.rst new file mode 100644 index 00000000..1486c73a --- /dev/null +++ b/doc/numpy/reference/ndarray.rst @@ -0,0 +1,382 @@ +ndarray +======= + +.. contents :: Table of Contents + +A `ndarray`_ is an N-dimensional array which contains items of the same type and size, where N is the number of dimensions and is specified in the form of a ``shape`` tuple. Optionally, the numpy ``dtype`` for the objects contained may also be specified. + +.. _ndarray: http://docs.scipy.org/doc/numpy/reference/arrays.ndarray.html +.. _dtype: http://docs.scipy.org/doc/numpy/reference/arrays.dtypes.html#data-type-objects-dtype + + ```` contains the structures and methods necessary to move raw data between C++ and Python and create ndarrays from the data + + + +synopsis +-------- + +:: + + namespace boost + { + namespace python + { + namespace numpy + { + + class ndarray : public object + { + + public: + + enum bitflag + { + NONE=0x0, C_CONTIGUOUS=0x1, F_CONTIGUOUS=0x2, V_CONTIGUOUS=0x1|0x2, + ALIGNED=0x4, WRITEABLE=0x8, BEHAVED=0x4|0x8, + CARRAY_RO=0x1|0x4, CARRAY=0x1|0x4|0x8, CARRAY_MIS=0x1|0x8, + FARRAY_RO=0x2|0x4, FARRAY=0x2|0x4|0x8, FARRAY_MIS=0x2|0x8, + UPDATE_ALL=0x1|0x2|0x4, VARRAY=0x1|0x2|0x8, ALL=0x1|0x2|0x4|0x8 + }; + + ndarray view(dtype const & dt) const; + ndarray astype(dtype const & dt) const; + ndarray copy() const; + int const shape(int n) const; + int const strides(int n) const; + char * get_data() const; + dtype get_dtype() const; + python::object get_base() const; + void set_base(object const & base); + Py_intptr_t const * get_shape() const; + Py_intptr_t const * get_strides() const; + int const get_nd() const; + + bitflag const get_flags() const; + + ndarray transpose() const; + ndarray squeeze() const; + ndarray reshape(tuple const & shape) const; + object scalarize() const; + }; + + ndarray zeros(tuple const & shape, dtype const & dt); + ndarray zeros(int nd, Py_intptr_t const * shape, dtype const & dt); + + ndarray empty(tuple const & shape, dtype const & dt); + ndarray empty(int nd, Py_intptr_t const * shape, dtype const & dt); + + ndarray array(object const & obj); + ndarray array(object const & obj, dtype const & dt); + + template + ndarray from_data(void * data,dtype const & dt,Container shape,Container strides,python::object const & owner); + template + ndarray from_data(void const * data, dtype const & dt, Container shape, Container strides, object const & owner); + + ndarray from_object(object const & obj, dtype const & dt,int nd_min, int nd_max, ndarray::bitflag flags=ndarray::NONE); + ndarray from_object(object const & obj, dtype const & dt,int nd, ndarray::bitflag flags=ndarray::NONE); + ndarray from_object(object const & obj, dtype const & dt, ndarray::bitflag flags=ndarray::NONE); + ndarray from_object(object const & obj, int nd_min, int nd_max,ndarray::bitflag flags=ndarray::NONE); + ndarray from_object(object const & obj, int nd, ndarray::bitflag flags=ndarray::NONE); + ndarray from_object(object const & obj, ndarray::bitflag flags=ndarray::NONE) + + ndarray::bitflag operator|(ndarray::bitflag a, ndarray::bitflag b) ; + ndarray::bitflag operator&(ndarray::bitflag a, ndarray::bitflag b); + + } + + +constructors +------------ + +:: + + ndarray view(dtype const & dt) const; + +:Returns: new ndarray with old ndarray data cast as supplied dtype + +:: + + ndarray astype(dtype const & dt) const; + +:Returns: new ndarray with old ndarray data converted to supplied dtype + +:: + + ndarray copy() const; + +:Returns: Copy of calling ndarray object + +:: + + ndarray transpose() const; + +:Returns: An ndarray with the rows and columns interchanged + +:: + + ndarray squeeze() const; + +:Returns: An ndarray with all unit-shaped dimensions removed + +:: + + ndarray reshape(tuple const & shape) const; + +:Requirements: The new ``shape`` of the ndarray must be supplied as a tuple + +:Returns: An ndarray with the same data but reshaped to the ``shape`` supplied + + +:: + + object scalarize() const; + +:Returns: A scalar if the ndarray has only one element, otherwise it returns the entire array + +:: + + ndarray zeros(tuple const & shape, dtype const & dt); + ndarray zeros(int nd, Py_intptr_t const * shape, dtype const & dt); + +:Requirements: The following parameters must be supplied as required : + + * the ``shape`` or the size of all dimensions, as a tuple + * the ``dtype`` of the data + * the ``nd`` size for a square shaped ndarray + * the ``shape`` Py_intptr_t + +:Returns: A new ndarray with the given shape and data type, with data initialized to zero. + +:: + + ndarray empty(tuple const & shape, dtype const & dt); + ndarray empty(int nd, Py_intptr_t const * shape, dtype const & dt); + + +:Requirements: The following parameters must be supplied : + + * the ``shape`` or the size of all dimensions, as a tuple + * the ``dtype`` of the data + * the ``shape`` Py_intptr_t + +:Returns: A new ndarray with the given shape and data type, with data left uninitialized. + +:: + + ndarray array(object const & obj); + ndarray array(object const & obj, dtype const & dt); + +:Returns: A new ndarray from an arbitrary Python sequence, with dtype of each element specified optionally + +:: + + template + inline ndarray from_data(void * data,dtype const & dt,Container shape,Container strides,python::object const & owner) + +:Requirements: The following parameters must be supplied : + + * the ``data`` which is a generic C++ data container + * the dtype ``dt`` of the data + * the ``shape`` of the ndarray as Python object + * the ``strides`` of each dimension of the array as a Python object + * the ``owner`` of the data, in case it is not the ndarray itself + +:Returns: ndarray with attributes and data supplied + +:Note: The ``Container`` typename must be one that is convertible to a std::vector or python object type + +:: + + ndarray from_object(object const & obj, dtype const & dt,int nd_min, int nd_max, ndarray::bitflag flags=ndarray::NONE); + +:Requirements: The following parameters must be supplied : + + * the ``obj`` Python object to convert to ndarray + * the dtype ``dt`` of the data + * minimum number of dimensions ``nd_min`` of the ndarray as Python object + * maximum number of dimensions ``nd_max`` of the ndarray as Python object + * optional ``flags`` bitflags + +:Returns: ndarray constructed with dimensions and data supplied as parameters + +:: + + inline ndarray from_object(object const & obj, dtype const & dt, int nd, ndarray::bitflag flags=ndarray::NONE); + +:Requirements: The following parameters must be supplied : + + * the ``obj`` Python object to convert to ndarray + * the dtype ``dt`` of the data + * number of dimensions ``nd`` of the ndarray as Python object + * optional ``flags`` bitflags + +:Returns: ndarray with dimensions ``nd`` x ``nd`` and suplied parameters + +:: + + inline ndarray from_object(object const & obj, dtype const & dt, ndarray::bitflag flags=ndarray::NONE) + +:Requirements: The following parameters must be supplied : + + * the ``obj`` Python object to convert to ndarray + * the dtype ``dt`` of the data + * optional ``flags`` bitflags + +:Returns: Supplied Python object as ndarray + +:: + + ndarray from_object(object const & obj, int nd_min, int nd_max, ndarray::bitflag flags=ndarray::NONE); + +:Requirements: The following parameters must be supplied : + + * the ``obj`` Python object to convert to ndarray + * minimum number of dimensions ``nd_min`` of the ndarray as Python object + * maximum number of dimensions ``nd_max`` of the ndarray as Python object + * optional ``flags`` bitflags + +:Returns: ndarray with supplied dimension limits and parameters + +:Note: dtype need not be supplied here + +:: + + inline ndarray from_object(object const & obj, int nd, ndarray::bitflag flags=ndarray::NONE); + +:Requirements: The following parameters must be supplied : + + * the ``obj`` Python object to convert to ndarray + * the dtype ``dt`` of the data + * number of dimensions ``nd`` of the ndarray as Python object + * optional ``flags`` bitflags + +:Returns: ndarray of ``nd`` x ``nd`` dimensions constructed from the supplied object + +:: + + inline ndarray from_object(object const & obj, ndarray::bitflag flags=ndarray::NONE) + +:Requirements: The following parameters must be supplied : + + * the ``obj`` Python object to convert to ndarray + * optional ``flags`` bitflags + +:Returns: ndarray of same dimensions and dtype as supplied Python object + + +accessors +--------- + +:: + + int const shape(int n) const; + +:Returns: The size of the n-th dimension of the ndarray + +:: + + int const strides(int n) const; + +:Returns: The stride of the nth dimension. + +:: + + char * get_data() const; + +:Returns: Array's raw data pointer as a char + +:Note: This returns char so stride math works properly on it.User will have to reinterpret_cast it. + +:: + + dtype get_dtype() const; + +:Returns: Array's data-type descriptor object (dtype) + + +:: + + object get_base() const; + +:Returns: Object that owns the array's data, or None if the array owns its own data. + + +:: + + void set_base(object const & base); + +:Returns: Set the object that owns the array's data. Exercise caution while using this + + +:: + + Py_intptr_t const * get_shape() const; + +:Returns: Shape of the array as an array of integers + + +:: + + Py_intptr_t const * get_strides() const; + +:Returns: Stride of the array as an array of integers + + +:: + + int const get_nd() const; + +:Returns: Number of array dimensions + + +:: + + bitflag const get_flags() const; + +:Returns: Array flags + +:: + + inline ndarray::bitflag operator|(ndarray::bitflag a, ndarray::bitflag b) + +:Returns: bitflag logically OR-ed as (a | b) + +:: + + inline ndarray::bitflag operator&(ndarray::bitflag a, ndarray::bitflag b) + +:Returns: bitflag logically AND-ed as (a & b) + + +Example(s) +---------- + +:: + + namespace p = boost::python; + namespace np = boost::python::numpy; + + p::object tu = p::make_tuple('a','b','c') ; + np::ndarray example_tuple = np::array (tu) ; + + p::list l ; + np::ndarray example_list = np::array (l) ; + + np::dtype dt = np::dtype::get_builtin(); + np::ndarray example_list1 = np::array (l,dt); + + int data[] = {1,2,3,4} ; + p::tuple shape = p::make_tuple(4) ; + p::tuple stride = p::make_tuple(4) ; + p::object own ; + np::ndarray data_ex = np::from_data(data,dt,shape,stride,own); + + uint8_t mul_data[][4] = {{1,2,3,4},{5,6,7,8},{1,3,5,7}}; + shape = p::make_tuple(3,2) ; + stride = p::make_tuple(4,2) ; + np::dtype dt1 = np::dtype::get_builtin(); + + np::ndarray mul_data_ex = np::from_data(mul_data,dt1, p::make_tuple(3,4),p::make_tuple(4,1),p::object()); + mul_data_ex = np::from_data(mul_data,dt1, shape,stride,p::object()); + diff --git a/doc/numpy/reference/unary_ufunc.rst b/doc/numpy/reference/unary_ufunc.rst new file mode 100644 index 00000000..eaec034c --- /dev/null +++ b/doc/numpy/reference/unary_ufunc.rst @@ -0,0 +1,103 @@ +unary_ufunc +=========== + +.. contents :: Table of Contents + +A ``unary_ufunc`` is a struct used as an intermediate step to broadcast a single argument so that a C++ function can be converted to a ufunc like function + + ```` contains the ``unary_ufunc`` structure definitions + + +synopsis +-------- + +:: + + namespace boost + { + namespace python + { + namespace numpy + { + + template + struct unary_ufunc + { + + static object call(TUnaryFunctor & self, + object const & input, + object const & output) ; + + static object make(); + + }; + } + } + } + + +constructors +------------ + +:: + + struct example_unary_ufunc + { + typedef any_valid_type argument_type; + typedef any_valid_type result_type; + }; + +:Requirements: The ``any_valid`` type must be defined using typedef as a valid C++ type in order to use the struct methods correctly + +:Note: The struct must be exposed as a Python class, and an instance of the class must be created to use the ``call`` method corresponding to the ``__call__`` attribute of the Python object + +accessors +--------- + +:: + + template + static object call(TUnaryFunctor & self, + object const & input, + object const & output); + +:Requires: Typenames ``TUnaryFunctor`` and optionally ``TArgument`` for argument type and ``TResult`` for result type + +:Effects: Passes a Python object to the underlying C++ functor after broadcasting its arguments + +:: + + template + static object make(); + +:Requires: Typenames ``TUnaryFunctor`` and optionally ``TArgument`` for argument type and ``TResult`` for result type + +:Returns: A Python function object to call the overloaded () operator in the struct (in typical usage) + + + +Example(s) +---------- + +:: + + namespace p = boost::python; + namespace np = boost::python::numpy; + + struct UnarySquare + { + typedef double argument_type; + typedef double result_type; + double operator()(double r) const { return r * r;} + }; + + p::object ud = p::class_ >("UnarySquare").def("__call__", np::unary_ufunc::make()); + p::object inst = ud(); + std::cout << "Square of unary scalar 1.0 is " << p::extract (p::str(inst.attr("__call__")(1.0))) << std::endl ; + diff --git a/doc/numpy/rst.css b/doc/numpy/rst.css new file mode 100644 index 00000000..afd9a98c --- /dev/null +++ b/doc/numpy/rst.css @@ -0,0 +1,149 @@ +@import url("doc/src/boostbook.css"); +@import url("doc/src/docutils.css"); +/* Copyright David Abrahams 2006. Distributed under the Boost + Software License, Version 1.0. (See accompanying + file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + */ + +dl.docutils dt { + font-weight: bold } + +img.boost-logo { + border: none; + vertical-align: middle +} + +pre.literal-block span.concept { + font-style: italic; +} + +.nav { +display: inline; +list-style-type: none; +} + +.prevpage { +padding-top: -5px; +text-align: left; +float: left; +} + +.nextpage { +padding-top: -20px; +text-align: right; +float: right; +} + +div.small { + font-size: smaller } + +h2 a { + font-size: 90%; +} +h3 a { + font-size: 80%; +} +h4 a { + font-size: 70%; +} +h5 a { + font-size: 60%; +} + +dl,table +{ + text-align: left; + font-size: 10pt; + line-height: 1.15; +} + + +/*============================================================================= + Tables +=============================================================================*/ + +/* The only clue docutils gives us that tables are logically tables, + and not, e.g., footnotes, is that they have border="1". Therefore + we're keying off of that. We used to manually patch docutils to + add a "table" class to all logical tables, but that proved much too + fragile. +*/ + + table[border="1"] + { + width: 92%; + margin-left: 4%; + margin-right: 4%; + } + + table[border="1"] + { + padding: 4px; + } + + /* Table Cells */ + table[border="1"] tr td + { + padding: 0.5em; + text-align: left; + font-size: 9pt; + } + + table[border="1"] tr th + { + padding: 0.5em 0.5em 0.5em 0.5em; + border: 1pt solid white; + font-size: 80%; + } + + @media screen + { + + /* Tables */ + table[border="1"] tr td + { + border: 1px solid #DCDCDC; + } + + table[border="1"] tr th + { + background-color: #F0F0F0; + border: 1px solid #DCDCDC; + } + + pre, + .screen + { + border: 1px solid #DCDCDC; + } + + td pre + td .screen + { + border: 0px + } + + .sidebar pre + { + border: 0px + } + + } + + pre, + .screen + { + font-size: 9pt; + display: block; + margin: 1pc 4% 0pc 4%; + padding: 0.5pc 0.5pc 0.5pc 0.5pc; + } + + /* Program listings in tables don't get borders */ + td pre, + td .screen + { + margin: 0pc 0pc 0pc 0pc; + padding: 0pc 0pc 0pc 0pc; + } + diff --git a/doc/numpy/tutorial/dtype.rst b/doc/numpy/tutorial/dtype.rst new file mode 100644 index 00000000..557e72ba --- /dev/null +++ b/doc/numpy/tutorial/dtype.rst @@ -0,0 +1,54 @@ +How to use dtypes +================= + +Here is a brief tutorial to show how to create ndarrays with built-in python data types, and extract the types and values of member variables + +Like before, first get the necessary headers, setup the namespaces and initialize the Python runtime and numpy module:: + + #include + #include + + namespace p = boost::python; + namespace np = boost::python::numpy; + + int main(int argc, char **argv) + { + Py_Initialize(); + np::initialize(); + +Next, we create the shape and dtype. We use the get_builtin method to get the numpy dtype corresponding to the builtin C++ dtype +Here, we will create a 3x3 array passing a tuple with (3,3) for the size, and double as the data type :: + + p::tuple shape = p::make_tuple(3, 3); + np::dtype dtype = np::dtype::get_builtin(); + np::ndarray a = np::zeros(shape, dtype); + +Finally, we can print the array using the extract method in the python namespace. +Here, we first convert the variable into a string, and then extract it as a C++ character array from the python string using the template :: + + std::cout << "Original array:\n" << p::extract(p::str(a)) << std::endl; + +We can also print the dtypes of the data members of the ndarray by using the get_dtype method for the ndarray :: + + std::cout << "Datatype is:\n" << p::extract(p::str(a.get_dtype())) << std::endl ; + +We can also create custom dtypes and build ndarrays with the custom dtypes + +We use the dtype constructor to create a custom dtype. This constructor takes a list as an argument. + +The list should contain one or more tuples of the format (variable name, variable type) + +So first create a tuple with a variable name and its dtype, double, to create a custom dtype :: + + p::tuple for_custom_dtype = p::make_tuple("ha",dtype) ; + +Next, create a list, and add this tuple to the list. Then use the list to create the custom dtype :: + + p::list list_for_dtype ; + list_for_dtype.append(for_custom_dtype) ; + np::dtype custom_dtype = np::dtype(list_for_dtype) ; + +We are now ready to create an ndarray with dimensions specified by \*shape\* and of custom dtpye :: + + np::ndarray new_array = np::zeros(shape,custom_dtype); + } diff --git a/doc/numpy/tutorial/fromdata.rst b/doc/numpy/tutorial/fromdata.rst new file mode 100644 index 00000000..33bcee45 --- /dev/null +++ b/doc/numpy/tutorial/fromdata.rst @@ -0,0 +1,56 @@ +How to access data using raw pointers +===================================== + +One of the advantages of the ndarray wrapper is that the same data can be used in both Python and C++ and changes can be made to reflect at both ends. +The from_data method makes this possible. + +Like before, first get the necessary headers, setup the namespaces and initialize the Python runtime and numpy module:: + + #include + #include + + namespace p = boost::python; + namespace np = boost::python::numpy; + + int main(int argc, char **argv) + { + Py_Initialize(); + np::initialize(); + +Create an array in C++ , and pass the pointer to it to the from_data method to create an ndarray:: + + int arr[] = {1,2,3,4,5}; + np::ndarray py_array = np::from_data(arr, np::dtype::get_builtin(), + p::make_tuple(5), + p::make_tuple(sizeof(int)), + p::object()); + +Print the source C++ array, as well as the ndarray, to check if they are the same:: + + std::cout << "C++ array :" << std::endl; + for (int j=0;j<4;j++) + { + std::cout << arr[j] << ' '; + } + std::cout << std::endl + << "Python ndarray :" << p::extract(p::str(py_array)) << std::endl; + +Now, change an element in the Python ndarray, and check if the value changed correspondingly in the source C++ array:: + + py_array[1] = 5 ; + std::cout << "Is the change reflected in the C++ array used to create the ndarray ? " << std::endl; + for (int j = 0; j < 5; j++) + { + std::cout << arr[j] << ' '; + } + +Next, change an element of the source C++ array and see if it is reflected in the Python ndarray:: + + arr[2] = 8; + std::cout << std::endl + << "Is the change reflected in the Python ndarray ?" << std::endl + << p::extract(p::str(py_array)) << std::endl; + } + +As we can see, the changes are reflected across the ends. This happens because the from_data method passes the C++ array by reference to create the ndarray, and thus uses the same locations for storing data. + diff --git a/doc/numpy/tutorial/index.rst b/doc/numpy/tutorial/index.rst new file mode 100644 index 00000000..3de2ef53 --- /dev/null +++ b/doc/numpy/tutorial/index.rst @@ -0,0 +1,12 @@ +Boost.Python NumPy extension Tutorial +===================================== + +.. toctree:: + :maxdepth: 2 + + simple + dtype + ndarray + ufunc + fromdata + diff --git a/doc/numpy/tutorial/ndarray.rst b/doc/numpy/tutorial/ndarray.rst new file mode 100644 index 00000000..b270da70 --- /dev/null +++ b/doc/numpy/tutorial/ndarray.rst @@ -0,0 +1,99 @@ +Creating ndarrays +================= + +The Boost.Numpy library exposes quite a few methods to create ndarrays. ndarrays can be created in a variety of ways, include empty arrays and zero filled arrays. +ndarrays can also be created from arbitrary python sequences as well as from data and dtypes. + +This tutorial will introduce you to some of the ways in which you can create ndarrays. The methods covered here include creating ndarrays from arbitrary Python sequences, as well as from C++ containers, using both unit and non-unit strides + +First, as before, initialise the necessary namepaces and runtimes :: + + #include + #include + + namespace p = boost::python; + namespace np = boost::python::numpy; + + int main(int argc, char **argv) + { + Py_Initialize(); + np::initialize(); + +Let's now create an ndarray from a simple tuple. We first create a tuple object, and then pass it to the array method, to generate the necessary tuple :: + + p::object tu = p::make_tuple('a','b','c'); + np::ndarray example_tuple = np::array(tu); + +Let's now try the same with a list. We create an empty list, add an element using the append method, and as before, call the array method :: + + p::list l; + l.append('a'); + np::ndarray example_list = np::array (l); + +Optionally, we can also specify a dtype for the array :: + + np::dtype dt = np::dtype::get_builtin(); + np::ndarray example_list1 = np::array (l,dt); + +We can also create an array by supplying data arrays and a few other parameters. + +First,create an integer array :: + + int data[] = {1,2,3,4,5}; + +Create a shape, and strides, needed by the function :: + + p::tuple shape = p::make_tuple(5); + p::tuple stride = p::make_tuple(sizeof(int)); + +Here, shape is (4,) , and the stride is `sizeof(int)``. +A stride is the number of bytes that must be traveled to get to the next desired element while constructing the ndarray. + +The function also needs an owner, to keep track of the data array passed. Passing none is dangerous :: + + p::object own; + +The from_data function takes the data array, datatype,shape,stride and owner as arguments and returns an ndarray :: + + np::ndarray data_ex1 = np::from_data(data,dt, shape,stride,own); + +Now let's print the ndarray we created :: + + std::cout << "Single dimensional array ::" << std::endl + << p::extract(p::str(data_ex)) << std::endl; + +Let's make it a little more interesting. Lets make an 3x2 ndarray from a multi-dimensional array using non-unit strides + +First lets create a 3x4 array of 8-bit integers :: + + uint8_t mul_data[][4] = {{1,2,3,4},{5,6,7,8},{1,3,5,7}}; + +Now let's create an array of 3x2 elements, picking the first and third elements from each row . For that, the shape will be 3x2. +The strides will be 4x2 i.e. 4 bytes to go to the next desired row, and 2 bytes to go to the next desired column :: + + shape = p::make_tuple(3,2); + stride = p::make_tuple(sizeof(uint8_t)*2,sizeof(uint8_t)); + +Get the numpy dtype for the built-in 8-bit integer data type :: + + np::dtype dt1 = np::dtype::get_builtin(); + +Now lets first create and print out the ndarray as is. +Notice how we can pass the shape and strides in the function directly, as well as the owner. The last part can be done because we don't have any use to +manipulate the "owner" object :: + + np::ndarray mul_data_ex = np::from_data(mul_data, dt1, + p::make_tuple(3,4), + p::make_tuple(4,1), + p::object()); + std::cout << "Original multi dimensional array :: " << std::endl + << p::extract(p::str(mul_data_ex)) << std::endl; + +Now create the new ndarray using the shape and strides and print out the array we created using non-unit strides :: + + mul_data_ex = np::from_data(mul_data, dt1, shape, stride, p::object()); + std::cout << "Selective multidimensional array :: "<(p::str(mul_data_ex)) << std::endl ; + } + +.. note:: The from_data method will throw ``error_already_set`` if the number of elements dictated by the shape and the corresponding strides don't match. diff --git a/doc/numpy/tutorial/simple.rst b/doc/numpy/tutorial/simple.rst new file mode 100644 index 00000000..889eea11 --- /dev/null +++ b/doc/numpy/tutorial/simple.rst @@ -0,0 +1,41 @@ +A simple tutorial on Arrays +=========================== + +Let's start with a simple tutorial to create and modify arrays. + +Get the necessary headers for numpy components and set up necessary namespaces:: + + #include + #include + + namespace p = boost::python; + namespace np = boost::python::numpy; + +Initialise the Python runtime, and the numpy module. Failure to call these results in segmentation errors:: + + int main(int argc, char **argv) + { + Py_Initialize(); + np::initialize(); + + +Zero filled n-dimensional arrays can be created using the shape and data-type of the array as a parameter. Here, the shape is 3x3 and the datatype is the built-in float type:: + + p::tuple shape = p::make_tuple(3, 3); + np::dtype dtype = np::dtype::get_builtin(); + np::ndarray a = np::zeros(shape, dtype); + +You can also create an empty array like this :: + + np::ndarray b = np::empty(shape,dtype); + +Print the original and reshaped array. The array a which is a list is first converted to a string, and each value in the list is extracted using extract< >:: + + std::cout << "Original array:\n" << p::extract(p::str(a)) << std::endl; + + // Reshape the array into a 1D array + a = a.reshape(p::make_tuple(9)); + // Print it again. + std::cout << "Reshaped array:\n" << p::extract(p::str(a)) << std::endl; + } + diff --git a/doc/numpy/tutorial/ufunc.rst b/doc/numpy/tutorial/ufunc.rst new file mode 100644 index 00000000..a3571dbc --- /dev/null +++ b/doc/numpy/tutorial/ufunc.rst @@ -0,0 +1,120 @@ +Ufuncs +====== + +Ufuncs or universal functions operate on ndarrays element by element, and support array broadcasting, type casting, and other features. + +Lets try and see how we can use the binary and unary ufunc methods + +After the neccessary includes :: + + #include + #include + + namespace p = boost::python; + namespace np = boost::python::numpy; + +Now we create the structs necessary to implement the ufuncs. The typedefs *must* be made as the ufunc generators take these typedefs as inputs and return an error otherwise :: + + struct UnarySquare + { + typedef double argument_type; + typedef double result_type; + + double operator()(double r) const { return r * r;} + }; + + struct BinarySquare + { + typedef double first_argument_type; + typedef double second_argument_type; + typedef double result_type; + + double operator()(double a,double b) const { return (a*a + b*b) ; } + }; + +Initialise the Python runtime and the numpy module :: + + int main(int argc, char **argv) + { + Py_Initialize(); + np::initialize(); + +Now expose the struct UnarySquare to Python as a class, and let ud be the class object. :: + + p::object ud = p::class_ >("UnarySquare"); + ud.def("__call__", np::unary_ufunc::make()); + +Let inst be an instance of the class ud :: + + p::object inst = ud(); + +Use the "__call__" method to call the overloaded () operator and print the value :: + + std::cout << "Square of unary scalar 1.0 is " << p::extract(p::str(inst.attr("__call__")(1.0))) << std::endl; + +Create an array in C++ :: + + int arr[] = {1,2,3,4} ; + + +..and use it to create the ndarray in Python :: + + np::ndarray demo_array = np::from_data(arr, np::dtype::get_builtin(), + p::make_tuple(4), + p::make_tuple(4), + p::object()); + +Print out the demo array :: + + std::cout << "Demo array is " << p::extract(p::str(demo_array)) << std::endl; + +Call the "__call__" method to perform the operation and assign the value to result_array :: + + p::object result_array = inst.attr("__call__")(demo_array); + +Print the resultant array :: + + std::cout << "Square of demo array is " << p::extract(p::str(result_array)) << std::endl; + +Lets try the same with a list :: + + p::list li; + li.append(3); + li.append(7); + +Print out the demo list :: + + std::cout << "Demo list is " << p::extract(p::str(li)) << std::endl; + +Call the ufunc for the list :: + + result_array = inst.attr("__call__")(li); + +And print the list out :: + + std::cout << "Square of demo list is " << p::extract(p::str(result_array)) << std::endl; + +Now lets try Binary ufuncs. Again, expose the struct BinarySquare to Python as a class, and let ud be the class object :: + + ud = p::class_ >("BinarySquare"); + ud.def("__call__", np::binary_ufunc::make()); + +And initialise ud :: + + inst = ud(); + +Print the two input lists :: + + std::cout << "The two input list for binary ufunc are " << std::endl + << p::extract(p::str(demo_array)) << std::endl + << p::extract(p::str(demo_array)) << std::endl; + +Call the binary ufunc taking demo_array as both inputs :: + + result_array = inst.attr("__call__")(demo_array,demo_array); + +And print the output :: + + std::cout << "Square of list with binary ufunc is " << p::extract(p::str(result_array)) << std::endl; + } + diff --git a/doc/python.qbk b/doc/python.qbk index 0a3b7d0d..ffd2d0dd 100644 --- a/doc/python.qbk +++ b/doc/python.qbk @@ -50,6 +50,7 @@ The development of these features was funded in part by grants to `Boost Consult * [link glossary Glossary] * [link support Support Resources] * [link faq Frequently Asked Questions (FAQs)] +* [@numpy/index.html NumPy Extension Documentation] [endsect] diff --git a/example/Jamroot b/example/Jamroot index 27ac49e8..fe9d69ec 100644 --- a/example/Jamroot +++ b/example/Jamroot @@ -1,43 +1,35 @@ -# Copyright David Abrahams 2006. Distributed under the Boost -# Software License, Version 1.0. (See accompanying -# file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +# Copyright Stefan Seefeld 2016. +# Distributed under the Boost Software License, Version 1.0. +# (See accompanying file LICENSE_1_0.txt or copy at +# http://www.boost.org/LICENSE_1_0.txt) -# Specify the path to the Boost project. If you move this project, -# adjust this path to refer to the Boost root directory. -use-project boost - : ../../.. ; +import python ; -# Set up the project-wide requirements that everything uses the -# boost_python library from the project whose global ID is -# /boost/python. -project - : requirements /boost/python//boost_python - /boost//headers - : usage-requirements /boost//headers - ; - -# Declare the three extension modules. You can specify multiple -# source files after the colon separated by spaces. -python-extension getting_started1 : getting_started1.cpp ; -python-extension getting_started2 : getting_started2.cpp ; -python-extension std_pair_ext : std_pair.cpp ; - -# A little "rule" (function) to clean up the syntax of declaring tests -# of these extension modules. -local rule run-test ( test-name : sources + ) +if ! [ python.configured ] { - import testing ; - testing.make-test run-pyd : $(sources) : : $(test-name) ; + ECHO "warning: no Python configured in user-config.jam" ; + ECHO "warning: will use default configuration" ; + using python ; } -# Declare test targets -run-test test1 : getting_started1 test_getting_started1.py ; -run-test test2 : getting_started2 test_getting_started2.py ; -run-test test3 : std_pair_ext test_std_pair.py ; +# Adjust the following if Boost.Python isn't installed in a default location +lib boost_python ; -# A target that runs all the tests -alias test : test1 test2 test3 ; +project + : requirements +# /path/to/boost/python + boost_python +; -# Only run tests when explicitly requested -explicit test test1 test2 test3 ; +rule run-test ( test-name : sources + ) +{ + import testing ; + testing.make-test run-pyd : $(sources) : : $(test-name) ; +} +build-project quickstart ; +build-project tutorial ; +if [ python.numpy ] +{ + build-project numpy ; +} diff --git a/example/README b/example/README deleted file mode 100644 index 29a94f67..00000000 --- a/example/README +++ /dev/null @@ -1,16 +0,0 @@ -.. Copyright David Abrahams 2006. Distributed under the Boost -.. Software License, Version 1.0. (See accompanying -.. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) - -To get started with the Boost Python Library, use the examples -getting_started1.cpp and getting_started2.cpp. Invoking - - bjam --toolset=your-toolset test - -in this directory will build and run the examples. See -http://www.boost.org/more/getting_started.html for details about the ---toolset= option. - -If you move this example from its place in the Boost development tree -you'll need to edit the two lines indicated in Jamroot and -boost-build.jam. diff --git a/example/README.md b/example/README.md new file mode 100644 index 00000000..b090cbe1 --- /dev/null +++ b/example/README.md @@ -0,0 +1,11 @@ +![logo](https://raw.githubusercontent.com/boostorg/python/develop/doc/images/bpl.png) + +# Examples + +This directory contains various examples using Boost.Python. +You may compile these using the `bjam` command either in this directory +or in any of the subdirectories. +You may need to adjust the paths in the Jamroot file if Boost.Python +is not installed in a default location. +See http://boostorg.github.io/python/doc/html/building/no_install_quickstart.html +for details. diff --git a/example/boost-build.jam b/example/boost-build.jam deleted file mode 100755 index 9b8d19e0..00000000 --- a/example/boost-build.jam +++ /dev/null @@ -1,7 +0,0 @@ -# Copyright David Abrahams 2006. Distributed under the Boost -# Software License, Version 1.0. (See accompanying -# file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) - -# Edit this path to point at the tools/build/src subdirectory of your -# Boost installation. Absolute paths work, too. -boost-build ../../../tools/build/src ; diff --git a/example/getting_started1.cpp b/example/getting_started1.cpp deleted file mode 100644 index 09d68103..00000000 --- a/example/getting_started1.cpp +++ /dev/null @@ -1,25 +0,0 @@ -// Copyright Ralf W. Grosse-Kunstleve 2002-2004. Distributed under the Boost -// Software License, Version 1.0. (See accompanying -// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) - -#include -#include -#include - -namespace { // Avoid cluttering the global namespace. - - // A couple of simple C++ functions that we want to expose to Python. - std::string greet() { return "hello, world"; } - int square(int number) { return number * number; } -} - -namespace python = boost::python; - -// Python requires an exported function called init in every -// extension module. This is where we build the module contents. -BOOST_PYTHON_MODULE(getting_started1) -{ - // Add regular functions to the module. - python::def("greet", greet); - python::def("square", square); -} diff --git a/example/getting_started2.cpp b/example/getting_started2.cpp deleted file mode 100644 index ee8af32e..00000000 --- a/example/getting_started2.cpp +++ /dev/null @@ -1,41 +0,0 @@ -// Copyright Ralf W. Grosse-Kunstleve 2002-2004. Distributed under the Boost -// Software License, Version 1.0. (See accompanying -// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) - -#include -#include -#include -#include -#include - -namespace { // Avoid cluttering the global namespace. - - // A friendly class. - class hello - { - public: - hello(const std::string& country) { this->country = country; } - std::string greet() const { return "Hello from " + country; } - private: - std::string country; - }; - - // A function taking a hello object as an argument. - std::string invite(const hello& w) { - return w.greet() + "! Please come soon!"; - } -} - -BOOST_PYTHON_MODULE(getting_started2) -{ - using namespace boost::python; - class_("hello", init()) - // Add a regular member function. - .def("greet", &hello::greet) - // Add invite() as a member of hello! - .def("invite", invite) - ; - - // Also add invite() as a regular function to the module. - def("invite", invite); -} diff --git a/example/numpy/Jamfile b/example/numpy/Jamfile new file mode 100644 index 00000000..ac70a6ff --- /dev/null +++ b/example/numpy/Jamfile @@ -0,0 +1,29 @@ +# Copyright Stefan Seefeld 2016. +# Distributed under the Boost Software License, Version 1.0. +# (See accompanying file LICENSE_1_0.txt or copy at +# http://www.boost.org/LICENSE_1_0.txt) + +import python ; + +# Adjust the following if Boost.Python isn't installed in a default location +lib boost_numpy + : + : /usr/local/Boost/lib + /usr/local/Boost/include + ; + +project numpy + : requirements + /usr/local/Boost/include + boost_numpy + . + ; + +exe simple : simple.cpp boost_numpy /python//python ; +exe dtype : dtype.cpp boost_numpy /python//python ; +exe ndarray : ndarray.cpp /python//python ; +exe fromdata : fromdata.cpp /python//python ; +exe ufunc : ufunc.cpp /python//python ; +exe wrap : wrap.cpp /python//python ; + +python-extension gaussian : gaussian.cpp ; diff --git a/example/numpy/demo_gaussian.py b/example/numpy/demo_gaussian.py new file mode 100644 index 00000000..0b1c7899 --- /dev/null +++ b/example/numpy/demo_gaussian.py @@ -0,0 +1,37 @@ +# Copyright Jim Bosch 2010-2012. +# Distributed under the Boost Software License, Version 1.0. +# (See accompanying file LICENSE_1_0.txt or copy at +# http://www.boost.org/LICENSE_1_0.txt) + +import numpy +import gaussian + +mu = numpy.zeros(2, dtype=float) +sigma = numpy.identity(2, dtype=float) +sigma[0, 1] = 0.15 +sigma[1, 0] = 0.15 + +g = gaussian.bivariate_gaussian(mu, sigma) + +r = numpy.linspace(-40, 40, 1001) +x, y = numpy.meshgrid(r, r) + +z = g(x, y) + +s = z.sum() * (r[1] - r[0])**2 +print "sum (should be ~ 1):", s + +xc = (z * x).sum() / z.sum() +print "x centroid (should be ~ %f): %f" % (mu[0], xc) + +yc = (z * y).sum() / z.sum() +print "y centroid (should be ~ %f): %f" % (mu[1], yc) + +xx = (z * (x - xc)**2).sum() / z.sum() +print "xx moment (should be ~ %f): %f" % (sigma[0,0], xx) + +yy = (z * (y - yc)**2).sum() / z.sum() +print "yy moment (should be ~ %f): %f" % (sigma[1,1], yy) + +xy = 0.5 * (z * (x - xc) * (y - yc)).sum() / z.sum() +print "xy moment (should be ~ %f): %f" % (sigma[0,1], xy) diff --git a/example/numpy/dtype.cpp b/example/numpy/dtype.cpp new file mode 100644 index 00000000..749a36b5 --- /dev/null +++ b/example/numpy/dtype.cpp @@ -0,0 +1,49 @@ +// Copyright Ankit Daftery 2011-2012. +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +/** + * @brief An example to show how to create ndarrays with built-in python data types, and extract + * the types and values of member variables + * + * @todo Add an example to show type conversion. + * Add an example to show use of user-defined types + * + */ + +#include +#include + +namespace p = boost::python; +namespace np = boost::python::numpy; + +int main(int argc, char **argv) +{ + // Initialize the Python runtime. + Py_Initialize(); + // Initialize NumPy + np::initialize(); + // Create a 3x3 shape... + p::tuple shape = p::make_tuple(3, 3); + // ...as well as a type for C++ double + np::dtype dtype = np::dtype::get_builtin(); + // Construct an array with the above shape and type + np::ndarray a = np::zeros(shape, dtype); + // Print the array + std::cout << "Original array:\n" << p::extract(p::str(a)) << std::endl; + // Print the datatype of the elements + std::cout << "Datatype is:\n" << p::extract(p::str(a.get_dtype())) << std::endl ; + // Using user defined dtypes to create dtype and an array of the custom dtype + // First create a tuple with a variable name and its dtype, double, to create a custom dtype + p::tuple for_custom_dtype = p::make_tuple("ha",dtype) ; + // The list needs to be created, because the constructor to create the custom dtype + // takes a list of (variable,variable_type) as an argument + p::list list_for_dtype ; + list_for_dtype.append(for_custom_dtype) ; + // Create the custom dtype + np::dtype custom_dtype = np::dtype(list_for_dtype) ; + // Create an ndarray with the custom dtype + np::ndarray new_array = np::zeros(shape,custom_dtype); + +} diff --git a/example/numpy/fromdata.cpp b/example/numpy/fromdata.cpp new file mode 100644 index 00000000..bd073cc6 --- /dev/null +++ b/example/numpy/fromdata.cpp @@ -0,0 +1,48 @@ +// Copyright Ankit Daftery 2011-2012. +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +/** + * @brief An example to show how to access data using raw pointers. This shows that you can use and + * manipulate data in either Python or C++ and have the changes reflected in both. + */ + +#include +#include + +namespace p = boost::python; +namespace np = boost::python::numpy; + + +int main(int argc, char **argv) +{ + // Initialize the Python runtime. + Py_Initialize(); + // Initialize NumPy + np::initialize(); + // Create an array in C++ + int arr[] = {1,2,3,4} ; + // Create the ndarray in Python + np::ndarray py_array = np::from_data(arr, np::dtype::get_builtin() , p::make_tuple(4), p::make_tuple(4), p::object()); + // Print the ndarray that we just created, and the source C++ array + std::cout << "C++ array :" << std::endl ; + for (int j=0;j<4;j++) + { + std::cout << arr[j] << ' ' ; + } + std::cout << std::endl << "Python ndarray :" << p::extract(p::str(py_array)) << std::endl; + // Change an element in the python ndarray + py_array[1] = 5 ; + // And see if the C++ container is changed or not + std::cout << "Is the change reflected in the C++ array used to create the ndarray ? " << std::endl ; + for (int j = 0;j<4 ; j++) + { + std::cout << arr[j] << ' ' ; + } + // Conversely, change it in C++ + arr[2] = 8 ; + // And see if the changes are reflected in the Python ndarray + std::cout << std::endl << "Is the change reflected in the Python ndarray ?" << std::endl << p::extract(p::str(py_array)) << std::endl; + +} diff --git a/example/numpy/gaussian.cpp b/example/numpy/gaussian.cpp new file mode 100644 index 00000000..5f138b39 --- /dev/null +++ b/example/numpy/gaussian.cpp @@ -0,0 +1,315 @@ +// Copyright Jim Bosch 2010-2012. +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +#include + +#include +#include + +#ifndef M_PI +#include +const double M_PI = boost::math::constants::pi(); +#endif + +namespace bp = boost::python; +namespace bn = boost::python::numpy; + +/** + * A 2x2 matrix class, purely for demonstration purposes. + * + * Instead of wrapping this class with Boost.Python, we'll convert it to/from numpy.ndarray. + */ +class matrix2 { +public: + + double & operator()(int i, int j) { + return _data[i*2 + j]; + } + + double const & operator()(int i, int j) const { + return _data[i*2 + j]; + } + + double const * data() const { return _data; } + +private: + double _data[4]; +}; + +/** + * A 2-element vector class, purely for demonstration purposes. + * + * Instead of wrapping this class with Boost.Python, we'll convert it to/from numpy.ndarray. + */ +class vector2 { +public: + + double & operator[](int i) { + return _data[i]; + } + + double const & operator[](int i) const { + return _data[i]; + } + + double const * data() const { return _data; } + + vector2 operator+(vector2 const & other) const { + vector2 r; + r[0] = _data[0] + other[0]; + r[1] = _data[1] + other[1]; + return r; + } + + vector2 operator-(vector2 const & other) const { + vector2 r; + r[0] = _data[0] - other[0]; + r[1] = _data[1] - other[1]; + return r; + } + +private: + double _data[2]; +}; + +/** + * Matrix-vector multiplication. + */ +vector2 operator*(matrix2 const & m, vector2 const & v) { + vector2 r; + r[0] = m(0, 0) * v[0] + m(0, 1) * v[1]; + r[1] = m(1, 0) * v[0] + m(1, 1) * v[1]; + return r; +} + +/** + * Vector inner product. + */ +double dot(vector2 const & v1, vector2 const & v2) { + return v1[0] * v2[0] + v1[1] * v2[1]; +} + +/** + * This class represents a simple 2-d Gaussian (Normal) distribution, defined by a + * mean vector 'mu' and a covariance matrix 'sigma'. + */ +class bivariate_gaussian { +public: + + vector2 const & get_mu() const { return _mu; } + + matrix2 const & get_sigma() const { return _sigma; } + + /** + * Evaluate the density of the distribution at a point defined by a two-element vector. + */ + double operator()(vector2 const & p) const { + vector2 u = _cholesky * (p - _mu); + return 0.5 * _cholesky(0, 0) * _cholesky(1, 1) * std::exp(-0.5 * dot(u, u)) / M_PI; + } + + /** + * Evaluate the density of the distribution at an (x, y) point. + */ + double operator()(double x, double y) const { + vector2 p; + p[0] = x; + p[1] = y; + return operator()(p); + } + + /** + * Construct from a mean vector and covariance matrix. + */ + bivariate_gaussian(vector2 const & mu, matrix2 const & sigma) + : _mu(mu), _sigma(sigma), _cholesky(compute_inverse_cholesky(sigma)) + {} + +private: + + /** + * This evaluates the inverse of the Cholesky factorization of a 2x2 matrix; + * it's just a shortcut in evaluating the density. + */ + static matrix2 compute_inverse_cholesky(matrix2 const & m) { + matrix2 l; + // First do cholesky factorization: l l^t = m + l(0, 0) = std::sqrt(m(0, 0)); + l(0, 1) = m(0, 1) / l(0, 0); + l(1, 1) = std::sqrt(m(1, 1) - l(0,1) * l(0,1)); + // Now do forward-substitution (in-place) to invert: + l(0, 0) = 1.0 / l(0, 0); + l(1, 0) = l(0, 1) = -l(0, 1) / l(1, 1); + l(1, 1) = 1.0 / l(1, 1); + return l; + } + + vector2 _mu; + matrix2 _sigma; + matrix2 _cholesky; + +}; + +/* + * We have a two options for wrapping get_mu and get_sigma into NumPy-returning Python methods: + * - we could deep-copy the data, making totally new NumPy arrays; + * - we could make NumPy arrays that point into the existing memory. + * The latter is often preferable, especially if the arrays are large, but it's dangerous unless + * the reference counting is correct: the returned NumPy array needs to hold a reference that + * keeps the memory it points to from being deallocated as long as it is alive. This is what the + * "owner" argument to from_data does - the NumPy array holds a reference to the owner, keeping it + * from being destroyed. + * + * Note that this mechanism isn't completely safe for data members that can have their internal + * storage reallocated. A std::vector, for instance, can be invalidated when it is resized, + * so holding a Python reference to a C++ class that holds a std::vector may not be a guarantee + * that the memory in the std::vector will remain valid. + */ + +/** + * These two functions are custom wrappers for get_mu and get_sigma, providing the shallow-copy + * conversion with reference counting described above. + * + * It's also worth noting that these return NumPy arrays that cannot be modified in Python; + * the const overloads of vector::data() and matrix::data() return const references, + * and passing a const pointer to from_data causes NumPy's 'writeable' flag to be set to false. + */ +static bn::ndarray py_get_mu(bp::object const & self) { + vector2 const & mu = bp::extract(self)().get_mu(); + return bn::from_data( + mu.data(), + bn::dtype::get_builtin(), + bp::make_tuple(2), + bp::make_tuple(sizeof(double)), + self + ); +} +static bn::ndarray py_get_sigma(bp::object const & self) { + matrix2 const & sigma = bp::extract(self)().get_sigma(); + return bn::from_data( + sigma.data(), + bn::dtype::get_builtin(), + bp::make_tuple(2, 2), + bp::make_tuple(2 * sizeof(double), sizeof(double)), + self + ); +} + +/** + * To allow the constructor to work, we need to define some from-Python converters from NumPy arrays + * to the matrix/vector types. The rvalue-from-python functionality is not well-documented in Boost.Python + * itself; you can learn more from boost/python/converter/rvalue_from_python_data.hpp. + */ + +/** + * We start with two functions that just copy a NumPy array into matrix/vector objects. These will be used + * in the templated converted below. The first just uses the operator[] overloads provided by + * bp::object. + */ +static void copy_ndarray_to_mv2(bn::ndarray const & array, vector2 & vec) { + vec[0] = bp::extract(array[0]); + vec[1] = bp::extract(array[1]); +} + +/** + * Here, we'll take the alternate approach of using the strides to access the array's memory directly. + * This can be much faster for large arrays. + */ +static void copy_ndarray_to_mv2(bn::ndarray const & array, matrix2 & mat) { + // Unfortunately, get_strides() can't be inlined, so it's best to call it once up-front. + Py_intptr_t const * strides = array.get_strides(); + for (int i = 0; i < 2; ++i) { + for (int j = 0; j < 2; ++j) { + mat(i, j) = *reinterpret_cast(array.get_data() + i * strides[0] + j * strides[1]); + } + } +} + +/** + * Here's the actual converter. Because we've separated the differences into the above functions, + * we can write a single template class that works for both matrix2 and vector2. + */ +template +struct mv2_from_python { + + /** + * Register the converter. + */ + mv2_from_python() { + bp::converter::registry::push_back( + &convertible, + &construct, + bp::type_id< T >() + ); + } + + /** + * Test to see if we can convert this to the desired type; if not return zero. + * If we can convert, returned pointer can be used by construct(). + */ + static void * convertible(PyObject * p) { + try { + bp::object obj(bp::handle<>(bp::borrowed(p))); + std::auto_ptr array( + new bn::ndarray( + bn::from_object(obj, bn::dtype::get_builtin(), N, N, bn::ndarray::V_CONTIGUOUS) + ) + ); + if (array->shape(0) != 2) return 0; + if (N == 2 && array->shape(1) != 2) return 0; + return array.release(); + } catch (bp::error_already_set & err) { + bp::handle_exception(); + return 0; + } + } + + /** + * Finish the conversion by initializing the C++ object into memory prepared by Boost.Python. + */ + static void construct(PyObject * obj, bp::converter::rvalue_from_python_stage1_data * data) { + // Extract the array we passed out of the convertible() member function. + std::auto_ptr array(reinterpret_cast(data->convertible)); + // Find the memory block Boost.Python has prepared for the result. + typedef bp::converter::rvalue_from_python_storage storage_t; + storage_t * storage = reinterpret_cast(data); + // Use placement new to initialize the result. + T * m_or_v = new (storage->storage.bytes) T(); + // Fill the result with the values from the NumPy array. + copy_ndarray_to_mv2(*array, *m_or_v); + // Finish up. + data->convertible = storage->storage.bytes; + } + +}; + + +BOOST_PYTHON_MODULE(gaussian) { + bn::initialize(); + + // Register the from-python converters + mv2_from_python< vector2, 1 >(); + mv2_from_python< matrix2, 2 >(); + + typedef double (bivariate_gaussian::*call_vector)(vector2 const &) const; + + bp::class_("bivariate_gaussian", bp::init()) + + // Declare the constructor (wouldn't work without the from-python converters). + .def(bp::init< vector2 const &, matrix2 const & >()) + + // Use our custom reference-counting getters + .add_property("mu", &py_get_mu) + .add_property("sigma", &py_get_sigma) + + // First overload accepts a two-element array argument + .def("__call__", (call_vector)&bivariate_gaussian::operator()) + + // This overload works like a binary NumPy universal function: you can pass + // in scalars or arrays, and the C++ function will automatically be called + // on each element of an array argument. + .def("__call__", bn::binary_ufunc::make()) + ; +} diff --git a/example/numpy/ndarray.cpp b/example/numpy/ndarray.cpp new file mode 100644 index 00000000..d7b57aa7 --- /dev/null +++ b/example/numpy/ndarray.cpp @@ -0,0 +1,71 @@ +// Copyright Ankit Daftery 2011-2012. +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +/** + * @brief An example to show how to create ndarrays using arbitrary Python sequences. + * + * The Python sequence could be any object whose __array__ method returns an array, or any + * (nested) sequence. This example also shows how to create arrays using both unit and + * non-unit strides. + */ + +#include +#include + +namespace p = boost::python; +namespace np = boost::python::numpy; + +#if _MSC_VER +using boost::uint8_t; +#endif + +int main(int argc, char **argv) +{ + // Initialize the Python runtime. + Py_Initialize(); + // Initialize NumPy + np::initialize(); + // Create an ndarray from a simple tuple + p::object tu = p::make_tuple('a','b','c') ; + np::ndarray example_tuple = np::array (tu) ; + // and from a list + p::list l ; + np::ndarray example_list = np::array (l) ; + // Optionally, you can also specify a dtype + np::dtype dt = np::dtype::get_builtin(); + np::ndarray example_list1 = np::array (l,dt); + // You can also create an array by supplying data.First,create an integer array + int data[] = {1,2,3,4} ; + // Create a shape, and strides, needed by the function + p::tuple shape = p::make_tuple(4) ; + p::tuple stride = p::make_tuple(4) ; + // The function also needs an owner, to keep track of the data array passed. Passing none is dangerous + p::object own ; + // The from_data function takes the data array, datatype,shape,stride and owner as arguments + // and returns an ndarray + np::ndarray data_ex = np::from_data(data,dt,shape,stride,own); + // Print the ndarray we created + std::cout << "Single dimensional array ::" << std::endl << p::extract < char const * > (p::str(data_ex)) << std::endl ; + // Now lets make an 3x2 ndarray from a multi-dimensional array using non-unit strides + // First lets create a 3x4 array of 8-bit integers + uint8_t mul_data[][4] = {{1,2,3,4},{5,6,7,8},{1,3,5,7}}; + // Now let's create an array of 3x2 elements, picking the first and third elements from each row + // For that, the shape will be 3x2 + shape = p::make_tuple(3,2) ; + // The strides will be 4x2 i.e. 4 bytes to go to the next desired row, and 2 bytes to go to the next desired column + stride = p::make_tuple(4,2) ; + // Get the numpy dtype for the built-in 8-bit integer data type + np::dtype dt1 = np::dtype::get_builtin(); + // First lets create and print out the ndarray as is + np::ndarray mul_data_ex = np::from_data(mul_data,dt1, p::make_tuple(3,4),p::make_tuple(4,1),p::object()); + std::cout << "Original multi dimensional array :: " << std::endl << p::extract < char const * > (p::str(mul_data_ex)) << std::endl ; + // Now create the new ndarray using the shape and strides + mul_data_ex = np::from_data(mul_data,dt1, shape,stride,p::object()); + // Print out the array we created using non-unit strides + std::cout << "Selective multidimensional array :: "< (p::str(mul_data_ex)) << std::endl ; + +} + + diff --git a/example/numpy/simple.cpp b/example/numpy/simple.cpp new file mode 100644 index 00000000..ad598bde --- /dev/null +++ b/example/numpy/simple.cpp @@ -0,0 +1,32 @@ +// Copyright 2011 Stefan Seefeld. +// Distributed under the Boost Software License, Version 1.0. (See +// accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +#include +#include + +namespace p = boost::python; +namespace np = boost::python::numpy; + +int main(int argc, char **argv) +{ + // Initialize the Python runtime. + Py_Initialize(); + // Initialize NumPy + np::initialize(); + // Create a 3x3 shape... + p::tuple shape = p::make_tuple(3, 3); + // ...as well as a type for C++ float + np::dtype dtype = np::dtype::get_builtin(); + // Construct an array with the above shape and type + np::ndarray a = np::zeros(shape, dtype); + // Construct an empty array with the above shape and dtype as well + np::ndarray b = np::empty(shape,dtype); + // Print the array + std::cout << "Original array:\n" << p::extract(p::str(a)) << std::endl; + // Reshape the array into a 1D array + a = a.reshape(p::make_tuple(9)); + // Print it again. + std::cout << "Reshaped array:\n" << p::extract(p::str(a)) << std::endl; +} diff --git a/example/numpy/ufunc.cpp b/example/numpy/ufunc.cpp new file mode 100644 index 00000000..5fb69204 --- /dev/null +++ b/example/numpy/ufunc.cpp @@ -0,0 +1,86 @@ +// Copyright Ankit Daftery 2011-2012. +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +/** + * @brief An example to demonstrate use of universal functions or ufuncs + * + * + * @todo Calling the overloaded () operator is in a roundabout manner, find a simpler way + * None of the methods like np::add, np::multiply etc are supported as yet + */ + +#include +#include + +namespace p = boost::python; +namespace np = boost::python::numpy; + + +// Create the structs necessary to implement the ufuncs +// The typedefs *must* be made + +struct UnarySquare +{ + typedef double argument_type; + typedef double result_type; + + double operator()(double r) const { return r * r;} +}; + +struct BinarySquare +{ + typedef double first_argument_type; + typedef double second_argument_type; + typedef double result_type; + + double operator()(double a,double b) const { return (a*a + b*b) ; } +}; + +int main(int argc, char **argv) +{ + // Initialize the Python runtime. + Py_Initialize(); + // Initialize NumPy + np::initialize(); + // Expose the struct UnarySquare to Python as a class, and let ud be the class object + p::object ud = p::class_ >("UnarySquare") + .def("__call__", np::unary_ufunc::make()); + // Let inst be an instance of the class ud + p::object inst = ud(); + // Use the "__call__" method to call the overloaded () operator and print the value + std::cout << "Square of unary scalar 1.0 is " << p::extract (p::str(inst.attr("__call__")(1.0))) << std::endl ; + // Create an array in C++ + int arr[] = {1,2,3,4} ; + // ..and use it to create the ndarray in Python + np::ndarray demo_array = np::from_data(arr, np::dtype::get_builtin() , p::make_tuple(4), p::make_tuple(4), p::object()); + // Print out the demo array + std::cout << "Demo array is " << p::extract (p::str(demo_array)) << std::endl ; + // Call the "__call__" method to perform the operation and assign the value to result_array + p::object result_array = inst.attr("__call__")(demo_array) ; + // Print the resultant array + std::cout << "Square of demo array is " << p::extract (p::str(result_array)) << std::endl ; + // Lets try the same with a list + p::list li ; + li.append(3); + li.append(7); + // Print out the demo list + std::cout << "Demo list is " << p::extract (p::str(li)) << std::endl ; + // Call the ufunc for the list + result_array = inst.attr("__call__")(li) ; + // And print the list out + std::cout << "Square of demo list is " << p::extract (p::str(result_array)) << std::endl ; + // Now lets try Binary ufuncs + // Expose the struct BinarySquare to Python as a class, and let ud be the class object + ud = p::class_ >("BinarySquare") + .def("__call__", np::binary_ufunc::make()); + // Again initialise inst as an instance of the class ud + inst = ud(); + // Print the two input listsPrint the two input lists + std::cout << "The two input list for binary ufunc are " << std::endl << p::extract (p::str(demo_array)) << std::endl << p::extract (p::str(demo_array)) << std::endl ; + // Call the binary ufunc taking demo_array as both inputs + result_array = inst.attr("__call__")(demo_array,demo_array) ; + std::cout << "Square of list with binary ufunc is " << p::extract (p::str(result_array)) << std::endl ; +} + diff --git a/example/numpy/wrap.cpp b/example/numpy/wrap.cpp new file mode 100644 index 00000000..33f71c85 --- /dev/null +++ b/example/numpy/wrap.cpp @@ -0,0 +1,135 @@ +// Copyright Jim Bosch 2011-2012. +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +/** + * A simple example showing how to wrap a couple of C++ functions that + * operate on 2-d arrays into Python functions that take NumPy arrays + * as arguments. + * + * If you find have a lot of such functions to wrap, you may want to + * create a C++ array type (or use one of the many existing C++ array + * libraries) that maps well to NumPy arrays and create Boost.Python + * converters. There's more work up front than the approach here, + * but much less boilerplate per function. See the "Gaussian" example + * included with Boost.NumPy for an example of custom converters, or + * take a look at the "ndarray" project on GitHub for a more complete, + * high-level solution. + * + * Note that we're using embedded Python here only to make a convenient + * self-contained example; you could just as easily put the wrappers + * in a regular C++-compiled module and imported them in regular + * Python. Again, see the Gaussian demo for an example. + */ + +#include +#include +#include + +namespace p = boost::python; +namespace np = boost::python::numpy; + +// This is roughly the most efficient way to write a C/C++ function that operates +// on a 2-d NumPy array - operate directly on the array by incrementing a pointer +// with the strides. +void fill1(double * array, int rows, int cols, int row_stride, int col_stride) { + double * row_iter = array; + double n = 0.0; // just a counter we'll fill the array with. + for (int i = 0; i < rows; ++i, row_iter += row_stride) { + double * col_iter = row_iter; + for (int j = 0; j < cols; ++j, col_iter += col_stride) { + *col_iter = ++n; + } + } +} + +// Here's a simple wrapper function for fill1. It requires that the passed +// NumPy array be exactly what we're looking for - no conversion from nested +// sequences or arrays with other data types, because we want to modify it +// in-place. +void wrap_fill1(np::ndarray const & array) { + if (array.get_dtype() != np::dtype::get_builtin()) { + PyErr_SetString(PyExc_TypeError, "Incorrect array data type"); + p::throw_error_already_set(); + } + if (array.get_nd() != 2) { + PyErr_SetString(PyExc_TypeError, "Incorrect number of dimensions"); + p::throw_error_already_set(); + } + fill1(reinterpret_cast(array.get_data()), + array.shape(0), array.shape(1), + array.strides(0) / sizeof(double), array.strides(1) / sizeof(double)); +} + +// Another fill function that takes a double**. This is less efficient, because +// it's not the native NumPy data layout, but it's common enough in C/C++ that +// it's worth its own example. This time we don't pass the strides, and instead +// in wrap_fill2 we'll require the C_CONTIGUOUS bitflag, which guarantees that +// the column stride is 1 and the row stride is the number of columns. That +// restricts the arrays that can be passed to fill2 (it won't work on most +// subarray views or transposes, for instance). +void fill2(double ** array, int rows, int cols) { + double n = 0.0; // just a counter we'll fill the array with. + for (int i = 0; i < rows; ++i) { + for (int j = 0; j < cols; ++j) { + array[i][j] = ++n; + } + } +} +// Here's the wrapper for fill2; it's a little more complicated because we need +// to check the flags and create the array of pointers. +void wrap_fill2(np::ndarray const & array) { + if (array.get_dtype() != np::dtype::get_builtin()) { + PyErr_SetString(PyExc_TypeError, "Incorrect array data type"); + p::throw_error_already_set(); + } + if (array.get_nd() != 2) { + PyErr_SetString(PyExc_TypeError, "Incorrect number of dimensions"); + p::throw_error_already_set(); + } + if (!(array.get_flags() & np::ndarray::C_CONTIGUOUS)) { + PyErr_SetString(PyExc_TypeError, "Array must be row-major contiguous"); + p::throw_error_already_set(); + } + double * iter = reinterpret_cast(array.get_data()); + int rows = array.shape(0); + int cols = array.shape(1); + boost::scoped_array ptrs(new double*[rows]); + for (int i = 0; i < rows; ++i, iter += cols) { + ptrs[i] = iter; + } + fill2(ptrs.get(), array.shape(0), array.shape(1)); +} + +BOOST_PYTHON_MODULE(example) { + np::initialize(); // have to put this in any module that uses Boost.NumPy + p::def("fill1", wrap_fill1); + p::def("fill2", wrap_fill2); +} + +int main(int argc, char **argv) +{ + // This line makes our module available to the embedded Python intepreter. +# if PY_VERSION_HEX >= 0x03000000 + PyImport_AppendInittab("example", &PyInit_example); +# else + PyImport_AppendInittab("example", &initexample); +# endif + // Initialize the Python runtime. + Py_Initialize(); + + PyRun_SimpleString( + "import example\n" + "import numpy\n" + "z1 = numpy.zeros((5,6), dtype=float)\n" + "z2 = numpy.zeros((4,3), dtype=float)\n" + "example.fill1(z1)\n" + "example.fill2(z2)\n" + "print z1\n" + "print z2\n" + ); + Py_Finalize(); +} + + diff --git a/example/project.zip b/example/project.zip deleted file mode 100644 index d863defd..00000000 Binary files a/example/project.zip and /dev/null differ diff --git a/example/quickstart/Jamroot b/example/quickstart/Jamfile similarity index 51% rename from example/quickstart/Jamroot rename to example/quickstart/Jamfile index 8425638c..6dd35130 100644 --- a/example/quickstart/Jamroot +++ b/example/quickstart/Jamfile @@ -1,23 +1,15 @@ -# Copyright David Abrahams 2006. Distributed under the Boost -# Software License, Version 1.0. (See accompanying -# file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +# Copyright Stefan Seefeld 2016. +# Distributed under the Boost Software License, Version 1.0. +# (See accompanying file LICENSE_1_0.txt or copy at +# http://www.boost.org/LICENSE_1_0.txt) -# Specify the path to the Boost project. If you move this project, -# adjust the path to refer to the Boost root directory. -use-project boost - : ../../../.. ; - -# Set up the project-wide requirements that everything uses the -# boost_python library defined in the project whose global ID is -# /boost/python. -project boost-python-quickstart - : requirements /boost/python//boost_python - /boost//headers - : usage-requirements /boost//headers - ; - -# Make the definition of the python-extension rule available import python ; +import testing ; + +project quickstart + : requirements + . + ; # Declare a Python extension called hello. python-extension extending : extending.cpp ; @@ -25,8 +17,6 @@ python-extension extending : extending.cpp ; # Declare an executable called embedding that embeds Python exe embedding : embedding.cpp /python//python ; -import testing ; - # Declare a test of the extension module testing.make-test run-pyd : extending test_extending.py : : test_ext ; diff --git a/example/quickstart/boost-build.jam b/example/quickstart/boost-build.jam deleted file mode 100644 index cf910e79..00000000 --- a/example/quickstart/boost-build.jam +++ /dev/null @@ -1,7 +0,0 @@ -# Copyright David Abrahams 2006. Distributed under the Boost -# Software License, Version 1.0. (See accompanying -# file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) - -# Edit this path to point at the tools/build/src subdirectory of your -# Boost installation. Absolute paths work, too. -boost-build ../../../../tools/build/src ; diff --git a/example/quickstart/script.py b/example/quickstart/script.py index 5a8faf79..c3e034ba 100644 --- a/example/quickstart/script.py +++ b/example/quickstart/script.py @@ -1,6 +1,7 @@ +#! /usr/bin/env python # Copyright Stefan Seefeld 2006. Distributed under the Boost # Software License, Version 1.0. (See accompanying # file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) -print 'Hello World !' +print('Hello World !') number = 42 diff --git a/example/quickstart/test_extending.py b/example/quickstart/test_extending.py index 14616f7c..938c7b90 100644 --- a/example/quickstart/test_extending.py +++ b/example/quickstart/test_extending.py @@ -1,3 +1,4 @@ +#! /usr/bin/env python # Copyright Ralf W. Grosse-Kunstleve 2006. Distributed under the Boost # Software License, Version 1.0. (See accompanying # file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) diff --git a/example/std_pair.cpp b/example/std_pair.cpp deleted file mode 100644 index edf98dd6..00000000 --- a/example/std_pair.cpp +++ /dev/null @@ -1,49 +0,0 @@ -// Copyright Ralf W. Grosse-Kunstleve 2002-2004. Distributed under the Boost -// Software License, Version 1.0. (See accompanying -// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) - -#include -#include -#include -#include - -namespace { // Avoid cluttering the global namespace. - - // Converts a std::pair instance to a Python tuple. - template - struct std_pair_to_tuple - { - static PyObject* convert(std::pair const& p) - { - return boost::python::incref( - boost::python::make_tuple(p.first, p.second).ptr()); - } - static PyTypeObject const *get_pytype () {return &PyTuple_Type; } - }; - - // Helper for convenience. - template - struct std_pair_to_python_converter - { - std_pair_to_python_converter() - { - boost::python::to_python_converter< - std::pair, - std_pair_to_tuple, - true //std_pair_to_tuple has get_pytype - >(); - } - }; - - // Example function returning a std::pair. - std::pair - foo() { return std::pair(3, 5); } - -} // namespace anonymous - -BOOST_PYTHON_MODULE(std_pair_ext) -{ - using namespace boost::python; - std_pair_to_python_converter(); - def("foo", foo); -} diff --git a/example/test_getting_started1.py b/example/test_getting_started1.py deleted file mode 100644 index 32dc1f6e..00000000 --- a/example/test_getting_started1.py +++ /dev/null @@ -1,21 +0,0 @@ -# Copyright Ralf W. Grosse-Kunstleve 2006. Distributed under the Boost -# Software License, Version 1.0. (See accompanying -# file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) -r'''>>> import getting_started1 - >>> print getting_started1.greet() - hello, world - >>> number = 11 - >>> print number, '*', number, '=', getting_started1.square(number) - 11 * 11 = 121 -''' - -def run(args = None): - if args is not None: - import sys - sys.argv = args - import doctest, test_getting_started1 - return doctest.testmod(test_getting_started1) - -if __name__ == '__main__': - import sys - sys.exit(run()[0]) diff --git a/example/test_getting_started2.py b/example/test_getting_started2.py deleted file mode 100644 index ae86017b..00000000 --- a/example/test_getting_started2.py +++ /dev/null @@ -1,34 +0,0 @@ -# Copyright Ralf W. Grosse-Kunstleve 2006. Distributed under the Boost -# Software License, Version 1.0. (See accompanying -# file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) -r'''>>> from getting_started2 import * - >>> hi = hello('California') - >>> hi.greet() - 'Hello from California' - >>> invite(hi) - 'Hello from California! Please come soon!' - >>> hi.invite() - 'Hello from California! Please come soon!' - - >>> class wordy(hello): - ... def greet(self): - ... return hello.greet(self) + ', where the weather is fine' - ... - >>> hi2 = wordy('Florida') - >>> hi2.greet() - 'Hello from Florida, where the weather is fine' - >>> invite(hi2) - 'Hello from Florida! Please come soon!' -''' - -def run(args = None): - if args is not None: - import sys - sys.argv = args - import doctest, test_getting_started2 - return doctest.testmod(test_getting_started2) - -if __name__ == '__main__': - import sys - sys.exit(run()[0]) - diff --git a/example/test_std_pair.py b/example/test_std_pair.py deleted file mode 100644 index 64f239fe..00000000 --- a/example/test_std_pair.py +++ /dev/null @@ -1,6 +0,0 @@ -# Copyright Ralf W. Grosse-Kunstleve 2006. Distributed under the Boost -# Software License, Version 1.0. (See accompanying -# file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) -import std_pair_ext -assert std_pair_ext.foo() == (3, 5) -print "OK" diff --git a/example/tutorial/Jamfile b/example/tutorial/Jamfile new file mode 100644 index 00000000..a32272e7 --- /dev/null +++ b/example/tutorial/Jamfile @@ -0,0 +1,19 @@ +# Copyright Stefan Seefeld 2016. +# Distributed under the Boost Software License, Version 1.0. +# (See accompanying file LICENSE_1_0.txt or copy at +# http://www.boost.org/LICENSE_1_0.txt) + +import python ; + +project tutorial + : requirements + . + ; + +python-extension hello_ext : hello.cpp ; + +run-test hello : hello_ext hello.py ; + +alias test : hello ; +explicit test ; + diff --git a/example/tutorial/Jamroot b/example/tutorial/Jamroot deleted file mode 100644 index ac5e1f03..00000000 --- a/example/tutorial/Jamroot +++ /dev/null @@ -1,51 +0,0 @@ -# Copyright David Abrahams 2006. Distributed under the Boost -# Software License, Version 1.0. (See accompanying -# file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) - -import python ; - -if ! [ python.configured ] -{ - ECHO "notice: no Python configured in user-config.jam" ; - ECHO "notice: will use default configuration" ; - using python ; -} - -# Specify the path to the Boost project. If you move this project, -# adjust this path to refer to the Boost root directory. -use-project boost - : ../../../.. ; - -# Set up the project-wide requirements that everything uses the -# boost_python library from the project whose global ID is -# /boost/python. -project - : requirements /boost/python//boost_python - /boost//headers - : usage-requirements /boost//headers - ; - -# Declare the three extension modules. You can specify multiple -# source files after the colon separated by spaces. -python-extension hello_ext : hello.cpp ; - -# Put the extension and Boost.Python DLL in the current directory, so -# that running script by hand works. -install convenient_copy - : hello_ext - : on SHARED_LIB PYTHON_EXTENSION - . - ; - -# A little "rule" (function) to clean up the syntax of declaring tests -# of these extension modules. -local rule run-test ( test-name : sources + ) -{ - import testing ; - testing.make-test run-pyd : $(sources) : : $(test-name) ; -} - -# Declare test targets -run-test hello : hello_ext hello.py ; - - diff --git a/example/tutorial/hello.py b/example/tutorial/hello.py index d18b1c53..31f75565 100755 --- a/example/tutorial/hello.py +++ b/example/tutorial/hello.py @@ -1,7 +1,8 @@ +#! /usr/bin/env python # Copyright Joel de Guzman 2002-2007. Distributed under the Boost # Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt # or copy at http://www.boost.org/LICENSE_1_0.txt) # Hello World Example from the tutorial import hello_ext -print hello_ext.greet() +print(hello_ext.greet()) diff --git a/include/boost/python/converter/registered.hpp b/include/boost/python/converter/registered.hpp index ad9a00b3..a622250d 100644 --- a/include/boost/python/converter/registered.hpp +++ b/include/boost/python/converter/registered.hpp @@ -1,19 +1,21 @@ // Copyright David Abrahams 2002. +// Copyright Stefan Seefeld 2016. // Distributed under the Boost Software License, Version 1.0. (See // accompanying file LICENSE_1_0.txt or copy at // http://www.boost.org/LICENSE_1_0.txt) -#ifndef REGISTERED_DWA2002710_HPP -# define REGISTERED_DWA2002710_HPP -# include -# include -# include -# include -# include -# include -# include -# include -# include +#ifndef boost_python_converter_registered_hpp_ +#define boost_python_converter_registered_hpp_ + +#include +#include +#include +#include +#include +#include +#include +#include +#include #if defined(BOOST_PYTHON_TRACE_REGISTRY) \ || defined(BOOST_PYTHON_CONVERTER_REGISTRY_APPLE_MACH_WORKAROUND) # include @@ -75,7 +77,16 @@ namespace detail { registry::lookup_shared_ptr(type_id >()); } - + +#if __cplusplus >= 201103L + template + inline void + register_shared_ptr0(std::shared_ptr*) + { + registry::lookup_shared_ptr(type_id >()); + } +#endif + template inline void register_shared_ptr1(T const volatile*) @@ -112,4 +123,4 @@ namespace detail }}} // namespace boost::python::converter -#endif // REGISTERED_DWA2002710_HPP +#endif diff --git a/include/boost/python/converter/shared_ptr_from_python.hpp b/include/boost/python/converter/shared_ptr_from_python.hpp index c0910776..bb2ae863 100644 --- a/include/boost/python/converter/shared_ptr_from_python.hpp +++ b/include/boost/python/converter/shared_ptr_from_python.hpp @@ -1,63 +1,65 @@ // Copyright David Abrahams 2002. +// Copyright Stefan Seefeld 2016. // Distributed under the Boost Software License, Version 1.0. (See // accompanying file LICENSE_1_0.txt or copy at // http://www.boost.org/LICENSE_1_0.txt) -#ifndef SHARED_PTR_FROM_PYTHON_DWA20021130_HPP -# define SHARED_PTR_FROM_PYTHON_DWA20021130_HPP -# include -# include -# include -# include -# include +#ifndef boost_python_converter_shared_ptr_from_python_hpp_ +#define boost_python_converter_shared_ptr_from_python_hpp_ + +#include +#include +#include +#include +#include #ifndef BOOST_PYTHON_NO_PY_SIGNATURES # include #endif -# include +#include +#include namespace boost { namespace python { namespace converter { -template +template class SP> struct shared_ptr_from_python { - shared_ptr_from_python() - { - converter::registry::insert(&convertible, &construct, type_id >() + shared_ptr_from_python() + { + converter::registry::insert(&convertible, &construct, type_id >() #ifndef BOOST_PYTHON_NO_PY_SIGNATURES - , &converter::expected_from_python_type_direct::get_pytype + , &converter::expected_from_python_type_direct::get_pytype #endif - ); - } + ); + } private: - static void* convertible(PyObject* p) - { - if (p == Py_None) - return p; + static void* convertible(PyObject* p) + { + if (p == Py_None) + return p; - return converter::get_lvalue_from_python(p, registered::converters); - } + return converter::get_lvalue_from_python(p, registered::converters); + } - static void construct(PyObject* source, rvalue_from_python_stage1_data* data) + static void construct(PyObject* source, rvalue_from_python_stage1_data* data) + { + void* const storage = ((converter::rvalue_from_python_storage >*)data)->storage.bytes; + // Deal with the "None" case. + if (data->convertible == source) + new (storage) SP(); + else { - void* const storage = ((converter::rvalue_from_python_storage >*)data)->storage.bytes; - // Deal with the "None" case. - if (data->convertible == source) - new (storage) shared_ptr(); - else - { - boost::shared_ptr hold_convertible_ref_count( - (void*)0, shared_ptr_deleter(handle<>(borrowed(source))) ); - // use aliasing constructor - new (storage) shared_ptr( - hold_convertible_ref_count, - static_cast(data->convertible)); - } - - data->convertible = storage; + SP hold_convertible_ref_count( + (void*)0, shared_ptr_deleter(handle<>(borrowed(source))) ); + // use aliasing constructor + new (storage) SP(hold_convertible_ref_count, + static_cast(data->convertible)); } + + data->convertible = storage; + } }; }}} // namespace boost::python::converter -#endif // SHARED_PTR_FROM_PYTHON_DWA20021130_HPP +#endif diff --git a/include/boost/python/converter/shared_ptr_to_python.hpp b/include/boost/python/converter/shared_ptr_to_python.hpp index fe867ace..cc686461 100644 --- a/include/boost/python/converter/shared_ptr_to_python.hpp +++ b/include/boost/python/converter/shared_ptr_to_python.hpp @@ -1,14 +1,16 @@ // Copyright David Abrahams 2003. +// Copyright Stefan Seefeld 2016. // Distributed under the Boost Software License, Version 1.0. (See // accompanying file LICENSE_1_0.txt or copy at // http://www.boost.org/LICENSE_1_0.txt) -#ifndef SHARED_PTR_TO_PYTHON_DWA2003224_HPP -# define SHARED_PTR_TO_PYTHON_DWA2003224_HPP -# include -# include -# include -# include +#ifndef boost_python_converter_shared_ptr_to_python_hpp_ +#define boost_python_converter_shared_ptr_to_python_hpp_ + +#include +#include +#include +#include namespace boost { namespace python { namespace converter { @@ -23,6 +25,19 @@ PyObject* shared_ptr_to_python(shared_ptr const& x) return converter::registered const&>::converters.to_python(&x); } +#if __cplusplus >= 201103L +template +PyObject* shared_ptr_to_python(std::shared_ptr const& x) +{ + if (!x) + return python::detail::none(); + else if (shared_ptr_deleter* d = std::get_deleter(x)) + return incref(get_pointer(d->owner)); + else + return converter::registered const&>::converters.to_python(&x); +} +#endif + }}} // namespace boost::python::converter -#endif // SHARED_PTR_TO_PYTHON_DWA2003224_HPP +#endif diff --git a/include/boost/python/detail/is_shared_ptr.hpp b/include/boost/python/detail/is_shared_ptr.hpp index 547af3f1..bef0e05a 100755 --- a/include/boost/python/detail/is_shared_ptr.hpp +++ b/include/boost/python/detail/is_shared_ptr.hpp @@ -1,17 +1,23 @@ // Copyright David Abrahams 2003. +// Copyright Stefan Seefeld 2016. // Distributed under the Boost Software License, Version 1.0. (See // accompanying file LICENSE_1_0.txt or copy at // http://www.boost.org/LICENSE_1_0.txt) -#ifndef IS_SHARED_PTR_DWA2003224_HPP -# define IS_SHARED_PTR_DWA2003224_HPP -# include -# include +#ifndef boost_python_detail_is_shared_ptr_hpp_ +#define boost_python_detail_is_shared_ptr_hpp_ + +#include +#include namespace boost { namespace python { namespace detail { BOOST_PYTHON_IS_XXX_DEF(shared_ptr, shared_ptr, 1) - +#if __cplusplus >= 201103L +template +struct is_shared_ptr > : std::true_type {}; +#endif + }}} // namespace boost::python::detail -#endif // IS_SHARED_PTR_DWA2003224_HPP +#endif diff --git a/include/boost/python/detail/value_is_shared_ptr.hpp b/include/boost/python/detail/value_is_shared_ptr.hpp index 361c369b..53e687f0 100644 --- a/include/boost/python/detail/value_is_shared_ptr.hpp +++ b/include/boost/python/detail/value_is_shared_ptr.hpp @@ -1,17 +1,28 @@ // Copyright David Abrahams 2003. +// Copyright Stefan Seefeld 2016. // Distributed under the Boost Software License, Version 1.0. (See // accompanying file LICENSE_1_0.txt or copy at // http://www.boost.org/LICENSE_1_0.txt) -#ifndef VALUE_IS_SHARED_PTR_DWA2003224_HPP -# define VALUE_IS_SHARED_PTR_DWA2003224_HPP -# include -# include +#ifndef boost_python_detail_value_is_shared_ptr_hpp_ +#define boost_python_detail_value_is_shared_ptr_hpp_ + +#include +#include namespace boost { namespace python { namespace detail { -BOOST_PYTHON_VALUE_IS_XXX_DEF(shared_ptr, shared_ptr, 1) - +template +struct value_is_shared_ptr +{ + static bool const value = is_shared_ptr + ::type> + ::type> + ::value; + typedef mpl::bool_ type; +}; + }}} // namespace boost::python::detail #endif // VALUE_IS_SHARED_PTR_DWA2003224_HPP diff --git a/include/boost/python/make_constructor.hpp b/include/boost/python/make_constructor.hpp index 093703bb..92a7951d 100644 --- a/include/boost/python/make_constructor.hpp +++ b/include/boost/python/make_constructor.hpp @@ -45,8 +45,13 @@ namespace detail template void dispatch(U* x, mpl::true_) const { - std::auto_ptr owner(x); - dispatch(owner, mpl::false_()); +#if __cplusplus < 201103L + std::auto_ptr owner(x); + dispatch(owner, mpl::false_()); +#else + std::unique_ptr owner(x); + dispatch(std::move(owner), mpl::false_()); +#endif } template @@ -58,7 +63,11 @@ namespace detail void* memory = holder::allocate(this->m_self, offsetof(instance_t, storage), sizeof(holder)); try { +#if __cplusplus < 201103L (new (memory) holder(x))->install(this->m_self); +#else + (new (memory) holder(std::move(x)))->install(this->m_self); +#endif } catch(...) { holder::deallocate(this->m_self, memory); diff --git a/include/boost/python/numpy.hpp b/include/boost/python/numpy.hpp new file mode 100644 index 00000000..cd5876a8 --- /dev/null +++ b/include/boost/python/numpy.hpp @@ -0,0 +1,33 @@ +// Copyright Jim Bosch 2010-2012. +// Copyright Stefan Seefeld 2016. +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +#ifndef boost_python_numpy_hpp_ +#define boost_python_numpy_hpp_ + +#include +#include +#include +#include +#include +#include + +namespace boost { namespace python { namespace numpy { + +/** + * @brief Initialize the Numpy C-API + * + * This must be called before using anything in boost.numpy; + * It should probably be the first line inside BOOST_PYTHON_MODULE. + * + * @internal This just calls the Numpy C-API functions "import_array()" + * and "import_ufunc()", and then calls + * dtype::register_scalar_converters(). + */ +void initialize(bool register_scalar_converters=true); + +}}} // namespace boost::python::numpy + +#endif diff --git a/include/boost/python/numpy/dtype.hpp b/include/boost/python/numpy/dtype.hpp new file mode 100644 index 00000000..1284f9e5 --- /dev/null +++ b/include/boost/python/numpy/dtype.hpp @@ -0,0 +1,117 @@ +// Copyright Jim Bosch 2010-2012. +// Copyright Stefan Seefeld 2016. +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +#ifndef boost_python_numpy_dtype_hpp_ +#define boost_python_numpy_dtype_hpp_ + +/** + * @file boost/python/numpy/dtype.hpp + * @brief Object manager for Python's numpy.dtype class. + */ + +#include +#include + +#include +#include + +namespace boost { namespace python { namespace numpy { + +/** + * @brief A boost.python "object manager" (subclass of object) for numpy.dtype. + * + * @todo This could have a lot more interesting accessors. + */ +class dtype : public object { + static python::detail::new_reference convert(object::object_cref arg, bool align); +public: + + /// @brief Convert an arbitrary Python object to a data-type descriptor object. + template + explicit dtype(T arg, bool align=false) : object(convert(arg, align)) {} + + /** + * @brief Get the built-in numpy dtype associated with the given scalar template type. + * + * This is perhaps the most useful part of the numpy API: it returns the dtype object + * corresponding to a built-in C++ type. This should work for any integer or floating point + * type supported by numpy, and will also work for std::complex if + * sizeof(std::complex) == 2*sizeof(T). + * + * It can also be useful for users to add explicit specializations for POD structs + * that return field-based dtypes. + */ + template static dtype get_builtin(); + + /// @brief Return the size of the data type in bytes. + int get_itemsize() const; + + /** + * @brief Compare two dtypes for equivalence. + * + * This is more permissive than equality tests. For instance, if long and int are the same + * size, the dtypes corresponding to each will be equivalent, but not equal. + */ + friend bool equivalent(dtype const & a, dtype const & b); + + /** + * @brief Register from-Python converters for NumPy's built-in array scalar types. + * + * This is usually called automatically by initialize(), and shouldn't be called twice + * (doing so just adds unused converters to the Boost.Python registry). + */ + static void register_scalar_converters(); + + BOOST_PYTHON_FORWARD_OBJECT_CONSTRUCTORS(dtype, object); + +}; + +bool equivalent(dtype const & a, dtype const & b); + +namespace detail +{ + +template dtype get_int_dtype(); + +template dtype get_float_dtype(); + +template dtype get_complex_dtype(); + +template ::value> +struct builtin_dtype; + +template +struct builtin_dtype { + static dtype get() { return get_int_dtype< 8*sizeof(T), boost::is_unsigned::value >(); } +}; + +template <> +struct builtin_dtype { + static dtype get(); +}; + +template +struct builtin_dtype { + static dtype get() { return get_float_dtype< 8*sizeof(T) >(); } +}; + +template +struct builtin_dtype< std::complex, false > { + static dtype get() { return get_complex_dtype< 16*sizeof(T) >(); } +}; + +} // namespace detail + +template +inline dtype dtype::get_builtin() { return detail::builtin_dtype::get(); } + +} // namespace boost::python::numpy + +namespace converter { +NUMPY_OBJECT_MANAGER_TRAITS(numpy::dtype); +}}} // namespace boost::python::converter + +#endif diff --git a/include/boost/python/numpy/internal.hpp b/include/boost/python/numpy/internal.hpp new file mode 100644 index 00000000..fed31cbb --- /dev/null +++ b/include/boost/python/numpy/internal.hpp @@ -0,0 +1,35 @@ +// Copyright Jim Bosch 2010-2012. +// Copyright Stefan Seefeld 2016. +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +#ifndef boost_python_numpy_internal_hpp_ +#define boost_python_numpy_internal_hpp_ + +/** + * @file boost/python/numpy/internal.hpp + * @brief Internal header file to include the Numpy C-API headers. + * + * This should only be included by source files in the boost.numpy library itself. + */ + +#include +#ifdef BOOST_PYTHON_NUMPY_INTERNAL +#define NO_IMPORT_ARRAY +#define NO_IMPORT_UFUNC +#else +#ifndef BOOST_PYTHON_NUMPY_INTERNAL_MAIN +ERROR_internal_hpp_is_for_internal_use_only +#endif +#endif +#define PY_ARRAY_UNIQUE_SYMBOL BOOST_NUMPY_ARRAY_API +#define PY_UFUNC_UNIQUE_SYMBOL BOOST_UFUNC_ARRAY_API +#include +#include +#include + +#define NUMPY_OBJECT_MANAGER_TRAITS_IMPL(pytype,manager) \ + PyTypeObject const * object_manager_traits::get_pytype() { return &pytype; } + +#endif diff --git a/include/boost/python/numpy/invoke_matching.hpp b/include/boost/python/numpy/invoke_matching.hpp new file mode 100644 index 00000000..90ec8ae2 --- /dev/null +++ b/include/boost/python/numpy/invoke_matching.hpp @@ -0,0 +1,186 @@ +// Copyright Jim Bosch 2010-2012. +// Copyright Stefan Seefeld 2016. +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +#ifndef boost_python_numpy_invoke_matching_hpp_ +#define boost_python_numpy_invoke_matching_hpp_ + +/** + * @brief Template invocation based on dtype matching. + */ + +#include +#include +#include + +namespace boost { namespace python { namespace numpy { +namespace detail +{ + +struct add_pointer_meta +{ + template + struct apply + { + typedef typename boost::add_pointer::type type; + }; + +}; + +struct dtype_template_match_found {}; +struct nd_template_match_found {}; + +template +struct dtype_template_invoker +{ + + template + void operator()(T *) const + { + if (dtype::get_builtin() == m_dtype) + { + m_func.Function::template apply(); + throw dtype_template_match_found(); + } + } + + dtype_template_invoker(dtype const & dtype_, Function func) + : m_dtype(dtype_), m_func(func) {} + +private: + dtype const & m_dtype; + Function m_func; +}; + +template +struct dtype_template_invoker< boost::reference_wrapper > +{ + + template + void operator()(T *) const + { + if (dtype::get_builtin() == m_dtype) + { + m_func.Function::template apply(); + throw dtype_template_match_found(); + } + } + + dtype_template_invoker(dtype const & dtype_, Function & func) + : m_dtype(dtype_), m_func(func) {} + +private: + dtype const & m_dtype; + Function & m_func; +}; + +template +struct nd_template_invoker +{ + template + void operator()(boost::mpl::integral_c *) const + { + if (m_nd == N) + { + m_func.Function::template apply(); + throw nd_template_match_found(); + } + } + + nd_template_invoker(int nd, Function func) : m_nd(nd), m_func(func) {} + +private: + int m_nd; + Function m_func; +}; + +template +struct nd_template_invoker< boost::reference_wrapper > +{ + template + void operator()(boost::mpl::integral_c *) const + { + if (m_nd == N) + { + m_func.Function::template apply(); + throw nd_template_match_found(); + } + } + + nd_template_invoker(int nd, Function & func) : m_nd(nd), m_func(func) {} + +private: + int m_nd; + Function & m_func; +}; + +} // namespace boost::python::numpy::detail + +template +void invoke_matching_nd(int nd, Function f) +{ + detail::nd_template_invoker invoker(nd, f); + try { boost::mpl::for_each< Sequence, detail::add_pointer_meta >(invoker);} + catch (detail::nd_template_match_found &) { return;} + PyErr_SetString(PyExc_TypeError, "number of dimensions not found in template list."); + python::throw_error_already_set(); +} + +template +void invoke_matching_dtype(dtype const & dtype_, Function f) +{ + detail::dtype_template_invoker invoker(dtype_, f); + try { boost::mpl::for_each< Sequence, detail::add_pointer_meta >(invoker);} + catch (detail::dtype_template_match_found &) { return;} + PyErr_SetString(PyExc_TypeError, "dtype not found in template list."); + python::throw_error_already_set(); +} + +namespace detail +{ + +template +struct array_template_invoker_wrapper_2 +{ + template + void apply() const { m_func.Function::template apply();} + array_template_invoker_wrapper_2(Function & func) : m_func(func) {} + +private: + Function & m_func; +}; + +template +struct array_template_invoker_wrapper_1 +{ + template + void apply() const { invoke_matching_nd(m_nd, array_template_invoker_wrapper_2(m_func));} + array_template_invoker_wrapper_1(int nd, Function & func) : m_nd(nd), m_func(func) {} + +private: + int m_nd; + Function & m_func; +}; + +template +struct array_template_invoker_wrapper_1< DimSequence, boost::reference_wrapper > + : public array_template_invoker_wrapper_1< DimSequence, Function > +{ + array_template_invoker_wrapper_1(int nd, Function & func) + : array_template_invoker_wrapper_1< DimSequence, Function >(nd, func) {} +}; + +} // namespace boost::python::numpy::detail + +template +void invoke_matching_array(ndarray const & array_, Function f) +{ + detail::array_template_invoker_wrapper_1 wrapper(array_.get_nd(), f); + invoke_matching_dtype(array_.get_dtype(), wrapper); +} + +}}} // namespace boost::python::numpy + +#endif diff --git a/include/boost/python/numpy/matrix.hpp b/include/boost/python/numpy/matrix.hpp new file mode 100644 index 00000000..af20e8f9 --- /dev/null +++ b/include/boost/python/numpy/matrix.hpp @@ -0,0 +1,82 @@ +// Copyright Jim Bosch 2010-2012. +// Copyright Stefan Seefeld 2016. +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +#ifndef boost_python_numpy_matrix_hpp_ +#define boost_python_numpy_matrix_hpp_ + +/** + * @brief Object manager for numpy.matrix. + */ + +#include +#include +#include + +namespace boost { namespace python { namespace numpy { + +/** + * @brief A boost.python "object manager" (subclass of object) for numpy.matrix. + * + * @internal numpy.matrix is defined in Python, so object_manager_traits::get_pytype() + * is implemented by importing numpy and getting the "matrix" attribute of the module. + * We then just hope that doesn't get destroyed while we need it, because if we put + * a dynamic python object in a static-allocated boost::python::object or handle<>, + * bad things happen when Python shuts down. I think this solution is safe, but I'd + * love to get that confirmed. + */ +class matrix : public ndarray +{ + static object construct(object_cref obj, dtype const & dt, bool copy); + static object construct(object_cref obj, bool copy); +public: + + BOOST_PYTHON_FORWARD_OBJECT_CONSTRUCTORS(matrix, ndarray); + + /// @brief Equivalent to "numpy.matrix(obj,dt,copy)" in Python. + explicit matrix(object const & obj, dtype const & dt, bool copy=true) + : ndarray(extract(construct(obj, dt, copy))) {} + + /// @brief Equivalent to "numpy.matrix(obj,copy=copy)" in Python. + explicit matrix(object const & obj, bool copy=true) + : ndarray(extract(construct(obj, copy))) {} + + /// \brief Return a view of the matrix with the given dtype. + matrix view(dtype const & dt) const; + + /// \brief Copy the scalar (deep for all non-object fields). + matrix copy() const; + + /// \brief Transpose the matrix. + matrix transpose() const; + +}; + +/** + * @brief CallPolicies that causes a function that returns a numpy.ndarray to + * return a numpy.matrix instead. + */ +template +struct as_matrix : Base +{ + static PyObject * postcall(PyObject *, PyObject * result) + { + object a = object(handle<>(result)); + numpy::matrix m(a, false); + Py_INCREF(m.ptr()); + return m.ptr(); + } +}; + +} // namespace boost::python::numpy + +namespace converter +{ + +NUMPY_OBJECT_MANAGER_TRAITS(numpy::matrix); + +}}} // namespace boost::python::converter + +#endif diff --git a/include/boost/python/numpy/ndarray.hpp b/include/boost/python/numpy/ndarray.hpp new file mode 100644 index 00000000..2985907b --- /dev/null +++ b/include/boost/python/numpy/ndarray.hpp @@ -0,0 +1,296 @@ +// Copyright Jim Bosch 2010-2012. +// Copyright Stefan Seefeld 2016. +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +#ifndef boost_python_numpy_ndarray_hpp_ +#define boost_python_numpy_ndarray_hpp_ + +/** + * @brief Object manager and various utilities for numpy.ndarray. + */ + +#include +#include +#include +#include +#include +#include + +namespace boost { namespace python { namespace numpy { + +/** + * @brief A boost.python "object manager" (subclass of object) for numpy.ndarray. + * + * @todo This could have a lot more functionality (like boost::python::numeric::array). + * Right now all that exists is what was needed to move raw data between C++ and Python. + */ +class ndarray : public object +{ + + /** + * @brief An internal struct that's byte-compatible with PyArrayObject. + * + * This is just a hack to allow inline access to this stuff while hiding numpy/arrayobject.h + * from the user. + */ + struct array_struct + { + PyObject_HEAD + char * data; + int nd; + Py_intptr_t * shape; + Py_intptr_t * strides; + PyObject * base; + PyObject * descr; + int flags; + PyObject * weakreflist; + }; + + /// @brief Return the held Python object as an array_struct. + array_struct * get_struct() const { return reinterpret_cast(this->ptr()); } + +public: + + /** + * @brief Enum to represent (some) of Numpy's internal flags. + * + * These don't match the actual Numpy flag values; we can't get those without including + * numpy/arrayobject.h or copying them directly. That's very unfortunate. + * + * @todo I'm torn about whether this should be an enum. It's very convenient to not + * make these simple integer values for overloading purposes, but the need to + * define every possible combination and custom bitwise operators is ugly. + */ + enum bitflag + { + NONE=0x0, C_CONTIGUOUS=0x1, F_CONTIGUOUS=0x2, V_CONTIGUOUS=0x1|0x2, + ALIGNED=0x4, WRITEABLE=0x8, BEHAVED=0x4|0x8, + CARRAY_RO=0x1|0x4, CARRAY=0x1|0x4|0x8, CARRAY_MIS=0x1|0x8, + FARRAY_RO=0x2|0x4, FARRAY=0x2|0x4|0x8, FARRAY_MIS=0x2|0x8, + UPDATE_ALL=0x1|0x2|0x4, VARRAY=0x1|0x2|0x8, ALL=0x1|0x2|0x4|0x8 + }; + + BOOST_PYTHON_FORWARD_OBJECT_CONSTRUCTORS(ndarray, object); + + /// @brief Return a view of the scalar with the given dtype. + ndarray view(dtype const & dt) const; + + /// @brief Copy the array, cast to a specified type. + ndarray astype(dtype const & dt) const; + + /// @brief Copy the scalar (deep for all non-object fields). + ndarray copy() const; + + /// @brief Return the size of the nth dimension. + Py_intptr_t shape(int n) const { return get_shape()[n]; } + + /// @brief Return the stride of the nth dimension. + Py_intptr_t strides(int n) const { return get_strides()[n]; } + + /** + * @brief Return the array's raw data pointer. + * + * This returns char so stride math works properly on it. It's pretty much + * expected that the user will have to reinterpret_cast it. + */ + char * get_data() const { return get_struct()->data; } + + /// @brief Return the array's data-type descriptor object. + dtype get_dtype() const; + + /// @brief Return the object that owns the array's data, or None if the array owns its own data. + object get_base() const; + + /// @brief Set the object that owns the array's data. Use with care. + void set_base(object const & base); + + /// @brief Return the shape of the array as an array of integers (length == get_nd()). + Py_intptr_t const * get_shape() const { return get_struct()->shape; } + + /// @brief Return the stride of the array as an array of integers (length == get_nd()). + Py_intptr_t const * get_strides() const { return get_struct()->strides; } + + /// @brief Return the number of array dimensions. + int get_nd() const { return get_struct()->nd; } + + /// @brief Return the array flags. + bitflag get_flags() const; + + /// @brief Reverse the dimensions of the array. + ndarray transpose() const; + + /// @brief Eliminate any unit-sized dimensions. + ndarray squeeze() const; + + /// @brief Equivalent to self.reshape(*shape) in Python. + ndarray reshape(python::tuple const & shape) const; + + /** + * @brief If the array contains only a single element, return it as an array scalar; otherwise return + * the array. + * + * @internal This is simply a call to PyArray_Return(); + */ + object scalarize() const; +}; + +/** + * @brief Construct a new array with the given shape and data type, with data initialized to zero. + */ +ndarray zeros(python::tuple const & shape, dtype const & dt); +ndarray zeros(int nd, Py_intptr_t const * shape, dtype const & dt); + +/** + * @brief Construct a new array with the given shape and data type, with data left uninitialized. + */ +ndarray empty(python::tuple const & shape, dtype const & dt); +ndarray empty(int nd, Py_intptr_t const * shape, dtype const & dt); + +/** + * @brief Construct a new array from an arbitrary Python sequence. + * + * @todo This does't seem to handle ndarray subtypes the same way that "numpy.array" does in Python. + */ +ndarray array(object const & obj); +ndarray array(object const & obj, dtype const & dt); + +namespace detail +{ + +ndarray from_data_impl(void * data, + dtype const & dt, + std::vector const & shape, + std::vector const & strides, + object const & owner, + bool writeable); + +template +ndarray from_data_impl(void * data, + dtype const & dt, + Container shape, + Container strides, + object const & owner, + bool writeable, + typename boost::enable_if< boost::is_integral >::type * enabled = NULL) +{ + std::vector shape_(shape.begin(),shape.end()); + std::vector strides_(strides.begin(), strides.end()); + return from_data_impl(data, dt, shape_, strides_, owner, writeable); +} + +ndarray from_data_impl(void * data, + dtype const & dt, + object const & shape, + object const & strides, + object const & owner, + bool writeable); + +} // namespace boost::python::numpy::detail + +/** + * @brief Construct a new ndarray object from a raw pointer. + * + * @param[in] data Raw pointer to the first element of the array. + * @param[in] dt Data type descriptor. Often retrieved with dtype::get_builtin(). + * @param[in] shape Shape of the array as STL container of integers; must have begin() and end(). + * @param[in] strides Shape of the array as STL container of integers; must have begin() and end(). + * @param[in] owner An arbitray Python object that owns that data pointer. The array object will + * keep a reference to the object, and decrement it's reference count when the + * array goes out of scope. Pass None at your own peril. + * + * @todo Should probably take ranges of iterators rather than actual container objects. + */ +template +inline ndarray from_data(void * data, + dtype const & dt, + Container shape, + Container strides, + python::object const & owner) +{ + return numpy::detail::from_data_impl(data, dt, shape, strides, owner, true); +} + +/** + * @brief Construct a new ndarray object from a raw pointer. + * + * @param[in] data Raw pointer to the first element of the array. + * @param[in] dt Data type descriptor. Often retrieved with dtype::get_builtin(). + * @param[in] shape Shape of the array as STL container of integers; must have begin() and end(). + * @param[in] strides Shape of the array as STL container of integers; must have begin() and end(). + * @param[in] owner An arbitray Python object that owns that data pointer. The array object will + * keep a reference to the object, and decrement it's reference count when the + * array goes out of scope. Pass None at your own peril. + * + * This overload takes a const void pointer and sets the "writeable" flag of the array to false. + * + * @todo Should probably take ranges of iterators rather than actual container objects. + */ +template +inline ndarray from_data(void const * data, + dtype const & dt, + Container shape, + Container strides, + python::object const & owner) +{ + return numpy::detail::from_data_impl(const_cast(data), dt, shape, strides, owner, false); +} + +/** + * @brief Transform an arbitrary object into a numpy array with the given requirements. + * + * @param[in] obj An arbitrary python object to convert. Arrays that meet the requirements + * will be passed through directly. + * @param[in] dt Data type descriptor. Often retrieved with dtype::get_builtin(). + * @param[in] nd_min Minimum number of dimensions. + * @param[in] nd_max Maximum number of dimensions. + * @param[in] flags Bitwise OR of flags specifying additional requirements. + */ +ndarray from_object(object const & obj, dtype const & dt, + int nd_min, int nd_max, ndarray::bitflag flags=ndarray::NONE); + +inline ndarray from_object(object const & obj, dtype const & dt, + int nd, ndarray::bitflag flags=ndarray::NONE) +{ + return from_object(obj, dt, nd, nd, flags); +} + +inline ndarray from_object(object const & obj, dtype const & dt, ndarray::bitflag flags=ndarray::NONE) +{ + return from_object(obj, dt, 0, 0, flags); +} + +ndarray from_object(object const & obj, int nd_min, int nd_max, + ndarray::bitflag flags=ndarray::NONE); + +inline ndarray from_object(object const & obj, int nd, ndarray::bitflag flags=ndarray::NONE) +{ + return from_object(obj, nd, nd, flags); +} + +inline ndarray from_object(object const & obj, ndarray::bitflag flags=ndarray::NONE) +{ + return from_object(obj, 0, 0, flags); +} + +inline ndarray::bitflag operator|(ndarray::bitflag a, ndarray::bitflag b) +{ + return ndarray::bitflag(int(a) | int(b)); +} + +inline ndarray::bitflag operator&(ndarray::bitflag a, ndarray::bitflag b) +{ + return ndarray::bitflag(int(a) & int(b)); +} + +} // namespace boost::python::numpy + +namespace converter +{ + +NUMPY_OBJECT_MANAGER_TRAITS(numpy::ndarray); + +}}} // namespace boost::python::converter + +#endif diff --git a/include/boost/python/numpy/numpy_object_mgr_traits.hpp b/include/boost/python/numpy/numpy_object_mgr_traits.hpp new file mode 100644 index 00000000..8f9f4440 --- /dev/null +++ b/include/boost/python/numpy/numpy_object_mgr_traits.hpp @@ -0,0 +1,36 @@ +// Copyright Jim Bosch 2010-2012. +// Copyright Stefan Seefeld 2016. +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +#ifndef boost_python_numpy_numpy_object_mgr_traits_hpp_ +#define boost_python_numpy_numpy_object_mgr_traits_hpp_ + +/** + * @brief Macro that specializes object_manager_traits by requiring a + * source-file implementation of get_pytype(). + */ + +#define NUMPY_OBJECT_MANAGER_TRAITS(manager) \ +template <> \ +struct object_manager_traits \ +{ \ + BOOST_STATIC_CONSTANT(bool, is_specialized = true); \ + static inline python::detail::new_reference adopt(PyObject* x) \ + { \ + return python::detail::new_reference(python::pytype_check((PyTypeObject*)get_pytype(), x)); \ + } \ + static bool check(PyObject* x) \ + { \ + return ::PyObject_IsInstance(x, (PyObject*)get_pytype()); \ + } \ + static manager* checked_downcast(PyObject* x) \ + { \ + return python::downcast((checked_downcast_impl)(x, (PyTypeObject*)get_pytype())); \ + } \ + static PyTypeObject const * get_pytype(); \ +} + +#endif + diff --git a/include/boost/python/numpy/scalars.hpp b/include/boost/python/numpy/scalars.hpp new file mode 100644 index 00000000..0ba23c41 --- /dev/null +++ b/include/boost/python/numpy/scalars.hpp @@ -0,0 +1,58 @@ +// Copyright Jim Bosch 2010-2012. +// Copyright Stefan Seefeld 2016. +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +#ifndef boost_python_numpy_scalars_hpp_ +#define boost_python_numpy_scalars_hpp_ + +/** + * @brief Object managers for array scalars (currently only numpy.void is implemented). + */ + +#include +#include +#include + +namespace boost { namespace python { namespace numpy { + +/** + * @brief A boost.python "object manager" (subclass of object) for numpy.void. + * + * @todo This could have a lot more functionality. + */ +class void_ : public object +{ + static python::detail::new_reference convert(object_cref arg, bool align); +public: + + /** + * @brief Construct a new array scalar with the given size and void dtype. + * + * Data is initialized to zero. One can create a standalone scalar object + * with a certain dtype "dt" with: + * @code + * void_ scalar = void_(dt.get_itemsize()).view(dt); + * @endcode + */ + explicit void_(Py_ssize_t size); + + BOOST_PYTHON_FORWARD_OBJECT_CONSTRUCTORS(void_, object); + + /// @brief Return a view of the scalar with the given dtype. + void_ view(dtype const & dt) const; + + /// @brief Copy the scalar (deep for all non-object fields). + void_ copy() const; + +}; + +} // namespace boost::python::numpy + +namespace converter +{ +NUMPY_OBJECT_MANAGER_TRAITS(numpy::void_); +}}} // namespace boost::python::converter + +#endif diff --git a/include/boost/python/numpy/ufunc.hpp b/include/boost/python/numpy/ufunc.hpp new file mode 100644 index 00000000..9262b378 --- /dev/null +++ b/include/boost/python/numpy/ufunc.hpp @@ -0,0 +1,205 @@ +// Copyright Jim Bosch 2010-2012. +// Copyright Stefan Seefeld 2016. +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +#ifndef boost_python_numpy_ufunc_hpp_ +#define boost_python_numpy_ufunc_hpp_ + +/** + * @brief Utilities to create ufunc-like broadcasting functions out of C++ functors. + */ + +#include +#include +#include +#include + +namespace boost { namespace python { namespace numpy { + +/** + * @brief A boost.python "object manager" (subclass of object) for PyArray_MultiIter. + * + * multi_iter is a Python object, but a very low-level one. It should generally only be used + * in loops of the form: + * @code + * while (iter.not_done()) { + * ... + * iter.next(); + * } + * @endcode + * + * @todo I can't tell if this type is exposed in Python anywhere; if it is, we should use that name. + * It's more dangerous than most object managers, however - maybe it actually belongs in + * a detail namespace? + */ +class multi_iter : public object +{ +public: + + BOOST_PYTHON_FORWARD_OBJECT_CONSTRUCTORS(multi_iter, object); + + /// @brief Increment the iterator. + void next(); + + /// @brief Check if the iterator is at its end. + bool not_done() const; + + /// @brief Return a pointer to the element of the nth broadcasted array. + char * get_data(int n) const; + + /// @brief Return the number of dimensions of the broadcasted array expression. + int get_nd() const; + + /// @brief Return the shape of the broadcasted array expression as an array of integers. + Py_intptr_t const * get_shape() const; + + /// @brief Return the shape of the broadcasted array expression in the nth dimension. + Py_intptr_t shape(int n) const; + +}; + +/// @brief Construct a multi_iter over a single sequence or scalar object. +multi_iter make_multi_iter(object const & a1); + +/// @brief Construct a multi_iter by broadcasting two objects. +multi_iter make_multi_iter(object const & a1, object const & a2); + +/// @brief Construct a multi_iter by broadcasting three objects. +multi_iter make_multi_iter(object const & a1, object const & a2, object const & a3); + +/** + * @brief Helps wrap a C++ functor taking a single scalar argument as a broadcasting ufunc-like + * Python object. + * + * Typical usage looks like this: + * @code + * struct TimesPI + * { + * typedef double argument_type; + * typedef double result_type; + * double operator()(double input) const { return input * M_PI; } + * }; + * + * BOOST_PYTHON_MODULE(example) + * { + * class_< TimesPI >("TimesPI") + * .def("__call__", unary_ufunc::make()); + * } + * @endcode + * + */ +template +struct unary_ufunc +{ + + /** + * @brief A C++ function with object arguments that broadcasts its arguments before + * passing them to the underlying C++ functor. + */ + static object call(TUnaryFunctor & self, object const & input, object const & output) + { + dtype in_dtype = dtype::get_builtin(); + dtype out_dtype = dtype::get_builtin(); + ndarray in_array = from_object(input, in_dtype, ndarray::ALIGNED); + ndarray out_array = (output != object()) ? + from_object(output, out_dtype, ndarray::ALIGNED | ndarray::WRITEABLE) + : zeros(in_array.get_nd(), in_array.get_shape(), out_dtype); + multi_iter iter = make_multi_iter(in_array, out_array); + while (iter.not_done()) + { + TArgument * argument = reinterpret_cast(iter.get_data(0)); + TResult * result = reinterpret_cast(iter.get_data(1)); + *result = self(*argument); + iter.next(); + } + return out_array.scalarize(); + } + + /** + * @brief Construct a boost.python function object from call() with reasonable keyword names. + * + * Users will often want to specify their own keyword names with the same signature, but this + * is a convenient shortcut. + */ + static object make() + { + return make_function(call, default_call_policies(), (arg("input"), arg("output")=object())); + } +}; + +/** + * @brief Helps wrap a C++ functor taking a pair of scalar arguments as a broadcasting ufunc-like + * Python object. + * + * Typical usage looks like this: + * @code + * struct CosSum + * { + * typedef double first_argument_type; + * typedef double second_argument_type; + * typedef double result_type; + * double operator()(double input1, double input2) const { return std::cos(input1 + input2); } + * }; + * + * BOOST_PYTHON_MODULE(example) + * { + * class_< CosSum >("CosSum") + * .def("__call__", binary_ufunc::make()); + * } + * @endcode + * + */ +template +struct binary_ufunc +{ + + static object + call(TBinaryFunctor & self, object const & input1, object const & input2, + object const & output) + { + dtype in1_dtype = dtype::get_builtin(); + dtype in2_dtype = dtype::get_builtin(); + dtype out_dtype = dtype::get_builtin(); + ndarray in1_array = from_object(input1, in1_dtype, ndarray::ALIGNED); + ndarray in2_array = from_object(input2, in2_dtype, ndarray::ALIGNED); + multi_iter iter = make_multi_iter(in1_array, in2_array); + ndarray out_array = (output != object()) + ? from_object(output, out_dtype, ndarray::ALIGNED | ndarray::WRITEABLE) + : zeros(iter.get_nd(), iter.get_shape(), out_dtype); + iter = make_multi_iter(in1_array, in2_array, out_array); + while (iter.not_done()) + { + TArgument1 * argument1 = reinterpret_cast(iter.get_data(0)); + TArgument2 * argument2 = reinterpret_cast(iter.get_data(1)); + TResult * result = reinterpret_cast(iter.get_data(2)); + *result = self(*argument1, *argument2); + iter.next(); + } + return out_array.scalarize(); + } + + static object make() + { + return make_function(call, default_call_policies(), + (arg("input1"), arg("input2"), arg("output")=object())); + } + +}; + +} // namespace boost::python::numpy + +namespace converter +{ + +NUMPY_OBJECT_MANAGER_TRAITS(numpy::multi_iter); + +}}} // namespace boost::python::converter + +#endif diff --git a/include/boost/python/object/class_metadata.hpp b/include/boost/python/object/class_metadata.hpp index 5009c176..8e750b85 100644 --- a/include/boost/python/object/class_metadata.hpp +++ b/include/boost/python/object/class_metadata.hpp @@ -1,43 +1,46 @@ -// Copyright David Abrahams 2004. Distributed under the Boost -// Software License, Version 1.0. (See accompanying -// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) -#ifndef CLASS_METADATA_DWA2004719_HPP -# define CLASS_METADATA_DWA2004719_HPP -# include +// Copyright David Abrahams 2004. +// Copyright Stefan Seefeld 2016. +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) -# include -# include -# include -# include -# include -# include +#ifndef boost_python_object_class_metadata_hpp_ +#define boost_python_object_class_metadata_hpp_ -# include -# include +#include +#include +#include +#include +#include +#include +#include -# include -# include +#include +#include -# include -# include -# include +#include +#include -# include -# include -# include -# include -# include -# include -# include -# include +#include +#include +#include -# include -# include +#include +#include +#include +#include +#include +#include +#include +#include -# include +#include +#include -# include -# include +#include + +#include +#include namespace boost { namespace python { namespace objects { @@ -80,18 +83,22 @@ struct register_base_of // Preamble of register_class. Also used for callback classes, which // need some registration of their own. // + template inline void register_shared_ptr_from_python_and_casts(T*, Bases) { - // Constructor performs registration - python::detail::force_instantiate(converter::shared_ptr_from_python()); + // Constructor performs registration + python::detail::force_instantiate(converter::shared_ptr_from_python()); +#if __cplusplus >= 201103L + python::detail::force_instantiate(converter::shared_ptr_from_python()); +#endif - // - // register all up/downcasts here. We're using the alternate - // interface to mpl::for_each to avoid an MSVC 6 bug. - // - register_dynamic_id(); - mpl::for_each(register_base_of(), (Bases*)0, (add_pointer*)0); + // + // register all up/downcasts here. We're using the alternate + // interface to mpl::for_each to avoid an MSVC 6 bug. + // + register_dynamic_id(); + mpl::for_each(register_base_of(), (Bases*)0, (add_pointer*)0); } // @@ -220,8 +227,7 @@ struct class_metadata template inline static void register_aux2(T2*, Callback) { - objects::register_shared_ptr_from_python_and_casts((T2*)0, bases()); - + objects::register_shared_ptr_from_python_and_casts((T2*)0, bases()); class_metadata::maybe_register_callback_class((T2*)0, Callback()); class_metadata::maybe_register_class_to_python((T2*)0, is_noncopyable()); @@ -282,9 +288,8 @@ struct class_metadata template inline static void maybe_register_callback_class(T2*, mpl::true_) { - objects::register_shared_ptr_from_python_and_casts( + objects::register_shared_ptr_from_python_and_casts( (wrapped*)0, mpl::single_view()); - // explicit qualification of type_id makes msvc6 happy objects::copy_class_object(python::type_id(), python::type_id()); } @@ -292,4 +297,4 @@ struct class_metadata }}} // namespace boost::python::object -#endif // CLASS_METADATA_DWA2004719_HPP +#endif diff --git a/include/boost/python/object/make_ptr_instance.hpp b/include/boost/python/object/make_ptr_instance.hpp index 9fdb23f6..3a281902 100644 --- a/include/boost/python/object/make_ptr_instance.hpp +++ b/include/boost/python/object/make_ptr_instance.hpp @@ -21,7 +21,11 @@ struct make_ptr_instance template static inline Holder* construct(void* storage, PyObject*, Arg& x) { - return new (storage) Holder(x); +#if __cplusplus < 201103L + return new (storage) Holder(x); +#else + return new (storage) Holder(std::move(x)); +#endif } template diff --git a/include/boost/python/object/pointer_holder.hpp b/include/boost/python/object/pointer_holder.hpp index 27d95193..b28cbd83 100644 --- a/include/boost/python/object/pointer_holder.hpp +++ b/include/boost/python/object/pointer_holder.hpp @@ -107,13 +107,21 @@ struct pointer_holder_back_reference : instance_holder template inline pointer_holder::pointer_holder(Pointer p) +#if __cplusplus < 201103L : m_p(p) +#else + : m_p(std::move(p)) +#endif { } template inline pointer_holder_back_reference::pointer_holder_back_reference(Pointer p) +#if __cplusplus < 201103L : m_p(p) +#else + : m_p(std::move(p)) +#endif { } diff --git a/include/boost/python/object/py_function.hpp b/include/boost/python/object/py_function.hpp index ba9aadf4..05cedfa0 100644 --- a/include/boost/python/object/py_function.hpp +++ b/include/boost/python/object/py_function.hpp @@ -135,7 +135,11 @@ struct py_function {} py_function(py_function const& rhs) - : m_impl(rhs.m_impl) +#if __cplusplus < 201103L + : m_impl(rhs.m_impl) +#else + : m_impl(std::move(rhs.m_impl)) +#endif {} PyObject* operator()(PyObject* args, PyObject* kw) const @@ -164,7 +168,11 @@ struct py_function } private: +#if __cplusplus < 201103L mutable std::auto_ptr m_impl; +#else + mutable std::unique_ptr m_impl; +#endif }; }}} // namespace boost::python::objects diff --git a/include/boost/python/object_core.hpp b/include/boost/python/object_core.hpp index 61857796..209310ff 100644 --- a/include/boost/python/object_core.hpp +++ b/include/boost/python/object_core.hpp @@ -422,6 +422,7 @@ inline api::object_base& api::object_base::operator=(api::object_base const& rhs inline api::object_base::~object_base() { + assert( Py_REFCNT(m_ptr) > 0 ); Py_DECREF(m_ptr); } diff --git a/include/boost/python/refcount.hpp b/include/boost/python/refcount.hpp index 5ba5433c..aa6aa5db 100755 --- a/include/boost/python/refcount.hpp +++ b/include/boost/python/refcount.hpp @@ -27,12 +27,14 @@ inline T* xincref(T* p) template inline void decref(T* p) { + assert( Py_REFCNT(python::upcast(p)) > 0 ); Py_DECREF(python::upcast(p)); } template inline void xdecref(T* p) { + assert( !p || Py_REFCNT(python::upcast(p)) > 0 ); Py_XDECREF(python::upcast(p)); } diff --git a/include/boost/python/str.hpp b/include/boost/python/str.hpp index 426a3a25..434c8c98 100644 --- a/include/boost/python/str.hpp +++ b/include/boost/python/str.hpp @@ -184,7 +184,7 @@ class str : public detail::str_base template long count(T1 const& sub,T2 const& start, T3 const& end) const { - return base::count(object(sub), object(start)); + return base::count(object(sub), object(start), object(end)); } #if PY_VERSION_HEX < 0x03000000 diff --git a/include/boost/python/to_python_indirect.hpp b/include/boost/python/to_python_indirect.hpp index 23ad0263..af6ed33b 100644 --- a/include/boost/python/to_python_indirect.hpp +++ b/include/boost/python/to_python_indirect.hpp @@ -86,8 +86,10 @@ namespace detail // copy constructor. # if defined(__ICL) && __ICL < 600 typedef boost::shared_ptr smart_pointer; -# else +# elif __cplusplus < 201103L typedef std::auto_ptr smart_pointer; +# else + typedef std::unique_ptr smart_pointer; # endif typedef objects::pointer_holder holder_t; diff --git a/include/boost/python/to_python_value.hpp b/include/boost/python/to_python_value.hpp index aaabb9cf..2681f8a3 100644 --- a/include/boost/python/to_python_value.hpp +++ b/include/boost/python/to_python_value.hpp @@ -1,30 +1,32 @@ // Copyright David Abrahams 2002. +// Copyright Stefan Seefeld 2016. // Distributed under the Boost Software License, Version 1.0. (See // accompanying file LICENSE_1_0.txt or copy at // http://www.boost.org/LICENSE_1_0.txt) -#ifndef TO_PYTHON_VALUE_DWA200221_HPP -# define TO_PYTHON_VALUE_DWA200221_HPP -# include +#ifndef boost_python_to_python_value_hpp_ +#define boost_python_to_python_value_hpp_ -# include -# include -# include +#include -# include -# include -# include -# include -# include +#include +#include +#include -# include -# include +#include +#include +#include +#include +#include -# include +#include +#include -# include -# include -# include +#include + +#include +#include +#include namespace boost { namespace python { @@ -114,10 +116,16 @@ struct object_manager_get_pytype BOOST_STATIC_CONSTANT(bool, uses_registry = false); private: #ifndef BOOST_PYTHON_NO_PY_SIGNATURES - template - PyTypeObject const* get_pytype(boost::type &> *) const {return converter::registered::converters.to_python_target_type();} - template - PyTypeObject const* get_pytype(boost::type &> *) const {return converter::registered::converters.to_python_target_type();} + template + PyTypeObject const* get_pytype(boost::type &> *) const {return converter::registered::converters.to_python_target_type();} + template + PyTypeObject const* get_pytype(boost::type &> *) const {return converter::registered::converters.to_python_target_type();} +# if __cplusplus >= 201103L + template + PyTypeObject const* get_pytype(boost::type &> *) const {return converter::registered::converters.to_python_target_type();} + template + PyTypeObject const* get_pytype(boost::type &> *) const {return converter::registered::converters.to_python_target_type();} +# endif #endif }; } @@ -168,4 +176,4 @@ namespace detail }} // namespace boost::python -#endif // TO_PYTHON_VALUE_DWA200221_HPP +#endif diff --git a/index.html b/index.html index 9c6acc88..1e1a2753 100644 --- a/index.html +++ b/index.html @@ -1,12 +1,13 @@ - - - + - - - - -Automatically loading index page... if nothing happens, please go to -doc/index.html. - + + + + + Automatic redirection failed, click this + link  
+

© Copyright Stefan Seefeld, 2015

+

Distributed under the Boost Software License, Version 1.0. (See accompanying + file http://www.boost.org/LICENSE_1_0.txt)

+ diff --git a/pyste/NEWS b/pyste/NEWS deleted file mode 100644 index 31a5ceba..00000000 --- a/pyste/NEWS +++ /dev/null @@ -1,212 +0,0 @@ -.. Copyright Bruno da Silva de Oliveira 2006. Distributed under the Boost -.. Software License, Version 1.0. (See accompanying -.. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) - -25 April 2005 -- Fixed bug where the code for wrappers of member functions were defined outside -the pyste namespace. Reported by Dan Haffey. - -9 October 2004 -- Applied a patch by Christian Hudon that fixed an issue with files -that had a tail and relative includes. - -18 July 2004 -- Applied a patch by Paul Bridger that solves some problems for wrapper -methods. -- Applied a patch by Baptiste Lepilleur that allows the user to inject -code inside the class definition. -- Applied another patch by Baptiste Lepilleur that inserts two new command-line -options that helps with writing makefiles. - -27 May 2004 -Applied patch by Paul Bridger that solves a problem on windows regarding -spaces on paths. Thanks Paul! - -Applied another patch that fixes the module name if pyste is run from -another directory of where the .pyste file is located. Patch contributted -by Paul Bridger. - -17 May 2004 -Applied a patch by Roman Yakovenko that makes the export of unnamed enums -better. Thanks Roman! - -23 October 2003 -Fixed bug where a class would appear more than one in the generated code. - -6 October 2003 -Fixed bug reported by Niall Douglas (using his patch) about UniqueInt not -appearing correctly with --multiple. - -Added precompiled header support on windows systems (using #pragma hdrstop). -Suggested by Niall Douglas. - -Fixed a bug with -I directive and AllFromHeader. Reported by Scott Snyder. - -4 October 2003 -Added return_self, thanks for Niall Douglas for pointing out that it was -missing. - -Added --file-list, where you can pass a file where the pyste files are listed -one per line. Also suggested by Niall Douglas. - -Documentation has been finally updated, after a long wait. Please let me know -if you spot any mistake! - -2 October 2003 -Scott Snyder found a typo in ClassExporter that prevented -= and *= operators -from being exported. Thanks Scott! - -20 September 2003 -Added return_by_value in the list of policies supported. Thanks to Niall -Douglas for the remainder. - -19 September 2003 -Better support for unnamed enums, plus they are by default exported to the -parent's namespace. Normal enums can have the same behaviour using the function -export_values on the Enum object. Feature requested by Niall Douglas. - -10 September 2003 -A new variable is accessible in the Pyste files: INTERFACE_FILE contains the -full path of the pyste file. - -4 September 2003 -Now it is possible to override protected and private pure virtual functions -in Python, as requested by Roman Yakovenko. - -23 August 2003 -Fixed bug where some Imports where not writing their include files. -Now whenever the declarations change, the cache files are rebuilt -automatically. - -19 August 2003 -Fixed a bug related to the generation of the bases<> template. - -17 August 2003 -Added support for insertion of user code in the generated code. - -16 August 2003 -Applied a patch by Gottfried Ganssauge that adds exception specifiers to -wrapper functions and pointer declarations. Thanks a lot Gottfried!! - -Applied a patch by Prabhu Ramachandran that fixes ae problem with the -pure virtual method generation. Thanks again Prabhu! - -10 August 2003 -Support for incremental generation of the code has been added. This changes -how --multiple works; documentation of this new feature will follow. Thanks -to Prabhu Ramachandran, that saw the need for this feature and discussed a -solution. - -Automatically convert \ to / in Windows systems before passing the paths to -gccxml. - -Fixed a bug reported by Prabhu Ramachandran, where in some classes the virtual -methods were being definied incorrectly. Thanks a lot Prabhu! - -7 July 2003 -Applied 2 patches by Prabhu Ramachandran: a fix in the new --multiple method, -and two new functions "hold_with_shared_ptr" and its counterpart for auto_ptr. -Thanks a lot Prabhu! - -Fixed a bug where the macro BOOST_PYTHON_OPAQUE_SPECIALIZED_TYPE_ID was being -called multiple times for the same type. -Thanks to Gottfried Ganßauge for reporting this! - -Fixed bug where using AllFromHeader didn't use bases<> when exporting -hierarchies. - -Fixed the staticmethod bug. - -5 July 2003 -Changed how --multiple works: now it generates one cpp file for each pyste -file, makeing easier to integrate Pyste with build systems. - -4 July 2003 -Applied patch that solved a bug in ClassExporter and added a distutils install -script (install/setup.py), both contributed by Prabhu Ramachandran. -Thanks Prabhu! - -2 July 2003 -Jim Wilson found a bug where types like "char**" were being interpreted as -"char*". Thanks Jim! - -16 June 2003 -Thanks to discussions with David Abrahams and Roman Sulzhyk, some behaviours -have changed: - -- If you export a derived class without exporting its base classes, the derived - class will explicitly export the bases's methods and attributes. Before, if - you were interested in the bases's methods, you had to export the base - classes too. - -- Added a new function, no_override. When a member function is specified as - "no_override", no virtual wrappers are generated for it, improving - performance and letting the code more clean. - -- There was a bug in which the policy of virtual member functions was being - ignored (patch by Roman Sulzhyk). - -Thanks again to Roman Sulzhyk for the patches and discussion in the c++-sig. - -4 June 2003 -Major improvements in memory usage. - -3 June 2003 -Appliced a patch from Giulio Eulisse that allows unnamed enumerations to be -exported with an AllFromHeader construct. Thanks a lot Giulio! - -2 June 2003 -Added a new construct, add_method. See documentation. - -23 May 2003 -Support for global variables added. -Various bug fixes. - -08 May 2003 -Fixed bug where in a certain cases the GCCXMLParser would end up with multiple -declarations of the same class - -22 Apr 2003 -- Now shows a warning when the user tries to export a forward-declared class. - Forward-declared classes are ignored by the AllFromHeader construct. -- Fixed a bug where classes, functions and enums where being exported, even if - excluded from a AllFromHeader construct. - -16 Apr 2003 -Added a more generic (but ugly) code to declare the smart pointer converters. - -07 Apr 2003 -- Removed the warnings about forward declarations: it was not accurate enough. - Another strategy must be thought of. -- Fixed bug in the --multiple mode, where the order of the class instantiations - could end up wrong. -- Lots of fixes in the documentation, pointed out by Dirk Gerrits. Thanks Dirk! -- Fixed support for the return_opaque_pointer policy (the support macro was not - being declared). - - -06 Apr 2003 -Support for the improved static data members support of Boost.Python. - -05 Apr 2003 -New option for generating the bindings: --multiple. - -02 Apr 2003 -Forward declarations are now detected and a warning is generated. - -24 Mar 2003 -Default policy for functions/methods that return const T& is now -return_value_policy(). - -22 Mar 2003 -Exporting virtual methods of the base classes in the derived classes too. - -21 Mar 2003 -Added manual support for boost::shared_ptr and std::auto_ptr (see doc). - -19 Mar 2003 -Added support for int, double, float and long operators acting as expected in -python. - -14 Mar 2003 -Fixed bug: Wrappers for protected and virtual methods were not being generated. diff --git a/pyste/README b/pyste/README deleted file mode 100644 index c378f391..00000000 --- a/pyste/README +++ /dev/null @@ -1,35 +0,0 @@ -.. Copyright Bruno da Silva de Oliveira 2006. Distributed under the Boost -.. Software License, Version 1.0. (See accompanying -.. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) - -Pyste - Python Semi-Automatic Exporter -====================================== - -Pyste is a Boost.Python code generator. The user specifies the classes and -functions to be exported using a simple interface file, which following the -Boost.Python's philosophy, is simple Python code. Pyste then uses GCCXML to -parse all the headers and extract the necessary information to automatically -generate C++ code. - -The documentation can be found in the file index.html accompaning this README. - -Enjoy! -Bruno da Silva de Oliveira (nicodemus@esss.com.br) - -Thanks -====== - -- David Abrahams, creator of Boost.Python, for tips on the syntax of the interface - file and support. -- Marcelo Camelo, for design tips, support and inspiration for this project. - Also, the name was his idea. 8) -- Brad King, creator of the excellent GCCXML (http://www.gccxml.org) -- Fredrik Lundh, creator of the elementtree library (http://effbot.org) - -Bugs -==== - -Pyste is a young tool, so please help it to get better! Send bug reports to -nicodemus@esss.com.br, accompaining the stack trace in case of exceptions. -If possible, run pyste with --debug, and send the resulting xmls too (pyste -will output a xml file with the same of each header it parsed). diff --git a/pyste/TODO b/pyste/TODO deleted file mode 100644 index 0b3c9024..00000000 --- a/pyste/TODO +++ /dev/null @@ -1,18 +0,0 @@ -.. Copyright Bruno da Silva de Oliveira 2006. Distributed under the Boost -.. Software License, Version 1.0. (See accompanying -.. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) - -- Make Pyste accept already-generated xml files - -- throw() declaration in virtual wrapper's member functions - -- Allow protected methods to be overriden in Python - -- Expose programmability to the Pyste files (listing members of a class, for - instance) - -- Virtual operators - -- args() support - -- set policies to methods with the same name diff --git a/pyste/dist/create_build.py b/pyste/dist/create_build.py deleted file mode 100644 index a6836995..00000000 --- a/pyste/dist/create_build.py +++ /dev/null @@ -1,55 +0,0 @@ -# Copyright Bruno da Silva de Oliveira 2006. Distributed under the Boost -# Software License, Version 1.0. (See accompanying -# file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) - -import os -import sys -import shutil -import fnmatch -from zipfile import ZipFile, ZIP_DEFLATED - -def findfiles(directory, mask): - def visit(files, dir, names): - for name in names: - if fnmatch.fnmatch(name, mask): - files.append(os.path.join(dir, name)) - files = [] - os.path.walk(directory, visit, files) - return files - - -def main(): - # test if PyXML is installed - try: - import _xmlplus.parsers.expat - pyxml = '--includes _xmlplus.parsers.expat' - except ImportError: - pyxml = '' - # create exe - status = os.system('python setup.py py2exe %s >& build.log' % pyxml) - if status != 0: - raise RuntimeError, 'Error creating EXE' - - # create distribution - import pyste - version = pyste.__VERSION__ - zip = ZipFile('pyste-%s.zip' % version, 'w', ZIP_DEFLATED) - # include the base files - dist_dir = 'dist/pyste' - for basefile in os.listdir(dist_dir): - zip.write(os.path.join(dist_dir, basefile), os.path.join('pyste', basefile)) - # include documentation - for doc_file in findfiles('../doc', '*.*'): - dest_name = os.path.join('pyste/doc', doc_file[3:]) - zip.write(doc_file, dest_name) - zip.write('../index.html', 'pyste/doc/index.html') - zip.close() - # cleanup - os.remove('build.log') - shutil.rmtree('build') - shutil.rmtree('dist') - - -if __name__ == '__main__': - sys.path.append('../src') - main() diff --git a/pyste/dist/setup.py b/pyste/dist/setup.py deleted file mode 100644 index fc7c74e2..00000000 --- a/pyste/dist/setup.py +++ /dev/null @@ -1,10 +0,0 @@ -# Copyright Bruno da Silva de Oliveira 2006. Distributed under the Boost -# Software License, Version 1.0. (See accompanying -# file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) - -from distutils.core import setup -import py2exe -import sys - -sys.path.append('../src') -setup(name='pyste', scripts=['../src/pyste.py']) diff --git a/pyste/doc/adding_new_methods.html b/pyste/doc/adding_new_methods.html deleted file mode 100644 index afa772bc..00000000 --- a/pyste/doc/adding_new_methods.html +++ /dev/null @@ -1,79 +0,0 @@ - - - -Adding New Methods - - - - - - - - - - -
- - Adding New Methods -
-
- - - - - - -
-

-Suppose that you want to add a function to a class, turning it into a member -function:

-
-    struct World
-    {
-        void set(std::string msg) { this->msg = msg; }
-        std::string msg;
-    };
-
-    std::string greet(World& w)
-    {
-        return w.msg;
-    }
-
-

-Here, we want to make greet work as a member function of the class World. We do -that using the add_method construct:

-
-    W = Class("World", "hello.h")
-    add_method(W, "greet")
-
-

-Notice also that then you can rename it, set its policy, just like a regular -member function:

-
-    rename(W.greet, 'Greet')
-
-

-Now from Python:

-
-    >>> import hello
-    >>> w = hello.World()
-    >>> w.set('Ni')
-    >>> w.greet()
-    'Ni'
-    >>> print 'Oh no! The knights who say Ni!'
-    Oh no! The knights who say Ni!
-
- - - - - - -
-
-
- - diff --git a/pyste/doc/exporting_an_entire_header.html b/pyste/doc/exporting_an_entire_header.html deleted file mode 100644 index db25325c..00000000 --- a/pyste/doc/exporting_an_entire_header.html +++ /dev/null @@ -1,85 +0,0 @@ - - - -Exporting An Entire Header - - - - - - - - - - -
- - Exporting An Entire Header -
-
- - - - - - -
-

-Pyste also supports a mechanism to export all declarations found in a header -file. Suppose again our file, hello.h:

-
-    struct World
-    {
-        World(std::string msg): msg(msg) {} 
-        void set(std::string msg) { this->msg = msg; }
-        std::string greet() { return msg; }
-        std::string msg;
-    };
-
-    enum choice { red, blue };
-    
-    void show(choice c) { std::cout << "value: " << (int)c << std::endl; } 
-
-

-You can just use the AllFromHeader construct:

-
-    hello = AllFromHeader("hello.h")
-
-

-this will export all the declarations found in hello.h, which is equivalent -to write:

-
-    Class("World", "hello.h")
-    Enum("choice", "hello.h")
-    Function("show", "hello.h")
-
-

-Note that you can still use the functions rename, set_policy, exclude, etc. Just access -the members of the header object like this:

-
-    rename(hello.World.greet, "Greet")
-    exclude(hello.World.set, "Set")
-
- - - - -
- - AllFromHeader is broken in some cases. Until it is fixed, -use at you own risk. -
- - - - - - -
-
-
- - diff --git a/pyste/doc/global_variables.html b/pyste/doc/global_variables.html deleted file mode 100644 index 0efd2950..00000000 --- a/pyste/doc/global_variables.html +++ /dev/null @@ -1,49 +0,0 @@ - - - -Global Variables - - - - - - - - - - -
- - Global Variables -
-
- - - - - - -
-

-To export global variables, use the Var construct:

-
-    Var("myglobal", "foo.h")
-
-

-Beware of non-const global variables: changes in Python won't reflect in C++! -If you really must change them in Python, you will have to write some accessor -functions, and export those.

- - - - - - -
-
-
- - diff --git a/pyste/doc/inserting_code.html b/pyste/doc/inserting_code.html deleted file mode 100644 index 97eb70f3..00000000 --- a/pyste/doc/inserting_code.html +++ /dev/null @@ -1,72 +0,0 @@ - - - -Inserting Code - - - - - - - - - -
- - Inserting Code -
-
- - - - - - -
-

-You can insert arbitrary code in the generated cpps, just use the functions -declaration_code and module_code. This will insert the given string in the -respective sections. Example:

-
-    ##file A.pyste
-    Class("A", "A.h")
-    declaration_code("/* declaration_code() comes here */\n")
-    module_code("/* module_code() comes here */\n")
-
-

-Will generate:

-
-    // Includes ====================================================================
-    #include <boost/python.hpp>
-
-    // Using =======================================================================
-    using namespace boost::python;
-
-    // Declarations ================================================================
-
-    /* declaration_code() comes here */
-
-    // Module ======================================================================
-    BOOST_PYTHON_MODULE(A)
-    {
-        class_< A >("A", init<  >())
-            .def(init< const A& >())
-        ;
-
-    /* module_code() comes here */
-    }
-
- - - - - - -
-
-
- - diff --git a/pyste/doc/introduction.html b/pyste/doc/introduction.html deleted file mode 100644 index 94388493..00000000 --- a/pyste/doc/introduction.html +++ /dev/null @@ -1,73 +0,0 @@ - - - -Introduction - - - - - - - - - -
- - Introduction -
-
- - - - - - -
-

What is Pyste?

-Pyste is a -Boost.Python code generator. The user specifies the classes and -functions to be exported using a simple interface file, which following the - -Boost.Python's philosophy, is simple Python code. Pyste then uses -GCCXML to -parse all the headers and extract the necessary information to automatically -generate C++ code.

-

Example

-Let's borrow the class World from the -tutorial:

-
-    struct World
-    {
-        void set(std::string msg) { this->msg = msg; }
-        std::string greet() { return msg; }
-        std::string msg;
-    };
-
-

-Here's the interface file for it, named world.pyste:

-
-    Class("World", "world.h")
-
-

-and that's it!

-

-The next step is invoke Pyste in the command-line:

-
python pyste.py --module=hello world.pyste

-this will create a file "hello.cpp" in the directory where the command was -run.

-

-Pyste supports the following features:

-
  • Functions
  • Classes
  • Class Templates
  • Virtual Methods
  • Overloading
  • Attributes
  • Enums (both "free" enums and class enums)
  • Nested Classes
  • Support for boost::shared_ptr and std::auto_ptr
  • Global Variables
- - - - - -
-
-
- - diff --git a/pyste/doc/policies.html b/pyste/doc/policies.html deleted file mode 100644 index 3628093b..00000000 --- a/pyste/doc/policies.html +++ /dev/null @@ -1,90 +0,0 @@ - - - -Policies - - - - - - - - - - -
- - Policies -
-
- - - - - - -
-

-Even thought Pyste can identify various elements in the C++ code, like virtual -member functions, attributes, and so on, one thing that it can't do is to -guess the semantics of functions that return pointers or references. In this -case, the user must manually specify the policy. Policies are explained in the - -tutorial.

-

-The policies in Pyste are named exactly as in -Boost.Python, only the syntax is -slightly different. For instance, this policy:

-
-    return_internal_reference<1, with_custodian_and_ward<1, 2> >()
-
-

-becomes in Pyste:

-
-    return_internal_reference(1, with_custodian_and_ward(1, 2))
-
-

-The user can specify policies for functions and virtual member functions with -the set_policy function:

-
-    set_policy(f, return_internal_reference())
-    set_policy(C.foo, return_value_policy(manage_new_object))
-
- - - - -
- - What if a function or member function needs a policy and -the user doesn't set one?

If a function needs a policy and one -was not set, Pyste will issue a error. The user should then go in the -interface file and set the policy for it, otherwise the generated cpp won't -compile. -
- - - - -
- - -Note that for functions that return const T&, the policy -return_value_policy<copy_const_reference>() wil be used by default, because -that's normally what you want. You can change it to something else if you need -to, though. -
- - - - - - -
-
-
- - diff --git a/pyste/doc/pyste.txt b/pyste/doc/pyste.txt deleted file mode 100644 index 186a31cb..00000000 --- a/pyste/doc/pyste.txt +++ /dev/null @@ -1,664 +0,0 @@ -[doc Pyste Documentation] - -[/ Copyright 2003 Bruno da Silva de Oliveira and Joel de Guzman. -Distributed under the Boost Software License, Version 1.0. (See -accompanying file LICENSE_1_0.txt or copy at -http://www.boost.org/LICENSE_1_0.txt) ] - -[def GCCXML [@http://www.gccxml.org GCCXML]] -[def Boost.Python [@../../index.html Boost.Python]] - -[page Introduction] - -[h2 What is Pyste?] - -Pyste is a Boost.Python code generator. The user specifies the classes and -functions to be exported using a simple ['interface file], which following the -Boost.Python's philosophy, is simple Python code. Pyste then uses GCCXML to -parse all the headers and extract the necessary information to automatically -generate C++ code. - -[h2 Example] - -Let's borrow the class [^World] from the [@../../doc/tutorial/doc/exposing_classes.html tutorial]: - - struct World - { - void set(std::string msg) { this->msg = msg; } - std::string greet() { return msg; } - std::string msg; - }; - -Here's the interface file for it, named [^world.pyste]: - - Class("World", "world.h") - -and that's it! - -The next step is invoke Pyste in the command-line: - -[pre python pyste.py --module=hello world.pyste] - -this will create a file "[^hello.cpp]" in the directory where the command was -run. - -Pyste supports the following features: - -* Functions -* Classes -* Class Templates -* Virtual Methods -* Overloading -* Attributes -* Enums (both "free" enums and class enums) -* Nested Classes -* Support for [^boost::shared_ptr] and [^std::auto_ptr] -* Global Variables - -[page Running Pyste] - -To run Pyste, you will need: - -* Python 2.2, available at [@http://www.python.org python's website]. -* The great [@http://effbot.org elementtree] library, from Fredrik Lundh. -* The excellent GCCXML, from Brad King. - -Installation for the tools is available in their respective webpages. - -[blurb -[$theme/note.gif] GCCXML must be accessible in the PATH environment variable, so -that Pyste can call it. How to do this varies from platform to platform. -] - -[h2 Ok, now what?] - -Well, now let's fire it up: - -[pre -''' ->python pyste.py - -Pyste version 0.9.26 - -Usage: - pyste [options] interface-files - -where options are: - --module= The name of the module that will be generated; - defaults to the first interface filename, without - the extension. - -I Add an include path - -D Define symbol - --multiple Create various cpps, instead of only one - (useful during development) - --out= Specify output filename (default: .cpp) - in --multiple mode, this will be a directory - --no-using Do not declare "using namespace boost"; - use explicit declarations instead - --pyste-ns= Set the namespace where new types will be declared; - default is the empty namespace - --debug Writes the xml for each file parsed in the current - directory - --cache-dir= Directory for cache files (speeds up future runs) - --only-create-cache Recreates all caches (doesn't generate code). - --generate-main Generates the _main.cpp file (in multiple mode) - --file-list A file with one pyste file per line. Use as a - substitute for passing the files in the command - line. - -h, --help Print this help and exit - -v, --version Print version information - -''' -] - -Options explained: - -The [^-I] and [^-D] are preprocessor flags, which are needed by GCCXML to parse -the header files correctly and by Pyste to find the header files declared in the -interface files. - -[^--out] names the output file (default: [^.cpp]), or in multiple mode, -names a output directory for the files (default: [^]). - -[^--no-using] tells Pyste to don't declare "[^using namespace boost;]" in the -generated cpp, using the namespace boost::python explicitly in all declarations. -Use only if you're having a name conflict in one of the files. - -Use [^--pyste-ns] to change the namespace where new types are declared (for -instance, the virtual wrappers). Use only if you are having any problems. By -default, Pyste uses the empty namespace. - -[^--debug] will write in the current directory a xml file as outputted by GCCXML -for each header parsed. Useful for bug reports. - -[^--file-list] names a file where each line points to a Pyste file. Use this instead -to pass the pyste files if you have a lot of them and your shell has some command line -size limit. - -The other options are explained below, in [@#multiple_mode [*Multiple Mode]] and -[@#cache [*Cache]]. - -[^-h, --help, -v, --version] are self-explaining, I believe. ;) - -So, the usage is simple enough: - -[pre >python pyste.py --module=mymodule file.pyste file2.pyste ...] - -will generate a file [^mymodule.cpp] in the same dir where the command was -executed. Now you can compile the file using the same instructions of the -[@../../doc/tutorial/doc/building_hello_world.html tutorial]. - -[h2 Wait... how do I set those I and D flags?] - -Don't worry: normally GCCXML is already configured correctly for your plataform, -so the search path to the standard libraries and the standard defines should -already be set. You only have to set the paths to other libraries that your code -needs, like Boost, for example. - -Plus, Pyste automatically uses the contents of the environment variable -[^INCLUDE] if it exists. Visual C++ users should run the [^Vcvars32.bat] file, -which for Visual C++ 6 is normally located at: - - C:\Program Files\Microsoft Visual Studio\VC98\bin\Vcvars32.bat - -with that, you should have little trouble setting up the flags. - -[blurb [$theme/note.gif][*A note about Psyco][br][br] -Although you don't have to install [@http://psyco.sourceforge.net/ Psyco] to -use Pyste, if you do, Pyste will make use of it to speed up the wrapper -generation. Speed ups of 30% can be achieved, so it's highly recommended. -] - - -[h2 Multiple Mode] - -The multiple mode is useful in large projects, where the presence of multiple -classes in a single file makes the compilation unpractical (excessive memory -usage, mostly). - -The solution is make Pyste generate multiple files, more specifically one cpp -file for each Pyste file. This files will contain a function named after the -file, for instance Export_MyPysteFile, which will contain all the code to export -the classes, enums, etc. You can pass as much files as you want this way: - -[pre >python pyste.py --module=mymodule file1.pyste file2.pyste] - -This will create the files [^mymodule/file1.cpp] and [^mymodule/file2.cpp]. You -can then later do: - -[pre >python pyste.py --module=mymodule file3.pyste] - -and [^mymodule/file3.cpp] will be generated. - -But compiling and linking this files won't be sufficient to generate your -extension. You have to also generate a file named [^main.cpp]; call pyste with -[*all] the Pyste files of your extension, and use the [^--generate-main] option: - -[pre >python pyste.py --module=mymodule --generate-main file1.pyste file2.pyste file3.pyste] - -Now compile and link all this files together and your extension is ready for -use. - -[h2 Cache] - -Pyste now supports a form of cache, which is a way to speed up the code -generation. Most of the time that Pyste takes to generate the code comes from -having to execute GCCXML (since being a front-end to GCC, it has to compile the -header files) and reading back the XML generated. - -When you use the [^--cache-dir=] option, Pyste will dump in the specified -directory the generated XMLs to a file named after the Pyste file, with the -extension [^.pystec]. The next time you run with this option, Pyste will use -the cache, instead of calling GCCXML again: - -[pre >python pyste.py --module=mymodule --cache-dir=cache file1.pyste] - -Will generate [^file1.cpp] and [^cache/file1.pystec]. Next time you execute -this command, the cache file will be used. Note that Pyste doesn't do any check -to ensure that the cache is up to date, but you can configure your build system to do that for you. - -When you run Pyste with [^--only-create-cache], all the cache files will be -created again, but no code will be generated. - -[page The Interface Files] - -The interface files are the heart of Pyste. The user creates one or more -interface files declaring the classes and functions he wants to export, and then -invokes Pyste passing the interface files to it. Pyste then generates a single -cpp file with Boost.Python code, with all the classes and functions exported. - -Besides declaring the classes and functions, the user has a number of other -options, like renaming e excluding classes and member functionis. Those are -explained later on. - -[h2 Basics] - -Suppose we have a class and some functions that we want to expose to Python -declared in the header [^hello.h]: - - struct World - { - World(std::string msg): msg(msg) {} - void set(std::string msg) { this->msg = msg; } - std::string greet() { return msg; } - std::string msg; - }; - - enum choice { red, blue }; - - namespace test { - - void show(choice c) { std::cout << "value: " << (int)c << std::endl; } - - } - -We create a file named [^hello.pyste] and create instances of the classes -[^Function], [^Class] and [^Enum]: - - Function("test::show", "hello.h") - Class("World", "hello.h") - Enum("choice", "hello.h") - -That will expose the class, the free function and the enum found in [^hello.h]. - -[h2 Inheritance] - -Pyste automatically generates the correct code (specifying [^bases<>] in the -[^class_] declaration) [*if] the Class() function that exports the base classes -and their children are in the same Pyste file. If that's not the case, you have -to indicate that there's a relationship between the Pyste files using the -[^Import] function specifying the other Pyste file. - -Suppose we have two classes, [^A] and [^B], and A is a base class for B. We -create two Pyste files: - -[^A.pyste]: - - Class("A", "A.h") - -[^B.pyste]: - - Import("A.pyste") - Class("B", "B.h") - -Note that we specify that [^B] needs to know about [^A] to be properly exported. - -[page:1 Renaming and Excluding] - -You can easily rename functions, classes, member functions, attributes, etc. Just use the -function [^rename], like this: - - World = Class("World", "hello.h") - rename(World, "IWorld") - show = Function("choice", "hello.h") - rename(show, "Show") - -You can rename member functions and attributes using this syntax: - - rename(World.greet, "Greet") - rename(World.set, "Set") - choice = Enum("choice", "hello.h") - rename(choice.red, "Red") - rename(choice.blue, "Blue") - -You can exclude functions, classes, member functions, attributes, etc, in the same way, -with the function [^exclude]: - - exclude(World.greet) - exclude(World.msg) - -To access the operators of a class, access the member [^operator] like this -(supposing that [^C] is a class being exported): - - exclude(C.operator['+']) - exclude(C.operator['*']) - exclude(C.operator['<<']) - -The string inside the brackets is the same as the name of the operator in C++.[br] - -[h2 Virtual Member Functions] - -Pyste automatically generates wrappers for virtual member functions, but you may -want to disable this behaviour (for performance reasons, for instance) if you do -not plan to override the functions in Python. To do this, use the function -[^final]: - - C = Class('C', 'C.h') - final(C.foo) # C::foo is a virtual member function - -No virtual wrapper code will be generated for the virtual member function -C::foo that way. - -[page:1 Policies] - -Even thought Pyste can identify various elements in the C++ code, like virtual -member functions, attributes, and so on, one thing that it can't do is to -guess the semantics of functions that return pointers or references. In this -case, the user must manually specify the policy. Policies are explained in the -[@../../doc/tutorial/doc/call_policies.html tutorial]. - -The policies in Pyste are named exactly as in Boost.Python, only the syntax is -slightly different. For instance, this policy: - - return_internal_reference<1, with_custodian_and_ward<1, 2> >() - -becomes in Pyste: - - return_internal_reference(1, with_custodian_and_ward(1, 2)) - -The user can specify policies for functions and virtual member functions with -the [^set_policy] function: - - set_policy(f, return_internal_reference()) - set_policy(C.foo, return_value_policy(manage_new_object)) - -[blurb -[$theme/note.gif] [*What if a function or member function needs a policy and -the user doesn't set one?][br][br] If a function needs a policy and one -was not set, Pyste will issue a error. The user should then go in the -interface file and set the policy for it, otherwise the generated cpp won't -compile. -] - -[blurb -[$theme/note.gif] -Note that for functions that return [^const T&], the policy -[^return_value_policy()] wil be used by default, because -that's normally what you want. You can change it to something else if you need -to, though. -] - -[page:1 Templates] - -Template classes can easily be exported too, but you can't export the template -itself... you have to export instantiations of it! So, if you want to export a -[^std::vector], you will have to export vectors of int, doubles, etc. - -Suppose we have this code: - - template - struct Point - { - T x; - T y; - }; - -And we want to export [^Point]s of int and double: - - Point = Template("Point", "point.h") - Point("int") - Point("double") - -Pyste will assign default names for each instantiation. In this example, those -would be "[^Point_int]" and "[^Point_double]", but most of the time users will want to -rename the instantiations: - - Point("int", "IPoint") // renames the instantiation - double_inst = Point("double") // another way to do the same - rename(double_inst, "DPoint") - -Note that you can rename, exclude, set policies, etc, in the [^Template] object -like you would do with a [^Function] or a [^Class]. This changes affect all -[*future] instantiations: - - Point = Template("Point", "point.h") - Point("float", "FPoint") // will have x and y as data members - rename(Point.x, "X") - rename(Point.y, "Y") - Point("int", "IPoint") // will have X and Y as data members - Point("double", "DPoint") // also will have X and Y as data member - -If you want to change a option of a particular instantiation, you can do so: - - Point = Template("Point", "point.h") - Point("int", "IPoint") - d_inst = Point("double", "DPoint") - rename(d_inst.x, "X") // only DPoint is affect by this renames, - rename(d_inst.y, "Y") // IPoint stays intact - -[blurb [$theme/note.gif] [*What if my template accepts more than one type?] -[br][br] -When you want to instantiate a template with more than one type, you can pass -either a string with the types separated by whitespace, or a list of strings -'''("int double" or ["int", "double"]''' would both work). -] - -[page:1 Wrappers] - -Suppose you have this function: - - std::vector names(); - -But you don't want to [@../../doc/v2/faq.html#question2 to export std::vector], -you want this function to return a python list of strings. Boost.Python has -excellent support for things like that: - - list names_wrapper() - { - list result; - // call original function - vector v = names(); - // put all the strings inside the python list - vector::iterator it; - for (it = v.begin(); it != v.end(); ++it){ - result.append(*it); - } - return result; - } - - BOOST_PYTHON_MODULE(test) - { - def("names", &names_wrapper); - } - -Nice heh? Pyste supports this mechanism too. You declare the [^names_wrapper] -function in a header named "[^test_wrappers.h]" and in the interface file: - - Include("test_wrappers.h") - names = Function("names", "test.h") - set_wrapper(names, "names_wrapper") - -You can optionally declare the function in the interface file itself: - - names_wrapper = Wrapper("names_wrapper", - """ - list names_wrapper() - { - // code to call name() and convert the vector to a list... - } - """) - names = Function("names", "test.h") - set_wrapper(names, names_wrapper) - -The same mechanism can be used with member functions too. Just remember that -the first parameter of wrappers for member functions is a pointer to the -class, as in: - - struct C - { - std::vector names(); - } - - list names_wrapper(C* c) - { - // same as before, calling c->names() and converting result to a list - } - -And then in the interface file: - - C = Class("C", "test.h") - set_wrapper(C.names, "names_wrapper") - -[blurb -[$theme/note.gif]Even though Boost.Python accepts either a pointer or a -reference to the class in wrappers for member functions as the first parameter, -Pyste expects them to be a [*pointer]. Doing otherwise will prevent your -code to compile when you set a wrapper for a virtual member function. -] - -[page:1 Exporting An Entire Header] - -Pyste also supports a mechanism to export all declarations found in a header -file. Suppose again our file, [^hello.h]: - - struct World - { - World(std::string msg): msg(msg) {} - void set(std::string msg) { this->msg = msg; } - std::string greet() { return msg; } - std::string msg; - }; - - enum choice { red, blue }; - - void show(choice c) { std::cout << "value: " << (int)c << std::endl; } - -You can just use the [^AllFromHeader] construct: - - hello = AllFromHeader("hello.h") - -this will export all the declarations found in [^hello.h], which is equivalent -to write: - - Class("World", "hello.h") - Enum("choice", "hello.h") - Function("show", "hello.h") - -Note that you can still use the functions [^rename], [^set_policy], [^exclude], etc. Just access -the members of the header object like this: - - rename(hello.World.greet, "Greet") - exclude(hello.World.set, "Set") - -[blurb -[$theme/note.gif] [*AllFromHeader is broken] in some cases. Until it is fixed, -use at you own risk. -] - - -[page:1 Smart Pointers] - -Pyste for now has manual support for smart pointers. Suppose: - - struct C - { - int value; - }; - - boost::shared_ptr newC(int value) - { - boost::shared_ptr c( new C() ); - c->value = value; - return c; - } - - void printC(boost::shared_ptr c) - { - std::cout << c->value << std::endl; - } - -To make [^newC] and [^printC] work correctly, you have to tell Pyste that a -convertor for [^boost::shared_ptr] is needed. - - C = Class('C', 'C.h') - use_shared_ptr(C) - Function('newC', 'C.h') - Function('printC', 'C.h') - -For [^std::auto_ptr]'s, use the function [^use_auto_ptr]. - -This system is temporary, and in the future the converters will automatically be -exported if needed, without the need to tell Pyste about them explicitly. - -[h2 Holders] - -If only the converter for the smart pointers is not enough and you need to -specify the smart pointer as the holder for a class, use the functions -[^hold_with_shared_ptr] and [^hold_with_auto_ptr]: - - C = Class('C', 'C.h') - hold_with_shared_ptr(C) - Function('newC', 'C.h') - Function('printC', 'C.h') - -[page:1 Global Variables] - -To export global variables, use the [^Var] construct: - - Var("myglobal", "foo.h") - -Beware of non-const global variables: changes in Python won't reflect in C++! -If you really must change them in Python, you will have to write some accessor -functions, and export those. - - -[page:1 Adding New Methods] - -Suppose that you want to add a function to a class, turning it into a member -function: - - struct World - { - void set(std::string msg) { this->msg = msg; } - std::string msg; - }; - - std::string greet(World& w) - { - return w.msg; - } - -Here, we want to make [^greet] work as a member function of the class [^World]. We do -that using the [^add_method] construct: - - W = Class("World", "hello.h") - add_method(W, "greet") - -Notice also that then you can rename it, set its policy, just like a regular -member function: - - rename(W.greet, 'Greet') - -Now from Python: - - >>> import hello - >>> w = hello.World() - >>> w.set('Ni') - >>> w.greet() - 'Ni' - >>> print 'Oh no! The knights who say Ni!' - Oh no! The knights who say Ni! - - -[page:1 Inserting Code] - -You can insert arbitrary code in the generated cpps, just use the functions -[^declaration_code] and [^module_code]. This will insert the given string in the -respective sections. Example: - - # file A.pyste - Class("A", "A.h") - declaration_code("/* declaration_code() comes here */\n") - module_code("/* module_code() comes here */\n") - -Will generate: - - // Includes ==================================================================== - #include - - // Using ======================================================================= - using namespace boost::python; - - // Declarations ================================================================ - - /* declaration_code() comes here */ - - // Module ====================================================================== - BOOST_PYTHON_MODULE(A) - { - class_< A >("A", init< >()) - .def(init< const A& >()) - ; - - /* module_code() comes here */ - } diff --git a/pyste/doc/renaming_and_excluding.html b/pyste/doc/renaming_and_excluding.html deleted file mode 100644 index ce6654c4..00000000 --- a/pyste/doc/renaming_and_excluding.html +++ /dev/null @@ -1,87 +0,0 @@ - - - -Renaming and Excluding - - - - - - - - - - -
- - Renaming and Excluding -
-
- - - - - - -
-

-You can easily rename functions, classes, member functions, attributes, etc. Just use the -function rename, like this:

-
-    World = Class("World", "hello.h")
-    rename(World, "IWorld")
-    show = Function("choice", "hello.h")
-    rename(show, "Show")
-
-

-You can rename member functions and attributes using this syntax:

-
-    rename(World.greet, "Greet")
-    rename(World.set, "Set")
-    choice = Enum("choice", "hello.h")
-    rename(choice.red, "Red")
-    rename(choice.blue, "Blue")
-
-

-You can exclude functions, classes, member functions, attributes, etc, in the same way, -with the function exclude:

-
-    exclude(World.greet)
-    exclude(World.msg)
-
-

-To access the operators of a class, access the member operator like this -(supposing that C is a class being exported):

-
-    exclude(C.operator['+'])
-    exclude(C.operator['*'])
-    exclude(C.operator['<<'])
-
-

-The string inside the brackets is the same as the name of the operator in C++.

-

Virtual Member Functions

-Pyste automatically generates wrappers for virtual member functions, but you may -want to disable this behaviour (for performance reasons, for instance) if you do -not plan to override the functions in Python. To do this, use the function -final:

-
-    C = Class('C', 'C.h')
-    final(C.foo) ##C::foo is a virtual member function
-
-

-No virtual wrapper code will be generated for the virtual member function -C::foo that way.

- - - - - - -
-
-
- - diff --git a/pyste/doc/running_pyste.html b/pyste/doc/running_pyste.html deleted file mode 100644 index 9bd9a3ae..00000000 --- a/pyste/doc/running_pyste.html +++ /dev/null @@ -1,200 +0,0 @@ - - - -Running Pyste - - - - - - - - - - -
- - Running Pyste -
-
- - - - - - -
-

-To run Pyste, you will need:

-

-Installation for the tools is available in their respective webpages.

- - - - -
- - -GCCXML must be accessible in the PATH environment variable, so -that Pyste can call it. How to do this varies from platform to platform. -
-

Ok, now what?

-Well, now let's fire it up:

-
-
->python pyste.py
-
-Pyste version 0.9.26
-
-Usage:
-    pyste [options] interface-files
-
-where options are:
-    --module=<name>         The name of the module that will be generated;
-                            defaults to the first interface filename, without
-                            the extension.
-    -I <path>               Add an include path
-    -D <symbol>             Define symbol
-    --multiple              Create various cpps, instead of only one
-                            (useful during development)
-    --out=<name>            Specify output filename (default: <module>.cpp)
-                            in --multiple mode, this will be a directory
-    --no-using              Do not declare "using namespace boost";
-                            use explicit declarations instead
-    --pyste-ns=<name>       Set the namespace where new types will be declared;
-                            default is the empty namespace
-    --debug                 Writes the xml for each file parsed in the current
-                            directory
-    --cache-dir=<dir>       Directory for cache files (speeds up future runs)
-    --only-create-cache     Recreates all caches (doesn't generate code).
-    --generate-main         Generates the _main.cpp file (in multiple mode)
-    --file-list             A file with one pyste file per line. Use as a 
-                            substitute for passing the files in the command
-                            line.
-    -h, --help              Print this help and exit
-    -v, --version           Print version information
-  
-                        
-

-Options explained:

-

-The -I and -D are preprocessor flags, which are needed by -GCCXML to parse -the header files correctly and by Pyste to find the header files declared in the -interface files.

-

---out names the output file (default: <module>.cpp), or in multiple mode, -names a output directory for the files (default: <module>).

-

---no-using tells Pyste to don't declare "using namespace boost;" in the -generated cpp, using the namespace boost::python explicitly in all declarations. -Use only if you're having a name conflict in one of the files.

-

-Use --pyste-ns to change the namespace where new types are declared (for -instance, the virtual wrappers). Use only if you are having any problems. By -default, Pyste uses the empty namespace.

-

---debug will write in the current directory a xml file as outputted by -GCCXML -for each header parsed. Useful for bug reports.

-

---file-list names a file where each line points to a Pyste file. Use this instead -to pass the pyste files if you have a lot of them and your shell has some command line -size limit.

-

-The other options are explained below, in -Multiple Mode and - -Cache.

-

--h, --help, -v, --version are self-explaining, I believe. ;)

-

-So, the usage is simple enough:

-
>python pyste.py --module=mymodule file.pyste file2.pyste ...

-will generate a file mymodule.cpp in the same dir where the command was -executed. Now you can compile the file using the same instructions of the - -tutorial.

-

Wait... how do I set those I and D flags?

-Don't worry: normally -GCCXML is already configured correctly for your plataform, -so the search path to the standard libraries and the standard defines should -already be set. You only have to set the paths to other libraries that your code -needs, like Boost, for example.

-

-Plus, Pyste automatically uses the contents of the environment variable -INCLUDE if it exists. Visual C++ users should run the Vcvars32.bat file, -which for Visual C++ 6 is normally located at:

-
-    C:\Program Files\Microsoft Visual Studio\VC98\bin\Vcvars32.bat
-
-

-with that, you should have little trouble setting up the flags.

- - - - -
-A note about Psyco

-Although you don't have to install -Psyco to -use Pyste, if you do, Pyste will make use of it to speed up the wrapper -generation. Speed ups of 30% can be achieved, so it's highly recommended. -
-

Multiple Mode

-The multiple mode is useful in large projects, where the presence of multiple -classes in a single file makes the compilation unpractical (excessive memory -usage, mostly).

-

-The solution is make Pyste generate multiple files, more specifically one cpp -file for each Pyste file. This files will contain a function named after the -file, for instance Export_MyPysteFile, which will contain all the code to export -the classes, enums, etc. You can pass as much files as you want this way:

-
>python pyste.py --module=mymodule file1.pyste file2.pyste

-This will create the files mymodule/file1.cpp and mymodule/file2.cpp. You -can then later do:

-
>python pyste.py --module=mymodule file3.pyste

-and mymodule/file3.cpp will be generated.

-

-But compiling and linking this files won't be sufficient to generate your -extension. You have to also generate a file named main.cpp; call pyste with -all the Pyste files of your extension, and use the --generate-main option:

-
>python pyste.py --module=mymodule --generate-main file1.pyste file2.pyste file3.pyste

-Now compile and link all this files together and your extension is ready for -use.

-

Cache

-Pyste now supports a form of cache, which is a way to speed up the code -generation. Most of the time that Pyste takes to generate the code comes from -having to execute -GCCXML (since being a front-end to GCC, it has to compile the -header files) and reading back the XML generated.

-

-When you use the --cache-dir=<dir> option, Pyste will dump in the specified -directory the generated XMLs to a file named after the Pyste file, with the -extension .pystec. The next time you run with this option, Pyste will use -the cache, instead of calling -GCCXML again:

-
>python pyste.py --module=mymodule --cache-dir=cache file1.pyste

-Will generate file1.cpp and cache/file1.pystec. Next time you execute -this command, the cache file will be used. Note that Pyste doesn't do any check -to ensure that the cache is up to date, but you can configure your build system to do that for you.

-

-When you run Pyste with --only-create-cache, all the cache files will be -created again, but no code will be generated.

- - - - - - -
-
-
- - diff --git a/pyste/doc/smart_pointers.html b/pyste/doc/smart_pointers.html deleted file mode 100644 index cddc96f2..00000000 --- a/pyste/doc/smart_pointers.html +++ /dev/null @@ -1,84 +0,0 @@ - - - -Smart Pointers - - - - - - - - - - -
- - Smart Pointers -
-
- - - - - - -
-

-Pyste for now has manual support for smart pointers. Suppose:

-
-    struct C
-    {
-        int value;
-    };
-
-    boost::shared_ptr<C> newC(int value)
-    {
-        boost::shared_ptr<C> c( new C() );
-        c->value = value;
-        return c;
-    }
-
-    void printC(boost::shared_ptr<C> c)
-    {
-        std::cout << c->value << std::endl;
-    }
-
-

-To make newC and printC work correctly, you have to tell Pyste that a -convertor for boost::shared_ptr<C> is needed.

-
-    C = Class('C', 'C.h')
-    use_shared_ptr(C)
-    Function('newC', 'C.h')
-    Function('printC', 'C.h')
-
-

-For std::auto_ptr's, use the function use_auto_ptr.

-

-This system is temporary, and in the future the converters will automatically be -exported if needed, without the need to tell Pyste about them explicitly.

-

Holders

-If only the converter for the smart pointers is not enough and you need to -specify the smart pointer as the holder for a class, use the functions -hold_with_shared_ptr and hold_with_auto_ptr:

-
-    C = Class('C', 'C.h')
-    hold_with_shared_ptr(C)
-    Function('newC', 'C.h')
-    Function('printC', 'C.h') 
-
- - - - - - -
-
-
- - diff --git a/pyste/doc/templates.html b/pyste/doc/templates.html deleted file mode 100644 index a1c1cfef..00000000 --- a/pyste/doc/templates.html +++ /dev/null @@ -1,102 +0,0 @@ - - - -Templates - - - - - - - - - - -
- - Templates -
-
- - - - - - -
-

-Template classes can easily be exported too, but you can't export the template -itself... you have to export instantiations of it! So, if you want to export a -std::vector, you will have to export vectors of int, doubles, etc.

-

-Suppose we have this code:

-
-    template <class T>
-    struct Point
-    {
-        T x;
-        T y;
-    };
-
-

-And we want to export Points of int and double:

-
-    Point = Template("Point", "point.h")
-    Point("int")
-    Point("double")
-
-

-Pyste will assign default names for each instantiation. In this example, those -would be "Point_int" and "Point_double", but most of the time users will want to -rename the instantiations:

-
-    Point("int", "IPoint")         // renames the instantiation
-    double_inst = Point("double")  // another way to do the same
-    rename(double_inst, "DPoint")
-
-

-Note that you can rename, exclude, set policies, etc, in the Template object -like you would do with a Function or a Class. This changes affect all -future instantiations:

-
-    Point = Template("Point", "point.h")
-    Point("float", "FPoint")        // will have x and y as data members
-    rename(Point.x, "X")
-    rename(Point.y, "Y")
-    Point("int", "IPoint")          // will have X and Y as data members
-    Point("double", "DPoint")       // also will have X and Y as data member
-
-

-If you want to change a option of a particular instantiation, you can do so:

-
-    Point = Template("Point", "point.h")
-    Point("int", "IPoint")          
-    d_inst = Point("double", "DPoint")       
-    rename(d_inst.x, "X")           // only DPoint is affect by this renames,
-    rename(d_inst.y, "Y")           // IPoint stays intact
-
- - - - -
- What if my template accepts more than one type? -

-When you want to instantiate a template with more than one type, you can pass -either a string with the types separated by whitespace, or a list of strings -("int double" or ["int", "double"] would both work). -
- - - - - - -
-
-
- - diff --git a/pyste/doc/the_interface_files.html b/pyste/doc/the_interface_files.html deleted file mode 100644 index 9c020043..00000000 --- a/pyste/doc/the_interface_files.html +++ /dev/null @@ -1,102 +0,0 @@ - - - -The Interface Files - - - - - - - - - - -
- - The Interface Files -
-
- - - - - - -
-

-The interface files are the heart of Pyste. The user creates one or more -interface files declaring the classes and functions he wants to export, and then -invokes Pyste passing the interface files to it. Pyste then generates a single -cpp file with -Boost.Python code, with all the classes and functions exported.

-

-Besides declaring the classes and functions, the user has a number of other -options, like renaming e excluding classes and member functionis. Those are -explained later on.

-

Basics

-Suppose we have a class and some functions that we want to expose to Python -declared in the header hello.h:

-
-    struct World
-    {
-        World(std::string msg): msg(msg) {} 
-        void set(std::string msg) { this->msg = msg; }
-        std::string greet() { return msg; }
-        std::string msg;
-    };
-
-    enum choice { red, blue };
-    
-    namespace test {
-    
-    void show(choice c) { std::cout << "value: " << (int)c << std::endl; }
-    
-    }
-
-

-We create a file named hello.pyste and create instances of the classes -Function, Class and Enum:

-
-    Function("test::show", "hello.h")
-    Class("World", "hello.h")
-    Enum("choice", "hello.h")
-
-

-That will expose the class, the free function and the enum found in hello.h.

-

Inheritance

-Pyste automatically generates the correct code (specifying bases<> in the -class_ declaration) if the Class() function that exports the base classes -and their children are in the same Pyste file. If that's not the case, you have -to indicate that there's a relationship between the Pyste files using the -Import function specifying the other Pyste file.

-

-Suppose we have two classes, A and B, and A is a base class for B. We -create two Pyste files:

-

-A.pyste:

-
-    Class("A", "A.h")
-
-

-B.pyste:

-
-    Import("A.pyste")
-    Class("B", "B.h")
-
-

-Note that we specify that B needs to know about A to be properly exported.

- - - - - - -
-
-
- - diff --git a/pyste/doc/theme/alert.gif b/pyste/doc/theme/alert.gif deleted file mode 100644 index 270764cc..00000000 Binary files a/pyste/doc/theme/alert.gif and /dev/null differ diff --git a/pyste/doc/theme/arrow.gif b/pyste/doc/theme/arrow.gif deleted file mode 100644 index e33db0fb..00000000 Binary files a/pyste/doc/theme/arrow.gif and /dev/null differ diff --git a/pyste/doc/theme/bkd.gif b/pyste/doc/theme/bkd.gif deleted file mode 100644 index dcabcb80..00000000 Binary files a/pyste/doc/theme/bkd.gif and /dev/null differ diff --git a/pyste/doc/theme/bkd2.gif b/pyste/doc/theme/bkd2.gif deleted file mode 100644 index b03d9ba9..00000000 Binary files a/pyste/doc/theme/bkd2.gif and /dev/null differ diff --git a/pyste/doc/theme/bulb.gif b/pyste/doc/theme/bulb.gif deleted file mode 100644 index 74f3baac..00000000 Binary files a/pyste/doc/theme/bulb.gif and /dev/null differ diff --git a/pyste/doc/theme/bullet.gif b/pyste/doc/theme/bullet.gif deleted file mode 100644 index da787e2e..00000000 Binary files a/pyste/doc/theme/bullet.gif and /dev/null differ diff --git a/pyste/doc/theme/l_arr.gif b/pyste/doc/theme/l_arr.gif deleted file mode 100644 index 5b3cb1cb..00000000 Binary files a/pyste/doc/theme/l_arr.gif and /dev/null differ diff --git a/pyste/doc/theme/l_arr_disabled.gif b/pyste/doc/theme/l_arr_disabled.gif deleted file mode 100644 index ed58a605..00000000 Binary files a/pyste/doc/theme/l_arr_disabled.gif and /dev/null differ diff --git a/pyste/doc/theme/note.gif b/pyste/doc/theme/note.gif deleted file mode 100644 index bd92f075..00000000 Binary files a/pyste/doc/theme/note.gif and /dev/null differ diff --git a/pyste/doc/theme/r_arr.gif b/pyste/doc/theme/r_arr.gif deleted file mode 100644 index 2dcdad11..00000000 Binary files a/pyste/doc/theme/r_arr.gif and /dev/null differ diff --git a/pyste/doc/theme/r_arr_disabled.gif b/pyste/doc/theme/r_arr_disabled.gif deleted file mode 100644 index 2100f78b..00000000 Binary files a/pyste/doc/theme/r_arr_disabled.gif and /dev/null differ diff --git a/pyste/doc/theme/smiley.gif b/pyste/doc/theme/smiley.gif deleted file mode 100644 index 4c848f8f..00000000 Binary files a/pyste/doc/theme/smiley.gif and /dev/null differ diff --git a/pyste/doc/theme/style.css b/pyste/doc/theme/style.css deleted file mode 100644 index 643df02a..00000000 --- a/pyste/doc/theme/style.css +++ /dev/null @@ -1,178 +0,0 @@ -/*============================================================================= - Copyright (c) 2003 Bruno da Silva de Oliveira - - Use, modification and distribution is subject to the Boost Software - License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at - http://www.boost.org/LICENSE_1_0.txt) -=============================================================================*/ - -body -{ - background-image: url(bkd.gif); - background-color: #FFFFFF; - margin: 1em 2em 1em 2em; -} - -h1 { font-family: Verdana, Arial, Helvetica, sans-serif; font-weight: bold; text-align: left; } -h2 { font: 140% sans-serif; font-weight: bold; text-align: left; } -h3 { font: 120% sans-serif; font-weight: bold; text-align: left; } -h4 { font: bold 100% sans-serif; font-weight: bold; text-align: left; } -h5 { font: italic 100% sans-serif; font-weight: bold; text-align: left; } -h6 { font: small-caps 100% sans-serif; font-weight: bold; text-align: left; } - -pre -{ - border-top: gray 1pt solid; - border-right: gray 1pt solid; - border-left: gray 1pt solid; - border-bottom: gray 1pt solid; - - padding-top: 2pt; - padding-right: 2pt; - padding-left: 2pt; - padding-bottom: 2pt; - - display: block; - font-family: "courier new", courier, mono; - background-color: #eeeeee; font-size: small -} - -code -{ - font-family: "Courier New", Courier, mono; - font-size: small -} - -tt -{ - display: inline; - font-family: "Courier New", Courier, mono; - color: #000099; - font-size: small -} - -p -{ - text-align: justify; - font-family: Georgia, "Times New Roman", Times, serif -} - -ul -{ - list-style-image: url(bullet.gif); - font-family: Georgia, "Times New Roman", Times, serif -} - -ol -{ - font-family: Georgia, "Times New Roman", Times, serif -} - -a -{ - font-weight: bold; - color: #003366; - text-decoration: none; -} - -a:hover { color: #8080FF; } - -.literal { color: #666666; font-style: italic} -.keyword { color: #000099} -.identifier {} -.comment { font-style: italic; color: #990000} -.special { color: #800040} -.preprocessor { color: #FF0000} -.string { font-style: italic; color: #666666} -.copyright { color: #666666; font-size: small} -.white_bkd { background-color: #FFFFFF} -.dk_grey_bkd { background-color: #999999} -.quotes { color: #666666; font-style: italic; font-weight: bold} - -.note_box -{ - display: block; - - border-top: gray 1pt solid; - border-right: gray 1pt solid; - border-left: gray 1pt solid; - border-bottom: gray 1pt solid; - - padding-right: 12pt; - padding-left: 12pt; - padding-bottom: 12pt; - padding-top: 12pt; - - font-family: Arial, Helvetica, sans-serif; - background-color: #E2E9EF; - font-size: small; text-align: justify -} - -.table_title -{ - background-color: #648CCA; - - font-family: Verdana, Arial, Helvetica, sans-serif; color: #FFFFFF; - font-weight: bold -; padding-top: 4px; padding-right: 4px; padding-bottom: 4px; padding-left: 4px -} - -.table_cells -{ - background-color: #E2E9EF; - - font-family: Geneva, Arial, Helvetica, san-serif; - font-size: small -; padding-top: 4px; padding-right: 4px; padding-bottom: 4px; padding-left: 4px -} - -.toc -{ - DISPLAY: block; - background-color: #E2E9EF - font-family: Arial, Helvetica, sans-serif; - - border-top: gray 1pt solid; - border-left: gray 1pt solid; - border-bottom: gray 1pt solid; - border-right: gray 1pt solid; - - padding-top: 24pt; - padding-right: 24pt; - padding-left: 24pt; - padding-bottom: 24pt; -} - -.toc_title -{ - background-color: #648CCA; - padding-top: 4px; - padding-right: 4px; - padding-bottom: 4px; - padding-left: 4px; - font-family: Geneva, Arial, Helvetica, san-serif; - color: #FFFFFF; - font-weight: bold -} - -.toc_cells -{ - background-color: #E2E9EF; - padding-top: 4px; - padding-right: 4px; - padding-bottom: 4px; - padding-left: 4px; - font-family: Geneva, Arial, Helvetica, san-serif; - font-size: small -} - -div.logo -{ - float: right; -} - -.toc_cells_L0 { background-color: #E2E9EF; padding-top: 4px; padding-right: 4px; padding-bottom: 4px; padding-left: 4px; font-family: Geneva, Arial, Helvetica, san-serif; font-size: small } -.toc_cells_L1 { background-color: #E2E9EF; padding-top: 4px; padding-right: 4px; padding-bottom: 4px; padding-left: 44px; font-family: Geneva, Arial, Helvetica, san-serif; font-size: small } -.toc_cells_L2 { background-color: #E2E9EF; padding-top: 4px; padding-right: 4px; padding-bottom: 4px; padding-left: 88px; font-family: Geneva, Arial, Helvetica, san-serif; font-size: small } -.toc_cells_L3 { background-color: #E2E9EF; padding-top: 4px; padding-right: 4px; padding-bottom: 4px; padding-left: 122px; font-family: Geneva, Arial, Helvetica, san-serif; font-size: small } -.toc_cells_L4 { background-color: #E2E9EF; padding-top: 4px; padding-right: 4px; padding-bottom: 4px; padding-left: 166px; font-family: Geneva, Arial, Helvetica, san-serif; font-size: small } diff --git a/pyste/doc/theme/u_arr.gif b/pyste/doc/theme/u_arr.gif deleted file mode 100644 index ada3d6e0..00000000 Binary files a/pyste/doc/theme/u_arr.gif and /dev/null differ diff --git a/pyste/doc/wrappers.html b/pyste/doc/wrappers.html deleted file mode 100644 index 534ae552..00000000 --- a/pyste/doc/wrappers.html +++ /dev/null @@ -1,124 +0,0 @@ - - - -Wrappers - - - - - - - - - - -
- - Wrappers -
-
- - - - - - -
-

-Suppose you have this function:

-
-    std::vector<std::string> names();
-
-

-But you don't want to -to export std::vector<std::string>, -you want this function to return a python list of strings. -Boost.Python has -excellent support for things like that:

-
-    list names_wrapper()
-    {
-        list result;
-        // call original function
-        vector<string> v = names();
-        // put all the strings inside the python list
-        vector<string>::iterator it;
-        for (it = v.begin(); it != v.end(); ++it){
-            result.append(*it);    
-        }
-        return result;
-    }
-    
-    BOOST_PYTHON_MODULE(test)
-    {
-        def("names", &names_wrapper);
-    }
-
-

-Nice heh? Pyste supports this mechanism too. You declare the names_wrapper -function in a header named "test_wrappers.h" and in the interface file:

-
-    Include("test_wrappers.h")
-    names = Function("names", "test.h")
-    set_wrapper(names, "names_wrapper")
-
-

-You can optionally declare the function in the interface file itself:

-
-    names_wrapper = Wrapper("names_wrapper",
-    """
-    list names_wrapper()
-    {
-        // code to call name() and convert the vector to a list...
-    }
-    """)
-    names = Function("names", "test.h")
-    set_wrapper(names, names_wrapper)
-
-

-The same mechanism can be used with member functions too. Just remember that -the first parameter of wrappers for member functions is a pointer to the -class, as in:

-
-    struct C
-    {
-        std::vector<std::string> names();
-    }
-
-    list names_wrapper(C* c)
-    {
-        // same as before, calling c->names() and converting result to a list 
-    }
-
-

-And then in the interface file:

-
-    C = Class("C", "test.h")
-    set_wrapper(C.names, "names_wrapper")
-
- - - - -
- -Even though -Boost.Python accepts either a pointer or a -reference to the class in wrappers for member functions as the first parameter, -Pyste expects them to be a pointer. Doing otherwise will prevent your -code to compile when you set a wrapper for a virtual member function. -
- - - - - - -
-
-
- - diff --git a/pyste/index.html b/pyste/index.html deleted file mode 100644 index 953b37c1..00000000 --- a/pyste/index.html +++ /dev/null @@ -1,90 +0,0 @@ - - - -Pyste Documentation - - - - - - - - - -
- - Pyste Documentation -
-
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Table of contents
- Introduction -
- Running Pyste -
- The Interface Files -
- Renaming and Excluding -
- Policies -
- Templates -
- Wrappers -
- Exporting An Entire Header -
- Smart Pointers -
- Global Variables -
- Adding New Methods -
- Inserting Code -
-
-
- - diff --git a/pyste/install/pyste.py b/pyste/install/pyste.py deleted file mode 100644 index da926235..00000000 --- a/pyste/install/pyste.py +++ /dev/null @@ -1,8 +0,0 @@ -#!/usr/bin/env python - -# Copyright Bruno da Silva de Oliveira 2006. Distributed under the Boost -# Software License, Version 1.0. (See accompanying -# file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) - -from Pyste import pyste -pyste.main() diff --git a/pyste/install/setup.py b/pyste/install/setup.py deleted file mode 100644 index c1703981..00000000 --- a/pyste/install/setup.py +++ /dev/null @@ -1,20 +0,0 @@ -# Copyright Prabhu Ramachandran 2006. Distributed under the Boost -# Software License, Version 1.0. (See accompanying -# file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) - -from distutils.core import setup -import sys - -setup (name = "Pyste", - version = "0.9.10", - description = "Pyste - Python Semi-Automatic Exporter", - maintainer = "Bruno da Silva de Oliveira", - maintainer_email = "nicodemus@globalite.com.br", - licence = "Boost License", - long_description = "Pyste is a Boost.Python code generator", - url = "http://www.boost.org/libs/python/pyste/index.html", - platforms = ['Any'], - packages = ['Pyste'], - scripts = ['pyste.py'], - package_dir = {'Pyste': '../src/Pyste'}, - ) diff --git a/pyste/src/Pyste/ClassExporter.py b/pyste/src/Pyste/ClassExporter.py deleted file mode 100644 index decaf628..00000000 --- a/pyste/src/Pyste/ClassExporter.py +++ /dev/null @@ -1,918 +0,0 @@ -# Copyright Bruno da Silva de Oliveira 2003. Use, modification and -# distribution is subject to the Boost Software License, Version 1.0. -# (See accompanying file LICENSE_1_0.txt or copy at -# http://www.boost.org/LICENSE_1_0.txt) - -import exporters -from Exporter import Exporter -from declarations import * -from settings import * -from policies import * -from SingleCodeUnit import SingleCodeUnit -from EnumExporter import EnumExporter -from utils import makeid, enumerate -import copy -import exporterutils -import re - -#============================================================================== -# ClassExporter -#============================================================================== -class ClassExporter(Exporter): - 'Generates boost.python code to export a class declaration' - - def __init__(self, info, parser_tail=None): - Exporter.__init__(self, info, parser_tail) - # sections of code - self.sections = {} - # template: each item in the list is an item into the class_<...> - # section. - self.sections['template'] = [] - # constructor: each item in the list is a parameter to the class_ - # constructor, like class_(...) - self.sections['constructor'] = [] - # inside: everything within the class_<> statement - self.sections['inside'] = [] - # scope: items outside the class statement but within its scope. - # scope* s = new scope(class<>()); - # ... - # delete s; - self.sections['scope'] = [] - # declarations: outside the BOOST_PYTHON_MODULE macro - self.sections['declaration'] = [] - self.sections['declaration-outside'] = [] - self.sections['include'] = [] - # a list of Constructor instances - self.constructors = [] - # a list of code units, generated by nested declarations - self.nested_codeunits = [] - - - def ScopeName(self): - return makeid(self.class_.FullName()) + '_scope' - - - def Name(self): - return self.info.name - - - def SetDeclarations(self, declarations): - Exporter.SetDeclarations(self, declarations) - if self.declarations: - decl = self.GetDeclaration(self.info.name) - if isinstance(decl, Typedef): - self.class_ = self.GetDeclaration(decl.type.name) - if not self.info.rename: - self.info.rename = decl.name - else: - self.class_ = decl - self.class_ = copy.deepcopy(self.class_) - else: - self.class_ = None - - - def ClassBases(self): - all_bases = [] - for level in self.class_.hierarchy: - for base in level: - all_bases.append(base) - return [self.GetDeclaration(x.name) for x in all_bases] - - - def Order(self): - '''Return the TOTAL number of bases that this class has, including the - bases' bases. Do this because base classes must be instantialized - before the derived classes in the module definition. - ''' - num_bases = len(self.ClassBases()) - return num_bases, self.class_.FullName() - - - def Export(self, codeunit, exported_names): - self.InheritMethods(exported_names) - self.MakeNonVirtual() - if not self.info.exclude: - self.ExportBasics() - self.ExportBases(exported_names) - self.ExportConstructors() - self.ExportVariables() - self.ExportVirtualMethods(codeunit) - self.ExportMethods() - self.ExportOperators() - self.ExportNestedClasses(exported_names) - self.ExportNestedEnums(exported_names) - self.ExportSmartPointer() - self.ExportOpaquePointerPolicies() - self.ExportAddedCode() - self.Write(codeunit) - exported_names[self.Name()] = 1 - - - def InheritMethods(self, exported_names): - '''Go up in the class hierarchy looking for classes that were not - exported yet, and then add their public members to this classes - members, as if they were members of this class. This allows the user to - just export one type and automatically get all the members from the - base classes. - ''' - valid_members = (Method, ClassVariable, NestedClass, ClassEnumeration) - fullnames = [x.FullName() for x in self.class_] - pointers = [x.PointerDeclaration(True) for x in self.class_ if isinstance(x, Method)] - fullnames = dict([(x, None) for x in fullnames]) - pointers = dict([(x, None) for x in pointers]) - for level in self.class_.hierarchy: - level_exported = False - for base in level: - base = self.GetDeclaration(base.name) - if base.FullName() not in exported_names: - for member in base: - if type(member) in valid_members: - member_copy = copy.deepcopy(member) - member_copy.class_ = self.class_.FullName() - if isinstance(member_copy, Method): - pointer = member_copy.PointerDeclaration(True) - if pointer not in pointers: - self.class_.AddMember(member) - pointers[pointer] = None - elif member_copy.FullName() not in fullnames: - self.class_.AddMember(member) - else: - level_exported = True - if level_exported: - break - def IsValid(member): - return isinstance(member, valid_members) and member.visibility == Scope.public - self.public_members = [x for x in self.class_ if IsValid(x)] - - - def Write(self, codeunit): - indent = self.INDENT - boost_ns = namespaces.python - pyste_ns = namespaces.pyste - code = '' - # begin a scope for this class if needed - nested_codeunits = self.nested_codeunits - needs_scope = self.sections['scope'] or nested_codeunits - if needs_scope: - scope_name = self.ScopeName() - code += indent + boost_ns + 'scope* %s = new %sscope(\n' %\ - (scope_name, boost_ns) - # export the template section - template_params = ', '.join(self.sections['template']) - code += indent + boost_ns + 'class_< %s >' % template_params - # export the constructor section - constructor_params = ', '.join(self.sections['constructor']) - code += '(%s)\n' % constructor_params - # export the inside section - in_indent = indent*2 - for line in self.sections['inside']: - code += in_indent + line + '\n' - # write the scope section and end it - if not needs_scope: - code += indent + ';\n' - else: - code += indent + ');\n' - for line in self.sections['scope']: - code += indent + line + '\n' - # write the contents of the nested classes - for nested_unit in nested_codeunits: - code += '\n' + nested_unit.Section('module') - # close the scope - code += indent + 'delete %s;\n' % scope_name - - # write the code to the module section in the codeunit - codeunit.Write('module', code + '\n') - - # write the declarations to the codeunit - declarations = '\n'.join(self.sections['declaration']) - for nested_unit in nested_codeunits: - declarations += nested_unit.Section('declaration') - if declarations: - codeunit.Write('declaration', declarations + '\n') - declarations_outside = '\n'.join(self.sections['declaration-outside']) - if declarations_outside: - codeunit.Write('declaration-outside', declarations_outside + '\n') - - # write the includes to the codeunit - includes = '\n'.join(self.sections['include']) - for nested_unit in nested_codeunits: - includes += nested_unit.Section('include') - if includes: - codeunit.Write('include', includes) - - - def Add(self, section, item): - 'Add the item into the corresponding section' - self.sections[section].append(item) - - - def ExportBasics(self): - '''Export the name of the class and its class_ statement.''' - class_name = self.class_.FullName() - self.Add('template', class_name) - name = self.info.rename or self.class_.name - self.Add('constructor', '"%s"' % name) - - - def ExportBases(self, exported_names): - 'Expose the bases of the class into the template section' - hierarchy = self.class_.hierarchy - exported = [] - for level in hierarchy: - for base in level: - if base.visibility == Scope.public and base.name in exported_names: - exported.append(base.name) - if exported: - break - if exported: - code = namespaces.python + 'bases< %s > ' % (', '.join(exported)) - self.Add('template', code) - - - def ExportConstructors(self): - '''Exports all the public contructors of the class, plus indicates if the - class is noncopyable. - ''' - py_ns = namespaces.python - indent = self.INDENT - - def init_code(cons): - 'return the init<>() code for the given contructor' - param_list = [p.FullName() for p in cons.parameters] - min_params_list = param_list[:cons.minArgs] - max_params_list = param_list[cons.minArgs:] - min_params = ', '.join(min_params_list) - max_params = ', '.join(max_params_list) - init = py_ns + 'init< ' - init += min_params - if max_params: - if min_params: - init += ', ' - init += py_ns + ('optional< %s >' % max_params) - init += ' >()' - return init - - constructors = [x for x in self.public_members if isinstance(x, Constructor)] - # don't export copy constructors if the class is abstract - # we could remove all constructors, but this will have the effect of - # inserting no_init in the declaration, which would not allow - # even subclasses to be instantiated. - self.constructors = constructors[:] - if self.class_.abstract: - for cons in constructors: - if cons.IsCopy(): - constructors.remove(cons) - break - - if not constructors: - # declare no_init - self.Add('constructor', py_ns + 'no_init') - else: - # write the constructor with less parameters to the constructor section - smaller = None - for cons in constructors: - if smaller is None or len(cons.parameters) < len(smaller.parameters): - smaller = cons - assert smaller is not None - self.Add('constructor', init_code(smaller)) - constructors.remove(smaller) - # write the rest to the inside section, using def() - for cons in constructors: - code = '.def(%s)' % init_code(cons) - self.Add('inside', code) - - # check if the class is copyable - if not self.class_.HasCopyConstructor() or self.class_.abstract: - self.Add('template', namespaces.boost + 'noncopyable') - - - def ExportVariables(self): - 'Export the variables of the class, both static and simple variables' - vars = [x for x in self.public_members if isinstance(x, Variable)] - for var in vars: - if self.info[var.name].exclude: - continue - name = self.info[var.name].rename or var.name - fullname = var.FullName() - if var.type.const: - def_ = '.def_readonly' - else: - def_ = '.def_readwrite' - code = '%s("%s", &%s)' % (def_, name, fullname) - self.Add('inside', code) - - - def OverloadName(self, method): - 'Returns the name of the overloads struct for the given method' - name = makeid(method.FullName()) - overloads = '_overloads_%i_%i' % (method.minArgs, method.maxArgs) - return name + overloads - - - def GetAddedMethods(self): - added_methods = self.info.__added__ - result = [] - if added_methods: - for name, rename in added_methods: - decl = self.GetDeclaration(name) - self.info[name].rename = rename - result.append(decl) - return result - - - def ExportMethods(self): - '''Export all the non-virtual methods of this class, plus any function - that is to be exported as a method''' - - declared = {} - def DeclareOverloads(m): - 'Declares the macro for the generation of the overloads' - if (isinstance(m, Method) and m.static) or type(m) == Function: - func = m.FullName() - macro = 'BOOST_PYTHON_FUNCTION_OVERLOADS' - else: - func = m.name - macro = 'BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS' - code = '%s(%s, %s, %i, %i)\n' % (macro, self.OverloadName(m), func, m.minArgs, m.maxArgs) - if code not in declared: - declared[code] = True - self.Add('declaration', code) - - - def Pointer(m): - 'returns the correct pointer declaration for the method m' - # check if this method has a wrapper set for him - wrapper = self.info[m.name].wrapper - if wrapper: - return '&' + wrapper.FullName() - else: - return m.PointerDeclaration() - - def IsExportable(m): - 'Returns true if the given method is exportable by this routine' - ignore = (Constructor, ClassOperator, Destructor) - return isinstance(m, Function) and not isinstance(m, ignore) and not m.virtual - - methods = [x for x in self.public_members if IsExportable(x)] - methods.extend(self.GetAddedMethods()) - - staticmethods = {} - - for method in methods: - method_info = self.info[method.name] - - # skip this method if it was excluded by the user - if method_info.exclude: - continue - - # rename the method if the user requested - name = method_info.rename or method.name - - # warn the user if this method needs a policy and doesn't have one - method_info.policy = exporterutils.HandlePolicy(method, method_info.policy) - - # check for policies - policy = method_info.policy or '' - if policy: - policy = ', %s%s()' % (namespaces.python, policy.Code()) - # check for overloads - overload = '' - if method.minArgs != method.maxArgs and not method_info.wrapper: - # add the overloads for this method - DeclareOverloads(method) - overload_name = self.OverloadName(method) - overload = ', %s%s()' % (namespaces.pyste, overload_name) - - # build the .def string to export the method - pointer = Pointer(method) - code = '.def("%s", %s' % (name, pointer) - code += policy - code += overload - code += ')' - self.Add('inside', code) - # static method - if isinstance(method, Method) and method.static: - staticmethods[name] = 1 - # add wrapper code if this method has one - wrapper = method_info.wrapper - if wrapper and wrapper.code: - self.Add('declaration', wrapper.code) - - # export staticmethod statements - for name in staticmethods: - code = '.staticmethod("%s")' % name - self.Add('inside', code) - - - - def MakeNonVirtual(self): - '''Make all methods that the user indicated to no_override no more virtual, delegating their - export to the ExportMethods routine''' - for member in self.class_: - if type(member) == Method and member.virtual: - member.virtual = not self.info[member.name].no_override - - - def ExportVirtualMethods(self, codeunit): - # check if this class has any virtual methods - has_virtual_methods = False - for member in self.class_: - if type(member) == Method and member.virtual: - has_virtual_methods = True - break - - holder = self.info.holder - if has_virtual_methods: - generator = _VirtualWrapperGenerator(self.class_, self.ClassBases(), self.info, codeunit) - if holder: - self.Add('template', holder(generator.FullName())) - else: - self.Add('template', generator.FullName()) - for definition in generator.GenerateDefinitions(): - self.Add('inside', definition) - self.Add('declaration', generator.GenerateVirtualWrapper(self.INDENT)) - else: - if holder: - self.Add('template', holder(self.class_.FullName())) - - # operators natively supported by boost - BOOST_SUPPORTED_OPERATORS = '+ - * / % ^ & ! ~ | < > == != <= >= << >> && || += -= '\ - '*= /= %= ^= &= |= <<= >>='.split() - # create a map for faster lookup - BOOST_SUPPORTED_OPERATORS = dict(zip(BOOST_SUPPORTED_OPERATORS, range(len(BOOST_SUPPORTED_OPERATORS)))) - - # a dict of operators that are not directly supported by boost, but can be exposed - # simply as a function with a special name - BOOST_RENAME_OPERATORS = { - '()' : '__call__', - } - - # converters which have a special name in python - # it's a map of a regular expression of the converter's result to the - # appropriate python name - SPECIAL_CONVERTERS = { - re.compile(r'(const)?\s*double$') : '__float__', - re.compile(r'(const)?\s*float$') : '__float__', - re.compile(r'(const)?\s*int$') : '__int__', - re.compile(r'(const)?\s*long$') : '__long__', - re.compile(r'(const)?\s*char\s*\*?$') : '__str__', - re.compile(r'(const)?.*::basic_string<.*>\s*(\*|\&)?$') : '__str__', - } - - - def ExportOperators(self): - 'Export all member operators and free operators related to this class' - - def GetFreeOperators(): - 'Get all the free (global) operators related to this class' - operators = [] - for decl in self.declarations: - if isinstance(decl, Operator): - # check if one of the params is this class - for param in decl.parameters: - if param.name == self.class_.FullName(): - operators.append(decl) - break - return operators - - def GetOperand(param): - 'Returns the operand of this parameter (either "self", or "other")' - if param.name == self.class_.FullName(): - return namespaces.python + 'self' - else: - return namespaces.python + ('other< %s >()' % param.name) - - - def HandleSpecialOperator(operator): - # gatter information about the operator and its parameters - result_name = operator.result.name - param1_name = '' - if operator.parameters: - param1_name = operator.parameters[0].name - - # check for str - ostream = 'basic_ostream' - is_str = result_name.find(ostream) != -1 and param1_name.find(ostream) != -1 - if is_str: - namespace = namespaces.python + 'self_ns::' - self_ = namespaces.python + 'self' - return '.def(%sstr(%s))' % (namespace, self_) - - # is not a special operator - return None - - - - frees = GetFreeOperators() - members = [x for x in self.public_members if type(x) == ClassOperator] - all_operators = frees + members - operators = [x for x in all_operators if not self.info['operator'][x.name].exclude] - - for operator in operators: - # gatter information about the operator, for use later - wrapper = self.info['operator'][operator.name].wrapper - if wrapper: - pointer = '&' + wrapper.FullName() - if wrapper.code: - self.Add('declaration-outside', wrapper.code) - else: - pointer = operator.PointerDeclaration() - rename = self.info['operator'][operator.name].rename - - # check if this operator will be exported as a method - export_as_method = wrapper or rename or operator.name in self.BOOST_RENAME_OPERATORS - - # check if this operator has a special representation in boost - special_code = HandleSpecialOperator(operator) - has_special_representation = special_code is not None - - if export_as_method: - # export this operator as a normal method, renaming or using the given wrapper - if not rename: - if wrapper: - rename = wrapper.name - else: - rename = self.BOOST_RENAME_OPERATORS[operator.name] - policy = '' - policy_obj = self.info['operator'][operator.name].policy - if policy_obj: - policy = ', %s()' % policy_obj.Code() - self.Add('inside', '.def("%s", %s%s)' % (rename, pointer, policy)) - - elif has_special_representation: - self.Add('inside', special_code) - - elif operator.name in self.BOOST_SUPPORTED_OPERATORS: - # export this operator using boost's facilities - op = operator - is_unary = isinstance(op, Operator) and len(op.parameters) == 1 or\ - isinstance(op, ClassOperator) and len(op.parameters) == 0 - if is_unary: - self.Add('inside', '.def( %s%sself )' % \ - (operator.name, namespaces.python)) - else: - # binary operator - if len(operator.parameters) == 2: - left_operand = GetOperand(operator.parameters[0]) - right_operand = GetOperand(operator.parameters[1]) - else: - left_operand = namespaces.python + 'self' - right_operand = GetOperand(operator.parameters[0]) - self.Add('inside', '.def( %s %s %s )' % \ - (left_operand, operator.name, right_operand)) - - # export the converters. - # export them as simple functions with a pre-determined name - - converters = [x for x in self.public_members if type(x) == ConverterOperator] - - def ConverterMethodName(converter): - result_fullname = converter.result.FullName() - result_name = converter.result.name - for regex, method_name in self.SPECIAL_CONVERTERS.items(): - if regex.match(result_fullname): - return method_name - else: - # extract the last name from the full name - result_name = makeid(result_name) - return 'to_' + result_name - - for converter in converters: - info = self.info['operator'][converter.result.FullName()] - # check if this operator should be excluded - if info.exclude: - continue - - special_code = HandleSpecialOperator(converter) - if info.rename or not special_code: - # export as method - name = info.rename or ConverterMethodName(converter) - pointer = converter.PointerDeclaration() - policy_code = '' - if info.policy: - policy_code = ', %s()' % info.policy.Code() - self.Add('inside', '.def("%s", %s%s)' % (name, pointer, policy_code)) - - elif special_code: - self.Add('inside', special_code) - - - - def ExportNestedClasses(self, exported_names): - nested_classes = [x for x in self.public_members if isinstance(x, NestedClass)] - for nested_class in nested_classes: - nested_info = self.info[nested_class.name] - nested_info.include = self.info.include - nested_info.name = nested_class.FullName() - exporter = self.__class__(nested_info) - exporter.SetDeclarations(self.declarations) - codeunit = SingleCodeUnit(None, None) - exporter.Export(codeunit, exported_names) - self.nested_codeunits.append(codeunit) - - - def ExportNestedEnums(self, exported_names): - nested_enums = [x for x in self.public_members if isinstance(x, ClassEnumeration)] - for enum in nested_enums: - enum_info = self.info[enum.name] - enum_info.include = self.info.include - enum_info.name = enum.FullName() - exporter = EnumExporter(enum_info) - exporter.SetDeclarations(self.declarations) - codeunit = SingleCodeUnit(None, None) - exporter.Export(codeunit, exported_names) - self.nested_codeunits.append(codeunit) - - - def ExportSmartPointer(self): - smart_ptr = self.info.smart_ptr - if smart_ptr: - class_name = self.class_.FullName() - smart_ptr = smart_ptr % class_name - self.Add('scope', '%sregister_ptr_to_python< %s >();' % (namespaces.python, smart_ptr)) - - - def ExportOpaquePointerPolicies(self): - # check all methods for 'return_opaque_pointer' policies - methods = [x for x in self.public_members if isinstance(x, Method)] - for method in methods: - return_opaque_policy = return_value_policy(return_opaque_pointer) - if self.info[method.name].policy == return_opaque_policy: - macro = exporterutils.EspecializeTypeID(method.result.name) - if macro: - self.Add('declaration-outside', macro) - - def ExportAddedCode(self): - if self.info.__code__: - for code in self.info.__code__: - self.Add('inside', code) - - -#============================================================================== -# Virtual Wrapper utils -#============================================================================== - -def _ParamsInfo(m, count=None): - if count is None: - count = len(m.parameters) - param_names = ['p%i' % i for i in range(count)] - param_types = [x.FullName() for x in m.parameters[:count]] - params = ['%s %s' % (t, n) for t, n in zip(param_types, param_names)] - #for i, p in enumerate(m.parameters[:count]): - # if p.default is not None: - # #params[i] += '=%s' % p.default - # params[i] += '=%s' % (p.name + '()') - params = ', '.join(params) - return params, param_names, param_types - - -class _VirtualWrapperGenerator(object): - 'Generates code to export the virtual methods of the given class' - - def __init__(self, class_, bases, info, codeunit): - self.class_ = copy.deepcopy(class_) - self.bases = bases[:] - self.info = info - self.wrapper_name = makeid(class_.FullName()) + '_Wrapper' - self.virtual_methods = None - self._method_count = {} - self.codeunit = codeunit - self.GenerateVirtualMethods() - - - SELF = 'py_self' - - - def DefaultImplementationNames(self, method): - '''Returns a list of default implementations for this method, one for each - number of default arguments. Always returns at least one name, and return from - the one with most arguments to the one with the least. - ''' - base_name = 'default_' + method.name - minArgs = method.minArgs - maxArgs = method.maxArgs - if minArgs == maxArgs: - return [base_name] - else: - return [base_name + ('_%i' % i) for i in range(minArgs, maxArgs+1)] - - - def Declaration(self, method, indent): - '''Returns a string with the declarations of the virtual wrapper and - its default implementations. This string must be put inside the Wrapper - body. - ''' - pyste = namespaces.pyste - python = namespaces.python - rename = self.info[method.name].rename or method.name - result = method.result.FullName() - return_str = 'return ' - if result == 'void': - return_str = '' - params, param_names, param_types = _ParamsInfo(method) - constantness = '' - if method.const: - constantness = ' const' - - # call_method callback - decl = indent + '%s %s(%s)%s%s {\n' % (result, method.name, params, constantness, method.Exceptions()) - param_names_str = ', '.join(param_names) - if param_names_str: - param_names_str = ', ' + param_names_str - - self_str = self.SELF - - decl += indent*2 + '%(return_str)s%(python)scall_method< %(result)s >' \ - '(%(self_str)s, "%(rename)s"%(param_names_str)s);\n' % locals() - decl += indent + '}\n' - - # default implementations (with overloading) - def DefaultImpl(method, param_names): - 'Return the body of a default implementation wrapper' - indent2 = indent * 2 - wrapper = self.info[method.name].wrapper - if not wrapper: - # return the default implementation of the class - return indent2 + '%s%s(%s);\n' % \ - (return_str, method.FullName(), ', '.join(param_names)) - else: - if wrapper.code: - self.codeunit.Write('declaration-outside', wrapper.code) - # return a call for the wrapper - params = ', '.join(['this'] + param_names) - return indent2 + '%s%s(%s);\n' % (return_str, wrapper.FullName(), params) - - if not method.abstract and method.visibility != Scope.private: - minArgs = method.minArgs - maxArgs = method.maxArgs - impl_names = self.DefaultImplementationNames(method) - for impl_name, argNum in zip(impl_names, range(minArgs, maxArgs+1)): - params, param_names, param_types = _ParamsInfo(method, argNum) - decl += '\n' - decl += indent + '%s %s(%s)%s {\n' % (result, impl_name, params, constantness) - decl += DefaultImpl(method, param_names) - decl += indent + '}\n' - return decl - - - def MethodDefinition(self, method): - '''Returns a list of lines, which should be put inside the class_ - statement to export this method.''' - # dont define abstract methods - pyste = namespaces.pyste - rename = self.info[method.name].rename or method.name - default_names = self.DefaultImplementationNames(method) - class_name = self.class_.FullName() - wrapper_name = pyste + self.wrapper_name - result = method.result.FullName() - is_method_unique = method.is_unique - constantness = '' - if method.const: - constantness = ' const' - - # create a list of default-impl pointers - minArgs = method.minArgs - maxArgs = method.maxArgs - if method.abstract: - default_pointers = [] - elif is_method_unique: - default_pointers = ['&%s::%s' % (wrapper_name, x) for x in default_names] - else: - default_pointers = [] - for impl_name, argNum in zip(default_names, range(minArgs, maxArgs+1)): - param_list = [x.FullName() for x in method.parameters[:argNum]] - params = ', '.join(param_list) - signature = '%s (%s::*)(%s)%s' % (result, wrapper_name, params, constantness) - default_pointer = '(%s)&%s::%s' % (signature, wrapper_name, impl_name) - default_pointers.append(default_pointer) - - # get the pointer of the method - pointer = method.PointerDeclaration() - if method.abstract: - pointer = namespaces.python + ('pure_virtual(%s)' % pointer) - - # warn the user if this method needs a policy and doesn't have one - method_info = self.info[method.name] - method_info.policy = exporterutils.HandlePolicy(method, method_info.policy) - - # Add policy to overloaded methods also - policy = method_info.policy or '' - if policy: - policy = ', %s%s()' % (namespaces.python, policy.Code()) - - # generate the defs - definitions = [] - # basic def - if default_pointers: - definitions.append('.def("%s", %s, %s%s)' % (rename, pointer, default_pointers[-1], policy)) - for default_pointer in default_pointers[:-1]: - definitions.append('.def("%s", %s%s)' % (rename, default_pointer, policy)) - else: - definitions.append('.def("%s", %s%s)' % (rename, pointer, policy)) - return definitions - - - def FullName(self): - return namespaces.pyste + self.wrapper_name - - - def GenerateVirtualMethods(self): - '''To correctly export all virtual methods, we must also make wrappers - for the virtual methods of the bases of this class, as if the methods - were from this class itself. - This method creates the instance variable self.virtual_methods. - ''' - def IsVirtual(m): - if type(m) is Method: - pure_virtual = m.abstract and m.virtual - virtual = m.virtual and m.visibility != Scope.private - return virtual or pure_virtual - else: - return False - - # extract the virtual methods, avoiding duplications. The duplication - # must take in account the full signature without the class name, so - # that inherited members are correctly excluded if the subclass overrides - # them. - def MethodSig(method): - if method.const: - const = ' const' - else: - const = '' - if method.result: - result = method.result.FullName() - else: - result = '' - params = ', '.join([x.FullName() for x in method.parameters]) - return '%s %s(%s)%s%s' % ( - result, method.name, params, const, method.Exceptions()) - - already_added = {} - self.virtual_methods = [] - for member in self.class_: - if IsVirtual(member): - already_added[MethodSig(member)] = None - self.virtual_methods.append(member) - - for base in self.bases: - base_methods = [copy.deepcopy(x) for x in base if IsVirtual(x)] - for base_method in base_methods: - self.class_.AddMember(base_method) - - all_methods = [x for x in self.class_ if IsVirtual(x)] - - for member in all_methods: - sig = MethodSig(member) - if IsVirtual(member) and not sig in already_added: - self.virtual_methods.append(member) - already_added[sig] = 0 - - - def Constructors(self): - return self.class_.Constructors(publics_only=True) - - - def GenerateDefinitions(self): - defs = [] - for method in self.virtual_methods: - exclude = self.info[method.name].exclude - # generate definitions only for public methods and non-abstract methods - if method.visibility == Scope.public and not exclude: - defs.extend(self.MethodDefinition(method)) - return defs - - - def GenerateVirtualWrapper(self, indent): - 'Return the wrapper for this class' - - # generate the class code - class_name = self.class_.FullName() - code = 'struct %s: %s\n' % (self.wrapper_name, class_name) - code += '{\n' - # generate constructors (with the overloads for each one) - for cons in self.Constructors(): # only public constructors - minArgs = cons.minArgs - maxArgs = cons.maxArgs - # from the min number of arguments to the max number, generate - # all version of the given constructor - cons_code = '' - for argNum in range(minArgs, maxArgs+1): - params, param_names, param_types = _ParamsInfo(cons, argNum) - if params: - params = ', ' + params - cons_code += indent + '%s(PyObject* %s_%s):\n' % \ - (self.wrapper_name, self.SELF, params) - cons_code += indent*2 + '%s(%s), %s(%s_) {}\n\n' % \ - (class_name, ', '.join(param_names), self.SELF, self.SELF) - code += cons_code - # generate the body - body = [] - for method in self.virtual_methods: - if not self.info[method.name].exclude: - body.append(self.Declaration(method, indent)) - body = '\n'.join(body) - code += body + '\n' - # add the self member - code += indent + 'PyObject* %s;\n' % self.SELF - code += '};\n' - return code diff --git a/pyste/src/Pyste/CodeExporter.py b/pyste/src/Pyste/CodeExporter.py deleted file mode 100644 index 382fffbd..00000000 --- a/pyste/src/Pyste/CodeExporter.py +++ /dev/null @@ -1,26 +0,0 @@ -# Copyright Bruno da Silva de Oliveira 2003. Use, modification and -# distribution is subject to the Boost Software License, Version 1.0. -# (See accompanying file LICENSE_1_0.txt or copy at -# http://www.boost.org/LICENSE_1_0.txt) - -from Exporter import Exporter - -#============================================================================== -# CodeExporter -#============================================================================== -class CodeExporter(Exporter): - - def __init__(self, info): - Exporter.__init__(self, info) - - - def Name(self): - return self.info.code - - - def Export(self, codeunit, exported_names): - codeunit.Write(self.info.section, self.info.code) - - - def WriteInclude(self, codeunit): - pass diff --git a/pyste/src/Pyste/CppParser.py b/pyste/src/Pyste/CppParser.py deleted file mode 100644 index be68a448..00000000 --- a/pyste/src/Pyste/CppParser.py +++ /dev/null @@ -1,247 +0,0 @@ -# Copyright Bruno da Silva de Oliveira 2003. Use, modification and -# distribution is subject to the Boost Software License, Version 1.0. -# (See accompanying file LICENSE_1_0.txt or copy at -# http://www.boost.org/LICENSE_1_0.txt) - -from GCCXMLParser import ParseDeclarations -import tempfile -import shutil -import os -import sys -import os.path -import settings -import shutil -import shelve -from cPickle import dump, load - -#============================================================================== -# exceptions -#============================================================================== -class CppParserError(Exception): pass - -#============================================================================== -# CppParser -#============================================================================== -class CppParser: - 'Parses a header file and returns a list of declarations' - - def __init__(self, includes=None, defines=None, cache_dir=None, version=None, gccxml_path = 'gccxml'): - 'includes and defines ar the directives given to gcc' - if includes is None: - includes = [] - if defines is None: - defines = [] - self.includes = includes - self.gccxml_path = gccxml_path - self.defines = defines - self.version = version - #if cache_dir is None: - # cache_dir = tempfile.mktemp() - # self.delete_cache = True - #else: - # self.delete_cache = False - self.delete_cache = False - self.cache_dir = cache_dir - self.cache_files = [] - self.mem_cache = {} - # create the cache dir - if cache_dir: - try: - os.makedirs(cache_dir) - except OSError: pass - - - def __del__(self): - self.Close() - - - def _IncludeParams(self, filename): - includes = self.includes[:] - filedir = os.path.dirname(filename) - if not filedir: - filedir = '.' - includes.insert(0, filedir) - includes = ['-I "%s"' % self.Unixfy(x) for x in includes] - return ' '.join(includes) - - - def _DefineParams(self): - defines = ['-D "%s"' % x for x in self.defines] - return ' '.join(defines) - - - def FindHeader(self, header): - if os.path.isfile(header): - return header - for path in self.includes: - filename = os.path.join(path, header) - if os.path.isfile(filename): - return filename - else: - name = os.path.basename(header) - raise RuntimeError, 'Header file "%s" not found!' % name - - - def AppendTail(self, filename, tail): - '''Creates a temporary file, appends the text tail to it, and returns - the filename of the file. - ''' - if hasattr(tempfile, 'mkstemp'): - f_no, temp = tempfile.mkstemp('.h') - f = file(temp, 'a') - os.close(f_no) - else: - temp = tempfile.mktemp('.h') - f = file(temp, 'a') - f.write('#include "%s"\n\n' % os.path.abspath(filename)) - f.write(tail) - f.write('\n') - f.close() - return temp - - - def Unixfy(self, path): - return path.replace('\\', '/') - - - def ParseWithGCCXML(self, header, tail): - '''Parses the given header using gccxml and GCCXMLParser. - ''' - header = self.FindHeader(header) - if tail: - filename = self.AppendTail(header, tail) - else: - filename = header - xmlfile = tempfile.mktemp('.xml') - try: - # get the params - includes = self._IncludeParams(filename) - defines = self._DefineParams() - # call gccxml - cmd = '%s %s %s "%s" -fxml=%s' - filename = self.Unixfy(filename) - xmlfile = self.Unixfy(xmlfile) - status = os.system(cmd % (self.gccxml_path, includes, defines, filename, xmlfile)) - if status != 0 or not os.path.isfile(xmlfile): - raise CppParserError, 'Error executing gccxml' - # parse the resulting xml - declarations = ParseDeclarations(xmlfile) - # make the declarations' location to point to the original file - if tail: - for decl in declarations: - decl_filename = os.path.normpath(os.path.normcase(decl.location[0])) - filename = os.path.normpath(os.path.normcase(filename)) - if decl_filename == filename: - decl.location = header, decl.location[1] - # return the declarations - return declarations - finally: - if settings.DEBUG and os.path.isfile(xmlfile): - debugname = os.path.basename(header) - debugname = os.path.splitext(debugname)[0] + '.xml' - print 'DEBUG:', debugname - shutil.copy(xmlfile, debugname) - # delete the temporary files - try: - os.remove(xmlfile) - if tail: - os.remove(filename) - except OSError: pass - - - def Parse(self, header, interface, tail=None): - '''Parses the given filename related to the given interface and returns - the (declarations, headerfile). The header returned is normally the - same as the given to this method (except that it is the full path), - except if tail is not None: in this case, the header is copied to a temp - filename and the tail code is appended to it before being passed on to - gccxml. This temp filename is then returned. - ''' - if tail is None: - tail = '' - tail = tail.strip() - declarations = self.GetCache(header, interface, tail) - if declarations is None: - declarations = self.ParseWithGCCXML(header, tail) - self.CreateCache(header, interface, tail, declarations) - header_fullpath = os.path.abspath(self.FindHeader(header)) - return declarations, header_fullpath - - - def CacheFileName(self, interface): - interface_name = os.path.basename(interface) - cache_file = os.path.splitext(interface_name)[0] + '.pystec' - cache_file = os.path.join(self.cache_dir, cache_file) - return cache_file - - - def GetCache(self, header, interface, tail): - key = (header, interface, tail) - # try memory cache first - if key in self.mem_cache: - return self.mem_cache[key] - - # get the cache from the disk - if self.cache_dir is None: - return None - header = self.FindHeader(header) - cache_file = self.CacheFileName(interface) - if os.path.isfile(cache_file): - f = file(cache_file, 'rb') - try: - version = load(f) - if version != self.version: - return None - cache = load(f) - if cache.has_key(key): - self.cache_files.append(cache_file) - return cache[key] - else: - return None - finally: - f.close() - else: - return None - - - def CreateCache(self, header, interface, tail, declarations): - key = (header, interface, tail) - - # our memory cache only holds one item - self.mem_cache.clear() - self.mem_cache[key] = declarations - - # save the cache in the disk - if self.cache_dir is None: - return - header = self.FindHeader(header) - cache_file = self.CacheFileName(interface) - if os.path.isfile(cache_file): - f = file(cache_file, 'rb') - try: - version = load(f) - cache = load(f) - finally: - f.close() - else: - cache = {} - cache[key] = declarations - self.cache_files.append(cache_file) - f = file(cache_file, 'wb') - try: - dump(self.version, f, 1) - dump(cache, f, 1) - finally: - f.close() - return cache_file - - - def Close(self): - if self.delete_cache and self.cache_files: - for filename in self.cache_files: - try: - os.remove(filename) - except OSError: - pass - self.cache_files = [] - shutil.rmtree(self.cache_dir) diff --git a/pyste/src/Pyste/EnumExporter.py b/pyste/src/Pyste/EnumExporter.py deleted file mode 100644 index 0107fbee..00000000 --- a/pyste/src/Pyste/EnumExporter.py +++ /dev/null @@ -1,58 +0,0 @@ -# Copyright Bruno da Silva de Oliveira 2003. Use, modification and -# distribution is subject to the Boost Software License, Version 1.0. -# (See accompanying file LICENSE_1_0.txt or copy at -# http://www.boost.org/LICENSE_1_0.txt) - -from Exporter import Exporter -from settings import * -import utils - -#============================================================================== -# EnumExporter -#============================================================================== -class EnumExporter(Exporter): - 'Exports enumerators' - - def __init__(self, info): - Exporter.__init__(self, info) - - - def SetDeclarations(self, declarations): - Exporter.SetDeclarations(self, declarations) - if self.declarations: - self.enum = self.GetDeclaration(self.info.name) - else: - self.enum = None - - def Export(self, codeunit, exported_names): - if self.info.exclude: - return - indent = self.INDENT - in_indent = self.INDENT*2 - rename = self.info.rename or self.enum.name - full_name = self.enum.FullName() - unnamed_enum = False - if rename.startswith('$_') or rename.startswith('._'): - unnamed_enum = True - code = '' - if not unnamed_enum: - code += indent + namespaces.python - code += 'enum_< %s >("%s")\n' % (full_name, rename) - for name in self.enum.values: - rename = self.info[name].rename or name - value_fullname = self.enum.ValueFullName(name) - if not unnamed_enum: - code += in_indent + '.value("%s", %s)\n' % (rename, value_fullname) - else: - code += indent + namespaces.python - code += 'scope().attr("%s") = (int)%s;\n' % (rename, value_fullname ) - if self.info.export_values and not unnamed_enum: - code += in_indent + '.export_values()\n' - if not unnamed_enum: - code += indent + ';\n' - code += '\n' - codeunit.Write('module', code) - exported_names[self.enum.FullName()] = 1 - - def Name(self): - return self.info.name diff --git a/pyste/src/Pyste/Exporter.py b/pyste/src/Pyste/Exporter.py deleted file mode 100644 index d87b37c5..00000000 --- a/pyste/src/Pyste/Exporter.py +++ /dev/null @@ -1,94 +0,0 @@ -# Copyright Bruno da Silva de Oliveira 2003. Use, modification and -# distribution is subject to the Boost Software License, Version 1.0. -# (See accompanying file LICENSE_1_0.txt or copy at -# http://www.boost.org/LICENSE_1_0.txt) - -import os.path - -#============================================================================== -# Exporter -#============================================================================== -class Exporter(object): - 'Base class for objects capable to generate boost.python code.' - - INDENT = ' ' * 4 - - def __init__(self, info, parser_tail=None): - self.info = info - self.parser_tail = parser_tail - self.interface_file = None - self.declarations = [] - - - def Name(self): - raise NotImplementedError(self.__class__.__name__) - - - def Tail(self): - return self.parser_tail - - - def Parse(self, parser): - self.parser = parser - header = self.info.include - tail = self.parser_tail - declarations, parser_header = parser.parse(header, tail) - self.parser_header = parser_header - self.SetDeclarations(declarations) - - - def SetParsedHeader(self, parsed_header): - self.parser_header = parsed_header - - - def SetDeclarations(self, declarations): - self.declarations = declarations - - - def GenerateCode(self, codeunit, exported_names): - self.WriteInclude(codeunit) - self.Export(codeunit, exported_names) - - - def WriteInclude(self, codeunit): - codeunit.Write('include', '#include <%s>\n' % self.info.include) - - - def Export(self, codeunit, exported_names): - 'subclasses must override this to do the real work' - pass - - - def GetDeclarations(self, fullname): - decls = [] - for decl in self.declarations: - if decl.FullName() == fullname: - decls.append(decl) - if not decls: - raise RuntimeError, 'no %s declaration found!' % fullname - return decls - - - def GetDeclaration(self, fullname): - decls = self.GetDeclarations(fullname) - #assert len(decls) == 1 - return decls[0] - - - def Order(self): - '''Returns a string that uniquely identifies this instance. All - exporters will be sorted by Order before being exported. - ''' - return 0, self.info.name - - - def Header(self): - return self.info.include - - - def __eq__(self, other): - return type(self) is type(other) and self.Name() == other.Name() \ - and self.interface_file == other.interface_file - - def __ne__(self, other): - return not self == other diff --git a/pyste/src/Pyste/FunctionExporter.py b/pyste/src/Pyste/FunctionExporter.py deleted file mode 100644 index 5765f65e..00000000 --- a/pyste/src/Pyste/FunctionExporter.py +++ /dev/null @@ -1,92 +0,0 @@ -# Copyright Bruno da Silva de Oliveira 2003. Use, modification and -# distribution is subject to the Boost Software License, Version 1.0. -# (See accompanying file LICENSE_1_0.txt or copy at -# http://www.boost.org/LICENSE_1_0.txt) - -from Exporter import Exporter -from policies import * -from declarations import * -from settings import * -import utils -import exporterutils - - -#============================================================================== -# FunctionExporter -#============================================================================== -class FunctionExporter(Exporter): - 'Generates boost.python code to export the given function.' - - def __init__(self, info, tail=None): - Exporter.__init__(self, info, tail) - - - def Export(self, codeunit, exported_names): - if not self.info.exclude: - decls = self.GetDeclarations(self.info.name) - for decl in decls: - self.info.policy = exporterutils.HandlePolicy(decl, self.info.policy) - self.ExportDeclaration(decl, len(decls) == 1, codeunit) - self.ExportOpaquePointer(decl, codeunit) - self.GenerateOverloads(decls, codeunit) - exported_names[self.Name()] = 1 - - - def ExportDeclaration(self, decl, unique, codeunit): - name = self.info.rename or decl.name - defs = namespaces.python + 'def("%s", ' % name - wrapper = self.info.wrapper - if wrapper: - pointer = '&' + wrapper.FullName() - else: - pointer = decl.PointerDeclaration() - defs += pointer - defs += self.PolicyCode() - overload = self.OverloadName(decl) - if overload: - defs += ', %s()' % (namespaces.pyste + overload) - defs += ');' - codeunit.Write('module', self.INDENT + defs + '\n') - # add the code of the wrapper - if wrapper and wrapper.code: - codeunit.Write('declaration', wrapper.code + '\n') - - - def OverloadName(self, decl): - if decl.minArgs != decl.maxArgs: - return '%s_overloads_%i_%i' % \ - (decl.name, decl.minArgs, decl.maxArgs) - else: - return '' - - - def GenerateOverloads(self, declarations, codeunit): - codes = {} - for decl in declarations: - overload = self.OverloadName(decl) - if overload and overload not in codes: - code = 'BOOST_PYTHON_FUNCTION_OVERLOADS(%s, %s, %i, %i)' %\ - (overload, decl.FullName(), decl.minArgs, decl.maxArgs) - codeunit.Write('declaration', code + '\n') - codes[overload] = None - - - def PolicyCode(self): - policy = self.info.policy - if policy is not None: - assert isinstance(policy, Policy) - return ', %s()' % policy.Code() - else: - return '' - - - def ExportOpaquePointer(self, function, codeunit): - if self.info.policy == return_value_policy(return_opaque_pointer): - typename = function.result.name - macro = exporterutils.EspecializeTypeID(typename) - if macro: - codeunit.Write('declaration-outside', macro) - - - def Name(self): - return self.info.name diff --git a/pyste/src/Pyste/GCCXMLParser.py b/pyste/src/Pyste/GCCXMLParser.py deleted file mode 100644 index 4a101720..00000000 --- a/pyste/src/Pyste/GCCXMLParser.py +++ /dev/null @@ -1,478 +0,0 @@ -# Copyright Bruno da Silva de Oliveira 2003. Use, modification and -# distribution is subject to the Boost Software License, Version 1.0. -# (See accompanying file LICENSE_1_0.txt or copy at -# http://www.boost.org/LICENSE_1_0.txt) - -from declarations import * -try: - # try to use internal elementtree - from xml.etree.cElementTree import ElementTree -except ImportError: - # try to use cElementTree if avaiable - try: - from cElementTree import ElementTree - except ImportError: - # fall back to the normal elementtree - from elementtree.ElementTree import ElementTree -from xml.parsers.expat import ExpatError -from copy import deepcopy -from utils import enumerate - - -#============================================================================== -# Exceptions -#============================================================================== -class InvalidXMLError(Exception): pass - -class ParserError(Exception): pass - -class InvalidContextError(ParserError): pass - - -#============================================================================== -# GCCXMLParser -#============================================================================== -class GCCXMLParser(object): - 'Parse a GCC_XML file and extract the top-level declarations.' - - interested_tags = {'Class':0, 'Function':0, 'Variable':0, 'Enumeration':0} - - def Parse(self, filename): - self.elements = self.GetElementsFromXML(filename) - # high level declarations - self.declarations = [] - self._names = {} - # parse the elements - for id in self.elements: - element, decl = self.elements[id] - if decl is None: - try: - self.ParseElement(id, element) - except InvalidContextError: - pass # ignore those nodes with invalid context - # (workaround gccxml bug) - - - def Declarations(self): - return self.declarations - - - def AddDecl(self, decl): - if decl.FullName() in self._names: - decl.is_unique= False - for d in self.declarations: - if d.FullName() == decl.FullName(): - d.is_unique = False - self._names[decl.FullName()] = 0 - self.declarations.append(decl) - - - def ParseElement(self, id, element): - method = 'Parse' + element.tag - if hasattr(self, method): - func = getattr(self, method) - func(id, element) - else: - self.ParseUnknown(id, element) - - - def GetElementsFromXML(self,filename): - 'Extracts a dictionary of elements from the gcc_xml file.' - - tree = ElementTree() - try: - tree.parse(filename) - except ExpatError: - raise InvalidXMLError, 'Not a XML file: %s' % filename - - root = tree.getroot() - if root.tag != 'GCC_XML': - raise InvalidXMLError, 'Not a valid GCC_XML file' - - # build a dictionary of id -> element, None - elementlist = root.getchildren() - elements = {} - for element in elementlist: - id = element.get('id') - if id: - elements[id] = element, None - return elements - - - def GetDecl(self, id): - if id not in self.elements: - if id == '_0': - raise InvalidContextError, 'Invalid context found in the xml file.' - else: - msg = 'ID not found in elements: %s' % id - raise ParserError, msg - - elem, decl = self.elements[id] - if decl is None: - self.ParseElement(id, elem) - elem, decl = self.elements[id] - if decl is None: - raise ParserError, 'Could not parse element: %s' % elem.tag - return decl - - - def GetType(self, id): - def Check(id, feature): - pos = id.find(feature) - if pos != -1: - id = id[:pos] + id[pos+1:] - return True, id - else: - return False, id - const, id = Check(id, 'c') - volatile, id = Check(id, 'v') - restricted, id = Check(id, 'r') - decl = self.GetDecl(id) - if isinstance(decl, Type): - res = deepcopy(decl) - if const: - res.const = const - if volatile: - res.volatile = volatile - if restricted: - res.restricted = restricted - else: - res = Type(decl.FullName(), const) - res.volatile = volatile - res.restricted = restricted - return res - - - def GetLocation(self, location): - file, line = location.split(':') - file = self.GetDecl(file) - return file, int(line) - - - def Update(self, id, decl): - element, _ = self.elements[id] - self.elements[id] = element, decl - - - def ParseUnknown(self, id, element): - name = '__Unknown_Element_%s' % id - decl = Unknown(name) - self.Update(id, decl) - - - def ParseNamespace(self, id, element): - namespace = element.get('name') - context = element.get('context') - if context: - outer = self.GetDecl(context) - if not outer.endswith('::'): - outer += '::' - namespace = outer + namespace - if namespace.startswith('::'): - namespace = namespace[2:] - self.Update(id, namespace) - - - def ParseFile(self, id, element): - filename = element.get('name') - self.Update(id, filename) - - - def ParseVariable(self, id, element): - # in gcc_xml, a static Field is declared as a Variable, so we check - # this and call the Field parser. - context = self.GetDecl(element.get('context')) - if isinstance(context, Class): - self.ParseField(id, element) - elem, decl = self.elements[id] - decl.static = True - else: - namespace = context - name = element.get('name') - type_ = self.GetType(element.get('type')) - location = self.GetLocation(element.get('location')) - variable = Variable(type_, name, namespace) - variable.location = location - self.AddDecl(variable) - self.Update(id, variable) - - - def GetArguments(self, element): - args = [] - for child in element: - if child.tag == 'Argument': - type = self.GetType(child.get('type')) - type.default = child.get('default') - args.append(type) - return args - - - def GetExceptions(self, exception_list): - if exception_list is None: - return None - - exceptions = [] - for t in exception_list.split(): - exceptions.append(self.GetType(t)) - - return exceptions - - - def ParseFunction(self, id, element, functionType=Function): - '''functionType is used because a Operator is identical to a normal - function, only the type of the function changes.''' - name = element.get('name') - returns = self.GetType(element.get('returns')) - namespace = self.GetDecl(element.get('context')) - location = self.GetLocation(element.get('location')) - params = self.GetArguments(element) - incomplete = bool(int(element.get('incomplete', 0))) - throws = self.GetExceptions(element.get('throw', None)) - function = functionType(name, namespace, returns, params, throws) - function.location = location - self.AddDecl(function) - self.Update(id, function) - - - def ParseOperatorFunction(self, id, element): - self.ParseFunction(id, element, Operator) - - - def GetHierarchy(self, bases): - '''Parses the string "bases" from the xml into a list of tuples of Base - instances. The first tuple is the most direct inheritance, and then it - goes up in the hierarchy. - ''' - - if bases is None: - return [] - base_names = bases.split() - this_level = [] - next_levels = [] - for base in base_names: - # get the visibility - split = base.split(':') - if len(split) == 2: - visib = split[0] - base = split[1] - else: - visib = Scope.public - decl = self.GetDecl(base) - if not isinstance(decl, Class): - # on windows, there are some classes which "bases" points to an - # "Unimplemented" tag, but we are not interested in this classes - # anyway - continue - base = Base(decl.FullName(), visib) - this_level.append(base) - # normalize with the other levels - for index, level in enumerate(decl.hierarchy): - if index < len(next_levels): - next_levels[index] = next_levels[index] + level - else: - next_levels.append(level) - hierarchy = [] - if this_level: - hierarchy.append(tuple(this_level)) - if next_levels: - hierarchy.extend(next_levels) - return hierarchy - - - def GetMembers(self, member_list): - # members must be a string with the ids of the members - if member_list is None: - return [] - members = [] - for member in member_list.split(): - decl = self.GetDecl(member) - if type(decl) in Class.ValidMemberTypes(): - members.append(decl) - return members - - - def ParseClass(self, id, element): - name = element.get('name') - abstract = bool(int(element.get('abstract', '0'))) - location = self.GetLocation(element.get('location')) - context = self.GetDecl(element.get('context')) - incomplete = bool(int(element.get('incomplete', 0))) - if isinstance(context, str): - class_ = Class(name, context, [], abstract) - else: - # a nested class - visib = element.get('access', Scope.public) - class_ = NestedClass( - name, context.FullName(), visib, [], abstract) - class_.incomplete = incomplete - # we have to add the declaration of the class before trying - # to parse its members and bases, to avoid recursion. - self.AddDecl(class_) - class_.location = location - self.Update(id, class_) - # now we can get the members and the bases - class_.hierarchy = self.GetHierarchy(element.get('bases')) - if class_.hierarchy: - class_.bases = class_.hierarchy[0] - members = self.GetMembers(element.get('members')) - for member in members: - class_.AddMember(member) - - - def ParseStruct(self, id, element): - self.ParseClass(id, element) - - - FUNDAMENTAL_RENAME = { - 'long long int' : 'boost::int64_t', - 'long long unsigned int' : 'boost::uint64_t', - } - - def ParseFundamentalType(self, id, element): - name = element.get('name') - name = self.FUNDAMENTAL_RENAME.get(name, name) - type_ = FundamentalType(name) - self.Update(id, type_) - - - def ParseArrayType(self, id, element): - type = self.GetType(element.get('type')) - min = element.get('min') - max = element.get('max') - array = ArrayType(type.name, type.const, min, max) - self.Update(id, array) - - - def ParseReferenceType(self, id, element): - type = self.GetType(element.get('type')) - expand = not isinstance(type, FunctionType) - ref = ReferenceType(type.name, type.const, None, expand, type.suffix) - self.Update(id, ref) - - - def ParsePointerType(self, id, element): - type = self.GetType(element.get('type')) - expand = not isinstance(type, FunctionType) - ref = PointerType(type.name, type.const, None, expand, type.suffix) - self.Update(id, ref) - - - def ParseFunctionType(self, id, element): - result = self.GetType(element.get('returns')) - args = self.GetArguments(element) - func = FunctionType(result, args) - self.Update(id, func) - - - def ParseMethodType(self, id, element): - class_ = self.GetDecl(element.get('basetype')).FullName() - result = self.GetType(element.get('returns')) - args = self.GetArguments(element) - method = MethodType(result, args, class_) - self.Update(id, method) - - - def ParseField(self, id, element): - name = element.get('name') - visib = element.get('access', Scope.public) - classname = self.GetDecl(element.get('context')).FullName() - type_ = self.GetType(element.get('type')) - static = bool(int(element.get('extern', '0'))) - location = self.GetLocation(element.get('location')) - var = ClassVariable(type_, name, classname, visib, static) - var.location = location - self.Update(id, var) - - - def ParseMethod(self, id, element, methodType=Method): - name = element.get('name') - result = self.GetType(element.get('returns')) - classname = self.GetDecl(element.get('context')).FullName() - visib = element.get('access', Scope.public) - static = bool(int(element.get('static', '0'))) - virtual = bool(int(element.get('virtual', '0'))) - abstract = bool(int(element.get('pure_virtual', '0'))) - const = bool(int(element.get('const', '0'))) - location = self.GetLocation(element.get('location')) - throws = self.GetExceptions(element.get('throw', None)) - params = self.GetArguments(element) - method = methodType( - name, classname, result, params, visib, virtual, abstract, static, const, throws) - method.location = location - self.Update(id, method) - - - def ParseOperatorMethod(self, id, element): - self.ParseMethod(id, element, ClassOperator) - - - def ParseConstructor(self, id, element): - name = element.get('name') - visib = element.get('access', Scope.public) - classname = self.GetDecl(element.get('context')).FullName() - location = self.GetLocation(element.get('location')) - params = self.GetArguments(element) - artificial = element.get('artificial', False) - ctor = Constructor(name, classname, params, visib) - ctor.location = location - self.Update(id, ctor) - - - def ParseDestructor(self, id, element): - name = element.get('name') - visib = element.get('access', Scope.public) - classname = self.GetDecl(element.get('context')).FullName() - virtual = bool(int(element.get('virtual', '0'))) - location = self.GetLocation(element.get('location')) - des = Destructor(name, classname, visib, virtual) - des.location = location - self.Update(id, des) - - - def ParseConverter(self, id, element): - self.ParseMethod(id, element, ConverterOperator) - - - def ParseTypedef(self, id, element): - name = element.get('name') - type = self.GetType(element.get('type')) - context = self.GetDecl(element.get('context')) - if isinstance(context, Class): - context = context.FullName() - typedef = Typedef(type, name, context) - self.Update(id, typedef) - self.AddDecl(typedef) - - - def ParseEnumeration(self, id, element): - name = element.get('name') - location = self.GetLocation(element.get('location')) - context = self.GetDecl(element.get('context')) - incomplete = bool(int(element.get('incomplete', 0))) - if isinstance(context, str): - enum = Enumeration(name, context) - else: - visib = element.get('access', Scope.public) - enum = ClassEnumeration(name, context.FullName(), visib) - self.AddDecl(enum) - enum.location = location - for child in element: - if child.tag == 'EnumValue': - name = child.get('name') - value = int(child.get('init')) - enum.values[name] = value - enum.incomplete = incomplete - self.Update(id, enum) - - - -def ParseDeclarations(filename): - 'Returns a list of the top declarations found in the gcc_xml file.' - - parser = GCCXMLParser() - parser.Parse(filename) - return parser.Declarations() - - -if __name__ == '__main__': - ParseDeclarations(r'D:\Programming\Libraries\boost-cvs\boost\libs\python\pyste\example\test.xml') diff --git a/pyste/src/Pyste/HeaderExporter.py b/pyste/src/Pyste/HeaderExporter.py deleted file mode 100644 index 47651ba7..00000000 --- a/pyste/src/Pyste/HeaderExporter.py +++ /dev/null @@ -1,81 +0,0 @@ -# Copyright Bruno da Silva de Oliveira 2003. Use, modification and -# distribution is subject to the Boost Software License, Version 1.0. -# (See accompanying file LICENSE_1_0.txt or copy at -# http://www.boost.org/LICENSE_1_0.txt) - -from Exporter import Exporter -from ClassExporter import ClassExporter -from FunctionExporter import FunctionExporter -from EnumExporter import EnumExporter -from VarExporter import VarExporter -from infos import * -from declarations import * -import os.path -import exporters -import MultipleCodeUnit - -#============================================================================== -# HeaderExporter -#============================================================================== -class HeaderExporter(Exporter): - 'Exports all declarations found in the given header' - - def __init__(self, info, parser_tail=None): - Exporter.__init__(self, info, parser_tail) - - - def WriteInclude(self, codeunit): - pass - - - def IsInternalName(self, name): - '''Returns true if the given name looks like a internal compiler - structure''' - return name.startswith('_') - - - def Export(self, codeunit, exported_names): - header = os.path.normpath(self.parser_header) - for decl in self.declarations: - # check if this declaration is in the header - location = os.path.abspath(decl.location[0]) - if location == header and not self.IsInternalName(decl.name): - # ok, check the type of the declaration and export it accordingly - self.HandleDeclaration(decl, codeunit, exported_names) - - - def HandleDeclaration(self, decl, codeunit, exported_names): - '''Dispatch the declaration to the appropriate method, that must create - a suitable info object for a Exporter, create a Exporter, set its - declarations and append it to the list of exporters. - ''' - dispatch_table = { - Class : ClassExporter, - Enumeration : EnumExporter, - Function : FunctionExporter, - Variable : VarExporter, - } - - exporter_class = dispatch_table.get(type(decl)) - if exporter_class is not None: - self.HandleExporter(decl, exporter_class, codeunit, exported_names) - - - def HandleExporter(self, decl, exporter_type, codeunit, exported_names): - # only export complete declarations - if not decl.incomplete: - info = self.info[decl.name] - info.name = decl.FullName() - info.include = self.info.include - exporter = exporter_type(info) - exporter.SetDeclarations(self.declarations) - exporter.SetParsedHeader(self.parser_header) - if isinstance(codeunit, MultipleCodeUnit.MultipleCodeUnit): - codeunit.SetCurrent(self.interface_file, exporter.Name()) - else: - codeunit.SetCurrent(exporter.Name()) - exporter.GenerateCode(codeunit, exported_names) - - - def Name(self): - return self.info.include diff --git a/pyste/src/Pyste/MultipleCodeUnit.py b/pyste/src/Pyste/MultipleCodeUnit.py deleted file mode 100644 index 65faad45..00000000 --- a/pyste/src/Pyste/MultipleCodeUnit.py +++ /dev/null @@ -1,135 +0,0 @@ -# Copyright Bruno da Silva de Oliveira 2003. Use, modification and -# distribution is subject to the Boost Software License, Version 1.0. -# (See accompanying file LICENSE_1_0.txt or copy at -# http://www.boost.org/LICENSE_1_0.txt) - -from SingleCodeUnit import SingleCodeUnit -import os -import utils -from SmartFile import SmartFile - - -#============================================================================== -# MultipleCodeUnit -#============================================================================== -class MultipleCodeUnit(object): - ''' - Represents a bunch of cpp files, where each cpp file represents a header - to be exported by pyste. Another cpp, named .cpp is created too. - ''' - - def __init__(self, modulename, outdir): - self.modulename = modulename - self.outdir = outdir - self.codeunits = {} # maps from a (filename, function) to a SingleCodeUnit - self.functions = [] - self._current = None - self.all = SingleCodeUnit(None, None) - - - def _FunctionName(self, interface_file): - name = os.path.splitext(interface_file)[0] - return 'Export_%s' % utils.makeid(name) - - - def _FileName(self, interface_file): - filename = os.path.basename(interface_file) - filename = '_%s.cpp' % os.path.splitext(filename)[0] - return os.path.join(self.outdir, filename) - - - def SetCurrent(self, interface_file, export_name): - 'Changes the current code unit' - if export_name is None: - self._current = None - elif export_name is '__all__': - self._current = self.all - else: - filename = self._FileName(interface_file) - function = self._FunctionName(interface_file) - try: - codeunit = self.codeunits[filename] - except KeyError: - codeunit = SingleCodeUnit(None, filename) - codeunit.module_definition = 'void %s()' % function - self.codeunits[filename] = codeunit - if function not in self.functions: - self.functions.append(function) - self._current = codeunit - - - def Current(self): - return self._current - - current = property(Current, SetCurrent) - - - def Write(self, section, code): - if self._current is not None: - self.current.Write(section, code) - - - def Section(self, section): - if self._current is not None: - return self.current.Section(section) - - - def _CreateOutputDir(self): - try: - os.mkdir(self.outdir) - except OSError: pass # already created - - - def Save(self): - # create the directory where all the files will go - self._CreateOutputDir(); - # order all code units by filename, and merge them all - codeunits = {} # filename => list of codeunits - - # While ordering all code units by file name, the first code - # unit in the list of code units is used as the main unit - # which dumps all the include, declaration and - # declaration-outside sections at the top of the file. - for filename, codeunit in self.codeunits.items(): - if filename not in codeunits: - # this codeunit is the main codeunit. - codeunits[filename] = [codeunit] - codeunit.Merge(self.all) - else: - main_unit = codeunits[filename][0] - for section in ('include', 'declaration', 'declaration-outside'): - main_unit.code[section] = main_unit.code[section] + codeunit.code[section] - codeunit.code[section] = '' - codeunits[filename].append(codeunit) - - # Now write all the codeunits appending them correctly. - for file_units in codeunits.values(): - append = False - for codeunit in file_units: - codeunit.Save(append) - if not append: - append = True - - - def GenerateMain(self, interfaces): - # generate the main cpp - filename = os.path.join(self.outdir, '_main.cpp') - fout = SmartFile(filename, 'w') - fout.write(utils.left_equals('Include')) - fout.write('#include \n\n') - fout.write(utils.left_equals('Exports')) - functions = [self._FunctionName(x) for x in interfaces] - for function in functions: - fout.write('void %s();\n' % function) - fout.write('\n') - fout.write(utils.left_equals('Module')) - fout.write('BOOST_PYTHON_MODULE(%s)\n' % self.modulename) - fout.write('{\n') - indent = ' ' * 4 - for function in functions: - fout.write(indent) - fout.write('%s();\n' % function) - fout.write('}\n') - - - diff --git a/pyste/src/Pyste/SingleCodeUnit.py b/pyste/src/Pyste/SingleCodeUnit.py deleted file mode 100644 index 2e59dbb8..00000000 --- a/pyste/src/Pyste/SingleCodeUnit.py +++ /dev/null @@ -1,121 +0,0 @@ -# Copyright Bruno da Silva de Oliveira 2003. Use, modification and -# distribution is subject to the Boost Software License, Version 1.0. -# (See accompanying file LICENSE_1_0.txt or copy at -# http://www.boost.org/LICENSE_1_0.txt) - -from settings import namespaces -import settings -from utils import remove_duplicated_lines, left_equals -from SmartFile import SmartFile - - -#============================================================================== -# SingleCodeUnit -#============================================================================== -class SingleCodeUnit: - ''' - Represents a cpp file, where other objects can write in one of the - predefined sections. - The avaiable sections are: - pchinclude - The pre-compiled header area - include - The include area of the cpp file - declaration - The part before the module definition - module - Inside the BOOST_PYTHON_MODULE macro - ''' - - def __init__(self, modulename, filename): - self.modulename = modulename - self.filename = filename - # define the avaiable sections - self.code = {} - # include section - self.code['pchinclude'] = '' - # include section - self.code['include'] = '' - # declaration section (inside namespace) - self.code['declaration'] = '' - # declaration (outside namespace) - self.code['declaration-outside'] = '' - # inside BOOST_PYTHON_MACRO - self.code['module'] = '' - # create the default module definition - self.module_definition = 'BOOST_PYTHON_MODULE(%s)' % modulename - - - def Write(self, section, code): - 'write the given code in the section of the code unit' - if section not in self.code: - raise RuntimeError, 'Invalid CodeUnit section: %s' % section - self.code[section] += code - - - def Merge(self, other): - for section in ('include', 'declaration', 'declaration-outside', 'module'): - self.code[section] = self.code[section] + other.code[section] - - - def Section(self, section): - return self.code[section] - - - def SetCurrent(self, *args): - pass - - - def Current(self): - pass - - - def Save(self, append=False): - 'Writes this code unit to the filename' - space = '\n\n' - if not append: - flag = 'w' - else: - flag = 'a' - fout = SmartFile(self.filename, flag) - fout.write('\n') - # includes - # boost.python header - if self.code['pchinclude']: - fout.write(left_equals('PCH')) - fout.write(self.code['pchinclude']+'\n') - fout.write('#ifdef _MSC_VER\n') - fout.write('#pragma hdrstop\n') - fout.write('#endif\n') - else: - fout.write(left_equals('Boost Includes')) - fout.write('#include \n') - # include numerical boost for int64 definitions - fout.write('#include \n') - fout.write('\n') - # other includes - if self.code['include']: - fout.write(left_equals('Includes')) - includes = remove_duplicated_lines(self.code['include']) - fout.write(includes) - fout.write(space) - # using - if settings.USING_BOOST_NS and not append: - fout.write(left_equals('Using')) - fout.write('using namespace boost::python;\n\n') - # declarations - declaration = self.code['declaration'] - declaration_outside = self.code['declaration-outside'] - if declaration_outside or declaration: - fout.write(left_equals('Declarations')) - if declaration_outside: - fout.write(declaration_outside + '\n\n') - if declaration: - pyste_namespace = namespaces.pyste[:-2] - fout.write('namespace %s {\n\n' % pyste_namespace) - fout.write(declaration) - fout.write('\n}// namespace %s\n' % pyste_namespace) - fout.write(space) - # module - fout.write(left_equals('Module')) - fout.write(self.module_definition + '\n') - fout.write('{\n') - fout.write(self.code['module']) - fout.write('}\n\n') - fout.close() diff --git a/pyste/src/Pyste/SmartFile.py b/pyste/src/Pyste/SmartFile.py deleted file mode 100644 index 039579e3..00000000 --- a/pyste/src/Pyste/SmartFile.py +++ /dev/null @@ -1,60 +0,0 @@ -# Copyright Bruno da Silva de Oliveira 2003. Use, modification and -# distribution is subject to the Boost Software License, Version 1.0. -# (See accompanying file LICENSE_1_0.txt or copy at -# http://www.boost.org/LICENSE_1_0.txt) - -import os -import md5 - -#============================================================================== -# SmartFile -#============================================================================== -class SmartFile(object): - ''' - A file-like object used for writing files. The given file will only be - actually written to disk if there's not a file with the same name, or if - the existing file is *different* from the file to be written. - ''' - - def __init__(self, filename, mode='w'): - self.filename = filename - self.mode = mode - self._contents = [] - self._closed = False - - - def __del__(self): - if not self._closed: - self.close() - - - def write(self, string): - self._contents.append(string) - - - def _dowrite(self, contents): - f = file(self.filename, self.mode) - f.write(contents) - f.close() - - - def _GetMD5(self, string): - return md5.new(string).digest() - - - def close(self): - # if the filename doesn't exist, write the file right away - this_contents = ''.join(self._contents) - if not os.path.isfile(self.filename): - self._dowrite(this_contents) - else: - # read the contents of the file already in disk - f = file(self.filename) - other_contents = f.read() - f.close() - # test the md5 for both files - this_md5 = self._GetMD5(this_contents) - other_md5 = self._GetMD5(other_contents) - if this_md5 != other_md5: - self._dowrite(this_contents) - self._closed = True diff --git a/pyste/src/Pyste/VarExporter.py b/pyste/src/Pyste/VarExporter.py deleted file mode 100644 index d3571e75..00000000 --- a/pyste/src/Pyste/VarExporter.py +++ /dev/null @@ -1,40 +0,0 @@ -# Copyright Bruno da Silva de Oliveira 2003. Use, modification and -# distribution is subject to the Boost Software License, Version 1.0. -# (See accompanying file LICENSE_1_0.txt or copy at -# http://www.boost.org/LICENSE_1_0.txt) - -from Exporter import Exporter -from settings import * -import utils - -#============================================================================== -# VarExporter -#============================================================================== -class VarExporter(Exporter): - '''Exports a global variable. - ''' - - def __init__(self, info): - Exporter.__init__(self, info) - - - def Export(self, codeunit, exported_names): - if self.info.exclude: return - decl = self.GetDeclaration(self.info.name) - if not decl.type.const: - msg = '---> Warning: The global variable "%s" is non-const:\n' \ - ' changes in Python will not reflect in C++.' - print msg % self.info.name - print - rename = self.info.rename or self.info.name - code = self.INDENT + namespaces.python - code += 'scope().attr("%s") = %s;\n' % (rename, self.info.name) - codeunit.Write('module', code) - - - def Order(self): - return 0, self.info.name - - - def Name(self): - return self.info.name diff --git a/pyste/src/Pyste/__init__.py b/pyste/src/Pyste/__init__.py deleted file mode 100644 index 02eec64b..00000000 --- a/pyste/src/Pyste/__init__.py +++ /dev/null @@ -1,6 +0,0 @@ -# Copyright Bruno da Silva de Oliveira 2003. Use, modification and -# distribution is subject to the Boost Software License, Version 1.0. -# (See accompanying file LICENSE_1_0.txt or copy at -# http://www.boost.org/LICENSE_1_0.txt) - - diff --git a/pyste/src/Pyste/declarations.py b/pyste/src/Pyste/declarations.py deleted file mode 100644 index 6eff97dc..00000000 --- a/pyste/src/Pyste/declarations.py +++ /dev/null @@ -1,653 +0,0 @@ -# Copyright Bruno da Silva de Oliveira 2003. Use, modification and -# distribution is subject to the Boost Software License, Version 1.0. -# (See accompanying file LICENSE_1_0.txt or copy at -# http://www.boost.org/LICENSE_1_0.txt) - -''' -Defines classes that represent declarations found in C++ header files. - -''' - -# version indicates the version of the declarations. Whenever a declaration -# changes, this variable should be updated, so that the caches can be rebuilt -# automatically -version = '1.0' - -#============================================================================== -# Declaration -#============================================================================== -class Declaration(object): - '''Base class for all declarations. - @ivar name: The name of the declaration. - @ivar namespace: The namespace of the declaration. - ''' - - def __init__(self, name, namespace): - ''' - @type name: string - @param name: The name of this declaration - @type namespace: string - @param namespace: the full namespace where this declaration resides. - ''' - self.name = name - self.namespace = namespace - self.location = '', -1 # (filename, line) - self.incomplete = False - self.is_unique = True - - - def FullName(self): - ''' - Returns the full qualified name: "boost::inner::Test" - @rtype: string - @return: The full name of the declaration. - ''' - namespace = self.namespace or '' - if namespace and not namespace.endswith('::'): - namespace += '::' - return namespace + self.name - - - def __repr__(self): - return '' % (self.FullName(), id(self)) - - - def __str__(self): - return 'Declaration of %s' % self.FullName() - - -#============================================================================== -# Class -#============================================================================== -class Class(Declaration): - ''' - Represents a C++ class or struct. Iteration through it yields its members. - - @type abstract: bool - @ivar abstract: if the class has any abstract methods. - - @type bases: tuple - @ivar bases: tuple with L{Base} instances, representing the most direct - inheritance. - - @type hierarchy: list - @ivar hierarchy: a list of tuples of L{Base} instances, representing - the entire hierarchy tree of this object. The first tuple is the parent - classes, and the other ones go up in the hierarchy. - ''' - - def __init__(self, name, namespace, members, abstract): - Declaration.__init__(self, name, namespace) - self.__members = members - self.__member_names = {} - self.abstract = abstract - self.bases = () - self.hierarchy = () - self.operator = {} - - - def __iter__(self): - '''iterates through the class' members. - ''' - return iter(self.__members) - - - def Constructors(self, publics_only=True): - '''Returns a list of the constructors for this class. - @rtype: list - ''' - constructors = [] - for member in self: - if isinstance(member, Constructor): - if publics_only and member.visibility != Scope.public: - continue - constructors.append(member) - return constructors - - - def HasCopyConstructor(self): - '''Returns true if this class has a public copy constructor. - @rtype: bool - ''' - for cons in self.Constructors(): - if cons.IsCopy(): - return True - return False - - - def HasDefaultConstructor(self): - '''Returns true if this class has a public default constructor. - @rtype: bool - ''' - for cons in self.Constructors(): - if cons.IsDefault(): - return True - return False - - - def AddMember(self, member): - if member.name in self.__member_names: - member.is_unique = False - for m in self: - if m.name == member.name: - m.is_unique = False - else: - member.is_unique = True - self.__member_names[member.name] = 1 - self.__members.append(member) - if isinstance(member, ClassOperator): - self.operator[member.name] = member - - - def ValidMemberTypes(): - return (NestedClass, Method, Constructor, Destructor, ClassVariable, - ClassOperator, ConverterOperator, ClassEnumeration) - ValidMemberTypes = staticmethod(ValidMemberTypes) - - -#============================================================================== -# NestedClass -#============================================================================== -class NestedClass(Class): - '''The declaration of a class/struct inside another class/struct. - - @type class: string - @ivar class: fullname of the class where this class is contained. - - @type visibility: L{Scope} - @ivar visibility: the visibility of this class. - ''' - - def __init__(self, name, class_, visib, members, abstract): - Class.__init__(self, name, None, members, abstract) - self.class_ = class_ - self.visibility = visib - - - def FullName(self): - '''The full name of this class, like ns::outer::inner. - @rtype: string - ''' - return '%s::%s' % (self.class_, self.name) - - -#============================================================================== -# Scope -#============================================================================== -class Scope: - '''Used to represent the visibility of various members inside a class. - @cvar public: public visibility - @cvar private: private visibility - @cvar protected: protected visibility - ''' - public = 'public' - private = 'private' - protected = 'protected' - - -#============================================================================== -# Base -#============================================================================== -class Base: - '''Represents a base class of another class. - @ivar _name: the full name of the base class. - @ivar _visibility: the visibility of the derivation. - ''' - - def __init__(self, name, visibility=Scope.public): - self.name = name - self.visibility = visibility - - -#============================================================================== -# Function -#============================================================================== -class Function(Declaration): - '''The declaration of a function. - @ivar _result: instance of L{Type} or None. - @ivar _parameters: list of L{Type} instances. - @ivar _throws: exception specifiers or None - ''' - - def __init__(self, name, namespace, result, params, throws=None): - Declaration.__init__(self, name, namespace) - # the result type: instance of Type, or None (constructors) - self.result = result - # the parameters: instances of Type - self.parameters = params - # the exception specification - self.throws = throws - - - def Exceptions(self): - if self.throws is None: - return "" - else: - return " throw(%s)" % ', '.join ([x.FullName() for x in self.throws]) - - - def PointerDeclaration(self, force=False): - '''Returns a declaration of a pointer to this function. - @param force: If True, returns a complete pointer declaration regardless - if this function is unique or not. - ''' - if self.is_unique and not force: - return '&%s' % self.FullName() - else: - result = self.result.FullName() - params = ', '.join([x.FullName() for x in self.parameters]) - return '(%s (*)(%s)%s)&%s' % (result, params, self.Exceptions(), self.FullName()) - - - def MinArgs(self): - min = 0 - for arg in self.parameters: - if arg.default is None: - min += 1 - return min - - minArgs = property(MinArgs) - - - def MaxArgs(self): - return len(self.parameters) - - maxArgs = property(MaxArgs) - - - -#============================================================================== -# Operator -#============================================================================== -class Operator(Function): - '''The declaration of a custom operator. Its name is the same as the - operator name in C++, ie, the name of the declaration "operator+(..)" is - "+". - ''' - - def FullName(self): - namespace = self.namespace or '' - if not namespace.endswith('::'): - namespace += '::' - return namespace + 'operator' + self.name - - -#============================================================================== -# Method -#============================================================================== -class Method(Function): - '''The declaration of a method. - - @ivar _visibility: the visibility of this method. - @ivar _virtual: if this method is declared as virtual. - @ivar _abstract: if this method is virtual but has no default implementation. - @ivar _static: if this method is static. - @ivar _class: the full name of the class where this method was declared. - @ivar _const: if this method is declared as const. - @ivar _throws: list of exception specificiers or None - ''' - - def __init__(self, name, class_, result, params, visib, virtual, abstract, static, const, throws=None): - Function.__init__(self, name, None, result, params, throws) - self.visibility = visib - self.virtual = virtual - self.abstract = abstract - self.static = static - self.class_ = class_ - self.const = const - - - def FullName(self): - return self.class_ + '::' + self.name - - - def PointerDeclaration(self, force=False): - '''Returns a declaration of a pointer to this member function. - @param force: If True, returns a complete pointer declaration regardless - if this function is unique or not. - ''' - if self.static: - # static methods are like normal functions - return Function.PointerDeclaration(self, force) - if self.is_unique and not force: - return '&%s' % self.FullName() - else: - result = self.result.FullName() - params = ', '.join([x.FullName() for x in self.parameters]) - const = '' - if self.const: - const = 'const' - return '(%s (%s::*)(%s) %s%s)&%s' %\ - (result, self.class_, params, const, self.Exceptions(), self.FullName()) - - -#============================================================================== -# Constructor -#============================================================================== -class Constructor(Method): - '''A class' constructor. - ''' - - def __init__(self, name, class_, params, visib): - Method.__init__(self, name, class_, None, params, visib, False, False, False, False) - - - def IsDefault(self): - '''Returns True if this constructor is a default constructor. - ''' - return len(self.parameters) == 0 and self.visibility == Scope.public - - - def IsCopy(self): - '''Returns True if this constructor is a copy constructor. - ''' - if len(self.parameters) != 1: - return False - param = self.parameters[0] - class_as_param = self.parameters[0].name == self.class_ - param_reference = isinstance(param, ReferenceType) - is_public = self.visibility == Scope.public - return param_reference and class_as_param and param.const and is_public - - - def PointerDeclaration(self, force=False): - return '' - - -#============================================================================== -# Destructor -#============================================================================== -class Destructor(Method): - 'The destructor of a class.' - - def __init__(self, name, class_, visib, virtual): - Method.__init__(self, name, class_, None, [], visib, virtual, False, False, False) - - def FullName(self): - return self.class_ + '::~' + self.name - - - def PointerDeclaration(self, force=False): - return '' - - - -#============================================================================== -# ClassOperator -#============================================================================== -class ClassOperator(Method): - 'A custom operator in a class.' - - def FullName(self): - return self.class_ + '::operator ' + self.name - - - -#============================================================================== -# ConverterOperator -#============================================================================== -class ConverterOperator(ClassOperator): - 'An operator in the form "operator OtherClass()".' - - def FullName(self): - return self.class_ + '::operator ' + self.result.FullName() - - - -#============================================================================== -# Type -#============================================================================== -class Type(Declaration): - '''Represents the type of a variable or parameter. - @ivar _const: if the type is constant. - @ivar _default: if this type has a default value associated with it. - @ivar _volatile: if this type was declared with the keyword volatile. - @ivar _restricted: if this type was declared with the keyword restricted. - @ivar _suffix: Suffix to get the full type name. '*' for pointers, for - example. - ''' - - def __init__(self, name, const=False, default=None, suffix=''): - Declaration.__init__(self, name, None) - # whatever the type is constant or not - self.const = const - # used when the Type is a function argument - self.default = default - self.volatile = False - self.restricted = False - self.suffix = suffix - - def __repr__(self): - if self.const: - const = 'const ' - else: - const = '' - return '' - - - def FullName(self): - if self.const: - const = 'const ' - else: - const = '' - return const + self.name + self.suffix - - -#============================================================================== -# ArrayType -#============================================================================== -class ArrayType(Type): - '''Represents an array. - @ivar min: the lower bound of the array, usually 0. Can be None. - @ivar max: the upper bound of the array. Can be None. - ''' - - def __init__(self, name, const, min, max): - 'min and max can be None.' - Type.__init__(self, name, const) - self.min = min - self.max = max - - - -#============================================================================== -# ReferenceType -#============================================================================== -class ReferenceType(Type): - '''A reference type.''' - - def __init__(self, name, const=False, default=None, expandRef=True, suffix=''): - Type.__init__(self, name, const, default) - if expandRef: - self.suffix = suffix + '&' - - -#============================================================================== -# PointerType -#============================================================================== -class PointerType(Type): - 'A pointer type.' - - def __init__(self, name, const=False, default=None, expandPointer=False, suffix=''): - Type.__init__(self, name, const, default) - if expandPointer: - self.suffix = suffix + '*' - - -#============================================================================== -# FundamentalType -#============================================================================== -class FundamentalType(Type): - 'One of the fundamental types, like int, void, etc.' - - def __init__(self, name, const=False, default=None): - Type.__init__(self, name, const, default) - - - -#============================================================================== -# FunctionType -#============================================================================== -class FunctionType(Type): - '''A pointer to a function. - @ivar _result: the return value - @ivar _parameters: a list of Types, indicating the parameters of the function. - @ivar _name: the name of the function. - ''' - - def __init__(self, result, parameters): - Type.__init__(self, '', False) - self.result = result - self.parameters = parameters - self.name = self.FullName() - - - def FullName(self): - full = '%s (*)' % self.result.FullName() - params = [x.FullName() for x in self.parameters] - full += '(%s)' % ', '.join(params) - return full - - -#============================================================================== -# MethodType -#============================================================================== -class MethodType(FunctionType): - '''A pointer to a member function of a class. - @ivar _class: The fullname of the class that the method belongs to. - ''' - - def __init__(self, result, parameters, class_): - self.class_ = class_ - FunctionType.__init__(self, result, parameters) - - - def FullName(self): - full = '%s (%s::*)' % (self.result.FullName(), self.class_) - params = [x.FullName() for x in self.parameters] - full += '(%s)' % ', '.join(params) - return full - - -#============================================================================== -# Variable -#============================================================================== -class Variable(Declaration): - '''Represents a global variable. - - @type _type: L{Type} - @ivar _type: The type of the variable. - ''' - - def __init__(self, type, name, namespace): - Declaration.__init__(self, name, namespace) - self.type = type - - -#============================================================================== -# ClassVariable -#============================================================================== -class ClassVariable(Variable): - '''Represents a class variable. - - @type _visibility: L{Scope} - @ivar _visibility: The visibility of this variable within the class. - - @type _static: bool - @ivar _static: Indicates if the variable is static. - - @ivar _class: Full name of the class that this variable belongs to. - ''' - - def __init__(self, type, name, class_, visib, static): - Variable.__init__(self, type, name, None) - self.visibility = visib - self.static = static - self.class_ = class_ - - - def FullName(self): - return self.class_ + '::' + self.name - - -#============================================================================== -# Enumeration -#============================================================================== -class Enumeration(Declaration): - '''Represents an enum. - - @type _values: dict of str => int - @ivar _values: holds the values for this enum. - ''' - - def __init__(self, name, namespace): - Declaration.__init__(self, name, namespace) - self.values = {} # dict of str => int - - - def ValueFullName(self, name): - '''Returns the full name for a value in the enum. - ''' - assert name in self.values - namespace = self.namespace - if namespace: - namespace += '::' - return namespace + name - - -#============================================================================== -# ClassEnumeration -#============================================================================== -class ClassEnumeration(Enumeration): - '''Represents an enum inside a class. - - @ivar _class: The full name of the class where this enum belongs. - @ivar _visibility: The visibility of this enum inside his class. - ''' - - def __init__(self, name, class_, visib): - Enumeration.__init__(self, name, None) - self.class_ = class_ - self.visibility = visib - - - def FullName(self): - return '%s::%s' % (self.class_, self.name) - - - def ValueFullName(self, name): - assert name in self.values - return '%s::%s' % (self.class_, name) - - -#============================================================================== -# Typedef -#============================================================================== -class Typedef(Declaration): - '''A Typedef declaration. - - @type _type: L{Type} - @ivar _type: The type of the typedef. - - @type _visibility: L{Scope} - @ivar _visibility: The visibility of this typedef. - ''' - - def __init__(self, type, name, namespace): - Declaration.__init__(self, name, namespace) - self.type = type - self.visibility = Scope.public - - - - - -#============================================================================== -# Unknown -#============================================================================== -class Unknown(Declaration): - '''A declaration that Pyste does not know how to handle. - ''' - - def __init__(self, name): - Declaration.__init__(self, name, None) diff --git a/pyste/src/Pyste/exporters.py b/pyste/src/Pyste/exporters.py deleted file mode 100644 index f573d01b..00000000 --- a/pyste/src/Pyste/exporters.py +++ /dev/null @@ -1,12 +0,0 @@ -# Copyright Bruno da Silva de Oliveira 2003. Use, modification and -# distribution is subject to the Boost Software License, Version 1.0. -# (See accompanying file LICENSE_1_0.txt or copy at -# http://www.boost.org/LICENSE_1_0.txt) - - -# a list of Exporter instances -exporters = [] - -current_interface = None # the current interface file being processed -importing = False # whetever we are now importing a pyste file. - # exporters created here shouldn't export themselves diff --git a/pyste/src/Pyste/exporterutils.py b/pyste/src/Pyste/exporterutils.py deleted file mode 100644 index 363700d2..00000000 --- a/pyste/src/Pyste/exporterutils.py +++ /dev/null @@ -1,87 +0,0 @@ -# Copyright Bruno da Silva de Oliveira 2003. Use, modification and -# distribution is subject to the Boost Software License, Version 1.0. -# (See accompanying file LICENSE_1_0.txt or copy at -# http://www.boost.org/LICENSE_1_0.txt) - -''' -Various helpers for interface files. -''' - -from settings import * -from policies import * -from declarations import * - -#============================================================================== -# FunctionWrapper -#============================================================================== -class FunctionWrapper(object): - '''Holds information about a wrapper for a function or a method. It is - divided in 2 parts: the name of the Wrapper, and its code. The code is - placed in the declaration section of the module, while the name is used to - def' the function or method (with the pyste namespace prepend to it). If - code is None, the name is left unchanged. - ''' - - def __init__(self, name, code=None): - self.name = name - self.code = code - - def FullName(self): - if self.code: - return namespaces.pyste + self.name - else: - return self.name - - -_printed_warnings = {} # used to avoid double-prints of warnings - -#============================================================================== -# HandlePolicy -#============================================================================== -def HandlePolicy(function, policy): - '''Show a warning to the user if the function needs a policy and doesn't - have one. Return a policy to the function, which is the given policy itself - if it is not None, or a default policy for this method. - ''' - - def IsString(type): - 'Return True if the Type instance can be considered a string' - return type.FullName() == 'const char*' - - def IsPyObject(type): - return type.FullName() == '_object *' # internal name of PyObject - - result = function.result - # if the function returns const char*, a policy is not needed - if IsString(result) or IsPyObject(result): - return policy - # if returns a const T&, set the default policy - if policy is None and result.const and isinstance(result, ReferenceType): - policy = return_value_policy(copy_const_reference) - # basic test if the result type demands a policy - needs_policy = isinstance(result, (ReferenceType, PointerType)) - # show a warning to the user, if needed - if needs_policy and policy is None: - global _printed_warnings - warning = '---> Error: %s returns a pointer or a reference, ' \ - 'but no policy was specified.' % function.FullName() - if warning not in _printed_warnings: - print warning - print - # avoid double prints of the same warning - _printed_warnings[warning] = 1 - return policy - - -#============================================================================== -# EspecializeTypeID -#============================================================================== -_exported_type_ids = {} -def EspecializeTypeID(typename): - global _exported_type_ids - macro = 'BOOST_PYTHON_OPAQUE_SPECIALIZED_TYPE_ID(%s)\n' % typename - if macro not in _exported_type_ids: - _exported_type_ids[macro] = 1 - return macro - else: - return None diff --git a/pyste/src/Pyste/infos.py b/pyste/src/Pyste/infos.py deleted file mode 100644 index 2a4f01ea..00000000 --- a/pyste/src/Pyste/infos.py +++ /dev/null @@ -1,259 +0,0 @@ -# Copyright Bruno da Silva de Oliveira 2003. Use, modification and -# distribution is subject to the Boost Software License, Version 1.0. -# (See accompanying file LICENSE_1_0.txt or copy at -# http://www.boost.org/LICENSE_1_0.txt) - -import os.path -import copy -import exporters -from ClassExporter import ClassExporter -from FunctionExporter import FunctionExporter -from EnumExporter import EnumExporter -from HeaderExporter import HeaderExporter -from VarExporter import VarExporter -from CodeExporter import CodeExporter -from exporterutils import FunctionWrapper -from utils import makeid -import warnings - -#============================================================================== -# DeclarationInfo -#============================================================================== -class DeclarationInfo: - - def __init__(self, otherInfo=None): - self.__infos = {} - self.__attributes = {} - if otherInfo is not None: - self.__infos = copy.deepcopy(otherInfo.__infos) - self.__attributes = copy.deepcopy(otherInfo.__attributes) - - - def __getitem__(self, name): - 'Used to access sub-infos' - if name.startswith('__'): - raise AttributeError - default = DeclarationInfo() - default._Attribute('name', name) - return self.__infos.setdefault(name, default) - - - def __getattr__(self, name): - return self[name] - - - def _Attribute(self, name, value=None): - if value is None: - # get value - return self.__attributes.get(name) - else: - # set value - self.__attributes[name] = value - - - def AddExporter(self, exporter): - # this was causing a much serious bug, as reported by Niall Douglas: - # another solution must be found! - #if not exporters.importing: - if exporter not in exporters.exporters: - exporters.exporters.append(exporter) - exporter.interface_file = exporters.current_interface - - -#============================================================================== -# FunctionInfo -#============================================================================== -class FunctionInfo(DeclarationInfo): - - def __init__(self, name, include, tail=None, otherOption=None, - exporter_class = FunctionExporter): - DeclarationInfo.__init__(self, otherOption) - self._Attribute('name', name) - self._Attribute('include', include) - self._Attribute('exclude', False) - # create a FunctionExporter - exporter = exporter_class(InfoWrapper(self), tail) - self.AddExporter(exporter) - - -#============================================================================== -# ClassInfo -#============================================================================== -class ClassInfo(DeclarationInfo): - - def __init__(self, name, include, tail=None, otherInfo=None, - exporter_class = ClassExporter): - DeclarationInfo.__init__(self, otherInfo) - self._Attribute('name', name) - self._Attribute('include', include) - self._Attribute('exclude', False) - # create a ClassExporter - exporter = exporter_class(InfoWrapper(self), tail) - self.AddExporter(exporter) - - -#============================================================================== -# templates -#============================================================================== -def GenerateName(name, type_list): - name = name.replace('::', '_') - names = [name] + type_list - return makeid('_'.join(names)) - - -class ClassTemplateInfo(DeclarationInfo): - - def __init__(self, name, include, - exporter_class = ClassExporter): - DeclarationInfo.__init__(self) - self._Attribute('name', name) - self._Attribute('include', include) - self._exporter_class = exporter_class - - - def Instantiate(self, type_list, rename=None): - if not rename: - rename = GenerateName(self._Attribute('name'), type_list) - # generate code to instantiate the template - types = ', '.join(type_list) - tail = 'typedef %s< %s > %s;\n' % (self._Attribute('name'), types, rename) - tail += 'void __instantiate_%s()\n' % rename - tail += '{ sizeof(%s); }\n\n' % rename - # create a ClassInfo - class_ = ClassInfo(rename, self._Attribute('include'), tail, self, - exporter_class = self._exporter_class) - return class_ - - - def __call__(self, types, rename=None): - if isinstance(types, str): - types = types.split() - return self.Instantiate(types, rename) - -#============================================================================== -# EnumInfo -#============================================================================== -class EnumInfo(DeclarationInfo): - - def __init__(self, name, include, exporter_class = EnumExporter): - DeclarationInfo.__init__(self) - self._Attribute('name', name) - self._Attribute('include', include) - self._Attribute('exclude', False) - self._Attribute('export_values', False) - exporter = exporter_class(InfoWrapper(self)) - self.AddExporter(exporter) - - -#============================================================================== -# HeaderInfo -#============================================================================== -class HeaderInfo(DeclarationInfo): - - def __init__(self, include, exporter_class = HeaderExporter): - warnings.warn('AllFromHeader is not working in all cases in the current version.') - DeclarationInfo.__init__(self) - self._Attribute('include', include) - exporter = exporter_class(InfoWrapper(self)) - self.AddExporter(exporter) - - -#============================================================================== -# VarInfo -#============================================================================== -class VarInfo(DeclarationInfo): - - def __init__(self, name, include, exporter_class = VarExporter): - DeclarationInfo.__init__(self) - self._Attribute('name', name) - self._Attribute('include', include) - exporter = exporter_class(InfoWrapper(self)) - self.AddExporter(exporter) - - -#============================================================================== -# CodeInfo -#============================================================================== -class CodeInfo(DeclarationInfo): - - def __init__(self, code, section, exporter_class = CodeExporter): - DeclarationInfo.__init__(self) - self._Attribute('code', code) - self._Attribute('section', section) - exporter = exporter_class(InfoWrapper(self)) - self.AddExporter(exporter) - - -#============================================================================== -# InfoWrapper -#============================================================================== -class InfoWrapper: - 'Provides a nicer interface for a info' - - def __init__(self, info): - self.__dict__['_info'] = info # so __setattr__ is not called - - def __getitem__(self, name): - return InfoWrapper(self._info[name]) - - def __getattr__(self, name): - return self._info._Attribute(name) - - def __setattr__(self, name, value): - self._info._Attribute(name, value) - - -#============================================================================== -# Functions -#============================================================================== -def exclude(info): - info._Attribute('exclude', True) - -def set_policy(info, policy): - info._Attribute('policy', policy) - -def rename(info, name): - info._Attribute('rename', name) - -def set_wrapper(info, wrapper): - if isinstance(wrapper, str): - wrapper = FunctionWrapper(wrapper) - info._Attribute('wrapper', wrapper) - -def instantiate(template, types, rename=None): - if isinstance(types, str): - types = types.split() - return template.Instantiate(types, rename) - -def use_shared_ptr(info): - info._Attribute('smart_ptr', 'boost::shared_ptr< %s >') - -def use_auto_ptr(info): - info._Attribute('smart_ptr', 'std::auto_ptr< %s >') - -def holder(info, function): - msg = "Expected a callable that accepts one string argument." - assert callable(function), msg - info._Attribute('holder', function) - -def add_method(info, name, rename=None): - added = info._Attribute('__added__') - if added is None: - info._Attribute('__added__', [(name, rename)]) - else: - added.append((name, rename)) - - -def class_code(info, code): - added = info._Attribute('__code__') - if added is None: - info._Attribute('__code__', [code]) - else: - added.append(code) - -def final(info): - info._Attribute('no_override', True) - - -def export_values(info): - info._Attribute('export_values', True) diff --git a/pyste/src/Pyste/policies.py b/pyste/src/Pyste/policies.py deleted file mode 100644 index 57ebd0de..00000000 --- a/pyste/src/Pyste/policies.py +++ /dev/null @@ -1,95 +0,0 @@ -# Copyright Bruno da Silva de Oliveira 2003. Use, modification and -# distribution is subject to the Boost Software License, Version 1.0. -# (See accompanying file LICENSE_1_0.txt or copy at -# http://www.boost.org/LICENSE_1_0.txt) - - - -class Policy(object): - 'Represents one of the call policies of boost.python.' - - def __init__(self): - if type(self) is Policy: - raise RuntimeError, "Can't create an instance of the class Policy" - - - def Code(self): - 'Returns the string corresponding to a instancialization of the policy.' - pass - - - def _next(self): - if self.next is not None: - return ', %s >' % self.next.Code() - else: - return ' >' - - - def __eq__(self, other): - try: - return self.Code() == other.Code() - except AttributeError: - return False - - - -class return_internal_reference(Policy): - 'Ties the return value to one of the parameters.' - - def __init__(self, param=1, next=None): - ''' - param is the position of the parameter, or None for "self". - next indicates the next policy, or None. - ''' - self.param = param - self.next=next - - - def Code(self): - c = 'return_internal_reference< %i' % self.param - c += self._next() - return c - - - -class with_custodian_and_ward(Policy): - 'Ties lifetime of two arguments of a function.' - - def __init__(self, custodian, ward, next=None): - self.custodian = custodian - self.ward = ward - self.next = next - - def Code(self): - c = 'with_custodian_and_ward< %i, %i' % (self.custodian, self.ward) - c += self._next() - return c - - - -class return_value_policy(Policy): - 'Policy to convert return values.' - - def __init__(self, which, next=None): - self.which = which - self.next = next - - - def Code(self): - c = 'return_value_policy< %s' % self.which - c += self._next() - return c - -class return_self(Policy): - - def Code(self): - return 'return_self<>' - - -# values for return_value_policy -reference_existing_object = 'reference_existing_object' -copy_const_reference = 'copy_const_reference' -copy_non_const_reference = 'copy_non_const_reference' -manage_new_object = 'manage_new_object' -return_opaque_pointer = 'return_opaque_pointer' -return_by_value = 'return_by_value' diff --git a/pyste/src/Pyste/pyste.py b/pyste/src/Pyste/pyste.py deleted file mode 100644 index cedffff5..00000000 --- a/pyste/src/Pyste/pyste.py +++ /dev/null @@ -1,424 +0,0 @@ -# Copyright Bruno da Silva de Oliveira 2003. Use, modification and -# distribution is subject to the Boost Software License, Version 1.0. -# (See accompanying file LICENSE_1_0.txt or copy at -# http://www.boost.org/LICENSE_1_0.txt) - -""" -Pyste version %s - -Usage: - pyste [options] interface-files - -where options are: - --module= The name of the module that will be generated; - defaults to the first interface filename, without - the extension. - -I Add an include path - -D Define symbol - --multiple Create various cpps, instead of only one - (useful during development) - --out= Specify output filename (default: .cpp) - in --multiple mode, this will be a directory - --no-using Do not declare "using namespace boost"; - use explicit declarations instead - --pyste-ns= Set the namespace where new types will be declared; - default is the empty namespace - --debug Writes the xml for each file parsed in the current - directory - --cache-dir= Directory for cache files (speeds up future runs) - --only-create-cache Recreates all caches (doesn't generate code). - --generate-main Generates the _main.cpp file (in multiple mode) - --file-list A file with one pyste file per line. Use as a - substitute for passing the files in the command - line. - --gccxml-path= Path to gccxml executable (default: gccxml) - --no-default-include Do not use INCLUDE environment variable for include - files to pass along gccxml. - -h, --help Print this help and exit - -v, --version Print version information -""" - -import sys -import os -import getopt -import exporters -import SingleCodeUnit -import MultipleCodeUnit -import infos -import exporterutils -import settings -import gc -import sys -from policies import * -from CppParser import CppParser, CppParserError -import time -import declarations - -__version__ = '0.9.30' - -def RecursiveIncludes(include): - 'Return a list containg the include dir and all its subdirectories' - dirs = [include] - def visit(arg, dir, names): - # ignore CVS dirs - if os.path.split(dir)[1] != 'CVS': - dirs.append(dir) - os.path.walk(include, visit, None) - return dirs - - -def GetDefaultIncludes(): - if 'INCLUDE' in os.environ: - include = os.environ['INCLUDE'] - return include.split(os.pathsep) - else: - return [] - - -def ProcessIncludes(includes): - if sys.platform == 'win32': - index = 0 - for include in includes: - includes[index] = include.replace('\\', '/') - index += 1 - - -def ReadFileList(filename): - f = file(filename) - files = [] - try: - for line in f: - line = line.strip() - if line: - files.append(line) - finally: - f.close() - return files - - -def ParseArguments(): - - def Usage(): - print __doc__ % __version__ - sys.exit(1) - - try: - options, files = getopt.getopt( - sys.argv[1:], - 'R:I:D:vh', - ['module=', 'multiple', 'out=', 'no-using', 'pyste-ns=', 'debug', 'cache-dir=', - 'only-create-cache', 'version', 'generate-main', 'file-list=', 'help', - 'gccxml-path=', 'no-default-include']) - except getopt.GetoptError, e: - print - print 'ERROR:', e - Usage() - - default_includes = GetDefaultIncludes() - includes = [] - defines = [] - module = None - out = None - multiple = False - cache_dir = None - create_cache = False - generate_main = False - gccxml_path = 'gccxml' - - for opt, value in options: - if opt == '-I': - includes.append(value) - elif opt == '-D': - defines.append(value) - elif opt == '-R': - includes.extend(RecursiveIncludes(value)) - elif opt == '--module': - module = value - elif opt == '--out': - out = value - elif opt == '--no-using': - settings.namespaces.python = 'boost::python::' - settings.USING_BOOST_NS = False - elif opt == '--pyste-ns': - settings.namespaces.pyste = value + '::' - elif opt == '--debug': - settings.DEBUG = True - elif opt == '--multiple': - multiple = True - elif opt == '--cache-dir': - cache_dir = value - elif opt == '--only-create-cache': - create_cache = True - elif opt == '--file-list': - files += ReadFileList(value) - elif opt in ['-h', '--help']: - Usage() - elif opt in ['-v', '--version']: - print 'Pyste version %s' % __version__ - sys.exit(2) - elif opt == '--generate-main': - generate_main = True - elif opt == '--gccxml-path': - gccxml_path = value - elif opt == '--no-default-include': - default_includes = [] - else: - print 'Unknown option:', opt - Usage() - - includes[0:0] = default_includes - if not files: - Usage() - if not module: - module = os.path.splitext(os.path.basename(files[0]))[0] - if not out: - out = module - if not multiple: - out += '.cpp' - for file in files: - d = os.path.dirname(os.path.abspath(file)) - if d not in sys.path: - sys.path.append(d) - - if create_cache and not cache_dir: - print 'Error: Use --cache-dir to indicate where to create the cache files!' - Usage() - sys.exit(3) - - if generate_main and not multiple: - print 'Error: --generate-main only valid in multiple mode.' - Usage() - sys.exit(3) - - ProcessIncludes(includes) - return includes, defines, module, out, files, multiple, cache_dir, create_cache, \ - generate_main, gccxml_path - - -def PCHInclude(*headers): - code = '\n'.join(['#include <%s>' % x for x in headers]) - infos.CodeInfo(code, 'pchinclude') - - -def CreateContext(): - 'create the context where a interface file will be executed' - context = {} - context['Import'] = Import - # infos - context['Function'] = infos.FunctionInfo - context['Class'] = infos.ClassInfo - context['Include'] = lambda header: infos.CodeInfo('#include <%s>\n' % header, 'include') - context['PCHInclude'] = PCHInclude - context['Template'] = infos.ClassTemplateInfo - context['Enum'] = infos.EnumInfo - context['AllFromHeader'] = infos.HeaderInfo - context['Var'] = infos.VarInfo - # functions - context['rename'] = infos.rename - context['set_policy'] = infos.set_policy - context['exclude'] = infos.exclude - context['set_wrapper'] = infos.set_wrapper - context['use_shared_ptr'] = infos.use_shared_ptr - context['use_auto_ptr'] = infos.use_auto_ptr - context['holder'] = infos.holder - context['add_method'] = infos.add_method - context['final'] = infos.final - context['export_values'] = infos.export_values - # policies - context['return_internal_reference'] = return_internal_reference - context['with_custodian_and_ward'] = with_custodian_and_ward - context['return_value_policy'] = return_value_policy - context['reference_existing_object'] = reference_existing_object - context['copy_const_reference'] = copy_const_reference - context['copy_non_const_reference'] = copy_non_const_reference - context['return_opaque_pointer'] = return_opaque_pointer - context['manage_new_object'] = manage_new_object - context['return_by_value'] = return_by_value - context['return_self'] = return_self - # utils - context['Wrapper'] = exporterutils.FunctionWrapper - context['declaration_code'] = lambda code: infos.CodeInfo(code, 'declaration-outside') - context['module_code'] = lambda code: infos.CodeInfo(code, 'module') - context['class_code'] = infos.class_code - return context - - -def Begin(): - # parse arguments - includes, defines, module, out, interfaces, multiple, cache_dir, create_cache, generate_main, gccxml_path = ParseArguments() - # run pyste scripts - for interface in interfaces: - ExecuteInterface(interface) - # create the parser - parser = CppParser(includes, defines, cache_dir, declarations.version, gccxml_path) - try: - if not create_cache: - if not generate_main: - return GenerateCode(parser, module, out, interfaces, multiple) - else: - return GenerateMain(module, out, OrderInterfaces(interfaces)) - else: - return CreateCaches(parser) - finally: - parser.Close() - - -def CreateCaches(parser): - # There is one cache file per interface so we organize the headers - # by interfaces. For each interface collect the tails from the - # exporters sharing the same header. - tails = JoinTails(exporters.exporters) - - # now for each interface file take each header, and using the tail - # get the declarations and cache them. - for interface, header in tails: - tail = tails[(interface, header)] - declarations = parser.ParseWithGCCXML(header, tail) - cachefile = parser.CreateCache(header, interface, tail, declarations) - print 'Cached', cachefile - - return 0 - - -_imported_count = {} # interface => count - -def ExecuteInterface(interface): - old_interface = exporters.current_interface - if not os.path.exists(interface): - if old_interface and os.path.exists(old_interface): - d = os.path.dirname(old_interface) - interface = os.path.join(d, interface) - if not os.path.exists(interface): - raise IOError, "Cannot find interface file %s."%interface - - _imported_count[interface] = _imported_count.get(interface, 0) + 1 - exporters.current_interface = interface - context = CreateContext() - context['INTERFACE_FILE'] = os.path.abspath(interface) - execfile(interface, context) - exporters.current_interface = old_interface - - -def Import(interface): - exporters.importing = True - ExecuteInterface(interface) - exporters.importing = False - - -def JoinTails(exports): - '''Returns a dict of {(interface, header): tail}, where tail is the - joining of all tails of all exports for the header. - ''' - tails = {} - for export in exports: - interface = export.interface_file - header = export.Header() - tail = export.Tail() or '' - if (interface, header) in tails: - all_tails = tails[(interface,header)] - all_tails += '\n' + tail - tails[(interface, header)] = all_tails - else: - tails[(interface, header)] = tail - - return tails - - - -def OrderInterfaces(interfaces): - interfaces_order = [(_imported_count[x], x) for x in interfaces] - interfaces_order.sort() - interfaces_order.reverse() - return [x for _, x in interfaces_order] - - - -def GenerateMain(module, out, interfaces): - codeunit = MultipleCodeUnit.MultipleCodeUnit(module, out) - codeunit.GenerateMain(interfaces) - return 0 - - -def GenerateCode(parser, module, out, interfaces, multiple): - # prepare to generate the wrapper code - if multiple: - codeunit = MultipleCodeUnit.MultipleCodeUnit(module, out) - else: - codeunit = SingleCodeUnit.SingleCodeUnit(module, out) - # stop referencing the exporters here - exports = exporters.exporters - exporters.exporters = None - exported_names = dict([(x.Name(), None) for x in exports]) - - # order the exports - order = {} - for export in exports: - if export.interface_file in order: - order[export.interface_file].append(export) - else: - order[export.interface_file] = [export] - exports = [] - interfaces_order = OrderInterfaces(interfaces) - for interface in interfaces_order: - exports.extend(order[interface]) - del order - del interfaces_order - - # now generate the code in the correct order - #print exported_names - tails = JoinTails(exports) - for i in xrange(len(exports)): - export = exports[i] - interface = export.interface_file - header = export.Header() - if header: - tail = tails[(interface, header)] - declarations, parsed_header = parser.Parse(header, interface, tail) - else: - declarations = [] - parsed_header = None - ExpandTypedefs(declarations, exported_names) - export.SetDeclarations(declarations) - export.SetParsedHeader(parsed_header) - if multiple: - codeunit.SetCurrent(export.interface_file, export.Name()) - export.GenerateCode(codeunit, exported_names) - # force collect of cyclic references - exports[i] = None - del declarations - del export - gc.collect() - # finally save the code unit - codeunit.Save() - if not multiple: - print 'Module %s generated' % module - return 0 - - -def ExpandTypedefs(decls, exported_names): - '''Check if the names in exported_names are a typedef, and add the real class - name in the dict. - ''' - for name in exported_names.keys(): - for decl in decls: - if isinstance(decl, declarations.Typedef): - exported_names[decl.type.FullName()] = None - -def UsePsyco(): - 'Tries to use psyco if possible' - try: - import psyco - psyco.profile() - except: pass - - -def main(): - start = time.clock() - UsePsyco() - status = Begin() - print '%0.2f seconds' % (time.clock()-start) - sys.exit(status) - - -if __name__ == '__main__': - main() diff --git a/pyste/src/Pyste/settings.py b/pyste/src/Pyste/settings.py deleted file mode 100644 index ba613b23..00000000 --- a/pyste/src/Pyste/settings.py +++ /dev/null @@ -1,21 +0,0 @@ -# Copyright Bruno da Silva de Oliveira 2003. Use, modification and -# distribution is subject to the Boost Software License, Version 1.0. -# (See accompanying file LICENSE_1_0.txt or copy at -# http://www.boost.org/LICENSE_1_0.txt) - - -#============================================================================== -# Global information -#============================================================================== - -DEBUG = False -USING_BOOST_NS = True - -class namespaces: - boost = 'boost::' - pyste = '' - python = '' # default is to not use boost::python namespace explicitly, so - # use the "using namespace" statement instead - -import sys -msvc = sys.platform == 'win32' diff --git a/pyste/src/Pyste/utils.py b/pyste/src/Pyste/utils.py deleted file mode 100644 index a8843e3f..00000000 --- a/pyste/src/Pyste/utils.py +++ /dev/null @@ -1,78 +0,0 @@ -# Copyright Bruno da Silva de Oliveira 2003. Use, modification and -# distribution is subject to the Boost Software License, Version 1.0. -# (See accompanying file LICENSE_1_0.txt or copy at -# http://www.boost.org/LICENSE_1_0.txt) - -from __future__ import generators -import string -import sys - -#============================================================================== -# enumerate -#============================================================================== -def enumerate(seq): - i = 0 - for x in seq: - yield i, x - i += 1 - - -#============================================================================== -# makeid -#============================================================================== -_valid_chars = string.ascii_letters + string.digits + '_' -_valid_chars = dict(zip(_valid_chars, _valid_chars)) - -def makeid(name): - 'Returns the name as a valid identifier' - if type(name) != str: - print type(name), name - newname = [] - for char in name: - if char not in _valid_chars: - char = '_' - newname.append(char) - newname = ''.join(newname) - # avoid duplications of '_' chars - names = [x for x in newname.split('_') if x] - return '_'.join(names) - - -#============================================================================== -# remove_duplicated_lines -#============================================================================== -def remove_duplicated_lines(text): - includes = text.splitlines() - d = dict([(include, 0) for include in includes]) - includes = d.keys() - includes.sort() - return '\n'.join(includes) - - -#============================================================================== -# left_equals -#============================================================================== -def left_equals(s): - s = '// %s ' % s - return s + ('='*(80-len(s))) + '\n' - - -#============================================================================== -# post_mortem -#============================================================================== -def post_mortem(): - - def info(type, value, tb): - if hasattr(sys, 'ps1') or not sys.stderr.isatty(): - # we are in interactive mode or we don't have a tty-like - # device, so we call the default hook - sys.__excepthook__(type, value, tb) - else: - import traceback, pdb - # we are NOT in interactive mode, print the exception... - traceback.print_exception(type, value, tb) - print - # ...then start the debugger in post-mortem mode. - pdb.pm() - - sys.excepthook = info diff --git a/pyste/tests/GCCXMLParserUT.py b/pyste/tests/GCCXMLParserUT.py deleted file mode 100644 index 7175c9c6..00000000 --- a/pyste/tests/GCCXMLParserUT.py +++ /dev/null @@ -1,341 +0,0 @@ -# Copyright Bruno da Silva de Oliveira 2003. Use, modification and -# distribution is subject to the Boost Software License, Version 1.0. -# (See accompanying file LICENSE_1_0.txt or copy at -# http://www.boost.org/LICENSE_1_0.txt) - -import sys -sys.path.append('../src') -import unittest -import tempfile -import os.path -from Pyste import GCCXMLParser -from Pyste.declarations import * - - -class Tester(unittest.TestCase): - - def TestConstructor(self, class_, method, visib): - self.assert_(isinstance(method, Constructor)) - self.assertEqual(method.FullName(), class_.FullName() + '::' + method.name) - self.assertEqual(method.result, None) - self.assertEqual(method.visibility, visib) - self.assert_(not method.virtual) - self.assert_(not method.abstract) - self.assert_(not method.static) - - def TestDefaultConstructor(self, class_, method, visib): - self.TestConstructor(class_, method, visib) - self.assert_(method.IsDefault()) - - def TestCopyConstructor(self, class_, method, visib): - self.TestConstructor(class_, method, visib) - self.assertEqual(len(method.parameters), 1) - param = method.parameters[0] - self.TestType( - param, - ReferenceType, - class_.FullName(), - 'const %s&' % class_.FullName(), - True) - self.assert_(method.IsCopy()) - - - def TestType(self, type_, classtype_, name, fullname, const): - self.assert_(isinstance(type_, classtype_)) - self.assertEqual(type_.name, name) - self.assertEqual(type_.namespace, None) - self.assertEqual(type_.FullName(), fullname) - self.assertEqual(type_.const, const) - - -class ClassBaseTest(Tester): - - def setUp(self): - self.base = GetDecl('Base') - - def testClass(self): - 'test the properties of the class Base' - self.assert_(isinstance(self.base, Class)) - self.assert_(self.base.abstract) - - - def testFoo(self): - 'test function foo in class Base' - foo = GetMember(self.base, 'foo') - self.assert_(isinstance(foo, Method)) - self.assertEqual(foo.visibility, Scope.public) - self.assert_(foo.virtual) - self.assert_(foo.abstract) - self.failIf(foo.static) - self.assertEqual(foo.class_, 'test::Base') - self.failIf(foo.const) - self.assertEqual(foo.FullName(), 'test::Base::foo') - self.assertEqual(foo.result.name, 'void') - self.assertEqual(len(foo.parameters), 1) - param = foo.parameters[0] - self.TestType(param, FundamentalType, 'int', 'int', False) - self.assertEqual(foo.namespace, None) - self.assertEqual( - foo.PointerDeclaration(1), '(void (test::Base::*)(int) )&test::Base::foo') - - def testX(self): - 'test the member x in class Base' - x = GetMember(self.base, 'x') - self.assertEqual(x.class_, 'test::Base') - self.assertEqual(x.FullName(), 'test::Base::x') - self.assertEqual(x.namespace, None) - self.assertEqual(x.visibility, Scope.private) - self.TestType(x.type, FundamentalType, 'int', 'int', False) - self.assertEqual(x.static, False) - - def testConstructors(self): - 'test constructors in class Base' - constructors = GetMembers(self.base, 'Base') - for cons in constructors: - if len(cons.parameters) == 0: - self.TestDefaultConstructor(self.base, cons, Scope.public) - elif len(cons.parameters) == 1: # copy constructor - self.TestCopyConstructor(self.base, cons, Scope.public) - elif len(cons.parameters) == 2: # other constructor - intp, floatp = cons.parameters - self.TestType(intp, FundamentalType, 'int', 'int', False) - self.TestType(floatp, FundamentalType, 'float', 'float', False) - - def testSimple(self): - 'test function simple in class Base' - simple = GetMember(self.base, 'simple') - self.assert_(isinstance(simple, Method)) - self.assertEqual(simple.visibility, Scope.protected) - self.assertEqual(simple.FullName(), 'test::Base::simple') - self.assertEqual(len(simple.parameters), 1) - param = simple.parameters[0] - self.TestType(param, ReferenceType, 'std::string', 'const std::string&', True) - self.TestType(simple.result, FundamentalType, 'bool', 'bool', False) - self.assertEqual( - simple.PointerDeclaration(1), - '(bool (test::Base::*)(const std::string&) )&test::Base::simple') - - - def testZ(self): - z = GetMember(self.base, 'z') - self.assert_(isinstance(z, Variable)) - self.assertEqual(z.visibility, Scope.public) - self.assertEqual(z.FullName(), 'test::Base::z') - self.assertEqual(z.type.name, 'int') - self.assertEqual(z.type.const, False) - self.assert_(z.static) - - -class ClassTemplateTest(Tester): - - def setUp(self): - self.template = GetDecl('Template') - - def testClass(self): - 'test the properties of the Template class' - self.assert_(isinstance(self.template, Class)) - self.assert_(not self.template.abstract) - self.assertEqual(self.template.FullName(), 'Template') - self.assertEqual(self.template.namespace, '') - self.assertEqual(self.template.name, 'Template') - - def testConstructors(self): - 'test the automatic constructors of the class Template' - constructors = GetMembers(self.template, 'Template') - for cons in constructors: - if len(cons.parameters) == 0: - self.TestDefaultConstructor(self.template, cons, Scope.public) - elif len(cons.parameters) == 1: - self.TestCopyConstructor(self.template, cons, Scope.public) - - - def testValue(self): - 'test the class variable value' - value = GetMember(self.template, 'value') - self.assert_(isinstance(value, ClassVariable)) - self.assert_(value.name, 'value') - self.TestType(value.type, FundamentalType, 'int', 'int', False) - self.assert_(not value.static) - self.assertEqual(value.visibility, Scope.public) - self.assertEqual(value.class_, 'Template') - self.assertEqual(value.FullName(), 'Template::value') - - def testBase(self): - 'test the superclasses of Template' - bases = self.template.bases - self.assertEqual(len(bases), 1) - base = bases[0] - self.assert_(isinstance(base, Base)) - self.assertEqual(base.name, 'test::Base') - self.assertEqual(base.visibility, Scope.protected) - - - -class FreeFuncTest(Tester): - - def setUp(self): - self.func = GetDecl('FreeFunc') - - def testFunc(self): - 'test attributes of FreeFunc' - self.assert_(isinstance(self.func, Function)) - self.assertEqual(self.func.name, 'FreeFunc') - self.assertEqual(self.func.FullName(), 'test::FreeFunc') - self.assertEqual(self.func.namespace, 'test') - self.assertEqual( - self.func.PointerDeclaration(1), - '(const test::Base& (*)(const std::string&, int))&test::FreeFunc') - - - def testResult(self): - 'test the return value of FreeFunc' - res = self.func.result - self.TestType(res, ReferenceType, 'test::Base', 'const test::Base&', True) - - def testParameters(self): - 'test the parameters of FreeFunc' - self.assertEqual(len(self.func.parameters), 2) - strp, intp = self.func.parameters - self.TestType(strp, ReferenceType, 'std::string', 'const std::string&', True) - self.assertEqual(strp.default, None) - self.TestType(intp, FundamentalType, 'int', 'int', False) - self.assertEqual(intp.default, '10') - - - -class testFunctionPointers(Tester): - - def testMethodPointer(self): - 'test declaration of a pointer-to-method' - meth = GetDecl('MethodTester') - param = meth.parameters[0] - fullname = 'void (test::Base::*)(int)' - self.TestType(param, PointerType, fullname, fullname, False) - - def testFunctionPointer(self): - 'test declaration of a pointer-to-function' - func = GetDecl('FunctionTester') - param = func.parameters[0] - fullname = 'void (*)(int)' - self.TestType(param, PointerType, fullname, fullname, False) - - - -# ============================================================================= -# Support routines -# ============================================================================= - -cppcode = ''' -namespace std { - class string; -} -namespace test { -class Base -{ -public: - Base(); - Base(const Base&); - Base(int, float); - - virtual void foo(int = 0.0) = 0; - static int z; -protected: - bool simple(const std::string&); -private: - int x; -}; - -void MethodTester( void (Base::*)(int) ); -void FunctionTester( void (*)(int) ); - - -const Base & FreeFunc(const std::string&, int=10); - -} - -template -struct Template: protected test::Base -{ - T value; - virtual void foo(int); -}; - -Template __aTemplateInt; -''' - -def GetXMLFile(): - '''Generates an gccxml file using the code from the global cppcode. - Returns the xml's filename.''' - # write the code to a header file - tmpfile = tempfile.mktemp() + '.h' - f = file(tmpfile, 'w') - f.write(cppcode) - f.close() - # run gccxml - outfile = tmpfile + '.xml' - if os.system('gccxml "%s" "-fxml=%s"' % (tmpfile, outfile)) != 0: - raise RuntimeError, 'Error executing GCCXML.' - # read the output file into the xmlcode - f = file(outfile) - xmlcode = f.read() - #print xmlcode - f.close() - # remove the header - os.remove(tmpfile) - return outfile - - - -def GetDeclarations(): - 'Uses the GCCXMLParser module to get the declarations.' - xmlfile = GetXMLFile() - declarations = GCCXMLParser.ParseDeclarations(xmlfile) - os.remove(xmlfile) - return declarations - -# the declarations to be analysed -declarations = GetDeclarations() - - -def GetDecl(name): - 'returns one of the top declarations given its name' - for decl in declarations: - if decl.name == name: - return decl - else: - raise RuntimeError, 'Declaration not found: %s' % name - - -def GetMember(class_, name): - 'gets the member of the given class by its name' - - res = None - multipleFound = False - for member in class_: - if member.name == name: - if res is not None: - multipleFound = True - break - res = member - if res is None or multipleFound: - raise RuntimeError, \ - 'No member or more than one member found in class %s: %s' \ - % (class_.name, name) - return res - - -def GetMembers(class_, name): - 'gets the members of the given class by its name' - res = [] - for member in class_: - if member.name == name: - res.append(member) - if len(res) in (0, 1): - raise RuntimeError, \ - 'GetMembers: 0 or 1 members found in class %s: %s' \ - % (class_.name, name) - return res - - -if __name__ == '__main__': - unittest.main() diff --git a/pyste/tests/SmartFileUT.py b/pyste/tests/SmartFileUT.py deleted file mode 100644 index 9e4e9988..00000000 --- a/pyste/tests/SmartFileUT.py +++ /dev/null @@ -1,84 +0,0 @@ -# Copyright Bruno da Silva de Oliveira 2003. Use, modification and -# distribution is subject to the Boost Software License, Version 1.0. -# (See accompanying file LICENSE_1_0.txt or copy at -# http://www.boost.org/LICENSE_1_0.txt) -import sys -sys.path.append('../src') -from SmartFile import * -import unittest -import tempfile -import os -import time - - -class SmartFileTest(unittest.TestCase): - - FILENAME = tempfile.mktemp() - - def setUp(self): - self._Clean() - - def tearDown(self): - self._Clean() - - def _Clean(self): - try: - os.remove(self.FILENAME) - except OSError: pass - - - def testNonExistant(self): - "Must override the file, as there's no file in the disk yet" - self.assert_(not os.path.isfile(self.FILENAME)) - f = SmartFile(self.FILENAME, 'w') - f.write('Testing 123\nTesting again.') - f.close() - self.assert_(os.path.isfile(self.FILENAME)) - - - def testOverride(self): - "Must override the file, because the contents are different" - contents = 'Contents!\nContents!' - # create the file normally first - f = file(self.FILENAME, 'w') - f.write(contents) - f.close() - file_time = os.path.getmtime(self.FILENAME) - self.assert_(os.path.isfile(self.FILENAME)) - time.sleep(2) - f = SmartFile(self.FILENAME, 'w') - f.write(contents + '_') - f.close() - new_file_time = os.path.getmtime(self.FILENAME) - self.assert_(new_file_time != file_time) - - - def testNoOverride(self): - "Must not override the file, because the contents are the same" - contents = 'Contents!\nContents!' - # create the file normally first - f = file(self.FILENAME, 'w') - f.write(contents) - f.close() - file_time = os.path.getmtime(self.FILENAME) - self.assert_(os.path.isfile(self.FILENAME)) - time.sleep(2) - f = SmartFile(self.FILENAME, 'w') - f.write(contents) - f.close() - new_file_time = os.path.getmtime(self.FILENAME) - self.assert_(new_file_time == file_time) - - - def testAutoClose(self): - "Must be closed when garbage-collected" - def foo(): - f = SmartFile(self.FILENAME) - f.write('testing') - self.assert_(not os.path.isfile(self.FILENAME)) - foo() - self.assert_(os.path.isfile(self.FILENAME)) - - -if __name__ == '__main__': - unittest.main() diff --git a/pyste/tests/abstract_test.h b/pyste/tests/abstract_test.h deleted file mode 100644 index e0fba2b2..00000000 --- a/pyste/tests/abstract_test.h +++ /dev/null @@ -1,22 +0,0 @@ -/* Copyright Bruno da Silva de Oliveira 2003. Use, modification and - distribution is subject to the Boost Software License, Version 1.0. - (See accompanying file LICENSE_1_0.txt or copy at - http://www.boost.org/LICENSE_1_0.txt) - */ -#include -#include - -namespace abstract { - -struct A { - virtual ~A() {} - virtual std::string f()=0; -}; - -struct B: A { - std::string f() { return "B::f"; } -}; - -std::string call(A* a) { return a->f(); } - -} diff --git a/pyste/tests/abstract_test.pyste b/pyste/tests/abstract_test.pyste deleted file mode 100644 index c65bb3ad..00000000 --- a/pyste/tests/abstract_test.pyste +++ /dev/null @@ -1,3 +0,0 @@ -Class('abstract::A', 'abstract_test.h') -Class('abstract::B', 'abstract_test.h') -Function('abstract::call', 'abstract_test.h') diff --git a/pyste/tests/abstract_testUT.py b/pyste/tests/abstract_testUT.py deleted file mode 100644 index 4dc61a26..00000000 --- a/pyste/tests/abstract_testUT.py +++ /dev/null @@ -1,26 +0,0 @@ -# Copyright Bruno da Silva de Oliveira 2003. Use, modification and -# distribution is subject to the Boost Software License, Version 1.0. -# (See accompanying file LICENSE_1_0.txt or copy at -# http://www.boost.org/LICENSE_1_0.txt) -import unittest -from _abstract_test import * - -class AbstractTest(unittest.TestCase): - - def testIt(self): - class C(A): - def f(self): - return 'C::f' - - a = A() - b = B() - c = C() - self.assertRaises(RuntimeError, a.f) - self.assertEqual(b.f(), 'B::f') - self.assertEqual(call(b), 'B::f') - self.assertEqual(c.f(), 'C::f') - self.assertEqual(call(c), 'C::f') - - -if __name__ == '__main__': - unittest.main() diff --git a/pyste/tests/add_test.h b/pyste/tests/add_test.h deleted file mode 100644 index 447e8814..00000000 --- a/pyste/tests/add_test.h +++ /dev/null @@ -1,18 +0,0 @@ -/* Copyright Bruno da Silva de Oliveira 2003. Use, modification and - distribution is subject to the Boost Software License, Version 1.0. - (See accompanying file LICENSE_1_0.txt or copy at - http://www.boost.org/LICENSE_1_0.txt) - */ -namespace add_test { - -struct C -{ - int x; -}; - -const int get_x(C& c) -{ - return c.x; -} - -} diff --git a/pyste/tests/add_test.pyste b/pyste/tests/add_test.pyste deleted file mode 100644 index cc7faa9e..00000000 --- a/pyste/tests/add_test.pyste +++ /dev/null @@ -1,2 +0,0 @@ -C = Class('add_test::C', 'add_test.h') -add_method(C, 'add_test::get_x') diff --git a/pyste/tests/add_testUT.py b/pyste/tests/add_testUT.py deleted file mode 100644 index 16f57a32..00000000 --- a/pyste/tests/add_testUT.py +++ /dev/null @@ -1,16 +0,0 @@ -# Copyright Bruno da Silva de Oliveira 2003. Use, modification and -# distribution is subject to the Boost Software License, Version 1.0. -# (See accompanying file LICENSE_1_0.txt or copy at -# http://www.boost.org/LICENSE_1_0.txt) -import unittest -from _add_test import * - -class AddMethodTest(unittest.TestCase): - - def testIt(self): - c = C() - c.x = 10 - self.assertEqual(c.get_x(), 10) - -if __name__ == '__main__': - unittest.main() diff --git a/pyste/tests/basic.cpp b/pyste/tests/basic.cpp deleted file mode 100644 index d07b6da6..00000000 --- a/pyste/tests/basic.cpp +++ /dev/null @@ -1,13 +0,0 @@ -/* Copyright Bruno da Silva de Oliveira 2003. Use, modification and - distribution is subject to the Boost Software License, Version 1.0. - (See accompanying file LICENSE_1_0.txt or copy at - http://www.boost.org/LICENSE_1_0.txt) - */ -#include "basic.h" - -namespace basic { - - int C::static_value = 3; - const int C::const_static_value = 100; - -} diff --git a/pyste/tests/basic.h b/pyste/tests/basic.h deleted file mode 100644 index 690fed2d..00000000 --- a/pyste/tests/basic.h +++ /dev/null @@ -1,69 +0,0 @@ -/* Copyright Bruno da Silva de Oliveira 2003. Use, modification and - distribution is subject to the Boost Software License, Version 1.0. - (See accompanying file LICENSE_1_0.txt or copy at - http://www.boost.org/LICENSE_1_0.txt) - */ -#ifndef BASIC_H -#define BASIC_H - - -#include - -namespace basic { - -struct C -{ - // test virtuallity - C(): value(1), const_value(0) {} - virtual int f(int x = 10) - { - return x*2; - } - - int foo(int x=1){ - return x+1; - } - - const std::string& get_name() { return name; } - void set_name(const std::string& name) { this->name = name; } -private: - std::string name; - -public: - // test data members - static int static_value; - static const int const_static_value; - - int value; - const int const_value; - - // test static functions - static int mul(int x, int y) { return x*y; } - static double mul(double x, double y) { return x*y; } - - static int square(int x=2) { return x*x; } -}; - -inline int call_f(C& c) -{ - return c.f(); -} - -inline int call_f(C& c, int x) -{ - return c.f(x); -} - -inline int get_static() -{ - return C::static_value; -} - -inline int get_value(C& c) -{ - return c.value; -} - -} - -#endif diff --git a/pyste/tests/basic.pyste b/pyste/tests/basic.pyste deleted file mode 100644 index 4fe0b5b3..00000000 --- a/pyste/tests/basic.pyste +++ /dev/null @@ -1,5 +0,0 @@ -Class('basic::C', 'basic.h') -Function('basic::call_f', 'basic.h') -Function('basic::get_static', 'basic.h') -Function('basic::get_value', 'basic.h') - diff --git a/pyste/tests/basicUT.py b/pyste/tests/basicUT.py deleted file mode 100644 index 9a18bcbf..00000000 --- a/pyste/tests/basicUT.py +++ /dev/null @@ -1,73 +0,0 @@ -# Copyright Bruno da Silva de Oliveira 2003. Use, modification and -# distribution is subject to the Boost Software License, Version 1.0. -# (See accompanying file LICENSE_1_0.txt or copy at -# http://www.boost.org/LICENSE_1_0.txt) -import unittest -from _basic import * - -class BasicExampleTest(unittest.TestCase): - - def testIt(self): - - # test virtual functions - class D(C): - def f(self, x=10): - return x+1 - - d = D() - c = C() - - self.assertEqual(c.f(), 20) - self.assertEqual(c.f(3), 6) - self.assertEqual(d.f(), 11) - self.assertEqual(d.f(3), 4) - self.assertEqual(call_f(c), 20) - self.assertEqual(call_f(c, 4), 8) - self.assertEqual(call_f(d), 11) - self.assertEqual(call_f(d, 3), 4) - - # test data members - def testValue(value): - self.assertEqual(c.value, value) - self.assertEqual(d.value, value) - self.assertEqual(get_value(c), value) - self.assertEqual(get_value(d), value) - testValue(1) - c.value = 30 - d.value = 30 - testValue(30) - self.assertEqual(c.const_value, 0) - self.assertEqual(d.const_value, 0) - def set_const_value(): - c.const_value = 12 - self.assertRaises(AttributeError, set_const_value) - - # test static data-members - def testStatic(value): - self.assertEqual(C.static_value, value) - self.assertEqual(c.static_value, value) - self.assertEqual(D.static_value, value) - self.assertEqual(d.static_value, value) - self.assertEqual(get_static(), value) - testStatic(3) - C.static_value = 10 - testStatic(10) - self.assertEqual(C.const_static_value, 100) - def set_const_static(): - C.const_static_value = 1 - self.assertRaises(AttributeError, set_const_static) - - # test static function - def test_mul(result, *args): - self.assertEqual(C.mul(*args), result) - self.assertEqual(c.mul(*args), result) - test_mul(16, 8, 2) - test_mul(6.0, 2.0, 3.0) - self.assertEqual(C.square(), 4) - self.assertEqual(c.square(), 4) - self.assertEqual(C.square(3), 9) - self.assertEqual(c.square(3), 9) - - -if __name__ == '__main__': - unittest.main() diff --git a/pyste/tests/code_test.h b/pyste/tests/code_test.h deleted file mode 100644 index 0a31205a..00000000 --- a/pyste/tests/code_test.h +++ /dev/null @@ -1,8 +0,0 @@ -/* Copyright Bruno da Silva de Oliveira 2003. Use, modification and - distribution is subject to the Boost Software License, Version 1.0. - (See accompanying file LICENSE_1_0.txt or copy at - http://www.boost.org/LICENSE_1_0.txt) - */ -struct A { - int x; -}; diff --git a/pyste/tests/code_test.pyste b/pyste/tests/code_test.pyste deleted file mode 100644 index 467996fd..00000000 --- a/pyste/tests/code_test.pyste +++ /dev/null @@ -1,9 +0,0 @@ -Class('A', 'code_test.h') -Include('string') -declaration_code(''' -int get(A& a) { return a.x; } - -std::string foo() { return "Hello!"; } -''') -module_code(' def("get", &get);\n') -module_code(' def("foo", &foo);\n') diff --git a/pyste/tests/code_testUT.py b/pyste/tests/code_testUT.py deleted file mode 100644 index 1059570a..00000000 --- a/pyste/tests/code_testUT.py +++ /dev/null @@ -1,18 +0,0 @@ -# Copyright Bruno da Silva de Oliveira 2003. Use, modification and -# distribution is subject to the Boost Software License, Version 1.0. -# (See accompanying file LICENSE_1_0.txt or copy at -# http://www.boost.org/LICENSE_1_0.txt) -import unittest -from _code_test import * - -class CodeTest(unittest.TestCase): - - def testIt(self): - a = A() - a.x = 12 - self.assertEqual(get(a), 12) - self.assertEqual(foo(), "Hello!") - - -if __name__ == '__main__': - unittest.main() diff --git a/pyste/tests/enums.h b/pyste/tests/enums.h deleted file mode 100644 index afe33ca4..00000000 --- a/pyste/tests/enums.h +++ /dev/null @@ -1,34 +0,0 @@ -/* Copyright Bruno da Silva de Oliveira 2003. Use, modification and - distribution is subject to the Boost Software License, Version 1.0. - (See accompanying file LICENSE_1_0.txt or copy at - http://www.boost.org/LICENSE_1_0.txt) - */ -#ifndef ENUMS_H -#define ENUMS_H - -namespace enums { - -enum color { red, blue }; - -struct X -{ - enum choices - { - good = 1, - bad = 2 - }; - - int set(choices c) - { - return (int)c; - } -}; - -enum { - x = 0, - y = 1 -}; - -} - -#endif diff --git a/pyste/tests/enums.pyste b/pyste/tests/enums.pyste deleted file mode 100644 index c18a1244..00000000 --- a/pyste/tests/enums.pyste +++ /dev/null @@ -1,8 +0,0 @@ -h = AllFromHeader('enums.h') -rename(h.color.red, 'Red') -rename(h.color.blue, 'Blue') -export_values(h.color) -rename(h.X.choices.bad, 'Bad') -rename(h.X.choices.good, 'Good') -rename(h.X.choices, 'Choices') - diff --git a/pyste/tests/enumsUT.py b/pyste/tests/enumsUT.py deleted file mode 100644 index 7c7720dc..00000000 --- a/pyste/tests/enumsUT.py +++ /dev/null @@ -1,24 +0,0 @@ -# Copyright Bruno da Silva de Oliveira 2003. Use, modification and -# distribution is subject to the Boost Software License, Version 1.0. -# (See accompanying file LICENSE_1_0.txt or copy at -# http://www.boost.org/LICENSE_1_0.txt) -import unittest -from _enums import * - -class EnumsTest(unittest.TestCase): - - def testIt(self): - self.assertEqual(int(Red), 0) - self.assertEqual(int(Blue), 1) - - self.assertEqual(int(X.Choices.Good), 1) - self.assertEqual(int(X.Choices.Bad), 2) - a = X() - self.assertEqual(a.set(a.Choices.Good), 1) - self.assertEqual(a.set(a.Choices.Bad), 2) - self.assertEqual(x, 0) - self.assertEqual(y, 1) - - -if __name__ == '__main__': - unittest.main() diff --git a/pyste/tests/header_test.h b/pyste/tests/header_test.h deleted file mode 100644 index 030d0d26..00000000 --- a/pyste/tests/header_test.h +++ /dev/null @@ -1,43 +0,0 @@ -/* Copyright Bruno da Silva de Oliveira 2003. Use, modification and - distribution is subject to the Boost Software License, Version 1.0. - (See accompanying file LICENSE_1_0.txt or copy at - http://www.boost.org/LICENSE_1_0.txt) - */ -#ifndef HEADER_TEST_H -#define HEADER_TEST_H - -#include -#include - -namespace header_test { - -enum choice { red, blue }; - -inline std::string choice_str(choice c) -{ - std::map choice_map; - choice_map[red] = "red"; - choice_map[blue] = "blue"; - return choice_map[c]; -} - -struct C -{ - choice c; - - std::string get() - { - return choice_str(c); - } -}; - -// test the exclusion of the following - -struct ForwardDeclared; // should be excluded automatically -struct A {}; -void foo(); -enum bar { value }; - -} - -#endif diff --git a/pyste/tests/header_test.pyste b/pyste/tests/header_test.pyste deleted file mode 100644 index 3bd55501..00000000 --- a/pyste/tests/header_test.pyste +++ /dev/null @@ -1,4 +0,0 @@ -h = AllFromHeader('header_test.h') -exclude(h.A) -exclude(h.foo) -exclude(h.bar) diff --git a/pyste/tests/header_testUT.py b/pyste/tests/header_testUT.py deleted file mode 100644 index aa0d4a16..00000000 --- a/pyste/tests/header_testUT.py +++ /dev/null @@ -1,27 +0,0 @@ -# Copyright Bruno da Silva de Oliveira 2003. Use, modification and -# distribution is subject to the Boost Software License, Version 1.0. -# (See accompanying file LICENSE_1_0.txt or copy at -# http://www.boost.org/LICENSE_1_0.txt) -import unittest -from _header_test import * - -class HeaderTest(unittest.TestCase): - - def testIt(self): - self.assertEqual(choice.red, 0) - self.assertEqual(choice.blue, 1) - self.assertEqual(choice_str(choice.blue), 'blue') - self.assertEqual(choice_str(choice.red), 'red') - c = C() - c.c = choice.blue - self.assertEqual(c.get(), 'blue') - c.c = choice.red - self.assertEqual(c.get(), 'red') - # the following classes/functions should not have being exported - self.assertRaises(NameError, lambda: A()) - self.assertRaises(NameError, lambda: foo()) - self.assertRaises(NameError, lambda: bar.value) - self.assertRaises(NameError, lambda: ForwardDeclared()) - -if __name__ == '__main__': - unittest.main() diff --git a/pyste/tests/infosUT.py b/pyste/tests/infosUT.py deleted file mode 100644 index 93769f34..00000000 --- a/pyste/tests/infosUT.py +++ /dev/null @@ -1,55 +0,0 @@ -# Copyright Bruno da Silva de Oliveira 2003. Use, modification and -# distribution is subject to the Boost Software License, Version 1.0. -# (See accompanying file LICENSE_1_0.txt or copy at -# http://www.boost.org/LICENSE_1_0.txt) -import sys -from Pyste.infos import * -from Pyste.policies import * -from Pyste.exporterutils import * -import unittest - -#================================================================================ -# InfosTest -#================================================================================ -class InfosTest(unittest.TestCase): - - def testFunctionInfo(self): - info = FunctionInfo('test::foo', 'foo.h') - rename(info, 'hello') - set_policy(info, return_internal_reference()) - set_wrapper(info, FunctionWrapper('foo_wrapper')) - - info = InfoWrapper(info) - - self.assertEqual(info.rename, 'hello') - self.assertEqual(info.policy.Code(), 'return_internal_reference< 1 >') - self.assertEqual(info.wrapper.name, 'foo_wrapper') - - - def testClassInfo(self): - info = ClassInfo('test::IFoo', 'foo.h') - rename(info.name, 'Name') - rename(info.exclude, 'Exclude') - rename(info, 'Foo') - rename(info.Bar, 'bar') - set_policy(info.Baz, return_internal_reference()) - rename(info.operator['>>'], 'from_string') - exclude(info.Bar) - set_wrapper(info.Baz, FunctionWrapper('baz_wrapper')) - - info = InfoWrapper(info) - - self.assertEqual(info.rename, 'Foo') - self.assertEqual(info['Bar'].rename, 'bar') - self.assertEqual(info['name'].rename, 'Name') - self.assertEqual(info['exclude'].rename, 'Exclude') - self.assertEqual(info['Bar'].exclude, True) - self.assertEqual(info['Baz'].policy.Code(), 'return_internal_reference< 1 >') - self.assertEqual(info['Baz'].wrapper.name, 'baz_wrapper') - self.assertEqual(info['operator']['>>'].rename, 'from_string') - - - - -if __name__ == '__main__': - unittest.main() diff --git a/pyste/tests/inherit.cpp b/pyste/tests/inherit.cpp deleted file mode 100644 index a75e8389..00000000 --- a/pyste/tests/inherit.cpp +++ /dev/null @@ -1,8 +0,0 @@ -/* Copyright Bruno da Silva de Oliveira 2003. Use, modification and - distribution is subject to the Boost Software License, Version 1.0. - (See accompanying file LICENSE_1_0.txt or copy at - http://www.boost.org/LICENSE_1_0.txt) - */ -#include "inherit.h" - -int inherit::C::s = 1; diff --git a/pyste/tests/inherit.h b/pyste/tests/inherit.h deleted file mode 100644 index 8f903f4f..00000000 --- a/pyste/tests/inherit.h +++ /dev/null @@ -1,43 +0,0 @@ -/* Copyright Bruno da Silva de Oliveira 2003. Use, modification and - distribution is subject to the Boost Software License, Version 1.0. - (See accompanying file LICENSE_1_0.txt or copy at - http://www.boost.org/LICENSE_1_0.txt) - */ -namespace inherit { - -template -class A -{ -public: - void set(T v) { mData = v; } - - T get() const { return mData; } - -private: - T mData; -}; - - -class B : public A -{ -public: - int go() { return get(); } -}; - -struct C : B -{ - enum ab { a = 1, b = 2 }; - int f1() { return 1; } - int x; - static int s; -}; - -struct D : C -{ - int f2() { return 2; } - int y; -}; - -struct X {}; -struct E: X, D {}; -} diff --git a/pyste/tests/inherit.pyste b/pyste/tests/inherit.pyste deleted file mode 100644 index 0dc02998..00000000 --- a/pyste/tests/inherit.pyste +++ /dev/null @@ -1,8 +0,0 @@ -A = Template('inherit::A', 'inherit.h') -A_int = A('int', 'A_int') - -Class('inherit::B', 'inherit.h') -Class('inherit::D', 'inherit.h') -E = Class('inherit::E', 'inherit.h') -exclude(E.s) -exclude(E.ab) diff --git a/pyste/tests/inherit2.h b/pyste/tests/inherit2.h deleted file mode 100644 index af9387bd..00000000 --- a/pyste/tests/inherit2.h +++ /dev/null @@ -1,35 +0,0 @@ -/* Copyright Bruno da Silva de Oliveira 2003. Use, modification and - distribution is subject to the Boost Software License, Version 1.0. - (See accompanying file LICENSE_1_0.txt or copy at - http://www.boost.org/LICENSE_1_0.txt) - */ -namespace inherit2 { - -struct A -{ - int x; - int getx() { return x; } - int foo() { return 0; } - int foo(int x) { return x; } -}; - -struct B : A -{ - int y; - int gety() { return y; } - int foo() { return 1; } -}; - -struct C : B -{ - int z; - int getz() { return z; } -}; - -struct D : C -{ - int w; - int getw() { return w; } -}; - -} diff --git a/pyste/tests/inherit2.pyste b/pyste/tests/inherit2.pyste deleted file mode 100644 index 38082139..00000000 --- a/pyste/tests/inherit2.pyste +++ /dev/null @@ -1,2 +0,0 @@ -Class('inherit2::B', 'inherit2.h') -Class('inherit2::D', 'inherit2.h') diff --git a/pyste/tests/inherit2UT.py b/pyste/tests/inherit2UT.py deleted file mode 100644 index 85afce61..00000000 --- a/pyste/tests/inherit2UT.py +++ /dev/null @@ -1,31 +0,0 @@ -# Copyright Bruno da Silva de Oliveira 2003. Use, modification and -# distribution is subject to the Boost Software License, Version 1.0. -# (See accompanying file LICENSE_1_0.txt or copy at -# http://www.boost.org/LICENSE_1_0.txt) -import unittest -from _inherit2 import * - -class InheritExampleTest(unittest.TestCase): - - def testIt(self): - b = B() - d = D() - - self.assert_(issubclass(D, B)) - b.x, b.y = 10, 5 - self.assertEqual(b.getx(), 10) - self.assertEqual(b.gety(), 5) - d.x, d.y, d.z, d.w = 20, 15, 10, 5 - self.assertEqual(d.getx(), 20) - self.assertEqual(d.gety(), 15) - self.assertEqual(d.getz(), 10) - self.assertEqual(d.getw(), 5) - self.assertEqual(b.foo(), 1) - self.assertEqual(b.foo(3), 3) - - def wrong(): - return b.getw() - self.assertRaises(AttributeError, wrong) - -if __name__ == '__main__': - unittest.main() diff --git a/pyste/tests/inherit3.h b/pyste/tests/inherit3.h deleted file mode 100644 index 1945fb51..00000000 --- a/pyste/tests/inherit3.h +++ /dev/null @@ -1,46 +0,0 @@ -/* Copyright Bruno da Silva de Oliveira 2003. Use, modification and - distribution is subject to the Boost Software License, Version 1.0. - (See accompanying file LICENSE_1_0.txt or copy at - http://www.boost.org/LICENSE_1_0.txt) - */ - -namespace inherit3 { - -struct A -{ - A() { x = 0; } - struct X { int y; }; - int x; - virtual int foo() { return 0; } - virtual int foo(int x) { return x; } - A operator+(A o) const - { - A r; - r.x = o.x + x; - return r; - } - enum E { i, j }; - -}; - -struct B: A -{ - B() { x = 0; } - struct X { int y; }; - int x; - int foo() { return 1; } - A operator+(A o) const - { - A r; - r.x = o.x + x; - return r; - } - enum E { i, j }; - -}; - -struct C: A -{ -}; - -} diff --git a/pyste/tests/inherit3.pyste b/pyste/tests/inherit3.pyste deleted file mode 100644 index f95c0605..00000000 --- a/pyste/tests/inherit3.pyste +++ /dev/null @@ -1,2 +0,0 @@ -Class('inherit3::B', 'inherit3.h') -Class('inherit3::C', 'inherit3.h') diff --git a/pyste/tests/inherit3UT.py b/pyste/tests/inherit3UT.py deleted file mode 100644 index b7dba1e9..00000000 --- a/pyste/tests/inherit3UT.py +++ /dev/null @@ -1,27 +0,0 @@ -# Copyright Bruno da Silva de Oliveira 2003. Use, modification and -# distribution is subject to the Boost Software License, Version 1.0. -# (See accompanying file LICENSE_1_0.txt or copy at -# http://www.boost.org/LICENSE_1_0.txt) -import unittest -from _inherit3 import * - -class testInherit3(unittest.TestCase): - - def testIt(self): - def testInst(c): - self.assertEqual(c.x, 0) - self.assertEqual(c.foo(3), 3) - x = c.X() - self.assertEqual(x.y, 0) - self.assertEqual(c.E.i, 0) - self.assertEqual(c.E.j, 1) - b = B() - c = C() - testInst(b) - testInst(c) - self.assertEqual(b.foo(), 1) - self.assertEqual(c.foo(), 0) - - -if __name__ == '__main__': - unittest.main() diff --git a/pyste/tests/inherit4.h b/pyste/tests/inherit4.h deleted file mode 100644 index a1cecfbc..00000000 --- a/pyste/tests/inherit4.h +++ /dev/null @@ -1,23 +0,0 @@ -/* Copyright Bruno da Silva de Oliveira 2003. Use, modification and - distribution is subject to the Boost Software License, Version 1.0. - (See accompanying file LICENSE_1_0.txt or copy at - http://www.boost.org/LICENSE_1_0.txt) - */ -namespace inherit4 { - -struct A -{ - int x; -}; - -struct B: A -{ - int y; -}; - -struct C: B -{ - int z; -}; - -} diff --git a/pyste/tests/inherit4.pyste b/pyste/tests/inherit4.pyste deleted file mode 100644 index 4809e022..00000000 --- a/pyste/tests/inherit4.pyste +++ /dev/null @@ -1,3 +0,0 @@ -Class('inherit4::A', 'inherit4.h') -Class('inherit4::B', 'inherit4.h') -Class('inherit4::C', 'inherit4.h') diff --git a/pyste/tests/inherit4UT.py b/pyste/tests/inherit4UT.py deleted file mode 100644 index f2c75c55..00000000 --- a/pyste/tests/inherit4UT.py +++ /dev/null @@ -1,31 +0,0 @@ -# Copyright Bruno da Silva de Oliveira 2003. Use, modification and -# distribution is subject to the Boost Software License, Version 1.0. -# (See accompanying file LICENSE_1_0.txt or copy at -# http://www.boost.org/LICENSE_1_0.txt) -import unittest -from _inherit4 import * - -class TestInherit4(unittest.TestCase): - - def testIt(self): - self.assert_(issubclass(B, A)) - self.assert_(issubclass(C, A)) - self.assert_(issubclass(C, B)) - a = A() - a.x = 1 - b = B() - b.x = 10 - b.y = 20 - c = C() - c.x = 100 - c.y = 200 - c.z = 300 - self.assertEqual(a.x, 1) - self.assertEqual(b.x, 10) - self.assertEqual(b.y, 20) - self.assertEqual(c.x, 100) - self.assertEqual(c.y, 200) - self.assertEqual(c.z, 300) - -if __name__ == '__main__': - unittest.main() diff --git a/pyste/tests/inheritUT.py b/pyste/tests/inheritUT.py deleted file mode 100644 index f3e7b406..00000000 --- a/pyste/tests/inheritUT.py +++ /dev/null @@ -1,33 +0,0 @@ -# Copyright Bruno da Silva de Oliveira 2003. Use, modification and -# distribution is subject to the Boost Software License, Version 1.0. -# (See accompanying file LICENSE_1_0.txt or copy at -# http://www.boost.org/LICENSE_1_0.txt) -import unittest -from _inherit import * - -class InheritExampleTest(unittest.TestCase): - - def testIt(self): - a = A_int() - b = B() - self.assert_(isinstance(b, A_int)) - self.assert_(issubclass(B, A_int)) - a.set(10) - self.assertEqual(a.get(), 10) - b.set(1) - self.assertEqual(b.go(), 1) - self.assertEqual(b.get(), 1) - - d = D() - self.assert_(issubclass(D, B)) - self.assertEqual(d.x, 0) - self.assertEqual(d.y, 0) - self.assertEqual(d.s, 1) - self.assertEqual(D.s, 1) - self.assertEqual(d.f1(), 1) - self.assertEqual(d.f2(), 2) - - - -if __name__ == '__main__': - unittest.main() diff --git a/pyste/tests/nested.cpp b/pyste/tests/nested.cpp deleted file mode 100644 index 6e167ab0..00000000 --- a/pyste/tests/nested.cpp +++ /dev/null @@ -1,9 +0,0 @@ -/* Copyright Bruno da Silva de Oliveira 2003. Use, modification and - distribution is subject to the Boost Software License, Version 1.0. - (See accompanying file LICENSE_1_0.txt or copy at - http://www.boost.org/LICENSE_1_0.txt) - */ -#include "nested.h" - -int nested::X::staticXValue = 10; -int nested::X::Y::staticYValue = 20; diff --git a/pyste/tests/nested.h b/pyste/tests/nested.h deleted file mode 100644 index 13ed6085..00000000 --- a/pyste/tests/nested.h +++ /dev/null @@ -1,32 +0,0 @@ -/* Copyright Bruno da Silva de Oliveira 2003. Use, modification and - distribution is subject to the Boost Software License, Version 1.0. - (See accompanying file LICENSE_1_0.txt or copy at - http://www.boost.org/LICENSE_1_0.txt) - */ - -#ifndef NESTED_H -#define NESTED_H - -namespace nested { - -struct X -{ - struct Y - { - int valueY; - static int staticYValue; - struct Z - { - int valueZ; - }; - }; - - static int staticXValue; - int valueX; -}; - -typedef X Root; - -} - -#endif diff --git a/pyste/tests/nested.pyste b/pyste/tests/nested.pyste deleted file mode 100644 index 48bb26b5..00000000 --- a/pyste/tests/nested.pyste +++ /dev/null @@ -1 +0,0 @@ -Class('nested::Root', 'nested.h') diff --git a/pyste/tests/nestedUT.py b/pyste/tests/nestedUT.py deleted file mode 100644 index 06c1c7be..00000000 --- a/pyste/tests/nestedUT.py +++ /dev/null @@ -1,19 +0,0 @@ -# Copyright Bruno da Silva de Oliveira 2003. Use, modification and -# distribution is subject to the Boost Software License, Version 1.0. -# (See accompanying file LICENSE_1_0.txt or copy at -# http://www.boost.org/LICENSE_1_0.txt) -import unittest -from _nested import * - -class NestedTest(unittest.TestCase): - - def testIt(self): - self.assertEqual(Root.staticXValue, 10) - self.assertEqual(Root.Y.staticYValue, 20) - z = Root.Y.Z() - z.valueZ = 3 - self.assertEqual(z.valueZ, 3) - - -if __name__ == '__main__': - unittest.main() diff --git a/pyste/tests/opaque.h b/pyste/tests/opaque.h deleted file mode 100644 index 1947830e..00000000 --- a/pyste/tests/opaque.h +++ /dev/null @@ -1,57 +0,0 @@ -/* Copyright Bruno da Silva de Oliveira 2003. Use, modification and - distribution is subject to the Boost Software License, Version 1.0. - (See accompanying file LICENSE_1_0.txt or copy at - http://www.boost.org/LICENSE_1_0.txt) - */ -#ifndef OPAQUE_H -#define OPAQUE_H - -#include - -namespace opaque { - - -struct C { - C(int v): value(v) {} - int value; -}; - - -inline C* new_C() -{ - return new C(10); -} - -inline C* new_C_zero() -{ - return new C(0); -} - -inline int get(C* c) -{ - return c->value; -} - -struct D { - D(double v): value(v) {} - double value; -}; - -struct A -{ - D* new_handle() - { - return new D(3.0); - } - - double get(D* d) - { - return d->value; - } - - int f(int x=0) { return x; } -}; - -} - -#endif diff --git a/pyste/tests/opaque.pyste b/pyste/tests/opaque.pyste deleted file mode 100644 index 8180d251..00000000 --- a/pyste/tests/opaque.pyste +++ /dev/null @@ -1,7 +0,0 @@ -foo = Function('opaque::new_C', 'opaque.h') -set_policy(foo, return_value_policy(return_opaque_pointer)) -foo = Function('opaque::new_C_zero', 'opaque.h') -set_policy(foo, return_value_policy(return_opaque_pointer)) -Function('opaque::get', 'opaque.h' ) -A = Class('opaque::A', 'opaque.h') -set_policy(A.new_handle, return_value_policy(return_opaque_pointer)) diff --git a/pyste/tests/opaqueUT.py b/pyste/tests/opaqueUT.py deleted file mode 100644 index 0f3e1e07..00000000 --- a/pyste/tests/opaqueUT.py +++ /dev/null @@ -1,24 +0,0 @@ -# Copyright Bruno da Silva de Oliveira 2003. Use, modification and -# distribution is subject to the Boost Software License, Version 1.0. -# (See accompanying file LICENSE_1_0.txt or copy at -# http://www.boost.org/LICENSE_1_0.txt) -import unittest -from _opaque import * - -class OpaqueTest(unittest.TestCase): - - def testIt(self): - - c = new_C() - self.assertEqual(get(c), 10) - c = new_C_zero() - self.assertEqual(get(c), 0) - a = A() - d = a.new_handle() - self.assertEqual(a.get(d), 3.0) - self.assertEqual(a.f(), 0) - self.assertEqual(a.f(3), 3) - - -if __name__ == '__main__': - unittest.main() diff --git a/pyste/tests/operators.cpp b/pyste/tests/operators.cpp deleted file mode 100644 index cecdaca0..00000000 --- a/pyste/tests/operators.cpp +++ /dev/null @@ -1,8 +0,0 @@ -/* Copyright Bruno da Silva de Oliveira 2003. Use, modification and - distribution is subject to the Boost Software License, Version 1.0. - (See accompanying file LICENSE_1_0.txt or copy at - http://www.boost.org/LICENSE_1_0.txt) - */ -#include "operators.h" - -double operators::C::x = 10; diff --git a/pyste/tests/operators.h b/pyste/tests/operators.h deleted file mode 100644 index 5d394421..00000000 --- a/pyste/tests/operators.h +++ /dev/null @@ -1,52 +0,0 @@ -/* Copyright Bruno da Silva de Oliveira 2003. Use, modification and - distribution is subject to the Boost Software License, Version 1.0. - (See accompanying file LICENSE_1_0.txt or copy at - http://www.boost.org/LICENSE_1_0.txt) - */ -#ifndef OPERATORS_H -#define OPERATORS_H - - -namespace operators { - -struct C -{ - static double x; - double value; - - const C operator+(const C other) const - { - C c; - c.value = value + other.value; - return c; - } - operator int() const - { - return (int)value; - } - - double operator()() - { - return C::x; - } - - double operator()(double other) - { - return C::x + other; - } - - operator const char*() { return "C"; } -}; - -inline const C operator*(const C& lhs, const C& rhs) -{ - C c; - c.value = lhs.value * rhs.value; - return c; -} - - -} - - -#endif diff --git a/pyste/tests/operators.pyste b/pyste/tests/operators.pyste deleted file mode 100644 index 4ab7a370..00000000 --- a/pyste/tests/operators.pyste +++ /dev/null @@ -1,2 +0,0 @@ -C = Class('operators::C', 'operators.h') -#exclude(C.operator['+']) diff --git a/pyste/tests/operatorsUT.py b/pyste/tests/operatorsUT.py deleted file mode 100644 index beb19317..00000000 --- a/pyste/tests/operatorsUT.py +++ /dev/null @@ -1,30 +0,0 @@ -# Copyright Bruno da Silva de Oliveira 2003. Use, modification and -# distribution is subject to the Boost Software License, Version 1.0. -# (See accompanying file LICENSE_1_0.txt or copy at -# http://www.boost.org/LICENSE_1_0.txt) -import unittest -from _operators import * - -class OperatorTest(unittest.TestCase): - - def testIt(self): - c = C() - c.value = 3.0 - d = C() - d.value = 2.0 - self.assertEqual(c.x, 10) - self.assertEqual(C.x, 10) - self.assertEqual(C.x, 10) - self.assertEqual((c * d).value, 6.0) - self.assertEqual((c + d).value, 5.0) - self.assertEqual(int(c), 3) - self.assertEqual(int(d), 2) - self.assertEqual(c(), 10) - self.assertEqual(d(), 10) - self.assertEqual(c(3.0), 13.0) - self.assertEqual(d(6.0), 16.0) - self.assertEqual(str(c), "C") - - -if __name__ == '__main__': - unittest.main() diff --git a/pyste/tests/policiesUT.py b/pyste/tests/policiesUT.py deleted file mode 100644 index 7255baeb..00000000 --- a/pyste/tests/policiesUT.py +++ /dev/null @@ -1,67 +0,0 @@ -# Copyright Bruno da Silva de Oliveira 2003. Use, modification and -# distribution is subject to the Boost Software License, Version 1.0. -# (See accompanying file LICENSE_1_0.txt or copy at -# http://www.boost.org/LICENSE_1_0.txt) -import sys -import unittest -from Pyste.policies import * - - -#================================================================================ -# PolicicesTest -#================================================================================ -class PoliciesTest(unittest.TestCase): - - def testReturnInternal(self): - 'tests the code from a simple internal_reference' - - x = return_internal_reference(1) - self.assertEqual(x.Code(), 'return_internal_reference< 1 >') - x = return_internal_reference(3) - self.assertEqual(x.Code(), 'return_internal_reference< 3 >') - - - def testCustodian(self): - 'tests the code from a simple custodian_and_ward' - - x = with_custodian_and_ward(1,2) - self.assertEqual(x.Code(), 'with_custodian_and_ward< 1, 2 >') - x = with_custodian_and_ward(3,4) - self.assertEqual(x.Code(), 'with_custodian_and_ward< 3, 4 >') - - - def testReturnPolicies(self): - 'tests all the return_value_policies' - - ret = 'return_value_policy< %s >' - x = return_value_policy(reference_existing_object) - self.assertEqual(x.Code(), ret % 'reference_existing_object') - x = return_value_policy(copy_const_reference) - self.assertEqual(x.Code(), ret % 'copy_const_reference') - x = return_value_policy(copy_non_const_reference) - self.assertEqual(x.Code(), ret % 'copy_non_const_reference') - x = return_value_policy(manage_new_object) - self.assertEqual(x.Code(), ret % 'manage_new_object') - x = return_value_policy(return_opaque_pointer) - self.assertEqual(x.Code(), ret % 'return_opaque_pointer') - - def testReturnWithCustodiam(self): - 'test the mix of return_internal with custodian' - - x = return_internal_reference(1, with_custodian_and_ward(3,2)) - self.assertEqual( - x.Code(), - 'return_internal_reference< 1, with_custodian_and_ward< 3, 2 > >') - - - def testReturnPoliciesWithInternal(self): - 'test the mix of return_internal with return_policy' - - x = return_internal_reference(1, return_value_policy(manage_new_object)) - self.assertEqual( - x.Code(), - 'return_internal_reference< 1, return_value_policy< manage_new_object > >') - - -if __name__ == '__main__': - unittest.main() diff --git a/pyste/tests/runtests.py b/pyste/tests/runtests.py deleted file mode 100644 index 4bf83b34..00000000 --- a/pyste/tests/runtests.py +++ /dev/null @@ -1,21 +0,0 @@ -# Copyright Bruno da Silva de Oliveira 2003. Use, modification and -# distribution is subject to the Boost Software License, Version 1.0. -# (See accompanying file LICENSE_1_0.txt or copy at -# http://www.boost.org/LICENSE_1_0.txt) -#!/usr/bin/python - -import sys -sys.path.append('../src/Pyste') -import unittest -import os.path -from glob import glob - -if __name__ == '__main__': - loader = unittest.defaultTestLoader - tests = [] - for name in glob('*UT.py'): - module = __import__(os.path.splitext(name)[0]) - tests.append(loader.loadTestsFromModule(module)) - runner = unittest.TextTestRunner() - result = runner.run(unittest.TestSuite(tests)) - sys.exit(not result.wasSuccessful()) diff --git a/pyste/tests/smart_ptr.h b/pyste/tests/smart_ptr.h deleted file mode 100644 index b230b917..00000000 --- a/pyste/tests/smart_ptr.h +++ /dev/null @@ -1,50 +0,0 @@ -/* Copyright Bruno da Silva de Oliveira 2003. Use, modification and - distribution is subject to the Boost Software License, Version 1.0. - (See accompanying file LICENSE_1_0.txt or copy at - http://www.boost.org/LICENSE_1_0.txt) - */ - -#ifndef SMART_PTR_H -#define SMART_PTR_H - - -#include -#include - -namespace smart_ptr { - -struct C -{ - int value; -}; - -inline boost::shared_ptr NewC() { return boost::shared_ptr( new C() ); } - -struct D -{ - boost::shared_ptr Get() { return ptr; } - void Set( boost::shared_ptr c ) { ptr = c; } -private: - boost::shared_ptr ptr; -}; - -inline std::auto_ptr NewD() { return std::auto_ptr( new D() ); } - - -// test an abstract class -struct A -{ - virtual int f() = 0; -}; - -struct B: A -{ - virtual int f(){ return 1; } -}; - -inline boost::shared_ptr NewA() { return boost::shared_ptr(new B()); } -inline int GetA(boost::shared_ptr a) { return a->f(); } - -} - -#endif diff --git a/pyste/tests/smart_ptr.pyste b/pyste/tests/smart_ptr.pyste deleted file mode 100644 index cfbdd81a..00000000 --- a/pyste/tests/smart_ptr.pyste +++ /dev/null @@ -1,13 +0,0 @@ -C = Class('smart_ptr::C', 'smart_ptr.h') -use_shared_ptr(C) - -D = Class('smart_ptr::D', 'smart_ptr.h') -use_auto_ptr(D) - -A = Class('smart_ptr::A', 'smart_ptr.h') -use_shared_ptr(A) - -Function('smart_ptr::NewC', 'smart_ptr.h') -Function('smart_ptr::NewD', 'smart_ptr.h') -Function('smart_ptr::NewA', 'smart_ptr.h') -Function('smart_ptr::GetA', 'smart_ptr.h') diff --git a/pyste/tests/smart_ptrUT.py b/pyste/tests/smart_ptrUT.py deleted file mode 100644 index 9d81f08d..00000000 --- a/pyste/tests/smart_ptrUT.py +++ /dev/null @@ -1,22 +0,0 @@ -# Copyright Bruno da Silva de Oliveira 2003. Use, modification and -# distribution is subject to the Boost Software License, Version 1.0. -# (See accompanying file LICENSE_1_0.txt or copy at -# http://www.boost.org/LICENSE_1_0.txt) -import unittest -from _smart_ptr import * - -class BasicExampleTest(unittest.TestCase): - - def testIt(self): - c = NewC() - d = NewD() - c.value = 3 - d.Set(c) - c1 = d.Get() - c1.value = 6 - self.assertEqual(c.value, 6) - a = NewA() - self.assertEqual(GetA(a), 1) - -if __name__ == '__main__': - unittest.main() diff --git a/pyste/tests/templates.h b/pyste/tests/templates.h deleted file mode 100644 index 7258e91c..00000000 --- a/pyste/tests/templates.h +++ /dev/null @@ -1,15 +0,0 @@ -/* Copyright Bruno da Silva de Oliveira 2003. Use, modification and - distribution is subject to the Boost Software License, Version 1.0. - (See accompanying file LICENSE_1_0.txt or copy at - http://www.boost.org/LICENSE_1_0.txt) - */ -namespace templates { - -template -struct Point -{ - T x; - T y; -}; - -} diff --git a/pyste/tests/templates.pyste b/pyste/tests/templates.pyste deleted file mode 100644 index 77eaceaa..00000000 --- a/pyste/tests/templates.pyste +++ /dev/null @@ -1,8 +0,0 @@ -Point = Template('templates::Point', 'templates.h') -rename(Point.x, 'i') -rename(Point.y, 'j') -IPoint = Point('int') -FPoint = Point('double', 'FPoint') -rename(IPoint, 'IPoint') -rename(IPoint.x, 'x') -rename(IPoint.y, 'y') diff --git a/pyste/tests/templatesUT.py b/pyste/tests/templatesUT.py deleted file mode 100644 index 0c4b08b5..00000000 --- a/pyste/tests/templatesUT.py +++ /dev/null @@ -1,30 +0,0 @@ -# Copyright Bruno da Silva de Oliveira 2003. Use, modification and -# distribution is subject to the Boost Software License, Version 1.0. -# (See accompanying file LICENSE_1_0.txt or copy at -# http://www.boost.org/LICENSE_1_0.txt) -import unittest -from _templates import * - -class TemplatesTest(unittest.TestCase): - - def testIt(self): - fp = FPoint() - fp.i = 3.0 - fp.j = 4.0 - ip = IPoint() - ip.x = 10 - ip.y = 3 - - self.assertEqual(fp.i, 3.0) - self.assertEqual(fp.j, 4.0) - self.assertEqual(ip.x, 10) - self.assertEqual(ip.y, 3) - self.assertEqual(type(fp.i), float) - self.assertEqual(type(fp.j), float) - self.assertEqual(type(ip.x), int) - self.assertEqual(type(ip.y), int) - - - -if __name__ == '__main__': - unittest.main() diff --git a/pyste/tests/test_all.py b/pyste/tests/test_all.py deleted file mode 100644 index ba3c54de..00000000 --- a/pyste/tests/test_all.py +++ /dev/null @@ -1,140 +0,0 @@ -#!/usr/bin/python -# Copyright Bruno da Silva de Oliveira 2003. Use, modification and -# distribution is subject to the Boost Software License, Version 1.0. -# (See accompanying file LICENSE_1_0.txt or copy at -# http://www.boost.org/LICENSE_1_0.txt) - -import os -import glob -import shutil -import sys -import time - -#============================================================================= -# win32 configuration -#============================================================================= -if sys.platform == 'win32': - - includes = '-ID:/programming/libraries/boost-cvs/boost -ID:/Bin/Python/include' - build_pyste_cmd = 'python ../src/Pyste/pyste.py --pyste-ns=pyste --cache-dir=cache %s ' % includes - compile_single_cmd = 'cl /nologo /GR /GX -c %s -I. ' % includes - link_single_cmd = 'link /nologo /DLL '\ - '/libpath:D:/programming/libraries/boost-cvs/lib /libpath:D:/Bin/Python/libs '\ - 'boost_python.lib python24.lib /out:_%s.dll ' - obj_ext = 'obj' - -#============================================================================= -# linux configuration -#============================================================================= -elif sys.platform == 'linux2': - - build_pyste_cmd = 'python ../src/Pyste/pyste.py -I. ' - compile_single_cmd = 'g++ -shared -c -I. -I/usr/include/python2.4 ' - link_single_cmd = 'g++ -shared -o _%s.so -lboost_python ' - obj_ext = 'o' - - - -def build_pyste(multiple, module): - rest = '%s --module=_%s %s.pyste' % (multiple, module, module) - execute(build_pyste_cmd + rest) - - -def compile_single(module): - module_obj = '' - if os.path.isfile(module+'.cpp'): - execute(compile_single_cmd + module+'.cpp') - module_obj = module + '.' + obj_ext - execute(compile_single_cmd + ('_%s.cpp' % module)) - link = link_single_cmd % module - execute(link + ('_%s.%s ' % (module, obj_ext)) + module_obj) - - -def compile_multiple(module): - module_obj = '' - if os.path.isfile(module+'.cpp'): - execute(compile_single_cmd + module+'.cpp') - module_obj = module + '.' + obj_ext - files = glob.glob('_%s/*.cpp' % module) - for f in files: - execute(compile_single_cmd + f) - def basename(name): - return os.path.basename(os.path.splitext(name)[0]) - objs = [basename(x) + '.' + obj_ext for x in files] - objs.append(module_obj) - execute((link_single_cmd % module) + ' '.join(objs)) - - -def execute(cmd): - os.system(cmd) - - -def run_tests(): - if os.system('python runtests.py') != 0: - raise RuntimeError, 'tests failed' - - -def cleanup(): - modules = get_modules() - extensions = '*.dll *.pyc *.obj *.exp *.lib *.o *.so' - files = [] - for module in modules: - files.append('_' + module + '.cpp') - for ext in extensions.split(): - files += glob.glob(ext) - files.append('build.log') - for file in files: - try: - os.remove(file) - except OSError: pass - - for module in modules: - try: - shutil.rmtree('_' + module) - except OSError: pass - - -def main(multiple, module=None): - if module is None: - modules = get_modules() - else: - modules = [module] - - start = time.clock() - for module in modules: - build_pyste(multiple, module) - print '-'*50 - print 'Building pyste files: %0.2f seconds' % (time.clock()-start) - print - - start = time.clock() - for module in modules: - if multiple: - compile_multiple(module) - else: - compile_single(module) - print '-'*50 - print 'Compiling files: %0.2f seconds' % (time.clock()-start) - print - if len(modules) == 1: - os.system('python %sUT.py' % modules[0]) - else: - run_tests() - #cleanup() - - -def get_modules(): - def getname(file): - return os.path.splitext(os.path.basename(file))[0] - return [getname(x) for x in glob.glob('*.pyste')] - -if __name__ == '__main__': - if len(sys.argv) > 1: - module = sys.argv[1] - else: - module = None - try: -# main('--multiple', module) - main('', module) - except RuntimeError, e: - print e diff --git a/pyste/tests/vars.cpp b/pyste/tests/vars.cpp deleted file mode 100644 index e2abcd33..00000000 --- a/pyste/tests/vars.cpp +++ /dev/null @@ -1,12 +0,0 @@ -/* Copyright Bruno da Silva de Oliveira 2003. Use, modification and - distribution is subject to the Boost Software License, Version 1.0. - (See accompanying file LICENSE_1_0.txt or copy at - http://www.boost.org/LICENSE_1_0.txt) - */ -#include "vars.h" - -const Color black = Color(0, 0, 0); -const Color red = Color(255, 0, 0); -const Color green = Color(0, 255, 0); -const Color blue = Color(0, 0, 255); -Color in_use = black; diff --git a/pyste/tests/vars.h b/pyste/tests/vars.h deleted file mode 100644 index 24e87d80..00000000 --- a/pyste/tests/vars.h +++ /dev/null @@ -1,24 +0,0 @@ -/* Copyright Bruno da Silva de Oliveira 2003. Use, modification and - distribution is subject to the Boost Software License, Version 1.0. - (See accompanying file LICENSE_1_0.txt or copy at - http://www.boost.org/LICENSE_1_0.txt) - */ - -struct Color -{ - Color(int r_ = 0, int g_ = 0, int b_ = 0): - r(r_), g(g_), b(b_) - {} - Color( const Color &c): - r(c.r), g(c.g), b(c.b) - {} - int r; - int g; - int b; -}; - -extern const Color black; -extern const Color red; -extern const Color green; -extern const Color blue; -extern Color in_use; diff --git a/pyste/tests/vars.pyste b/pyste/tests/vars.pyste deleted file mode 100644 index 3fd9d689..00000000 --- a/pyste/tests/vars.pyste +++ /dev/null @@ -1 +0,0 @@ -AllFromHeader('vars.h') diff --git a/pyste/tests/varsUT.py b/pyste/tests/varsUT.py deleted file mode 100644 index 4c32cbb2..00000000 --- a/pyste/tests/varsUT.py +++ /dev/null @@ -1,22 +0,0 @@ -# Copyright Bruno da Silva de Oliveira 2003. Use, modification and -# distribution is subject to the Boost Software License, Version 1.0. -# (See accompanying file LICENSE_1_0.txt or copy at -# http://www.boost.org/LICENSE_1_0.txt) -import unittest -import _vars - - -class VarsTest(unittest.TestCase): - - def testIt(self): - def testColor(c, r, g, b): - self.assertEqual(c.r, r) - self.assertEqual(c.g, g) - self.assertEqual(c.b, b) - testColor(_vars.black, 0, 0, 0) - testColor(_vars.red, 255, 0, 0) - testColor(_vars.green, 0, 255, 0) - testColor(_vars.blue, 0, 0, 255) - -if __name__ == '__main__': - unittest.main() diff --git a/pyste/tests/virtual.cpp b/pyste/tests/virtual.cpp deleted file mode 100644 index 070d9d34..00000000 --- a/pyste/tests/virtual.cpp +++ /dev/null @@ -1,75 +0,0 @@ -/* Copyright Bruno da Silva de Oliveira 2003. Use, modification and - distribution is subject to the Boost Software License, Version 1.0. - (See accompanying file LICENSE_1_0.txt or copy at - http://www.boost.org/LICENSE_1_0.txt) - */ - -// Includes ==================================================================== -#include -#include - -// Using ======================================================================= -using namespace boost::python; - -// Declarations ================================================================ - - -namespace { - - -struct virtual_C_Wrapper: virtual_::C -{ - virtual_C_Wrapper(PyObject* self_, const virtual_::C & p0): - virtual_::C(p0), self(self_) {} - - virtual_C_Wrapper(PyObject* self_): - virtual_::C(), self(self_) {} - - int f() { - return call_method< int >(self, "f"); - } - - int default_f() { - return virtual_::C::f(); - } - - void bar(int p0) { - call_method< void >(self, "bar", p0); - } - - void default_bar(int p0) { - virtual_::C::bar(p0); - } - - void bar(char * p0) { - call_method< void >(self, "bar", p0); - } - - void default_bar(char * p0) { - virtual_::C::bar(p0); - } - - int f_abs() { - return call_method< int >(self, "f_abs"); - } - - PyObject* self; -}; - - - -}// namespace - - -// Module ====================================================================== -BOOST_PYTHON_MODULE(virtual) -{ - class_< virtual_::C, boost::noncopyable, virtual_C_Wrapper >("C", init< >()) - .def("get_name", &virtual_::C::get_name) - .def("f", &virtual_::C::f, &virtual_C_Wrapper::default_f) - .def("bar", (void (virtual_::C::*)(int) )&virtual_::C::bar, (void (virtual_C_Wrapper::*)(int))&virtual_C_Wrapper::default_bar) - .def("bar", (void (virtual_::C::*)(char *) )&virtual_::C::bar, (void (virtual_C_Wrapper::*)(char *))&virtual_C_Wrapper::default_bar) - ; - - def("call_f", &virtual_::call_f); -} diff --git a/pyste/tests/virtual.h b/pyste/tests/virtual.h deleted file mode 100644 index d0bb194a..00000000 --- a/pyste/tests/virtual.h +++ /dev/null @@ -1,41 +0,0 @@ -/* Copyright Bruno da Silva de Oliveira 2003. Use, modification and - distribution is subject to the Boost Software License, Version 1.0. - (See accompanying file LICENSE_1_0.txt or copy at - http://www.boost.org/LICENSE_1_0.txt) - */ -namespace virtual_ { - -struct C -{ -public: - virtual int f() - { - return f_abs(); - } - - virtual void bar(int) {} - virtual void bar(char*) {} - - const char* get_name() - { - return name(); - } - virtual int dummy() { return 0; } - -protected: - virtual int f_abs() = 0; - -private: - virtual const char* name() { return "C"; } -}; - -struct D -{ - virtual int dummy() { return 0; } -}; - -inline int call_f(C& c) { return c.f(); } -inline int call_dummy(C* c) { return c->dummy(); } -inline int call_dummy(D* d) { return d->dummy(); } - -} diff --git a/pyste/tests/virtual.pyste b/pyste/tests/virtual.pyste deleted file mode 100644 index ef966412..00000000 --- a/pyste/tests/virtual.pyste +++ /dev/null @@ -1,6 +0,0 @@ -C = Class('virtual_::C', 'virtual.h') -final(C.dummy) -D = Class('virtual_::D', 'virtual.h') -final(D.dummy) -Function('virtual_::call_f', 'virtual.h') -Function('virtual_::call_dummy', 'virtual.h') diff --git a/pyste/tests/virtual2.h b/pyste/tests/virtual2.h deleted file mode 100644 index a6677ad1..00000000 --- a/pyste/tests/virtual2.h +++ /dev/null @@ -1,34 +0,0 @@ -/* Copyright Bruno da Silva de Oliveira 2003. Use, modification and - distribution is subject to the Boost Software License, Version 1.0. - (See accompanying file LICENSE_1_0.txt or copy at - http://www.boost.org/LICENSE_1_0.txt) - */ - -namespace virtual2 { - -struct A -{ - virtual int f() { return 0; } - virtual int f1() { return 10; } - virtual A* make_new() { return new A; } -}; - -struct B: A -{ - virtual int f() { return 1; } - virtual int f2() { return 20; } - virtual A* make_new() { return new B; } -}; - -inline int call_fs(A*a) -{ - int r = a->f1(); - B* b = dynamic_cast(a); - return r + b->f2(); -} - -inline int call_f(A* a) -{ - return a->f(); -} -} diff --git a/pyste/tests/virtual2.pyste b/pyste/tests/virtual2.pyste deleted file mode 100644 index 785b819c..00000000 --- a/pyste/tests/virtual2.pyste +++ /dev/null @@ -1,6 +0,0 @@ -A = Class('virtual2::A', 'virtual2.h') -set_policy(A.make_new, return_value_policy(manage_new_object)) -B = Class('virtual2::B', 'virtual2.h') -set_policy(B.make_new, return_value_policy(manage_new_object)) -Function('virtual2::call_fs', 'virtual2.h') -Function('virtual2::call_f', 'virtual2.h') diff --git a/pyste/tests/virtual2UT.py b/pyste/tests/virtual2UT.py deleted file mode 100644 index 312277d2..00000000 --- a/pyste/tests/virtual2UT.py +++ /dev/null @@ -1,40 +0,0 @@ -# Copyright Bruno da Silva de Oliveira 2003. Use, modification and -# distribution is subject to the Boost Software License, Version 1.0. -# (See accompanying file LICENSE_1_0.txt or copy at -# http://www.boost.org/LICENSE_1_0.txt) - -import unittest -from _virtual2 import * - -class Virtual2Test(unittest.TestCase): - - def testIt(self): - a = A() - self.assertEqual(a.f1(), 10) - b = B() - self.assertEqual(b.f1(), 10) - self.assertEqual(b.f2(), 20) - self.assertEqual(call_fs(b), 30) - self.assertEqual(call_f(a), 0) - self.assertEqual(call_f(b), 1) - nb = b.make_new() - na = a.make_new() - self.assertEqual(na.f1(), 10) - self.assertEqual(nb.f1(), 10) - self.assertEqual(nb.f2(), 20) - self.assertEqual(call_fs(nb), 30) - self.assertEqual(call_f(na), 0) - self.assertEqual(call_f(nb), 1) - class C(B): - def f1(self): return 1 - def f2(self): return 2 - def f(self): return 100 - - c = C() - self.assertEqual(call_fs(c), 3) - self.assertEqual(call_fs(c), 3) - self.assertEqual(call_f(c), 100) - - -if __name__ == '__main__': - unittest.main() diff --git a/pyste/tests/virtualUT.py b/pyste/tests/virtualUT.py deleted file mode 100644 index deff6818..00000000 --- a/pyste/tests/virtualUT.py +++ /dev/null @@ -1,55 +0,0 @@ -# Copyright Bruno da Silva de Oliveira 2003. Use, modification and -# distribution is subject to the Boost Software License, Version 1.0. -# (See accompanying file LICENSE_1_0.txt or copy at -# http://www.boost.org/LICENSE_1_0.txt) -import unittest -from _virtual import * - -class VirtualTest(unittest.TestCase): - - def testIt(self): - - class E(C): - def f_abs(self): - return 3 - def dummy(self): - # override should not work - return 100 - - class F(C): - def f(self): - return 10 - def name(self): - return 'F' - - class G(D): - def dummy(self): - # override should not work - return 100 - - e = E() - f = F() - - self.assertEqual(e.f(), 3) - self.assertEqual(call_f(e), 3) - self.assertEqual(f.f(), 10) - self.assertEqual(call_f(f), 10) - self.assertEqual(e.get_name(), 'C') - #self.assertEqual(e.get_name(), 'E') check this later - - c = C() - c.bar(1) # ok - c.bar('a') # ok - self.assertRaises(TypeError, c.bar, 1.0) - - # test no_overrides - d = G() - self.assertEqual(e.dummy(), 100) - self.assertEqual(call_dummy(e), 0) - self.assertEqual(d.dummy(), 100) - self.assertEqual(call_dummy(d), 0) - - - -if __name__ == '__main__': - unittest.main() diff --git a/pyste/tests/wrappertest.h b/pyste/tests/wrappertest.h deleted file mode 100644 index 2304fd84..00000000 --- a/pyste/tests/wrappertest.h +++ /dev/null @@ -1,51 +0,0 @@ -/* Copyright Bruno da Silva de Oliveira 2003. Use, modification and - distribution is subject to the Boost Software License, Version 1.0. - (See accompanying file LICENSE_1_0.txt or copy at - http://www.boost.org/LICENSE_1_0.txt) - */ -#ifndef WRAPPER_TEST -#define WRAPPER_TEST - - -#include - -namespace wrappertest { - -inline std::vector Range(int count) -{ - std::vector v; - v.reserve(count); - for (int i = 0; i < count; ++i){ - v.push_back(i); - } - return v; -} - - -struct C -{ - C() {} - - std::vector Mul(int value) - { - std::vector res; - res.reserve(value); - std::vector::const_iterator it; - std::vector v(Range(value)); - for (it = v.begin(); it != v.end(); ++it){ - res.push_back(*it * value); - } - return res; - } -}; - - -struct A -{ - virtual int f() { return 1; }; -}; - -inline int call_foo(A* a){ return a->f(); } -} -#endif - diff --git a/pyste/tests/wrappertest.pyste b/pyste/tests/wrappertest.pyste deleted file mode 100644 index 12ba47b6..00000000 --- a/pyste/tests/wrappertest.pyste +++ /dev/null @@ -1,21 +0,0 @@ -Include('wrappertest_wrappers.h') - -f = Function('wrappertest::Range', 'wrappertest.h') -set_wrapper(f, 'RangeWrapper') - -mul = Wrapper('MulWrapper', -''' -list MulWrapper(wrappertest::C& c, int value){ - return VectorToList(c.Mul(value)); -} -''' -) - -C = Class('wrappertest::C', 'wrappertest.h') -set_wrapper(C.Mul, mul) - - -A = Class('wrappertest::A', 'wrappertest.h') -set_wrapper(A.f, 'f_wrapper') - -Function('wrappertest::call_foo', 'wrappertest.h') diff --git a/pyste/tests/wrappertestUT.py b/pyste/tests/wrappertestUT.py deleted file mode 100644 index d770408b..00000000 --- a/pyste/tests/wrappertestUT.py +++ /dev/null @@ -1,24 +0,0 @@ -# Copyright Bruno da Silva de Oliveira 2003. Use, modification and -# distribution is subject to the Boost Software License, Version 1.0. -# (See accompanying file LICENSE_1_0.txt or copy at -# http://www.boost.org/LICENSE_1_0.txt) -import unittest -from _wrappertest import * - -class WrapperTest(unittest.TestCase): - - def testIt(self): - self.assertEqual(Range(10), range(10)) - self.assertEqual(C().Mul(10), [x*10 for x in range(10)]) - - a = A() - self.assertEqual(a.f(), 10) - self.assertEqual(call_foo(a), 10) - class D(A): - def f(self): return 2 - d = D() - self.assertEqual(d.f(), 2) - self.assertEqual(call_foo(d), 2) - -if __name__ == '__main__': - unittest.main() diff --git a/pyste/tests/wrappertest_wrappers.h b/pyste/tests/wrappertest_wrappers.h deleted file mode 100644 index 31570a05..00000000 --- a/pyste/tests/wrappertest_wrappers.h +++ /dev/null @@ -1,33 +0,0 @@ -/* Copyright Bruno da Silva de Oliveira 2003. Use, modification and - distribution is subject to the Boost Software License, Version 1.0. - (See accompanying file LICENSE_1_0.txt or copy at - http://www.boost.org/LICENSE_1_0.txt) - */ -#ifndef WRAPPER_TEST_WRAPPERS -#define WRAPPER_TEST_WRAPPERS - -#include -#include -#include "wrappertest.h" - -using namespace boost::python; - -template -list VectorToList(const std::vector & v) -{ - list res; - typename std::vector::const_iterator it; - for(it = v.begin(); it != v.end(); ++it){ - res.append(*it); - } - Py_XINCREF(res.ptr()); - return res; -} - -inline list RangeWrapper(int count){ - return VectorToList(wrappertest::Range(count)); -} - -inline int f_wrapper(wrappertest::A*) { return 10; } - -#endif diff --git a/release_notes.txt b/release_notes.txt deleted file mode 100644 index 1fd0f1b1..00000000 --- a/release_notes.txt +++ /dev/null @@ -1,223 +0,0 @@ -.. Copyright David Abrahams 2006. Distributed under the Boost -.. Software License, Version 1.0. (See accompanying -.. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) - -These are old release notes for Boost.Python v1 - -2000-11-22 10:00 - Ullrich fixed bug in operator_dispatcher. - -2000-11-21 10:00 - Changed all class and function names into lower_case. - - Ullrich updated documentation for operator wrapping. - -2000-11-20 10:00 - Ullrich renamed ExtensionClass:register_coerce() into - ExtensionClass:def_standard_coerce() and made it public - - Ullrich improved shared_pod_manager. - -2000-11-17 15:04 - Changed allocation strategy of shared_pod_manager to make it portable. - - Added pickling support + tests thanks to "Ralf W. Grosse-Kunstleve" - - - Added a specialization of Callback to prevent unsafe usage. - - Fixed Ullrich's operator_dispatcher refcount bug - - Removed const char* return values from virtual functions in tests; that - usage was unsafe. - - Ullrich changed Module::add() so that it steals a reference (fix of refcount bug) - - Ullrich added operator_dispatcher::create() optimization - - Ullrich changed design and implementation of TypeObjectBase::enable() (to eliminate low-level - code) and added shared_pod_manager optimization. - - -2000-11-15 12:01 - Fixed refcount bugs in operator calls. - - Added callback_adjust_refcount(PyObject*, Type) to account for different ownership - semantics of Callback's return types and Caller's arguments (which both use from_python()) - This bug caused refcount errors during operator calls. - - Moved operator_dispatcher into extclass.cpp - Gave it shared ownership of the objects it wraps - - Introduced sequence points in extension_class_coerce for exception-safety - - UPPER_CASE_MACRO_NAMES - - MixedCase template type argument names - - Changed internal error reporting to use Python exceptions so we don't force the - user to link in iostreams code - - Changed error return value of call_cmp to -1 - - Moved unwrap_* functions out of operator_dispatcher. This was transitional: when - I realized they didn't need to be declared in extclass.h I moved them out, but - now that operator_dispatcher itself is in extclass.cpp they could go back in. - - Numerous formatting tweaks - - Updated the BoundFunction::create() optimization and enabled it so it could actually be used! - -2000-11-15 00:26 - - Made Ullrich's operators support work with MSVC - - Cleaned up operators.h such that invalid define_operator<0> is no longer needed. - - Ullrich created operators.h to support wrapping of C++ operators (including the "__r*__" forms). - He added several auxiliary classes to extclass.h and extclass.cpp (most importantly, - py::detail::operator_dispatcher and py::operators) - -2000-11-13 22:29 - - removed obsolete ExtensionClassFromPython for good. - - removed unused class ExtensionType forward declaration - -2000-11-12 13:08 - - Added enum_as_int_converters for easier enum wrapping - - Introduced new conversion namespace macros: - PY_BEGIN_CONVERSION_NAMESPACE, - PY_END_CONVERSION_NAMESPACE, - PY_CONVERSION - - callback.h, gen_callback.py: - Added call() function so that a regular python function (as opposed to - method or other function-as-attribute) can be called. - - Added newlines for readability. - - class_wrapper.h: - Fixed a bug in add(), which allows non-method class attributes - - Ullrich has added def_raw for simple varargs and keyword support. - - Fixed version number check for __MWERKS__ - - Added tests for enums and non-method class attributes - - objects.h/objects.cpp: - Added py::String operator*= and operator* for repetition - - Change Dict::items(), keys(), and values() to return a List - - Added template versions of set_item, etc., methods so that users can optionally - use C++ types that have to_python() functions as parameters. - - Changed various Ptr by-value parameters to const Ptr& - - -======= Release ======= -2000-11-06 0:22 - Lots of documentation updates - - added 4-argument template constructor to py::Tuple - - added "add" member function to ClassWrapper<> to allow arbitrary Python - objects to be added to an extension class. - - gen_all.py now generates support for n argument member functions and n+1 - argument member functions at the suggestion of "Ralf W. Grosse-Kunstleve" - - - Added regression tests and re-ordered declare_base calls to verify that the - phantom base class issue is resolved. - -2000-11-04 17:35 - - Integrated Ullrich Koethe's brilliant from_python_experiment for better - error-reporting in many cases. - - extclass.h, gen_extclass.py: - removed special-case MSVC code - added much commentary - removed unused py_copy_to_new_value_holder - - init_function.h, gen_init_function.py: - added missing 'template' keyword on type-dependent template member usage - removed special-case MSVC code - added much commentary - -2000-11-04 0:36 - - Removed the need for the phantom base class that screwed up inheritance - hierarchies, introduced error-prone ordering dependencies, and complexified - logic in many places! - - extclass.h: Added some explanatory comments, removed wasteful m_self member - of HeldInstance - - extclass_demo.cpp: Added #pragmas which allow compilation in ansi strict - mode under Metrowerks - - functions.h: Added virtual_function as part of phantom base class removal; - expanded commentary - - pyptr.h: Added some missing 'typename's and a GCC workaround fix - - subclass.cpp: Added missing string literal const_cast<>s. - -2000-11-03 10:58 - - Fix friend function instantiation bug caught by Metrowerks (thanks - Metrowerks!) - - Add proof-of-concept for one technique of wrapping function that return a - pointer - - Worked around MSVC optimizer bug by writing to_python(double) and - to_python(float) out-of-line - -2000-11-02 23:25 - - Add /Zm200 option to vc6_prj to deal with MSVC resource limitations - - Remove conflicting /Ot option from vc6_prj release build - -======= Release ======= -2000-11-02 17:42 - - Added a fix for interactions between default virtual function - implementations and declare_base(). You still need to write your - declare_base() /after/ all member functions have been def()d for the two - classes concerned. Many, many thanks to Ullrich Koethe - for all his work on this. - - Added missing conversions: - to_python(float) - from_python(const char* const&) - from_python(const double&) - from_python(const float&) - - Added a Regression test for a reference-counting bug thanks to Mark Evans - () - - const-ify ClassBase::getattr() - - Add repr() function to Class - - Add to_python/from_python conversions for PyPtr - - Standardize set_item/get_item interfaces (instead of proxies) for Dict and List - - Add Reprable<> template to newtypes.h - - Fix a bug wherein the __module__ attribute would be lost for classes that have a - default virtual function implementation. - - Remove extra ';' in module.cpp thanks to "Ralf W. Grosse-Kunstleve" - - - Fix a bug in the code of example1.html diff --git a/src/SConscript b/src/SConscript index a1d3de6b..6d81a9dc 100644 --- a/src/SConscript +++ b/src/SConscript @@ -42,3 +42,13 @@ env.BoostLibrary( 'import.cpp', 'exec.cpp', 'object/function_doc_signature.cpp']) + +if env['NUMPY']: + env.BoostLibrary( + 'numpy', + ['numpy/dtype.cpp', + 'numpy/matrix.cpp', + 'numpy/ndarray.cpp', + 'numpy/numpy.cpp', + 'numpy/scalars.cpp', + 'numpy/ufunc.cpp']) diff --git a/src/numpy/dtype.cpp b/src/numpy/dtype.cpp new file mode 100644 index 00000000..13904ddd --- /dev/null +++ b/src/numpy/dtype.cpp @@ -0,0 +1,214 @@ +// Copyright Jim Bosch 2010-2012. +// Copyright Stefan Seefeld 2016. +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +#ifdef _MSC_VER +#include +#endif +#define BOOST_PYTHON_NUMPY_INTERNAL +#include + +#define DTYPE_FROM_CODE(code) \ + dtype(python::detail::new_reference(reinterpret_cast(PyArray_DescrFromType(code)))) + +#define BUILTIN_INT_DTYPE(bits) \ + template <> struct builtin_int_dtype< bits, false > { \ + static dtype get() { return DTYPE_FROM_CODE(NPY_INT ## bits); } \ + }; \ + template <> struct builtin_int_dtype< bits, true > { \ + static dtype get() { return DTYPE_FROM_CODE(NPY_UINT ## bits); } \ + }; \ + template dtype get_int_dtype< bits, false >(); \ + template dtype get_int_dtype< bits, true >() + +#define BUILTIN_FLOAT_DTYPE(bits) \ + template <> struct builtin_float_dtype< bits > { \ + static dtype get() { return DTYPE_FROM_CODE(NPY_FLOAT ## bits); } \ + }; \ + template dtype get_float_dtype< bits >() + +#define BUILTIN_COMPLEX_DTYPE(bits) \ + template <> struct builtin_complex_dtype< bits > { \ + static dtype get() { return DTYPE_FROM_CODE(NPY_COMPLEX ## bits); } \ + }; \ + template dtype get_complex_dtype< bits >() + +namespace boost { namespace python { namespace converter { +NUMPY_OBJECT_MANAGER_TRAITS_IMPL(PyArrayDescr_Type, numpy::dtype) +} // namespace boost::python::converter + +namespace numpy { +namespace detail { + +dtype builtin_dtype::get() { return DTYPE_FROM_CODE(NPY_BOOL); } + +template struct builtin_int_dtype; +template struct builtin_float_dtype; +template struct builtin_complex_dtype; + +template dtype get_int_dtype() { + return builtin_int_dtype::get(); +} +template dtype get_float_dtype() { return builtin_float_dtype::get(); } +template dtype get_complex_dtype() { return builtin_complex_dtype::get(); } + +BUILTIN_INT_DTYPE(8); +BUILTIN_INT_DTYPE(16); +BUILTIN_INT_DTYPE(32); +BUILTIN_INT_DTYPE(64); +BUILTIN_FLOAT_DTYPE(16); +BUILTIN_FLOAT_DTYPE(32); +BUILTIN_FLOAT_DTYPE(64); +BUILTIN_COMPLEX_DTYPE(64); +BUILTIN_COMPLEX_DTYPE(128); +#if NPY_BITSOF_LONGDOUBLE > NPY_BITSOF_DOUBLE +template <> struct builtin_float_dtype< NPY_BITSOF_LONGDOUBLE > { + static dtype get() { return DTYPE_FROM_CODE(NPY_LONGDOUBLE); } +}; +template dtype get_float_dtype< NPY_BITSOF_LONGDOUBLE >(); +template <> struct builtin_complex_dtype< 2 * NPY_BITSOF_LONGDOUBLE > { + static dtype get() { return DTYPE_FROM_CODE(NPY_CLONGDOUBLE); } +}; +template dtype get_complex_dtype< 2 * NPY_BITSOF_LONGDOUBLE >(); +#endif + +} // namespace detail + +python::detail::new_reference dtype::convert(object const & arg, bool align) +{ + PyArray_Descr* obj=NULL; + if (align) + { + if (PyArray_DescrAlignConverter(arg.ptr(), &obj) < 0) + throw_error_already_set(); + } + else + { + if (PyArray_DescrConverter(arg.ptr(), &obj) < 0) + throw_error_already_set(); + } + return python::detail::new_reference(reinterpret_cast(obj)); +} + +int dtype::get_itemsize() const { return reinterpret_cast(ptr())->elsize;} + +bool equivalent(dtype const & a, dtype const & b) { + // On Windows x64, the behaviour described on + // http://docs.scipy.org/doc/numpy/reference/c-api.array.html for + // PyArray_EquivTypes unfortunately does not extend as expected: + // "For example, on 32-bit platforms, NPY_LONG and NPY_INT are equivalent". + // This should also hold for 64-bit platforms (and does on Linux), but not + // on Windows. Implement an alternative: +#ifdef _MSC_VER + if (sizeof(long) == sizeof(int) && + // Manually take care of the type equivalence. + ((a == dtype::get_builtin() || a == dtype::get_builtin()) && + (b == dtype::get_builtin() || b == dtype::get_builtin()) || + (a == dtype::get_builtin() || a == dtype::get_builtin()) && + (b == dtype::get_builtin() || b == dtype::get_builtin()))) { + return true; + } else { + return PyArray_EquivTypes( + reinterpret_cast(a.ptr()), + reinterpret_cast(b.ptr()) + ); + } +#else + return PyArray_EquivTypes( + reinterpret_cast(a.ptr()), + reinterpret_cast(b.ptr()) + ); +#endif +} + +namespace +{ + +namespace pyconv = boost::python::converter; + +template +class array_scalar_converter +{ +public: + + static PyTypeObject const * get_pytype() + { + // This implementation depends on the fact that get_builtin returns pointers to objects + // NumPy has declared statically, and that the typeobj member also refers to a static + // object. That means we don't need to do any reference counting. + // In fact, I'm somewhat concerned that increasing the reference count of any of these + // might cause leaks, because I don't think Boost.Python ever decrements it, but it's + // probably a moot point if everything is actually static. + return reinterpret_cast(dtype::get_builtin().ptr())->typeobj; + } + + static void * convertible(PyObject * obj) + { + if (obj->ob_type == get_pytype()) + { + return obj; + } + else + { + dtype dt(python::detail::borrowed_reference(obj->ob_type)); + if (equivalent(dt, dtype::get_builtin())) + { + return obj; + } + } + return 0; + } + + static void convert(PyObject * obj, pyconv::rvalue_from_python_stage1_data* data) + { + void * storage = reinterpret_cast*>(data)->storage.bytes; + // We assume std::complex is a "standard layout" here and elsewhere; not guaranteed by + // C++03 standard, but true in every known implementation (and guaranteed by C++11). + PyArray_ScalarAsCtype(obj, reinterpret_cast(storage)); + data->convertible = storage; + } + + static void declare() + { + pyconv::registry::push_back(&convertible, &convert, python::type_id() +#ifndef BOOST_PYTHON_NO_PY_SIGNATURES + , &get_pytype +#endif + ); + } + +}; + +} // anonymous + +void dtype::register_scalar_converters() +{ + array_scalar_converter::declare(); + array_scalar_converter::declare(); + array_scalar_converter::declare(); + array_scalar_converter::declare(); + array_scalar_converter::declare(); + array_scalar_converter::declare(); + array_scalar_converter::declare(); +#ifdef _MSC_VER + // Since the npy_(u)int32 types are defined as long types and treated + // as being different from the int32 types, these converters must be declared + // explicitely. + array_scalar_converter::declare(); + array_scalar_converter::declare(); +#endif + array_scalar_converter::declare(); + array_scalar_converter::declare(); + array_scalar_converter::declare(); + array_scalar_converter::declare(); + array_scalar_converter< std::complex >::declare(); + array_scalar_converter< std::complex >::declare(); +#if NPY_BITSOF_LONGDOUBLE > NPY_BITSOF_DOUBLE + array_scalar_converter::declare(); + array_scalar_converter< std::complex >::declare(); +#endif +} + +}}} // namespace boost::python::numpy diff --git a/src/numpy/matrix.cpp b/src/numpy/matrix.cpp new file mode 100644 index 00000000..47d22616 --- /dev/null +++ b/src/numpy/matrix.cpp @@ -0,0 +1,63 @@ +// Copyright Jim Bosch 2010-2012. +// Copyright Stefan Seefeld 2016. +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +#define BOOST_PYTHON_NUMPY_INTERNAL +#include +#include + +namespace boost { namespace python { namespace numpy +{ +namespace detail +{ +inline object get_matrix_type() +{ + object module = import("numpy"); + return module.attr("matrix"); +} +} // namespace boost::python::numpy::detail +} // namespace boost::python::numpy + +namespace converter +{ + +PyTypeObject const * object_manager_traits::get_pytype() +{ + return reinterpret_cast(numpy::detail::get_matrix_type().ptr()); +} + +} // namespace boost::python::converter + +namespace numpy +{ + +object matrix::construct(object const & obj, dtype const & dt, bool copy) +{ + return numpy::detail::get_matrix_type()(obj, dt, copy); +} + +object matrix::construct(object const & obj, bool copy) +{ + return numpy::detail::get_matrix_type()(obj, object(), copy); +} + +matrix matrix::view(dtype const & dt) const +{ + return matrix(python::detail::new_reference + (PyObject_CallMethod(this->ptr(), const_cast("view"), const_cast("O"), dt.ptr()))); +} + +matrix matrix::copy() const +{ + return matrix(python::detail::new_reference + (PyObject_CallMethod(this->ptr(), const_cast("copy"), const_cast("")))); +} + +matrix matrix::transpose() const +{ + return matrix(extract(ndarray::transpose())); +} + +}}} // namespace boost::python::numpy diff --git a/src/numpy/ndarray.cpp b/src/numpy/ndarray.cpp new file mode 100644 index 00000000..710e3d49 --- /dev/null +++ b/src/numpy/ndarray.cpp @@ -0,0 +1,277 @@ +// Copyright Jim Bosch 2010-2012. +// Copyright Stefan Seefeld 2016. +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +#define BOOST_PYTHON_NUMPY_INTERNAL +#include +#include + +namespace boost { namespace python { +namespace converter +{ +NUMPY_OBJECT_MANAGER_TRAITS_IMPL(PyArray_Type, numpy::ndarray) +} // namespace boost::python::converter + +namespace numpy +{ +namespace detail +{ + +ndarray::bitflag numpy_to_bitflag(int const f) +{ + ndarray::bitflag r = ndarray::NONE; + if (f & NPY_C_CONTIGUOUS) r = (r | ndarray::C_CONTIGUOUS); + if (f & NPY_F_CONTIGUOUS) r = (r | ndarray::F_CONTIGUOUS); + if (f & NPY_ALIGNED) r = (r | ndarray::ALIGNED); + if (f & NPY_WRITEABLE) r = (r | ndarray::WRITEABLE); + return r; +} + +int bitflag_to_numpy(ndarray::bitflag f) +{ + int r = 0; + if (f & ndarray::C_CONTIGUOUS) r |= NPY_C_CONTIGUOUS; + if (f & ndarray::F_CONTIGUOUS) r |= NPY_F_CONTIGUOUS; + if (f & ndarray::ALIGNED) r |= NPY_ALIGNED; + if (f & ndarray::WRITEABLE) r |= NPY_WRITEABLE; + return r; +} + +bool is_c_contiguous(std::vector const & shape, + std::vector const & strides, + int itemsize) +{ + std::vector::const_reverse_iterator j = strides.rbegin(); + int total = itemsize; + for (std::vector::const_reverse_iterator i = shape.rbegin(); i != shape.rend(); ++i, ++j) + { + if (total != *j) return false; + total *= (*i); + } + return true; +} + +bool is_f_contiguous(std::vector const & shape, + std::vector const & strides, + int itemsize) +{ + std::vector::const_iterator j = strides.begin(); + int total = itemsize; + for (std::vector::const_iterator i = shape.begin(); i != shape.end(); ++i, ++j) + { + if (total != *j) return false; + total *= (*i); + } + return true; +} + +bool is_aligned(std::vector const & strides, + int itemsize) +{ + for (std::vector::const_iterator i = strides.begin(); i != strides.end(); ++i) + { + if (*i % itemsize) return false; + } + return true; +} + +inline PyArray_Descr * incref_dtype(dtype const & dt) +{ + Py_INCREF(dt.ptr()); + return reinterpret_cast(dt.ptr()); +} + +ndarray from_data_impl(void * data, + dtype const & dt, + python::object const & shape, + python::object const & strides, + python::object const & owner, + bool writeable) +{ + std::vector shape_(len(shape)); + std::vector strides_(len(strides)); + if (shape_.size() != strides_.size()) + { + PyErr_SetString(PyExc_ValueError, "Length of shape and strides arrays do not match."); + python::throw_error_already_set(); + } + for (std::size_t i = 0; i < shape_.size(); ++i) + { + shape_[i] = python::extract(shape[i]); + strides_[i] = python::extract(strides[i]); + } + return from_data_impl(data, dt, shape_, strides_, owner, writeable); +} + +ndarray from_data_impl(void * data, + dtype const & dt, + std::vector const & shape, + std::vector const & strides, + python::object const & owner, + bool writeable) +{ + if (shape.size() != strides.size()) + { + PyErr_SetString(PyExc_ValueError, "Length of shape and strides arrays do not match."); + python::throw_error_already_set(); + } + int itemsize = dt.get_itemsize(); + int flags = 0; + if (writeable) flags |= NPY_WRITEABLE; + if (is_c_contiguous(shape, strides, itemsize)) flags |= NPY_C_CONTIGUOUS; + if (is_f_contiguous(shape, strides, itemsize)) flags |= NPY_F_CONTIGUOUS; + if (is_aligned(strides, itemsize)) flags |= NPY_ALIGNED; + ndarray r(python::detail::new_reference + (PyArray_NewFromDescr(&PyArray_Type, + incref_dtype(dt), + shape.size(), + const_cast(&shape.front()), + const_cast(&strides.front()), + data, + flags, + NULL))); + r.set_base(owner); + return r; +} + +} // namespace detail + +ndarray ndarray::view(dtype const & dt) const +{ + return ndarray(python::detail::new_reference + (PyObject_CallMethod(this->ptr(), const_cast("view"), const_cast("O"), dt.ptr()))); +} + +ndarray ndarray::astype(dtype const & dt) const +{ + return ndarray(python::detail::new_reference + (PyObject_CallMethod(this->ptr(), const_cast("astype"), const_cast("O"), dt.ptr()))); +} + +ndarray ndarray::copy() const +{ + return ndarray(python::detail::new_reference + (PyObject_CallMethod(this->ptr(), const_cast("copy"), const_cast("")))); +} + +dtype ndarray::get_dtype() const +{ + return dtype(python::detail::borrowed_reference(get_struct()->descr)); +} + +python::object ndarray::get_base() const +{ + if (get_struct()->base == NULL) return object(); + return python::object(python::detail::borrowed_reference(get_struct()->base)); +} + +void ndarray::set_base(object const & base) +{ + Py_XDECREF(get_struct()->base); + if (base != object()) + { + Py_INCREF(base.ptr()); + get_struct()->base = base.ptr(); + } + else + { + get_struct()->base = NULL; + } +} + +ndarray::bitflag ndarray::get_flags() const +{ + return numpy::detail::numpy_to_bitflag(get_struct()->flags); +} + +ndarray ndarray::transpose() const +{ + return ndarray(python::detail::new_reference + (PyArray_Transpose(reinterpret_cast(this->ptr()), NULL))); +} + +ndarray ndarray::squeeze() const +{ + return ndarray(python::detail::new_reference + (PyArray_Squeeze(reinterpret_cast(this->ptr())))); +} + +ndarray ndarray::reshape(python::tuple const & shape) const +{ + return ndarray(python::detail::new_reference + (PyArray_Reshape(reinterpret_cast(this->ptr()), shape.ptr()))); +} + +python::object ndarray::scalarize() const +{ + Py_INCREF(ptr()); + return python::object(python::detail::new_reference(PyArray_Return(reinterpret_cast(ptr())))); +} + +ndarray zeros(python::tuple const & shape, dtype const & dt) +{ + int nd = len(shape); + boost::scoped_array dims(new Py_intptr_t[nd]); + for (int n=0; n(shape[n]); + return ndarray(python::detail::new_reference + (PyArray_Zeros(nd, dims.get(), detail::incref_dtype(dt), 0))); +} + +ndarray zeros(int nd, Py_intptr_t const * shape, dtype const & dt) +{ + return ndarray(python::detail::new_reference + (PyArray_Zeros(nd, const_cast(shape), detail::incref_dtype(dt), 0))); +} + +ndarray empty(python::tuple const & shape, dtype const & dt) +{ + int nd = len(shape); + boost::scoped_array dims(new Py_intptr_t[nd]); + for (int n=0; n(shape[n]); + return ndarray(python::detail::new_reference + (PyArray_Empty(nd, dims.get(), detail::incref_dtype(dt), 0))); +} + +ndarray empty(int nd, Py_intptr_t const * shape, dtype const & dt) +{ + return ndarray(python::detail::new_reference + (PyArray_Empty(nd, const_cast(shape), detail::incref_dtype(dt), 0))); +} + +ndarray array(python::object const & obj) +{ + return ndarray(python::detail::new_reference + (PyArray_FromAny(obj.ptr(), NULL, 0, 0, NPY_ENSUREARRAY, NULL))); +} + +ndarray array(python::object const & obj, dtype const & dt) +{ + return ndarray(python::detail::new_reference + (PyArray_FromAny(obj.ptr(), detail::incref_dtype(dt), 0, 0, NPY_ENSUREARRAY, NULL))); +} + +ndarray from_object(python::object const & obj, dtype const & dt, int nd_min, int nd_max, ndarray::bitflag flags) +{ + int requirements = detail::bitflag_to_numpy(flags); + return ndarray(python::detail::new_reference + (PyArray_FromAny(obj.ptr(), + detail::incref_dtype(dt), + nd_min, nd_max, + requirements, + NULL))); +} + +ndarray from_object(python::object const & obj, int nd_min, int nd_max, ndarray::bitflag flags) +{ + int requirements = detail::bitflag_to_numpy(flags); + return ndarray(python::detail::new_reference + (PyArray_FromAny(obj.ptr(), + NULL, + nd_min, nd_max, + requirements, + NULL))); +} + +}}} // namespace boost::python::numpy diff --git a/src/numpy/numpy.cpp b/src/numpy/numpy.cpp new file mode 100644 index 00000000..8e259bc7 --- /dev/null +++ b/src/numpy/numpy.cpp @@ -0,0 +1,33 @@ +// Copyright Jim Bosch 2010-2012. +// Copyright Stefan Seefeld 2016. +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +#define BOOST_PYTHON_NUMPY_INTERNAL_MAIN +#include +#include + +namespace boost { namespace python { namespace numpy { + +#if PY_MAJOR_VERSION == 2 +static void wrap_import_array() +{ + import_array(); +} +#else +static void * wrap_import_array() +{ + import_array(); +} +#endif + +void initialize(bool register_scalar_converters) +{ + wrap_import_array(); + import_ufunc(); + if (register_scalar_converters) + dtype::register_scalar_converters(); +} + +}}} // namespace boost::python::numpy diff --git a/src/numpy/scalars.cpp b/src/numpy/scalars.cpp new file mode 100644 index 00000000..3947c06f --- /dev/null +++ b/src/numpy/scalars.cpp @@ -0,0 +1,36 @@ +// Copyright Jim Bosch 2010-2012. +// Copyright Stefan Seefeld 2016. +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +#define BOOST_PYTHON_NUMPY_INTERNAL +#include + +namespace boost { namespace python { +namespace converter +{ +NUMPY_OBJECT_MANAGER_TRAITS_IMPL(PyVoidArrType_Type, numpy::void_) +} // namespace boost::python::converter + +namespace numpy +{ + +void_::void_(Py_ssize_t size) + : object(python::detail::new_reference + (PyObject_CallFunction((PyObject*)&PyVoidArrType_Type, const_cast("i"), size))) +{} + +void_ void_::view(dtype const & dt) const +{ + return void_(python::detail::new_reference + (PyObject_CallMethod(this->ptr(), const_cast("view"), const_cast("O"), dt.ptr()))); +} + +void_ void_::copy() const +{ + return void_(python::detail::new_reference + (PyObject_CallMethod(this->ptr(), const_cast("copy"), const_cast("")))); +} + +}}} // namespace boost::python::numpy diff --git a/src/numpy/ufunc.cpp b/src/numpy/ufunc.cpp new file mode 100644 index 00000000..173d7213 --- /dev/null +++ b/src/numpy/ufunc.cpp @@ -0,0 +1,65 @@ +// Copyright Jim Bosch 2010-2012. +// Copyright Stefan Seefeld 2016. +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +#define BOOST_PYTHON_NUMPY_INTERNAL +#include +#include + +namespace boost { namespace python { +namespace converter +{ +NUMPY_OBJECT_MANAGER_TRAITS_IMPL(PyArrayMultiIter_Type, numpy::multi_iter) +} // namespace boost::python::converter + +namespace numpy +{ + +multi_iter make_multi_iter(object const & a1) +{ + return multi_iter(python::detail::new_reference(PyArray_MultiIterNew(1, a1.ptr()))); +} + + multi_iter make_multi_iter(object const & a1, object const & a2) +{ + return multi_iter(python::detail::new_reference(PyArray_MultiIterNew(2, a1.ptr(), a2.ptr()))); +} + +multi_iter make_multi_iter(object const & a1, object const & a2, object const & a3) +{ + return multi_iter(python::detail::new_reference(PyArray_MultiIterNew(3, a1.ptr(), a2.ptr(), a3.ptr()))); +} + +void multi_iter::next() +{ + PyArray_MultiIter_NEXT(ptr()); +} + +bool multi_iter::not_done() const +{ + return PyArray_MultiIter_NOTDONE(ptr()); +} + +char * multi_iter::get_data(int i) const +{ + return reinterpret_cast(PyArray_MultiIter_DATA(ptr(), i)); +} + +int multi_iter::get_nd() const +{ + return reinterpret_cast(ptr())->nd; +} + +Py_intptr_t const * multi_iter::get_shape() const +{ + return reinterpret_cast(ptr())->dimensions; +} + +Py_intptr_t multi_iter::shape(int n) const +{ + return reinterpret_cast(ptr())->dimensions[n]; +} + +}}} // namespace boost::python::numpy diff --git a/src/str.cpp b/src/str.cpp index 0bc225aa..5122f7f5 100644 --- a/src/str.cpp +++ b/src/str.cpp @@ -162,6 +162,22 @@ bool str_base::endswith(object_cref suffix) const return result; } +bool str_base::endswith(object_cref suffix, object_cref start) const +{ + bool result = _BOOST_PYTHON_ASLONG(this->attr("endswith")(suffix,start).ptr()); + if (PyErr_Occurred()) + throw_error_already_set(); + return result; +} + +bool str_base::endswith(object_cref suffix, object_cref start, object_cref end) const +{ + bool result = _BOOST_PYTHON_ASLONG(this->attr("endswith")(suffix,start,end).ptr()); + if (PyErr_Occurred()) + throw_error_already_set(); + return result; +} + BOOST_PYTHON_DEFINE_STR_METHOD(expandtabs, 0) BOOST_PYTHON_DEFINE_STR_METHOD(expandtabs, 1) diff --git a/test/Jamfile.v2 b/test/Jamfile similarity index 94% rename from test/Jamfile.v2 rename to test/Jamfile index 4ce16135..72d60d37 100644 --- a/test/Jamfile.v2 +++ b/test/Jamfile @@ -4,6 +4,7 @@ import python ; import os ; +import ../../config/checks/config : requires ; lib socket ; @@ -85,7 +86,11 @@ bpl-test crossmod_exception [ bpl-test properties ] [ bpl-test return_arg ] [ bpl-test staticmethod ] -[ bpl-test shared_ptr ] +[ bpl-test boost_shared_ptr ] +[ bpl-test shared_ptr + : # sources + : [ requires cxx11_smart_ptr ] +] [ bpl-test enable_shared_from_this ] [ bpl-test andreas_beyer ] [ bpl-test wrapper_held_type ] @@ -102,7 +107,6 @@ bpl-test crossmod_exception [ bpl-test minimal ] [ bpl-test args ] [ bpl-test raw_ctor ] -[ bpl-test numpy : printer.py numeric_tests.py numarray_tests.py numpy.py numpy.cpp ] [ bpl-test enum : test_enum.py enum_ext.cpp ] [ bpl-test exception_translator ] [ bpl-test pearu1 : test_cltree.py cltree.cpp ] @@ -233,4 +237,14 @@ bpl-test crossmod_opaque [ py-compile-fail ./as_to_python_function.cpp ] [ py-compile-fail ./object_fail1.cpp ] +# --- NumPy tests --- + +[ numpy-test numpy/dtype ] +[ numpy-test numpy/ufunc ] +[ numpy-test numpy/templates ] +[ numpy-test numpy/ndarray ] +[ numpy-test numpy/indexing ] +[ numpy-test numpy/shapes ] + + ; diff --git a/test/SConscript b/test/SConscript index 896783c8..ee41547f 100644 --- a/test/SConscript +++ b/test/SConscript @@ -38,18 +38,15 @@ for test in [('injected',), ('properties',), ('return_arg',), ('staticmethod',), - ('shared_ptr',), + ('boost_shared_ptr',), ('enable_shared_from_this',), ('andreas_beyer',), ('polymorphism',), ('polymorphism2',), ('wrapper_held_type',), - ('polymorphism2_auto_ptr',), - ('auto_ptr',), ('minimal',), ('args',), ('raw_ctor',), - ('numpy',None, ['printer.py', 'numeric_tests.py', 'numarray_tests.py']), ('exception_translator',), ('test_enum', ['enum_ext']), ('test_cltree', ['cltree']), @@ -82,7 +79,7 @@ for test in [('injected',), ('extract',), ('crossmod_opaque', ['crossmod_opaque_a', 'crossmod_opaque_b']), ('opaque',), - ('voidptr',), +# ('voidptr',), ('pickle1',), ('pickle2',), ('pickle3',), @@ -94,6 +91,19 @@ for test in [('injected',), ('pointer_vector',)]: tests+=env.BPLTest(*test) +if env['CXX11']: + for test in [ + ('shared_ptr',), + ]: + tests+=env.BPLTest(*test) +else: + for test in [ + ('polymorphism2_auto_ptr',), + ('auto_ptr',), + ]: + tests+=env.BPLTest(*test) + + test = env.BoostRunPythonScript('test_builtin_converters.py') Depends( test, @@ -134,5 +144,17 @@ if platform.system() == 'Windows': tests+=env.BPLTest('calling_conventions') tests+=env.BPLTest('calling_conventions_mf') +if env['NUMPY']: + numpy_env = env.Clone() + numpy_env.BoostUseLib('numpy') + for test in [('numpy/dtype',), + ('numpy/ufunc',), + ('numpy/templates',), + ('numpy/ndarray',), + ('numpy/indexing',), + ('numpy/shapes',),]: + tests+=numpy_env.BPLTest(*test) + + env.BoostTestSummary(tests) AlwaysBuild(tests) diff --git a/class.cpp b/test/boost_shared_ptr.cpp similarity index 50% rename from class.cpp rename to test/boost_shared_ptr.cpp index 078bebdf..57e50aa3 100644 --- a/class.cpp +++ b/test/boost_shared_ptr.cpp @@ -1,28 +1,20 @@ +// Copyright David Abrahams 2002. +// Copyright Stefan Seefeld 2016. // Distributed under the Boost Software License, Version 1.0. (See // accompanying file LICENSE_1_0.txt or copy at // http://www.boost.org/LICENSE_1_0.txt) + #include -#include -#include #include +#include +#include +#include +#include +#include -using namespace boost::python; - -struct X -{ - int x; - X(int n) : x(n) { } -}; - -int x_function(X& x) -{ return x.x; -} - - -BOOST_PYTHON_MODULE(class_ext) -{ - class_("X", init()); - def("x_function", x_function); -} +using boost::shared_ptr; +#define MODULE boost_shared_ptr_ext +#include "shared_ptr.hpp" #include "module_tail.cpp" + diff --git a/test/boost_shared_ptr.py b/test/boost_shared_ptr.py new file mode 100644 index 00000000..31a2ad3d --- /dev/null +++ b/test/boost_shared_ptr.py @@ -0,0 +1,130 @@ +# Copyright David Abrahams 2004. Distributed under the Boost +# Software License, Version 1.0. (See accompanying +# file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +''' +>>> from boost_shared_ptr_ext import * + + Test that shared_ptr can be converted to shared_ptr + +>>> Y.store(YYY(42)) + +>>> x = X(17) +>>> null_x = null(x) +>>> null_x # should be None +>>> identity(null_x) # should also be None + +>>> a = New(1) +>>> A.call_f(a) +1 +>>> New(0) + +>>> type(factory(3)) + +>>> type(factory(42)) + + +>>> class P(Z): +... def v(self): +... return -Z.v(self); +... def __del__(self): +... print('bye') +... +>>> p = P(12) +>>> p.value() +12 +>>> p.v() +-12 +>>> look(p) +12 +>>> try: modify(p) +... except TypeError: pass +... else: 'print(expected a TypeError)' +>>> look(None) +-1 +>>> store(p) +>>> del p +>>> Z.get().v() +-12 +>>> Z.count() +1 +>>> Z.look_store() +12 +>>> Z.release() +bye +>>> Z.count() +0 + +>>> z = Z(13) +>>> z.value() +13 +>>> z.v() +13 +>>> try: modify(z) +... except TypeError: pass +... else: 'print(expected a TypeError)' + +>>> Z.get() # should be None +>>> store(z) +>>> assert Z.get() is z # show that deleter introspection works +>>> del z +>>> Z.get().value() +13 +>>> Z.count() +1 +>>> Z.look_store() +13 +>>> Z.release() +>>> Z.count() +0 + +>>> x = X(17) +>>> x.value() +17 +>>> look(x) +17 +>>> try: modify(x) +... except TypeError: pass +... else: 'print(expected a TypeError)' +>>> look(None) +-1 +>>> store(x) +>>> del x +>>> X.count() +1 +>>> X.look_store() +17 +>>> X.release() +>>> X.count() +0 + + +>>> y = Y(19) +>>> y.value() +19 +>>> modify(y) +>>> look(y) +-1 +>>> store(Y(23)) +>>> Y.count() +1 +>>> Y.look_store() +23 +>>> Y.release() +>>> Y.count() +0 +''' + +def run(args = None): + import sys + import doctest + + if args is not None: + sys.argv = args + return doctest.testmod(sys.modules.get(__name__)) + +if __name__ == '__main__': + print("running...") + import sys + status = run()[0] + if (status == 0): print("Done.") + sys.exit(status) diff --git a/test/exec.cpp b/test/exec.cpp index 9fb005ea..72ff571b 100644 --- a/test/exec.cpp +++ b/test/exec.cpp @@ -58,16 +58,6 @@ void eval_test() void exec_test() { - // Register the module with the interpreter - if (PyImport_AppendInittab(const_cast("embedded_hello"), -#if PY_VERSION_HEX >= 0x03000000 - PyInit_embedded_hello -#else - initembedded_hello -#endif - ) == -1) - throw std::runtime_error("Failed to add embedded_hello to the interpreter's " - "builtin modules"); // Retrieve the main module python::object main = python::import("__main__"); @@ -152,6 +142,20 @@ int main(int argc, char **argv) { BOOST_TEST(argc == 2 || argc == 3); std::string script = argv[1]; + + // Register the module with the interpreter + if (PyImport_AppendInittab(const_cast("embedded_hello"), +#if PY_VERSION_HEX >= 0x03000000 + PyInit_embedded_hello +#else + initembedded_hello +#endif + ) == -1) + { + BOOST_ERROR("Failed to add embedded_hello to the interpreter's " + "builtin modules"); + } + // Initialize the interpreter Py_Initialize(); diff --git a/test/long.py b/test/long.py index 3e5881de..f6392363 100644 --- a/test/long.py +++ b/test/long.py @@ -3,7 +3,7 @@ # file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) import sys if (sys.version_info.major >= 3): - from past.builtins import long + long = int ''' >>> from long_ext import * >>> print(new_long()) diff --git a/test/numarray_tests.py b/test/numarray_tests.py deleted file mode 100644 index be3d9d4e..00000000 --- a/test/numarray_tests.py +++ /dev/null @@ -1,63 +0,0 @@ -# Copyright David Abrahams 2006. Distributed under the Boost -# Software License, Version 1.0. (See accompanying -# file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) -import printer - -# So we can coerce portably across Python versions -bool = type(1 == 1) - -''' ->>> from numpy_ext import * ->>> x = new_array() ->>> y = x.copy() ->>> p = _printer() ->>> check = p.check ->>> exercise_numarray(x, p) - ->>> check(str(y)) - ->>> check(y.argmax()); ->>> check(y.argmax(0)); - ->>> check(y.argmin()); ->>> check(y.argmin(0)); - ->>> check(y.argsort()); ->>> check(y.argsort(1)); - ->>> y.byteswap(); ->>> check(y); - ->>> check(y.diagonal()); ->>> check(y.diagonal(1)); ->>> check(y.diagonal(0, 0)); ->>> check(y.diagonal(0, 1, 0)); - ->>> check(y.is_c_array()); - -# coerce because numarray still returns an int and the C++ interface forces -# the return type to bool ->>> check( bool(y.isbyteswapped()) ); - ->>> check(y.trace()); ->>> check(y.trace(1)); ->>> check(y.trace(0, 0)); ->>> check(y.trace(0, 1, 0)); - ->>> check(y.new('D').getshape()); ->>> check(y.new('D').type()); ->>> y.sort(); ->>> check(y); ->>> check(y.type()); - ->>> check(y.factory((1.2, 3.4))); ->>> check(y.factory((1.2, 3.4), "f8")) ->>> check(y.factory((1.2, 3.4), "f8", true)) ->>> check(y.factory((1.2, 3.4), "f8", true, false)) ->>> check(y.factory((1.2, 3.4), "f8", true, false, None)) ->>> check(y.factory((1.2, 3.4), "f8", true, false, None, (1,2,1))) - ->>> p.results -[] ->>> del p -''' diff --git a/test/numeric_tests.py b/test/numeric_tests.py deleted file mode 100644 index f5019212..00000000 --- a/test/numeric_tests.py +++ /dev/null @@ -1,39 +0,0 @@ -# Copyright David Abrahams 2006. Distributed under the Boost -# Software License, Version 1.0. (See accompanying -# file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) -import printer -''' ->>> from numpy_ext import * ->>> x = new_array() ->>> x[1,1] = 0.0 - ->>> try: take_array(3) -... except TypeError: pass -... else: print('expected a TypeError') - ->>> take_array(x) - ->>> print(x) -[[1 2 3] - [4 0 6] - [7 8 9]] - ->>> y = x.copy() - - ->>> p = _printer() ->>> check = p.check ->>> exercise(x, p) ->>> y[2,1] = 3 ->>> check(y); - ->>> check(y.astype('D')); - ->>> check(y.copy()); - ->>> check(y.typecode()); - ->>> p.results -[] ->>> del p -''' diff --git a/test/numpy.cpp b/test/numpy.cpp deleted file mode 100644 index 9472f92e..00000000 --- a/test/numpy.cpp +++ /dev/null @@ -1,136 +0,0 @@ -// Copyright David Abrahams 2002. -// Distributed under the Boost Software License, Version 1.0. (See -// accompanying file LICENSE_1_0.txt or copy at -// http://www.boost.org/LICENSE_1_0.txt) - -#include -#include -#include -#include -#include - -using namespace boost::python; -namespace py = boost::python; - -// See if we can invoke array() from C++ -numeric::array new_array() -{ - return numeric::array( - py::make_tuple( - py::make_tuple(1,2,3) - , py::make_tuple(4,5,6) - , py::make_tuple(7,8,9) - ) - ); -} - -// test argument conversion -void take_array(numeric::array /*x*/) -{ -} - -// A separate function to invoke the info() member. Must happen -// outside any doctests since this prints directly to stdout and the -// result text includes the address of the 'self' array. -void info(numeric::array const& z) -{ - z.info(); -} - -namespace -{ - object handle_error() - { - PyObject* type, *value, *traceback; - PyErr_Fetch(&type, &value, &traceback); - handle<> ty(type), v(value), tr(traceback); - return object("exception"); - str format("exception type: %sn"); - format += "exception value: %sn"; - format += "traceback:n%s" ; - object ret = format % py::make_tuple(ty, v, tr); - return ret; - } -} -#define CHECK(expr) \ -{ \ - object result; \ - try { result = object(expr); } \ - catch(error_already_set) \ - { \ - result = handle_error(); \ - } \ - check(result); \ -} - -// Tests which work on both Numeric and numarray array objects. Of -// course all of the operators "just work" since numeric::array -// inherits that behavior from object. -void exercise(numeric::array& y, object check) -{ - y[py::make_tuple(2,1)] = 3; - CHECK(y); - CHECK(y.astype('D')); - CHECK(y.copy()); - CHECK(y.typecode()); -} - -// numarray-specific tests. check is a callable object which we can -// use to record intermediate results, which are later compared with -// the results of corresponding python operations. -void exercise_numarray(numeric::array& y, object check) -{ - CHECK(str(y)); - - CHECK(y.argmax()); - CHECK(y.argmax(0)); - - CHECK(y.argmin()); - CHECK(y.argmin(0)); - - CHECK(y.argsort()); - CHECK(y.argsort(1)); - - y.byteswap(); - CHECK(y); - - CHECK(y.diagonal()); - CHECK(y.diagonal(1)); - CHECK(y.diagonal(0, 0)); - CHECK(y.diagonal(0, 1, 0)); - - CHECK(y.is_c_array()); - CHECK(y.isbyteswapped()); - - CHECK(y.trace()); - CHECK(y.trace(1)); - CHECK(y.trace(0, 0)); - CHECK(y.trace(0, 1, 0)); - - CHECK(y.new_("D").getshape()); - CHECK(y.new_("D").type()); - y.sort(); - CHECK(y); - CHECK(y.type()); - - CHECK(y.factory(py::make_tuple(1.2, 3.4))); - CHECK(y.factory(py::make_tuple(1.2, 3.4), "f8")); - CHECK(y.factory(py::make_tuple(1.2, 3.4), "f8", true)); - CHECK(y.factory(py::make_tuple(1.2, 3.4), "f8", true, false)); - CHECK(y.factory(py::make_tuple(1.2, 3.4), "f8", true, false, object())); - CHECK (y.factory(py::make_tuple(1.2, 3.4), "f8", true, false, object(), py::make_tuple(1,2,1))); - -} - -BOOST_PYTHON_MODULE(numpy_ext) -{ - def("new_array", new_array); - def("take_array", take_array); - def("exercise", exercise); - def("exercise_numarray", exercise_numarray); - def("set_module_and_type", &numeric::array::set_module_and_type); - def("get_module_name", &numeric::array::get_module_name); - def("info", info); -} - -#include "module_tail.cpp" diff --git a/test/numpy.py b/test/numpy.py deleted file mode 100644 index 49cc213a..00000000 --- a/test/numpy.py +++ /dev/null @@ -1,87 +0,0 @@ -# Copyright David Abrahams 2004. Distributed under the Boost -# Software License, Version 1.0. (See accompanying -# file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) - -false = 0; -true = 1; - -import doctest, numeric_tests -def _count_failures(test_modules = (numeric_tests,)): - failures = 0 - for m in test_modules: - failures += doctest.testmod(m)[0] - return failures - -def _run(args = None): - import sys, numarray_tests, numeric_tests - - if args is not None: - sys.argv = args - - # See which of the numeric modules are installed - has_numeric = 0 - try: import Numeric - except ImportError: pass - else: - has_numeric = 1 - m = Numeric - - has_numarray = 0 - try: import numarray - except ImportError: pass - else: - has_numarray = 1 - m = numarray - - # Bail if neither one is installed - if not (has_numeric or has_numarray): - return 0 - - # test the info routine outside the doctest. See numpy.cpp for an - # explanation - import numpy_ext - if (has_numarray): - numpy_ext.info(m.array((1,2,3))) - - failures = 0 - - # - # Run tests 4 different ways if both modules are installed, just - # to show that set_module_and_type() is working properly - # - - # run all the tests with default module search - print('testing default extension module:', \ - numpy_ext.get_module_name() or '[numeric support not installed]') - - failures += _count_failures() - - # test against Numeric if installed - if has_numeric: - print('testing Numeric module explicitly') - numpy_ext.set_module_and_type('Numeric', 'ArrayType') - - failures += _count_failures() - - if has_numarray: - print('testing numarray module explicitly') - numpy_ext.set_module_and_type('numarray', 'NDArray') - # Add the _numarray_tests to the list of things to test in - # this case. - failures += _count_failures((numarray_tests, numeric_tests)) - - # see that we can go back to the default - numpy_ext.set_module_and_type('', '') - print('testing default module again:', \ - numpy_ext.get_module_name() or '[numeric support not installed]') - - failures += _count_failures() - - return failures - -if __name__ == '__main__': - print("running...") - import sys - status = _run() - if (status == 0): print("Done.") - sys.exit(status) diff --git a/test/numpy/dtype.cpp b/test/numpy/dtype.cpp new file mode 100644 index 00000000..3a011a25 --- /dev/null +++ b/test/numpy/dtype.cpp @@ -0,0 +1,49 @@ +// Copyright Jim Bosch & Ankit Daftery 2010-2012. +// Copyright Stefan Seefeld 2016. +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +#include +#include + +namespace p = boost::python; +namespace np = boost::python::numpy; + +template +np::dtype accept(T) { + return np::dtype::get_builtin(); +} + +BOOST_PYTHON_MODULE(dtype_ext) +{ + np::initialize(); + // wrap dtype equivalence test, since it isn't available in Python API. + p::def("equivalent", np::equivalent); + // integers, by number of bits + p::def("accept_int8", accept); + p::def("accept_uint8", accept); + p::def("accept_int16", accept); + p::def("accept_uint16", accept); + p::def("accept_int32", accept); + p::def("accept_uint32", accept); + p::def("accept_int64", accept); + p::def("accept_uint64", accept); + // integers, by C name according to NumPy + p::def("accept_bool_", accept); + p::def("accept_byte", accept); + p::def("accept_ubyte", accept); + p::def("accept_short", accept); + p::def("accept_ushort", accept); + p::def("accept_intc", accept); + p::def("accept_uintc", accept); + // floats and complex + p::def("accept_float32", accept); + p::def("accept_complex64", accept< std::complex >); + p::def("accept_float64", accept); + p::def("accept_complex128", accept< std::complex >); + if (sizeof(long double) > sizeof(double)) { + p::def("accept_longdouble", accept); + p::def("accept_clongdouble", accept< std::complex >); + } +} diff --git a/test/numpy/dtype.py b/test/numpy/dtype.py new file mode 100644 index 00000000..a27ee0f5 --- /dev/null +++ b/test/numpy/dtype.py @@ -0,0 +1,63 @@ +#!/usr/bin/env python + +# Copyright Jim Bosch & Ankit Daftery 2010-2012. +# Distributed under the Boost Software License, Version 1.0. +# (See accompanying file LICENSE_1_0.txt or copy at +# http://www.boost.org/LICENSE_1_0.txt) + +import dtype_ext +import unittest +import numpy +import sys +if (sys.version_info.major >= 3): + long = int + +class DtypeTestCase(unittest.TestCase): + + def assertEquivalent(self, a, b): + return self.assert_(dtype_ext.equivalent(a, b), "%r is not equivalent to %r") + + def testIntegers(self): + for bits in (8, 16, 32, 64): + s = getattr(numpy, "int%d" % bits) + u = getattr(numpy, "uint%d" % bits) + fs = getattr(dtype_ext, "accept_int%d" % bits) + fu = getattr(dtype_ext, "accept_uint%d" % bits) + self.assertEquivalent(fs(s(1)), numpy.dtype(s)) + self.assertEquivalent(fu(u(1)), numpy.dtype(u)) + # these should just use the regular Boost.Python converters + self.assertEquivalent(fs(True), numpy.dtype(s)) + self.assertEquivalent(fu(True), numpy.dtype(u)) + self.assertEquivalent(fs(int(1)), numpy.dtype(s)) + self.assertEquivalent(fu(int(1)), numpy.dtype(u)) + self.assertEquivalent(fs(long(1)), numpy.dtype(s)) + self.assertEquivalent(fu(long(1)), numpy.dtype(u)) + for name in ("bool_", "byte", "ubyte", "short", "ushort", "intc", "uintc"): + t = getattr(numpy, name) + ft = getattr(dtype_ext, "accept_%s" % name) + self.assertEquivalent(ft(t(1)), numpy.dtype(t)) + # these should just use the regular Boost.Python converters + self.assertEquivalent(ft(True), numpy.dtype(t)) + if name != "bool_": + self.assertEquivalent(ft(int(1)), numpy.dtype(t)) + self.assertEquivalent(ft(long(1)), numpy.dtype(t)) + + + def testFloats(self): + f = numpy.float32 + c = numpy.complex64 + self.assertEquivalent(dtype_ext.accept_float32(f(numpy.pi)), numpy.dtype(f)) + self.assertEquivalent(dtype_ext.accept_complex64(c(1+2j)), numpy.dtype(c)) + f = numpy.float64 + c = numpy.complex128 + self.assertEquivalent(dtype_ext.accept_float64(f(numpy.pi)), numpy.dtype(f)) + self.assertEquivalent(dtype_ext.accept_complex128(c(1+2j)), numpy.dtype(c)) + if hasattr(numpy, "longdouble") and hasattr(dtype_ext, "accept_longdouble"): + f = numpy.longdouble + c = numpy.clongdouble + self.assertEquivalent(dtype_ext.accept_longdouble(f(numpy.pi)), numpy.dtype(f)) + self.assertEquivalent(dtype_ext.accept_clongdouble(c(1+2j)), numpy.dtype(c)) + + +if __name__=="__main__": + unittest.main() diff --git a/test/numpy/indexing.cpp b/test/numpy/indexing.cpp new file mode 100644 index 00000000..f3cc4571 --- /dev/null +++ b/test/numpy/indexing.cpp @@ -0,0 +1,28 @@ +// Copyright Jim Bosch & Ankit Daftery 2010-2012. +// Copyright Stefan Seefeld 2016. +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +#include +#include + +namespace p = boost::python; +namespace np = boost::python::numpy; + +p::object single(np::ndarray ndarr, int i) { return ndarr[i];} +p::object slice(np::ndarray ndarr, p::slice sl) { return ndarr[sl];} +p::object indexarray(np::ndarray ndarr, np::ndarray d1) { return ndarr[d1];} +p::object indexarray_2d(np::ndarray ndarr, np::ndarray d1,np::ndarray d2) { return ndarr[p::make_tuple(d1,d2)];} +p::object indexslice(np::ndarray ndarr, np::ndarray d1, p::slice sl) { return ndarr[p::make_tuple(d1, sl)];} + +BOOST_PYTHON_MODULE(indexing_ext) +{ + np::initialize(); + p::def("single", single); + p::def("slice", slice); + p::def("indexarray", indexarray); + p::def("indexarray", indexarray_2d); + p::def("indexslice", indexslice); + +} diff --git a/test/numpy/indexing.py b/test/numpy/indexing.py new file mode 100644 index 00000000..ebd9dcba --- /dev/null +++ b/test/numpy/indexing.py @@ -0,0 +1,55 @@ +#!/usr/bin/env python + +# Copyright Jim Bosch & Ankit Daftery 2010-2012. +# Distributed under the Boost Software License, Version 1.0. +# (See accompanying file LICENSE_1_0.txt or copy at +# http://www.boost.org/LICENSE_1_0.txt) + +import unittest +import numpy +import indexing_ext + +class TestIndexing(unittest.TestCase): + + def testSingle(self): + x = numpy.arange(0,10) + for i in range(0,10): + numpy.testing.assert_equal(indexing_ext.single(x,i), i) + for i in range(-10,0): + numpy.testing.assert_equal(indexing_ext.single(x,i),10+i) + + def testSlice(self): + x = numpy.arange(0,10) + sl = slice(3,8) + b = [3,4,5,6,7] + numpy.testing.assert_equal(indexing_ext.slice(x,sl), b) + + def testStepSlice(self): + x = numpy.arange(0,10) + sl = slice(3,8,2) + b = [3,5,7] + numpy.testing.assert_equal(indexing_ext.slice(x,sl), b) + + def testIndex(self): + x = numpy.arange(0,10) + chk = numpy.array([3,4,5,6]) + numpy.testing.assert_equal(indexing_ext.indexarray(x,chk),chk) + chk = numpy.array([[0,1],[2,3]]) + numpy.testing.assert_equal(indexing_ext.indexarray(x,chk),chk) + x = numpy.arange(9).reshape(3,3) + y = numpy.array([0,1]) + z = numpy.array([0,2]) + chk = numpy.array([0,5]) + numpy.testing.assert_equal(indexing_ext.indexarray(x,y,z),chk) + x = numpy.arange(0,10) + b = x>4 + chk = numpy.array([5,6,7,8,9]) + numpy.testing.assert_equal(indexing_ext.indexarray(x,b),chk) + x = numpy.arange(9).reshape(3,3) + b = numpy.array([0,2]) + sl = slice(0,3) + chk = numpy.array([[0,1,2],[6,7,8]]) + numpy.testing.assert_equal(indexing_ext.indexslice(x,b,sl),chk) + +if __name__=="__main__": + unittest.main() diff --git a/test/numpy/ndarray.cpp b/test/numpy/ndarray.cpp new file mode 100644 index 00000000..808872e8 --- /dev/null +++ b/test/numpy/ndarray.cpp @@ -0,0 +1,46 @@ +// Copyright Jim Bosch & Ankit Daftery 2010-2012. +// Copyright Stefan Seefeld 2016. +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +#include + +namespace p = boost::python; +namespace np = boost::python::numpy; + +np::ndarray zeros(p::tuple shape, np::dtype dt) { return np::zeros(shape, dt);} +np::ndarray array2(p::object obj, np::dtype dt) { return np::array(obj,dt);} +np::ndarray array1(p::object obj) { return np::array(obj);} +np::ndarray empty1(p::tuple shape, np::dtype dt) { return np::empty(shape,dt);} + +np::ndarray c_empty(p::tuple shape, np::dtype dt) +{ + // convert 'shape' to a C array so we can test the corresponding + // version of the constructor + unsigned len = p::len(shape); + Py_intptr_t *c_shape = new Py_intptr_t[len]; + for (unsigned i = 0; i != len; ++i) + c_shape[i] = p::extract(shape[i]); + np::ndarray result = np::empty(len, c_shape, dt); + delete [] c_shape; + return result; +} + +np::ndarray transpose(np::ndarray arr) { return arr.transpose();} +np::ndarray squeeze(np::ndarray arr) { return arr.squeeze();} +np::ndarray reshape(np::ndarray arr,p::tuple tup) { return arr.reshape(tup);} + +BOOST_PYTHON_MODULE(ndarray_ext) +{ + np::initialize(); + p::def("zeros", zeros); + p::def("zeros_matrix", zeros, np::as_matrix<>()); + p::def("array", array2); + p::def("array", array1); + p::def("empty", empty1); + p::def("c_empty", c_empty); + p::def("transpose", transpose); + p::def("squeeze", squeeze); + p::def("reshape", reshape); +} diff --git a/test/numpy/ndarray.py b/test/numpy/ndarray.py new file mode 100644 index 00000000..fb92a2a2 --- /dev/null +++ b/test/numpy/ndarray.py @@ -0,0 +1,79 @@ +#!/usr/bin/env python + +# Copyright Jim Bosch & Ankit Daftery 2010-2012. +# Distributed under the Boost Software License, Version 1.0. +# (See accompanying file LICENSE_1_0.txt or copy at +# http://www.boost.org/LICENSE_1_0.txt) + +import ndarray_ext +import unittest +import numpy + +class TestNdarray(unittest.TestCase): + + def testNdzeros(self): + for dtp in (numpy.int16, numpy.int32, numpy.float32, numpy.complex128): + v = numpy.zeros(60, dtype=dtp) + dt = numpy.dtype(dtp) + for shape in ((60,),(6,10),(4,3,5),(2,2,3,5)): + a1 = ndarray_ext.zeros(shape,dt) + a2 = v.reshape(a1.shape) + self.assertEqual(shape,a1.shape) + self.assert_((a1 == a2).all()) + + def testNdzeros_matrix(self): + for dtp in (numpy.int16, numpy.int32, numpy.float32, numpy.complex128): + dt = numpy.dtype(dtp) + shape = (6, 10) + a1 = ndarray_ext.zeros_matrix(shape, dt) + a2 = numpy.matrix(numpy.zeros(shape, dtype=dtp)) + self.assertEqual(shape,a1.shape) + self.assert_((a1 == a2).all()) + self.assertEqual(type(a1), type(a2)) + + def testNdarray(self): + a = range(0,60) + for dtp in (numpy.int16, numpy.int32, numpy.float32, numpy.complex128): + v = numpy.array(a, dtype=dtp) + dt = numpy.dtype(dtp) + a1 = ndarray_ext.array(a) + a2 = ndarray_ext.array(a,dt) + self.assert_((a1 == v).all()) + self.assert_((a2 == v).all()) + for shape in ((60,),(6,10),(4,3,5),(2,2,3,5)): + a1 = a1.reshape(shape) + self.assertEqual(shape,a1.shape) + a2 = a2.reshape(shape) + self.assertEqual(shape,a2.shape) + + def testNdempty(self): + for dtp in (numpy.int16, numpy.int32, numpy.float32, numpy.complex128): + dt = numpy.dtype(dtp) + for shape in ((60,),(6,10),(4,3,5),(2,2,3,5)): + a1 = ndarray_ext.empty(shape,dt) + a2 = ndarray_ext.c_empty(shape,dt) + self.assertEqual(shape,a1.shape) + self.assertEqual(shape,a2.shape) + + def testTranspose(self): + for dtp in (numpy.int16, numpy.int32, numpy.float32, numpy.complex128): + dt = numpy.dtype(dtp) + for shape in ((6,10),(4,3,5),(2,2,3,5)): + a1 = numpy.empty(shape,dt) + a2 = a1.transpose() + a1 = ndarray_ext.transpose(a1) + self.assertEqual(a1.shape,a2.shape) + + def testSqueeze(self): + a1 = numpy.array([[[3,4,5]]]) + a2 = a1.squeeze() + a1 = ndarray_ext.squeeze(a1) + self.assertEqual(a1.shape,a2.shape) + + def testReshape(self): + a1 = numpy.empty((2,2)) + a2 = ndarray_ext.reshape(a1,(1,4)) + self.assertEqual(a2.shape,(1,4)) + +if __name__=="__main__": + unittest.main() diff --git a/test/numpy/shapes.cpp b/test/numpy/shapes.cpp new file mode 100644 index 00000000..a245df15 --- /dev/null +++ b/test/numpy/shapes.cpp @@ -0,0 +1,22 @@ +// Copyright Jim Bosch & Ankit Daftery 2010-2012. +// Copyright Stefan Seefeld 2016. +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +#include + +namespace p = boost::python; +namespace np = boost::python::numpy; + +np::ndarray reshape(np::ndarray old_array, p::tuple shape) +{ + np::ndarray local_shape = old_array.reshape(shape); + return local_shape; +} + +BOOST_PYTHON_MODULE(shapes_ext) +{ + np::initialize(); + p::def("reshape", reshape); +} diff --git a/test/numpy/shapes.py b/test/numpy/shapes.py new file mode 100644 index 00000000..d0a0099c --- /dev/null +++ b/test/numpy/shapes.py @@ -0,0 +1,21 @@ +#!/usr/bin/env python + +# Copyright Jim Bosch & Ankit Daftery 2010-2012. +# Distributed under the Boost Software License, Version 1.0. +# (See accompanying file LICENSE_1_0.txt or copy at +# http://www.boost.org/LICENSE_1_0.txt) + +import shapes_ext +import unittest +import numpy + +class TestShapes(unittest.TestCase): + + def testShapes(self): + a1 = numpy.array([(0,1),(2,3)]) + a1_shape = (1,4) + a1 = shapes_ext.reshape(a1,a1_shape) + self.assertEqual(a1_shape,a1.shape) + +if __name__=="__main__": + unittest.main() diff --git a/test/numpy/templates.cpp b/test/numpy/templates.cpp new file mode 100644 index 00000000..83de6bd2 --- /dev/null +++ b/test/numpy/templates.cpp @@ -0,0 +1,63 @@ +// Copyright Jim Bosch & Ankit Daftery 2010-2012. +// Copyright Stefan Seefeld 2016. +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +#include +#include +#include + +namespace p = boost::python; +namespace np = boost::python::numpy; + +struct ArrayFiller +{ + + typedef boost::mpl::vector< short, int, float, std::complex > TypeSequence; + typedef boost::mpl::vector_c< int, 1, 2 > DimSequence; + + explicit ArrayFiller(np::ndarray const & arg) : argument(arg) {} + + template + void apply() const + { + if (N == 1) + { + char * p = argument.get_data(); + int stride = argument.strides(0); + int size = argument.shape(0); + for (int n = 0; n != size; ++n, p += stride) + *reinterpret_cast(p) = static_cast(n); + } + else + { + char * row_p = argument.get_data(); + int row_stride = argument.strides(0); + int col_stride = argument.strides(1); + int rows = argument.shape(0); + int cols = argument.shape(1); + int i = 0; + for (int n = 0; n != rows; ++n, row_p += row_stride) + { + char * col_p = row_p; + for (int m = 0; m != cols; ++i, ++m, col_p += col_stride) + *reinterpret_cast(col_p) = static_cast(i); + } + } + } + + np::ndarray argument; +}; + +void fill(np::ndarray const & arg) +{ + ArrayFiller filler(arg); + np::invoke_matching_array(arg, filler); +} + +BOOST_PYTHON_MODULE(templates_ext) +{ + np::initialize(); + p::def("fill", fill); +} diff --git a/test/numpy/templates.py b/test/numpy/templates.py new file mode 100755 index 00000000..8290b13a --- /dev/null +++ b/test/numpy/templates.py @@ -0,0 +1,28 @@ +#!/usr/bin/env python + +# Copyright Jim Bosch & Ankit Daftery 2010-2012. +# Distributed under the Boost Software License, Version 1.0. +# (See accompanying file LICENSE_1_0.txt or copy at +# http://www.boost.org/LICENSE_1_0.txt) + +import templates_ext +import unittest +import numpy + +class TestTemplates(unittest.TestCase): + + def testTemplates(self): + for dtype in (numpy.int16, numpy.int32, numpy.float32, numpy.complex128): + v = numpy.arange(12, dtype=dtype) + for shape in ((12,), (4, 3), (2, 6)): + a1 = numpy.zeros(shape, dtype=dtype) + a2 = v.reshape(a1.shape) + templates_ext.fill(a1) + self.assert_((a1 == a2).all()) + a1 = numpy.zeros((12,), dtype=numpy.float64) + self.assertRaises(TypeError, templates_ext.fill, a1) + a1 = numpy.zeros((12,2,3), dtype=numpy.float32) + self.assertRaises(TypeError, templates_ext.fill, a1) + +if __name__=="__main__": + unittest.main() diff --git a/test/numpy/ufunc.cpp b/test/numpy/ufunc.cpp new file mode 100644 index 00000000..3a9d43cb --- /dev/null +++ b/test/numpy/ufunc.cpp @@ -0,0 +1,36 @@ +// Copyright Jim Bosch & Ankit Daftery 2010-2012. +// Copyright Stefan Seefeld 2016. +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +#include + +namespace p = boost::python; +namespace np = boost::python::numpy; + +struct UnaryCallable +{ + typedef double argument_type; + typedef double result_type; + + double operator()(double r) const { return r * 2;} +}; + +struct BinaryCallable +{ + typedef double first_argument_type; + typedef double second_argument_type; + typedef double result_type; + + double operator()(double a, double b) const { return a * 2 + b * 3;} +}; + +BOOST_PYTHON_MODULE(ufunc_ext) +{ + np::initialize(); + p::class_("UnaryCallable") + .def("__call__", np::unary_ufunc::make()); + p::class_< BinaryCallable>("BinaryCallable") + .def("__call__", np::binary_ufunc::make()); +} diff --git a/test/numpy/ufunc.py b/test/numpy/ufunc.py new file mode 100755 index 00000000..e820121e --- /dev/null +++ b/test/numpy/ufunc.py @@ -0,0 +1,57 @@ +#!/usr/bin/env python + +# Copyright Jim Bosch & Ankit Daftery 2010-2012. +# Distributed under the Boost Software License, Version 1.0. +# (See accompanying file LICENSE_1_0.txt or copy at +# http://www.boost.org/LICENSE_1_0.txt) + +import ufunc_ext +import unittest +import numpy +from numpy.testing.utils import assert_array_almost_equal + +class TestUnary(unittest.TestCase): + + def testScalar(self): + f = ufunc_ext.UnaryCallable() + assert_array_almost_equal(f(1.0), 2.0) + assert_array_almost_equal(f(3.0), 6.0) + + def testArray(self): + f = ufunc_ext.UnaryCallable() + a = numpy.arange(5, dtype=float) + b = f(a) + assert_array_almost_equal(b, a*2.0) + c = numpy.zeros(5, dtype=float) + d = f(a,output=c) + self.assert_(c is d) + assert_array_almost_equal(d, a*2.0) + + def testList(self): + f = ufunc_ext.UnaryCallable() + a = range(5) + b = f(a) + assert_array_almost_equal(b/2.0, a) + +class TestBinary(unittest.TestCase): + + def testScalar(self): + f = ufunc_ext.BinaryCallable() + assert_array_almost_equal(f(1.0, 3.0), 11.0) + assert_array_almost_equal(f(3.0, 2.0), 12.0) + + def testArray(self): + f = ufunc_ext.BinaryCallable() + a = numpy.random.randn(5) + b = numpy.random.randn(5) + assert_array_almost_equal(f(a,b), (a*2+b*3)) + c = numpy.zeros(5, dtype=float) + d = f(a,b,output=c) + self.assert_(c is d) + assert_array_almost_equal(d, a*2 + b*3) + assert_array_almost_equal(f(a, 2.0), a*2 + 6.0) + assert_array_almost_equal(f(1.0, b), 2.0 + b*3) + + +if __name__=="__main__": + unittest.main() diff --git a/test/printer.py b/test/printer.py deleted file mode 100644 index 46e064af..00000000 --- a/test/printer.py +++ /dev/null @@ -1,13 +0,0 @@ -# Copyright David Abrahams 2006. Distributed under the Boost -# Software License, Version 1.0. (See accompanying -# file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) -class _printer(object): - def __init__(self): - self.results = []; - def __call__(self, *stuff): - for x in stuff: - self.results.append(str(x)) - def check(self, x): - if self.results[0] != str(x): - print(' Expected:\n %s\n but the C++ interface gave:\n %s' % (x, self.results[0])) - del self.results[0] diff --git a/test/shared_ptr.cpp b/test/shared_ptr.cpp index e5f20a73..f72e6f02 100644 --- a/test/shared_ptr.cpp +++ b/test/shared_ptr.cpp @@ -1,4 +1,5 @@ // Copyright David Abrahams 2002. +// Copyright Stefan Seefeld 2016. // Distributed under the Boost Software License, Version 1.0. (See // accompanying file LICENSE_1_0.txt or copy at // http://www.boost.org/LICENSE_1_0.txt) @@ -8,210 +9,11 @@ #include #include #include -#include -#include "test_class.hpp" - #include -using namespace boost::python; -using boost::shared_ptr; - -typedef test_class<> X; -typedef test_class<1> Y; - -template -struct functions -{ - static int look(shared_ptr const& x) - { - return (x.get()) ? x->value() : -1; - } - - static void store(shared_ptr x) - { - storage = x; - } - - static void release_store() - { - store(shared_ptr()); - } - - static void modify(shared_ptr& x) - { - x.reset(); - } - - static shared_ptr get() { return storage; } - static shared_ptr &get1() { return storage; } - - static int look_store() - { - return look(get()); - } - - template - static void expose(C const& c) - { - def("look", &look); - def("store", &store); - def("modify", &modify); - def("identity", &identity); - def("null", &null); - - const_cast(c) - .def("look", &look) - .staticmethod("look") - .def("store", &store) - .staticmethod("store") - .def("modify", &modify) - .staticmethod("modify") - .def("look_store", &look_store) - .staticmethod("look_store") - .def("identity", &identity) - .staticmethod("identity") - .def("null", &null) - .staticmethod("null") - .def("get1", &get1, return_internal_reference<>()) - .staticmethod("get1") - .def("get", &get) - .staticmethod("get") - .def("count", &T::count) - .staticmethod("count") - .def("release", &release_store) - .staticmethod("release") - ; - } - - static shared_ptr identity(shared_ptr x) { return x; } - static shared_ptr null(T const&) { return shared_ptr(); } - - - static shared_ptr storage; -}; - -template shared_ptr functions::storage; - -struct Z : test_class<2> -{ - Z(int x) : test_class<2>(x) {} - virtual int v() { return this->value(); } -}; - -struct ZWrap : Z -{ - ZWrap(PyObject* self, int x) - : Z(x), m_self(self) {} - - - virtual int v() { return call_method(m_self, "v"); } - int default_v() { return Z::v(); } - - - PyObject* m_self; -}; - -struct YY : Y -{ - YY(int n) : Y(n) {} -}; - -struct YYY : Y -{ - YYY(int n) : Y(n) {} -}; - -shared_ptr factory(int n) -{ - return shared_ptr(n < 42 ? new Y(n) : new YY(n)); -} - -// regressions from Nicodemus - struct A - { - virtual ~A() {}; // silence compiler warnings - virtual int f() = 0; - static int call_f(shared_ptr& a) { return a->f(); } - }; - - struct B: A - { - int f() { return 1; } - }; - - boost::shared_ptr New(bool make) - { - return boost::shared_ptr( make ? new B() : 0 ); - } - - struct A_Wrapper: A - { - A_Wrapper(PyObject* self_): - A(), self(self_) {} - - int f() { - return call_method< int >(self, "f"); - } - - PyObject* self; - }; - -// ------ - -// from Neal Becker - -struct Test { - boost::shared_ptr x; -}; -// ------ - - -BOOST_PYTHON_MODULE(shared_ptr_ext) -{ - class_, boost::noncopyable>("A") - .def("call_f", &A::call_f) - .staticmethod("call_f") - ; - - // This is the ugliness required to register a to-python converter - // for shared_ptr. - objects::class_value_wrapper< - shared_ptr - , objects::make_ptr_instance,A> > - >(); - - def("New", &New); - - def("factory", factory); - - functions::expose( - class_("X", init()) - .def("value", &X::value) - ); - - functions::expose( - class_ >("Y", init()) - .def("value", &Y::value) - ); - - class_, boost::noncopyable>("YY", init()) - ; - - class_, bases >("YYY", init()) - ; - - functions::expose( - class_("Z", init()) - .def("value", &Z::value) - .def("v", &Z::v, &ZWrap::default_v) - ); - -// from Neal Becker - class_ ("Test") - .def_readonly ("x", &Test::x, "x") - ; -// ------ -} +using std::shared_ptr; +#define MODULE shared_ptr_ext +#include "shared_ptr.hpp" #include "module_tail.cpp" diff --git a/test/shared_ptr.hpp b/test/shared_ptr.hpp new file mode 100644 index 00000000..9f9a4b69 --- /dev/null +++ b/test/shared_ptr.hpp @@ -0,0 +1,206 @@ +// Copyright David Abrahams 2002. +// Copyright Stefan Seefeld 2016. +// Distributed under the Boost Software License, Version 1.0. (See +// accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +#include "test_class.hpp" + +using namespace boost::python; + +typedef test_class<> X; +typedef test_class<1> Y; + +template +struct functions +{ + static int look(shared_ptr const& x) + { + return (x.get()) ? x->value() : -1; + } + + static void store(shared_ptr x) + { + storage = x; + } + + static void release_store() + { + store(shared_ptr()); + } + + static void modify(shared_ptr& x) + { + x.reset(); + } + + static shared_ptr get() { return storage; } + static shared_ptr &get1() { return storage; } + + static int look_store() + { + return look(get()); + } + + template + static void expose(C const& c) + { + def("look", &look); + def("store", &store); + def("modify", &modify); + def("identity", &identity); + def("null", &null); + + const_cast(c) + .def("look", &look) + .staticmethod("look") + .def("store", &store) + .staticmethod("store") + .def("modify", &modify) + .staticmethod("modify") + .def("look_store", &look_store) + .staticmethod("look_store") + .def("identity", &identity) + .staticmethod("identity") + .def("null", &null) + .staticmethod("null") + .def("get1", &get1, return_internal_reference<>()) + .staticmethod("get1") + .def("get", &get) + .staticmethod("get") + .def("count", &T::count) + .staticmethod("count") + .def("release", &release_store) + .staticmethod("release") + ; + } + + static shared_ptr identity(shared_ptr x) { return x; } + static shared_ptr null(T const&) { return shared_ptr(); } + + + static shared_ptr storage; +}; + +template shared_ptr functions::storage; + +struct Z : test_class<2> +{ + Z(int x) : test_class<2>(x) {} + virtual int v() { return this->value(); } +}; + +struct ZWrap : Z +{ + ZWrap(PyObject* self, int x) + : Z(x), m_self(self) {} + + + virtual int v() { return call_method(m_self, "v"); } + int default_v() { return Z::v(); } + + + PyObject* m_self; +}; + +struct YY : Y +{ + YY(int n) : Y(n) {} +}; + +struct YYY : Y +{ + YYY(int n) : Y(n) {} +}; + +shared_ptr factory(int n) +{ + return shared_ptr(n < 42 ? new Y(n) : new YY(n)); +} + +// regressions from Nicodemus + struct A + { + virtual ~A() {}; // silence compiler warnings + virtual int f() = 0; + static int call_f(shared_ptr& a) { return a->f(); } + }; + + struct B: A + { + int f() { return 1; } + }; + + shared_ptr New(bool make) + { + return shared_ptr( make ? new B() : 0 ); + } + + struct A_Wrapper: A + { + A_Wrapper(PyObject* self_): + A(), self(self_) {} + + int f() { + return call_method< int >(self, "f"); + } + + PyObject* self; + }; + +// ------ + +// from Neal Becker + +struct Test { + shared_ptr x; +}; +// ------ + + +BOOST_PYTHON_MODULE(MODULE) +{ + class_, boost::noncopyable>("A") + .def("call_f", &A::call_f) + .staticmethod("call_f") + ; + + // This is the ugliness required to register a to-python converter + // for shared_ptr. + objects::class_value_wrapper< + shared_ptr + , objects::make_ptr_instance,A> > + >(); + + def("New", &New); + + def("factory", factory); + + functions::expose( + class_("X", init()) + .def("value", &X::value) + ); + + functions::expose( + class_ >("Y", init()) + .def("value", &Y::value) + ); + + class_, boost::noncopyable>("YY", init()) + ; + + class_, bases >("YYY", init()) + ; + + functions::expose( + class_("Z", init()) + .def("value", &Z::value) + .def("v", &Z::v, &ZWrap::default_v) + ); + +// from Neal Becker + class_ ("Test") + .def_readonly ("x", &Test::x, "x") + ; +// ------ +} diff --git a/test/shared_ptr.py b/test/shared_ptr.py index c70b3174..d250ae7e 100644 --- a/test/shared_ptr.py +++ b/test/shared_ptr.py @@ -5,7 +5,7 @@ >>> from shared_ptr_ext import * Test that shared_ptr can be converted to shared_ptr - + >>> Y.store(YYY(42)) >>> x = X(17) @@ -121,7 +121,7 @@ def run(args = None): if args is not None: sys.argv = args return doctest.testmod(sys.modules.get(__name__)) - + if __name__ == '__main__': print("running...") import sys diff --git a/test/test_builtin_converters.py b/test/test_builtin_converters.py index c3191d9b..fb3c55c1 100644 --- a/test/test_builtin_converters.py +++ b/test/test_builtin_converters.py @@ -3,7 +3,7 @@ # file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) import sys if (sys.version_info.major >= 3): - from past.builtins import long + long = int r""" >>> from builtin_converters_ext import * diff --git a/todo.html b/todo.html deleted file mode 100755 index c2c4bdf7..00000000 --- a/todo.html +++ /dev/null @@ -1,240 +0,0 @@ - - - - - - -Boost.Python TODO list Boost - - - - - - -