Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DM-7021: Port to Python 3 #3

Merged
merged 4 commits into from
Aug 4, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ config.log
*.pyc
*_wrap.cc
*Lib.py
_build.*
doc/html
doc/xml
doc/doxygen.conf
doc/*.tag
doc/*.inc
Expand Down
12 changes: 6 additions & 6 deletions python/lsst/__init__.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
#
#
# LSST Data Management System
# Copyright 2008, 2009, 2010 LSST Corporation.
#
#
# This product includes software developed by the
# LSST Project (http://www.lsst.org/).
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the LSST License Statement and
# the GNU General Public License along with this program. If not,
#
# You should have received a copy of the LSST License Statement and
# the GNU General Public License along with this program. If not,
# see <http://www.lsstcorp.org/LegalNotices/>.
#

Expand Down
42 changes: 30 additions & 12 deletions python/lsst/pex/exceptions/wrappers.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
from future import standard_library
standard_library.install_aliases()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For my education: How do we know when to do this in our code? Or is it done by some munging script?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's the 64k question. Futurize (the script) knows when to do it based on the content of the file that is being futurized. See http://python-future.org/compatible_idioms.html for examples of how to work around portability issues. You do not want to do it in every file.

import warnings
import __builtin__
import builtins

from . import exceptionsLib
from future.utils import with_metaclass

registry = {}


def register(cls):
"""A Python decorator that adds a Python exception wrapper to the registry that maps C++ Exceptions
to their Python wrapper classes.
"""
registry[cls.WrappedClass] = cls
return cls


class ExceptionMeta(type):
"""A metaclass for custom exception wrappers, which adds lookup of class attributes
by delegating to the Swig-generated wrapper.
Expand All @@ -20,13 +25,12 @@ class ExceptionMeta(type):
def __getattr__(self, name):
return getattr(self.WrappedClass, name)


@register
class Exception(StandardError):
class Exception(with_metaclass(ExceptionMeta, builtins.Exception)):
"""The base class for Python-wrapped LSST C++ exceptions.
"""

__metaclass__ = ExceptionMeta

# wrappers.py is an implementation detail, not a public namespace, so we pretend this is defined
# in the package for pretty-printing purposes
__module__ = "lsst.pex.exceptions"
Expand All @@ -52,67 +56,81 @@ def __repr__(self):
def __str__(self):
return self.cpp.asString()


@register
class LogicError(Exception):
WrappedClass = exceptionsLib.LogicError


@register
class DomainError(LogicError):
WrappedClass = exceptionsLib.DomainError


@register
class InvalidParameterError(LogicError):
WrappedClass = exceptionsLib.InvalidParameterError


@register
class LengthError(LogicError):
WrappedClass = exceptionsLib.LengthError


@register
class OutOfRangeError(LogicError):
WrappedClass = exceptionsLib.OutOfRangeError


@register
class RuntimeError(Exception, __builtin__.RuntimeError):
class RuntimeError(Exception, builtins.RuntimeError):
WrappedClass = exceptionsLib.RuntimeError


@register
class RangeError(RuntimeError):
WrappedClass = exceptionsLib.RangeError


@register
class OverflowError(RuntimeError, __builtin__.OverflowError):
class OverflowError(RuntimeError, builtins.OverflowError):
WrappedClass = exceptionsLib.OverflowError


@register
class UnderflowError(RuntimeError, __builtin__.ArithmeticError):
class UnderflowError(RuntimeError, builtins.ArithmeticError):
WrappedClass = exceptionsLib.UnderflowError


@register
class NotFoundError(Exception, __builtin__.LookupError):
class NotFoundError(Exception, builtins.LookupError):
WrappedClass = exceptionsLib.NotFoundError


@register
class MemoryError(RuntimeError, __builtin__.MemoryError):
class MemoryError(RuntimeError, builtins.MemoryError):
WrappedClass = exceptionsLib.MemoryError


@register
class IoError(RuntimeError, __builtin__.IOError):
class IoError(RuntimeError, builtins.IOError):
WrappedClass = exceptionsLib.IoError


@register
class TypeError(RuntimeError, __builtin__.TypeError):
class TypeError(RuntimeError, builtins.TypeError):
WrappedClass = exceptionsLib.TypeError


@register
class TimeoutError(RuntimeError):
WrappedClass = exceptionsLib.TimeoutError


def translate(cpp):
"""Translate a C++ Exception instance to Python and return it."""
PyType = registry.get(type(cpp), None)
if PyType is None:
warnings.warn("Could not find appropriate Python type for C++ Exception")
PyType = Exception
return PyType(cpp)

4 changes: 3 additions & 1 deletion tests/Exception_2.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@
# see <http://www.lsstcorp.org/LegalNotices/>.
#

import sys
from builtins import str

import unittest

import lsst.pex.exceptions
import testLib


class ExceptionTestCase(unittest.TestCase):
"""A test case for C++/Python LsstCppExceptions."""

Expand Down
1 change: 1 addition & 0 deletions ups/pex_exceptions.table
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
setupRequired(base)
setupRequired(boost)
setupRequired(swig)
setupRequired(python_future)

envPrepend(LD_LIBRARY_PATH, ${PRODUCT_DIR}/lib)
envPrepend(DYLD_LIBRARY_PATH, ${PRODUCT_DIR}/lib)
Expand Down