sp-mishra / tinypy

Automatically exported from code.google.com/p/tinypy
Other
0 stars 0 forks source link

tp_print() fails to print strings containing embedded NUL chars #18

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. Run the following code:

foo = "abc" + chr(0) + "d"
print(foo)

What is the expected output? What do you see instead?

The expected output (as well as what Python 2.5 does) is "abc d\n" (since
the NUL char has no graphic representation). Instead, tinypy prints "abc\n".

What version of the product are you using? On what operating system?

Confirmed on both Slackware Linux 12.1 and Windows XP SP2 (and really on
any platform since the problem is algorithmic) using the latest tinypy
revision (r53). 

Please provide any additional information below.

The proposed resolution is to use fwrite() instead of printf() to print
strings since the length of the string is known in advance (stored inside
tp_string_ member of tp_obj). Patch included.

Original issue reported on code.google.com by denis.ka...@gmail.com on 18 Jun 2008 at 4:07

Attachments:

GoogleCodeExporter commented 9 years ago
Needs a test case.

Original comment by philhas...@gmail.com on 18 Jun 2008 at 11:15

GoogleCodeExporter commented 9 years ago
I've found that the strip() method of strings had a similar problem (used 
strlen()
instead of foo.string.len) while I was writing the test, so I patched up that 
as well
(test included).

Original comment by denis.ka...@gmail.com on 19 Jun 2008 at 12:40

Attachments:

GoogleCodeExporter commented 9 years ago
You should probably break this into two separate patches .. but don't bother 
this time :)

The strip patch is a bit long and won't work.  If it is passed something other 
than a
string it will have undefined behavior.  You want to use TP_TYPE(TP_STRING); to 
get
your parameter to assert that it is a string.  After that, you can make things 
a bit
more concise.

Original comment by philhas...@gmail.com on 20 Jun 2008 at 2:51

GoogleCodeExporter commented 9 years ago
How's this revised version?

Original comment by denis.ka...@gmail.com on 19 Jul 2008 at 2:28

Attachments:

GoogleCodeExporter commented 9 years ago
I think that looks good, feel free to commit this fix.

Original comment by philhas...@gmail.com on 24 Jul 2008 at 7:49

GoogleCodeExporter commented 9 years ago

Original comment by denis.ka...@gmail.com on 25 Jul 2008 at 8:53