The method seems to show up in both files, which I have attached to this email. 
Looking over the SWIG documentation briefly, I found no problems with the files.

- Alex


________________________________
 From: Steve Reinhardt <ste...@gmail.com>
To: Alex Tomala <alegomas...@yahoo.ca> 
Cc: gem5 mailing list <gem5-users@gem5.org> 
Sent: Thursday, January 2, 2014 6:35:43 PM
Subject: Re: [gem5-users] Calling a C++ function in Python
 


And yet the addChild method is showing up in 
build/*/python/m5/internal/param_ChildStates.i?  Is it also in the 
param_ChildStates.py file in that same directory?



On Thu, Jan 2, 2014 at 3:29 PM, Alex Tomala <alegomas...@yahoo.ca> wrote:

Hello,
>
>
>In the topology configuration file I accidentally did 
>childStates.getChild(child), instead of childStates.addChild(child). When I 
>fixed it, I still got the exact same error, which is:
>
>
>  childStates.addChild(child)
>
>  File "/home/alex/Desktop/gem5/gem5-stable/src/python/m5/SimObject.py", line 
>725, in __getattr__
>    % (self.__class__.__name__, attr)
>AttributeError: object 'ChildStates' has no attribute 'addChild'
>
>
>Best regards,
>
>
>Alex Tomala
>
>________________________________
> 
>From: Steve Reinhardt <ste...@gmail.com>
>To: Alex Tomala <alegomas...@yahoo.ca> 
>Cc: gem5 users mailing list <gem5-users@gem5.org> 
>Sent: Thursday, January 2, 2014 6:16:24 PM
>
>Subject: Re: [gem5-users] Calling a C++ function in Python
>
>
>
>I'm a little confused about where the 'getChild' call is coming from since 
>'addChild' is the method you added... are you leaving some parts out of the 
>code you're posting?
>
>
>
>On Thu, Jan 2, 2014 at 3:09 PM, Alex Tomala <alegomas...@yahoo.ca> wrote:
>
>Hello Steve,
>>
>>
>>I tried the following in a topology configuration file:
>>
>>
>>childStates = ChildStates()
>>childStates.addChild(child) #child is the MachineID
>>
>>
>>When I run gem5 I get the following error:
>>
>>
>>  childStates.getChild(child)
>>
>>  File "/home/alex/Desktop/gem5/gem5-stable/src/python/m5/SimObject.py", line 
>>725, in __getattr__
>>    % (self.__class__.__name__, attr)
>>AttributeError: object 'ChildStates' has no attribute 'getChild'
>>
>>
>>This is the only method I tried, but I can't think of any other method to get 
>>it done.
>>
>>
>>One day I may add some information to the wiki, but I am a tad busy now.
>>
>>
>>Best regards,
>>
>>
>>Alex Tomala
>>
>>________________________________
>>
>>From: Steve Reinhardt <ste...@gmail.com>
>>To: Alex Tomala <alegomas...@yahoo.ca>; gem5 users mailing list 
>><gem5-users@gem5.org> 
>>Sent: Thursday, January 2, 2014 5:22:47 PM
>>Subject: Re: [gem5-users] Calling a C++ function in Python
>> 
>>
>>
>>What have you tried and what problems are you having?  It should just be a 
>>matter of calling 'obj.addChild(x)' on your ChildStates object 'obj'.
>>
>>
>>It looks like you've figured most of this out already, but a lot of the 
>>current mechanism was added in this changeset (see particularly the comments 
>>in the added code in SimObject.py):
>>http://repo.gem5.org/gem5/rev/45c9f664a365
>>
>>
>>And here's an example of adding a method:
>>http://repo.gem5.org/gem5/rev/09dcdb40b109
>>
>>
>>If you're feeling ambitious, it would be great if you'd be willing to write 
>>up some documentation on how to add callable functions on the wiki, say on 
>>this page (or a page linked from it):
>>http://gem5.org/SimObjects
>>
>>
>>
>>Thanks,
>>
>>
>>Steve
>>
>>
>>
>>
>>On Thu, Jan 2, 2014 at 1:38 PM, Alex Tomala <alegomas...@yahoo.ca> wrote:
>>
>>Hello,
>>>
>>>
>>>I partially figure out the solution to the problem. I figured out that you 
>>>can over ride the exports_method method from the sim object python file to 
>>>add functions. Now my Python interfacing file looks like this:
>>>
>>>
>>> from m5.params import *
>>>from m5.SimObject import SimObject
>>>from Controller import RubyController
>>>
>>>
>>>class ChildStates(SimObject):
>>>type = 'ChildStates'
>>>cxx_class = 'ChildStates'
>>>cxx_header = '"mem/protocol/ChildStates.hh"
>>>childPorts = Param.Int("Number of Children Ports")
>>>@classmethod
>>>def export_methods(cls, code):
>>>        code('''
>>>void addChild(MachineID child);
>>>''')
>>>
>>>
>>>The *.i file associated with the custom SimObject now contains the proper 
>>>method. My only problem that I have now is that I don't know how to call the 
>>>method in Python. I am guessing that I am importing the wrong module or 
>>>something similar.  
>>>
>>>
>>>- Alex
>>>
>>>
>>>
>>>
>>>
>>>________________________________
>>> From: Alex Tomala <alegomas...@yahoo.ca>
>>>To: gem5 mailing list <gem5-users@gem5.org> 
>>>Sent: Wednesday, January 1, 2014 5:03:13 PM
>>>Subject: Calling a C++ function in Python
>>> 
>>>
>>>
>>>Hello,
>>>
>>>
>>>I created a custom SimObject in C++ that has a function that I want to 
>>>execute in Python. I am wondering how I can interface the function to Python 
>>>as instance.function_name does not seem to work. The function I am using is 
>>>called addChild and it takes a MachineID as an input.
>>>
>>>
>>>My current Python interfacing file looks like this:
>>>
>>>
>>>from m5.params import *
>>>from m5.SimObject import SimObject
>>>from Controller import RubyController
>>>
>>>
>>>class ChildStates(SimObject):
>>>type = 'ChildStates'
>>>cxx_class = 'ChildStates'
>>>cxx_header = '"mem/protocol/ChildStates.hh"
>>>childPorts = Param.Int("Number of Children Ports")
>>>
>>>
>>>Best regards,
>>>
>>>
>>>Alex
>>>
>>>
>>>_______________________________________________
>>>gem5-users mailing list
>>>gem5-users@gem5.org
>>>http://m5sim.org/cgi-bin/mailman/listinfo/gem5-users
>>>
>>
>>
>>
>
>
>
%module(package="m5.internal") param_ChildStates

%{
#include "sim/sim_object.hh"
#include "params/ChildStates.hh"
#include <cstddef>
#include "base/types.hh"
#include "mem/protocol/ChildStates.hh"
/**
  * This is a workaround for bug in swig. Prior to gcc 4.6.1 the STL
  * headers like vector, string, etc. used to automatically pull in
  * the cstddef header but starting with gcc 4.6.1 they no longer do.
  * This leads to swig generated a file that does not compile so we
  * explicitly include cstddef. Additionally, including version 2.0.4,
  * swig uses ptrdiff_t without the std:: namespace prefix which is
  * required with gcc 4.6.1. We explicitly provide access to it.
  */
