473,398 Members | 2,393 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Printing out the objects stack

Hi folks!

For getting a plan how a stack-based VM like Python works, I added a
function that prints out the current object stack. Unfortunately, it
crashes the VM. Could someone here take a look at it? What's wrong with
it?:

--- snip ---
static PyObject *
sys_stack(PyObject *self)
{
PyFrameObject *f = PyThreadState_GET()->frame;
PyObject
**i,
**begin = f->f_localsplus,
**end = f->f_valuestack;

end += f->f_code->co_stacksize;
flog( "co_name: %s\n"
"co_stacksize: %d\n"
"localsplus: %d\n"
"valuestack: %d\n",
PyString_AsString(f->f_code->co_name), f->f_code->co_stacksize,
f->f_localsplus, f->f_valuestack);
flog("locals:\n");
{
PyObject *list = f->f_code->co_names;
int len,i;

len = PyList_Size(list);
for (i=0; i<len; i++) {
PyObject *obi;
char *strval;
obi = PyList_GetItem(list, i);
if ((obi=PyObject_Str(obi))!=NULL) {
if ((strval=PyString_AsString(obi))!=NULL) {
flog("[%d] %s\n", i, strval);
}
}
Py_DECREF(obi);
}
Py_DECREF(list);
}
flog("end of locals\n");
for (i=begin; i<=end; i++) {
int o = ((int)f->f_valuestack - (int)i)/4;
PyObject *obi;
char *strval;

if (*i == NULL) {
flog("NULL\n");
break; }
if ((obi=PyObject_Str(*i)) != NULL) {
if ((strval=PyString_AsString(obi)) != NULL) {
flog("[%3d] %s\n", o, strval);
}
}
}
finished:
Py_INCREF(Py_None);
return Py_None;
}
--- snap ---

flog(fmt, ...) is my function to log to a file, sys_stack I've made available
to Python as sys.stack and PyFrame_New I modified so that it nulls the memory
allocated for the objects stack. Now the following Python code crashes...

--- snip ---
def f(foo,bar,boo,far):
foobar='foobar'
print foobar
sys.stack()

f('foo','bar','boo','far') # CRASH
--- snap ---

....and in my "logfile" I have...

--- snip ---
co_name: f
co_stacksize: 1
localsplus: 136139316
valuestack: 136139336
locals:
end of locals
[ 5] foo
[ 4] bar
[ 3] boo
[ 2] far
[ 1] foobar
[ 0] <built-in function stack>
--- snap ---

Now the following things are not clear to me:
-Why does the VM crash? Did I use the wrong stack boundaries?
-Why are no locales printed?
-Why is the function "stack" not right before or after "foo"
on the stack? When I disassemble the code of f with dis.dis,
it reveals that sys.stack and foo are pushed onto the stack
successively.

Greetings,
Fips
Oct 29 '06 #1
0 888

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: cppaddict | last post by:
I have a method that uses a fairly large object. The choice is between having a local object in the method or a static member object that the method uses. ------CHOICE 1--------- int...
2
by: AZRebelCowgirl73 | last post by:
I have casted the stack so that it will print however now it does print like required however it includes un-needed stuff! (Point2D.Double) (Point 2D.Double) (Point2D.Double) (Point2D.Double)...
18
by: Brett | last post by:
I have an ASP.NET page that displays work orders in a GridView. In that GridView is a checkbox column. When the user clicks a "Print" button, I create a report, using the .NET Framework printing...
8
by: Paulo J. Matos | last post by:
Hi all, I guess this is a recurring issue for someone who doesn't really know the python lib inside out. There must be a simple way to do this. I have a list of objects and I have defined a...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.