Skip to content

Commit

Permalink
Merge pull request #22 from blink1073/int_promote
Browse files Browse the repository at this point in the history
Add IntPromote behavior
  • Loading branch information
sccolbert committed Nov 10, 2013
2 parents 441ff55 + 5066c09 commit 2ca8b54
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 2 deletions.
10 changes: 8 additions & 2 deletions atom/scalars.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,18 @@ def __init__(self, default=False, factory=None):
class Int(Value):
""" A value of type `int`.
By default, ints are strictly typed. Pass strict=False to the
constructor to enable int casting for longs and floats.
"""
__slots__ = ()

def __init__(self, default=0, factory=None):
def __init__(self, default=0, factory=None, strict=True):
super(Int, self).__init__(default, factory)
self.set_validate_mode(Validate.Int, None)
if strict:
self.set_validate_mode(Validate.Int, None)
else:
self.set_validate_mode(Validate.IntPromote, None)


class Long(Value):
Expand Down
1 change: 1 addition & 0 deletions atom/src/behaviors.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ enum Mode
NoOp,
Bool,
Int,
IntPromote,
Long,
LongPromote,
Float,
Expand Down
1 change: 1 addition & 0 deletions atom/src/enumtypes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ int import_enumtypes()
add_long( dict_ptr, expand_enum( NoOp ) );
add_long( dict_ptr, expand_enum( Bool ) );
add_long( dict_ptr, expand_enum( Int ) );
add_long( dict_ptr, expand_enum( IntPromote ) );
add_long( dict_ptr, expand_enum( Long ) );
add_long( dict_ptr, expand_enum( LongPromote ) );
add_long( dict_ptr, expand_enum( Float ) );
Expand Down
21 changes: 21 additions & 0 deletions atom/src/validatebehavior.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <iostream>
#include "member.h"
#include "atomlist.h"
#include <limits> // std::numeric_limits


using namespace PythonHelpers;
Expand Down Expand Up @@ -206,6 +207,25 @@ int_handler( Member* member, CAtom* atom, PyObject* oldvalue, PyObject* newvalue
}


static PyObject*
int_promote_handler( Member* member, CAtom* atom, PyObject* oldvalue, PyObject* newvalue )
{
if( PyInt_Check( newvalue ) )
return newref( newvalue );
if( PyFloat_Check( newvalue ) ) {
double value = PyFloat_AS_DOUBLE( newvalue );
if( ( value < std::numeric_limits<long>::max() ) && ( value > std::numeric_limits<long>::min() ))
return PyInt_FromLong( static_cast<long>( value ) );
}
if( PyLong_Check( newvalue ) ) {
long value = PyLong_AsLong( newvalue );
if( ( value < std::numeric_limits<long>::max() ) && ( value > std::numeric_limits<long>::min() ))
return PyInt_FromLong( value );
}
return validate_type_fail( member, atom, newvalue, "int float or long" );
}


static PyObject*
long_handler( Member* member, CAtom* atom, PyObject* oldvalue, PyObject* newvalue )
{
Expand Down Expand Up @@ -658,6 +678,7 @@ handlers[] = {
no_op_handler,
bool_handler,
int_handler,
int_promote_handler,
long_handler,
long_promote_handler,
float_handler,
Expand Down

0 comments on commit 2ca8b54

Please sign in to comment.