Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
strange string
#1
i've got some code in a big function near the end:
    ...
    doc_str = getattr(function,'__doc__',None)
    print(len(doc_str),'doc_str =',repr(doc_str),flush=1)
    print(len(doc_str),'doc_str =',repr(doc_str[:150]),flush=1)
    return doc_str
the output from the 2nd print() call is:
Output:
0 doc_str = ''
in previous versions of the code it was getting data in doc_str so i put the 1st print() call back in. originally the change was adding len(doc_str), and [:150]. now with 2 print calls, the 1st prints out the quoted (by repr()) doc string i expect, but the len(doc_str) comes out 0. so what is happening is that after the getattr() call gets the doc string and assigns it to doc_str and the 1st print() call outputs 0 doc_str = ' followed by over 100 lines of documentation (looks like the correct documentation) then the ending quote from repr(). at this point it is curious how the len() can be 0 with lots of characters there. then the 2nd print() call with the sliced string (originally i added the slicing so as not to have 100+ lines) outputs:
Output:
0 doc_str = ''
what can cause this weird effect? it's losing data and len() is just wrong. the caller is getting a null string, too. it should be getting that documentation i see from the 1st print() call (less the quotes that repr() is adding in the print() call).

i added code r = repr(doc_str) to assign the repr() result to r then printed the length of r and got 3079. so repr() got 3077 characters and len() got 0. could this string be internally corrupt? it originally comes from the botocore library which has been working OK for me. this is the first time i am extracting doc strings from its client methods. i don't know if there is any C/binary code in botocore to mess up these stings.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Messages In This Thread
strange string - by Skaperen - Apr-19-2019, 08:22 PM
RE: strange string - by Gribouillis - Apr-20-2019, 07:56 AM
RE: strange string - by Skaperen - Apr-21-2019, 12:57 AM
RE: strange string - by Gribouillis - Apr-21-2019, 04:36 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Strange Characters in JSON returned string fioranosnake 4 5,257 Dec-02-2019, 07:25 PM
Last Post: fioranosnake
  Strange behaviour while splitting string? naknak12 2 2,606 Feb-18-2019, 01:57 PM
Last Post: naknak12

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020