C_learnings from trying to fix cpython bug #8452

argc passed to a C main function is a special type of variable.
it seems immutable. and not only that it seems like an implicit pointer.
whenever i use it in say memset(ptr,0,argc) it throws me a runtime error
“Use of uninitialised value of size 4”
on the valgrind

Phew,.. i have been trying to moonlight on fixing a cpython bug to add another readline function to the python readline module.
What a task it has been. i have clearly been mis evaluating my C knowledge. While a majority of the language concepts still seem to hold solid, boy my memory of the syntax has been lost.. and perhaps more sadly, my thinking/design thinking has gone so used to python that i just don’t think about types before i use variables… My brain just conveniently ignores them.. damn it.. that’s a bad addiction in general anand….

Anyway, even without all those problems, it turns since i haven’t used gcc, or worked on the cpython code base, it’s stupid of me to make optimistic time expectations.. that eventually led to frustrations..
Anyway, after what seemed to be 10 days’ amount of work (scattered over 2 months of free time), i have got to the state of getting some of the functions registered with python readline module and being able to call them…. Now time to get on to passing on the variables and functionality checking..

I think it’s a good time to write test cases.. will look into that…

i got the prototype function not found error enough no of times , to look it up finally, and learnt that it is an effect of C standard changes.
in a way to work around/for different C versions(pre-ANSI,post-ANSI,ISO C, etc..), it seems gcc has introduced an option of converting function declarations to the format as prescribed by the given C standard in preprocessor directives. Ofcourse, they don’t try to do it themselves, but allow the C programmer to check of the C-implementation and write a prototype(aka template) of function declarations to convert.

To run the gcc compiler with a strict check use -Wstrict-prototype.

Initial part of this activity was painstaking as i used to either cat and grep or manually search for function defs(based on their names and my guess at what they mean). But sometime after a couple of days, i came across cscope -R. i now just hit Ctrl-Shift-t and run cscope -R in the new terminal… I know there’s vim option but am lazy…:). Anyway, thanks to the authors of cscope program.

And now i run into some function called begin_unwind_frame, with cscope’s help i find it is in a file called prot.c and is supposed to be a function from the unwind protect module for bash. Now what the hell is unwind protect module?? I can’t figure from the source code of prot.c what it does, as it seems to use some magic variable ent to set and unset. weary of having to figure out which import header files,contain this variable, i reach for google and end up reading this instead. Ok that’s interesting but the only way it seems relevant to bash or readline is in ensuring that the file handler for the std. input stream gets set and released. Oh well then i can ignore it as after all my work is for the python readline module functionality and not even the interpreter’s readline functionality… thankfully.. phew..

Quote from the source code for bash/unwind_prot.c
“/* I can’t stand it anymore! Please can’t we just write the
whole Unix system in lisp or something? */”

Haha..

After all these diversions and excursions and more learnings and junk, i finally get the code for query_bindings function from bash builtin bind.def source code into cpython readline module and get it to compile and import the module fine.. (Mind it i had to figure out cpython’s method of registering a new function with python module. )So what happens now, when i call the query_bindings function? does it work yet? can i just post the diff and let cpython regular people chirp in and criticize/clean it up? Ofcourse not, it segfaults..
Meh…

well now to do some real work instead of just reacting to the compiler’s yellings and fixing errors.. i.e: to actually understand what that piece of C code does, it seems to involve me reading more linked C code so i have put it off, but can’t avoid it anymore.

turns out readline.h has some conventions. i need to figure out what exactly that keyseqs variable is i.e: the return type of the rl_invoking_keyseqs function… i have to go down the rabbit-hole of C call stack to find out and i forgot what i saw on the way, but it returns keymaps
and what is keymaps? it is a structure with a char and a function pointer(mapped to a readline function). ok that makes sense, now how do i convert this to a string, that too a python string and print it out. Once i convert it to a python string i can just return it and the python interpreter will take care of printing it, so atleast that part of the work doesn’t need to be done.. time to go poking out bash/builtins/bind.def for how it converts the output to a string and prints it..

Ok it turns out keyseqs is really just a pointer to a pointer to character data. So now the question now is how to convert it into a list of strings in python.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s