github.com/grumpyhome/grumpy@v0.3.1-0.20201208125205-7b775405bdf1/grumpy-runtime-src/runtime/baseexception.go (about)

     1  // Copyright 2016 Google Inc. All Rights Reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package grumpy
    16  
    17  import (
    18  	"reflect"
    19  )
    20  
    21  // BaseException represents Python 'BaseException' objects.
    22  type BaseException struct {
    23  	Object
    24  	args *Tuple
    25  }
    26  
    27  func toBaseExceptionUnsafe(o *Object) *BaseException {
    28  	return (*BaseException)(o.toPointer())
    29  }
    30  
    31  // ToObject upcasts e to an Object.
    32  func (e *BaseException) ToObject() *Object {
    33  	return &e.Object
    34  }
    35  
    36  // BaseExceptionType corresponds to the Python type 'BaseException'.
    37  var BaseExceptionType = newBasisType("BaseException", reflect.TypeOf(BaseException{}), toBaseExceptionUnsafe, ObjectType)
    38  
    39  func baseExceptionInit(f *Frame, o *Object, args Args, kwargs KWArgs) (*Object, *BaseException) {
    40  	e := toBaseExceptionUnsafe(o)
    41  	e.args = NewTuple(args.makeCopy()...)
    42  	return None, nil
    43  }
    44  
    45  func baseExceptionRepr(f *Frame, o *Object) (*Object, *BaseException) {
    46  	e := toBaseExceptionUnsafe(o)
    47  	argsString := "()"
    48  	if e.args != nil {
    49  		s, raised := Repr(f, e.args.ToObject())
    50  		if raised != nil {
    51  			return nil, raised
    52  		}
    53  		argsString = s.Value()
    54  	}
    55  	return NewStr(e.typ.Name() + argsString).ToObject(), nil
    56  }
    57  
    58  func baseExceptionStr(f *Frame, o *Object) (*Object, *BaseException) {
    59  	e := toBaseExceptionUnsafe(o)
    60  	if e.args == nil || len(e.args.elems) == 0 {
    61  		return NewStr("").ToObject(), nil
    62  	}
    63  	if len(e.args.elems) == 1 {
    64  		s, raised := ToStr(f, e.args.elems[0])
    65  		return s.ToObject(), raised
    66  	}
    67  	s, raised := ToStr(f, e.args.ToObject())
    68  	return s.ToObject(), raised
    69  }
    70  
    71  func initBaseExceptionType(map[string]*Object) {
    72  	BaseExceptionType.slots.Init = &initSlot{baseExceptionInit}
    73  	BaseExceptionType.slots.Repr = &unaryOpSlot{baseExceptionRepr}
    74  	BaseExceptionType.slots.Str = &unaryOpSlot{baseExceptionStr}
    75  }