#include <cstddef>
using std::ptrdiff_t;
%}

%import "stdint.i"
%import "base/types.hh"

%import "python/m5/internal/param_SimObject.i"


// stop swig from creating/wrapping default ctor/dtor
%nodefault ChildStates;
class ChildStates
    : public SimObject
{
  public:
    void addChild(MachineID child);
};

%include "params/ChildStates.hh"
# This file was automatically generated by SWIG (http://www.swig.org).
# Version 2.0.4
#
# Do not make changes to this file unless you know what you are doing--modify
# the SWIG interface file instead.



from sys import version_info
if version_info >= (2,6,0):
    def swig_import_helper():
        from os.path import dirname
        import imp
        fp = None
        try:
            fp, pathname, description = imp.find_module('_param_ChildStates', 
[dirname(__file__)])
        except ImportError:
            import _param_ChildStates
            return _param_ChildStates
        if fp is not None:
            try:
                _mod = imp.load_module('_param_ChildStates', fp, pathname, 
description)
            finally:
                fp.close()
            return _mod
    _param_ChildStates = swig_import_helper()
    del swig_import_helper
else:
    import _param_ChildStates
del version_info
try:
    _swig_property = property
except NameError:
    pass # Python < 2.2 doesn't have 'property'.
def _swig_setattr_nondynamic(self,class_type,name,value,static=1):
    if (name == "thisown"): return self.this.own(value)
    if (name == "this"):
        if type(value).__name__ == 'SwigPyObject':
            self.__dict__[name] = value
            return
    method = class_type.__swig_setmethods__.get(name,None)
    if method: return method(self,value)
    if (not static):
        self.__dict__[name] = value
    else:
        raise AttributeError("You cannot add attributes to %s" % self)

