Problem with printing time

9 views
Skip to first unread message

pir...@gmail.com

unread,
Feb 19, 2012, 4:59:55 PM2/19/12
to python-o...@googlegroups.com
Ok, so i have been able to get interruptions processing with my
multiboot-x86 port and get the timer to work with it (next will be
keyboard :-P ). I have done a little program at main.py that just loop
printing the current time and sleeping for a second:

from sys import time, wait
while True:
print "Time:", time()
wait(1000)

Now, every second a line is printed :-) The problem that i have is
that instead printed an integer, i got a lot of lines with "Time:
%li". At my printf function i don't have support for %li flag, but
since there's the function plat_putByte() to send characters to stdio
and the code i put before is obviusly printing from inside the Python
VM, i think i don't need it, do i? I don't want to add support for a
more complete printf function if i don't need it... :-/

--
"Si quieres viajar alrededor del mundo y ser invitado a hablar en un
monton de sitios diferentes, simplemente escribe un sistema operativo
Unix."
– Linus Tordvals, creador del sistema operativo Linux

Dean Hall

unread,
Feb 19, 2012, 11:14:23 PM2/19/12
to python-o...@googlegroups.com
You found a place where my porting layer is not complete and p14p depends on libc. I have used snprintf() in a handful of places in order to achieve string formatting. The following line of code is a simplified python snippet that exhibits the same issue:

print sys.time()

Behind the scenes, python has to convert the integer returned by sys.time() into a String object. In the VM, this conversion is performed by the function int_print() in src/vm/int.c Inside that function, you'll see where I use snprintf() to perform the conversion.

From the grep results below, you can see that snprintf() is used in the VM to:
1) format an int into a string
2) format a float into a string
3) format a heapdump string (only used for manual debugging)
4) format a user-given object into a string
X) Don't care

It should be pretty easy to create a sli_itoa() using a good google hit, for example, here:
http://www.jb.man.ac.uk/~slowe/cpp/itoa.html

There should also be plenty of code examples to create a float-to-char-string function.

However, I do NOT yet have a good idea how to handle situation #4, where the python code passes in the formatting string to the snprintf() function with potentially complicated options such as """print "%3x" % 42000""". I would appreciate help and suggestions on this.

$ pwd
p14p/src/vm

$ hg sum
parent: 813:d081349b5e3c tip
Fixes issue 221
branch: default

$ grep -n snprintf *
2) float.c:67: /* #196: Changed to use snprintf */
2) float.c:68: bytesWritten = snprintf((char *)&tBuffer, 32, "%f", ((pPmFloat_t) pf)->val);
X) heap.c:223: snprintf(filename, 32, "pmheapdump%02d.bin", n++);
1) int.c:150: /* #196: Changed to use snprintf */
1) int.c:152: snprintf((char *)&tBuffer, 12, "%li", (long int)((pPmInt_t)pint)->val);
1) obj.c:480: bytesWritten = snprintf((char *)&tBuffer, sizeof(tBuffer), "%li",
2) obj.c:487: bytesWritten = snprintf((char *)&tBuffer, sizeof(tBuffer), "%f",
X) pmFeatureDependencies.h:37: * REQUIRES stdio.h to have snprintf()
X) pmFeatureDependencies.h:117: * REQUIRES stdio.h to have snprintf()
X) pmFeatureDependencies.h:126: * REQUIRES stdio.h to have snprintf()
4) strobj.c:394: snprintretval = snprintf((char *)fmtdbuf, SIZEOF_FMTDBUF,
4) strobj.c:408: snprintretval = snprintf((char *)fmtdbuf, SIZEOF_FMTDBUF,
4) strobj.c:422: snprintretval = snprintf((char *)fmtdbuf, SIZEOF_FMTDBUF,
4) strobj.c:497: snprintretval = snprintf((char *)fmtdbuf, SIZEOF_FMTDBUF,
4) strobj.c:506: snprintretval = snprintf((char *)fmtdbuf, SIZEOF_FMTDBUF,
4) strobj.c:515: snprintretval = snprintf((char *)fmtdbuf, SIZEOF_FMTDBUF,

> --
> You are subscribed to the "python-on-a-chip" (or p14p for short) Google Group.
> Site: http://groups.google.com/group/python-on-a-chip

pir...@gmail.com

unread,
Feb 20, 2012, 4:44:27 AM2/20/12
to python-o...@googlegroups.com
> You found a place where my porting layer is not complete and p14p depends on libc.

Can i have a Steam achievement? Can i? Please... :-P


> Behind the scenes, python has to convert the integer returned by sys.time() into a String object.  In the VM, this conversion is performed by the function int_print() in src/vm/int.c  Inside that function, you'll see where I use snprintf() to perform the conversion.
>

Ok, i understand: instead of print directly to the screen, first all
is converted to a string and later this is printed. Make sense...

> It should be pretty easy to create a sli_itoa() using a good google hit, for example, here:
> http://www.jb.man.ac.uk/~slowe/cpp/itoa.html
>
> There should also be plenty of code examples to create a float-to-char-string function.
>

Good link! :-D Currently i have support for itoa() from a previous
attempt, i only need to add support for "%li" metachar. In any case,
in the future i was thinking to use NewLib when i have the code more
stable, this is just a quick hack (althought my code is getting pretty
neat... :-) )

> However, I do NOT yet have a good idea how to handle situation #4, where the python code passes in the formatting string to the snprintf() function with potentially complicated options such as """print "%3x" % 42000""".  I would appreciate help and suggestions on this.
>

Plan 9 code has a installable tokens system for print() function, i
don't know if it's what you are looking for... In any case you can
look at my code if you need inspiration :-)

http://code.google.com/r/piranna-p14p/source/browse/src/platform/multiboot-x86/lib/stdio.c?name=piranna_v09

(Yes, i need to unify the formating code... ¬¬)

pir...@gmail.com

unread,
Feb 21, 2012, 9:32:03 AM2/21/12
to python-o...@googlegroups.com, Daniel Cabrera Bernardo
It works! I've completed my printf function so "print sys.timer()"
could be able to show something meaningful for long integers and now i
have a timer using the system interruptions! :-D

Obviusly, the code is at my Google Code account ;-)


2012/2/20 pir...@gmail.com <pir...@gmail.com>:

Pantallazo del 2012-02-21 15:25:36.png

Dean Hall

unread,
Feb 21, 2012, 9:51:33 AM2/21/12
to python-o...@googlegroups.com
Good news! Way to go.

I forgot to mention that the v10 branch eliminated calling snprintf() for integers (I hadn't back ported that to the default branch)

I just ported the mbed platform to v10, so I think it should be easy for you to port as well.

!!Dean

> --
> You are subscribed to the "python-on-a-chip" (or p14p for short) Google Group.
> Site: http://groups.google.com/group/python-on-a-chip

> <Pantallazo del 2012-02-21 15:25:36.png>

pir...@gmail.com

unread,
Feb 21, 2012, 9:55:31 AM2/21/12
to python-o...@googlegroups.com
> Good news!  Way to go.
>
Thanks! :-)

> I forgot to mention that the v10 branch eliminated calling snprintf() for integers (I hadn't back ported that to the default branch)
>
> I just ported the mbed platform to v10, so I think it should be easy for you to port as well.
>

I wanted to keep on v09 until i have something more complete and
usable and later do the upgrade, but since v10 is getting stable, do
you think i should do it now and focus on it?

Reply all
Reply to author
Forward
0 new messages