def _swig_setattr(self,class_type,name,value):
    return _swig_setattr_nondynamic(self,class_type,name,value,0)

def _swig_getattr(self,class_type,name):
    if (name == "thisown"): return self.this.own()
    method = class_type.__swig_getmethods__.get(name,None)
    if method: return method(self)
    raise AttributeError(name)

def _swig_repr(self):
    try: strthis = "proxy of " + self.this.__repr__()
    except: strthis = ""
    return "<%s.%s; %s >" % (self.__class__.__module__, 
self.__class__.__name__, strthis,)

try:
    _object = object
    _newclass = 1
except AttributeError:
    class _object : pass
    _newclass = 0


def _swig_setattr_nondynamic_method(set):
    def set_attr(self,name,value):
        if (name == "thisown"): return self.this.own(value)
        if hasattr(self,name) or (name == "this"):
            set(self,name,value)
        else:
            raise AttributeError("You cannot add attributes to %s" % self)
    return set_attr


import param_SimObject
import drain
import serialize
class ChildStates(param_SimObject.SimObject):
    thisown = _swig_property(lambda x: x.this.own(), lambda x, v: 
x.this.own(v), doc='The membership flag')
    def __init__(self, *args, **kwargs): raise AttributeError("No constructor 
defined - class is abstract")
    __repr__ = _swig_repr
    def addChild(self, *args): return 
_param_ChildStates.ChildStates_addChild(self, *args)
ChildStates_swigregister = _param_ChildStates.ChildStates_swigregister
ChildStates_swigregister(ChildStates)

class ChildStatesParams(param_SimObject.SimObjectParams):
    thisown = _swig_property(lambda x: x.this.own(), lambda x, v: 
x.this.own(v), doc='The membership flag')
    __repr__ = _swig_repr
    def create(self): return _param_ChildStates.ChildStatesParams_create(self)
    childPorts = 
_swig_property(_param_ChildStates.ChildStatesParams_childPorts_get, 
_param_ChildStates.ChildStatesParams_childPorts_set)
    def __init__(self): 
        this = _param_ChildStates.new_ChildStatesParams()
        try: self.this.append(this)
        except: self.this = this
    __swig_destroy__ = _param_ChildStates.delete_ChildStatesParams
    __del__ = lambda self : None;
ChildStatesParams_swigregister = 
_param_ChildStates.ChildStatesParams_swigregister
ChildStatesParams_swigregister(ChildStatesParams)



_______________________________________________
gem5-users mailing list
gem5-users@gem5.org
http://m5sim.org/cgi-bin/mailman/listinfo/gem5-users

Reply via email to