Python 2.4.4 vs. 2.3.6

2006-12-27 Thread sndive
My top priority is stability of the interpreter. With that in mind
which version should I get: 2.4.4, 2.3.6
or something else.

I will be using gcc 2.3.2(x86), 3.3(arm) and 3.4.3(arm) to cross
compile it depending on the (embedded) platform.

Thank you.

-- 
http://mail.python.org/mailman/listinfo/python-list


Minor problem with configure (2.4.4)

2006-12-28 Thread sndive
configure expands
  AC_DEFINE(_POSIX_C_SOURCE, 200112L, Define to activate features from
IEEE Stds 1003.1-2001)

to

#define _POSIX_C_SOURCE 200112L

that causes problems because _POSIX_C_SOURCE is defined by system
headers and I get hideous
warnings during compilation.

I tried to fix it by adding AC_UNDEFINE(_POSIX_C_SOURCE);
before AC_DEFINE but autoconf 2.59 won't take it:

configure.in:273: error: possibly undefined macro: AC_UNDEFINE
  If this token and others are legitimate, please use
m4_pattern_allow.
  See the Autoconf documentation.

Ideas?

-- 
http://mail.python.org/mailman/listinfo/python-list


how to print a traceback?

2007-10-15 Thread sndive
i'm struggling to get some proxy code working with liburl[2] and want
to print
some tracebacks in my class ProxyHTTPConnection::request().
do i have to force an exception and print a backtrace in catch or
can i just print the current stack?
print_tb takes an argument and short of forcing an exception
i don;t know where to get the current backtrace object from.

thanks.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python Interview Questions

2007-10-30 Thread sndive

Krypto wrote:
> Hi,
>
> I have used Python for a couple of projects last year and I found it
> extremely useful. I could write two middle size projects in 2-3 months
> (part time). Right now I am a bit rusty and trying to catch up again
> with Python.
>
> I am now appearing for Job Interviews these days and I am wondering if
> anybody of you appeared for a Python Interview. Can you please share
> the questions you were asked. That will be great help to me.
>
> Thanks,

assert() in the c functions extending python ends the interview rather
quickly.
or at least it did for me @IronPort ages ago.
To be honest they have massively multithreaded app
and the last thing they needed was an appliance down because
some programmer was not sure what to do when a certain condition
arises.

-- 
http://mail.python.org/mailman/listinfo/python-list


simple? embedding question

2007-10-30 Thread sndive
suppose i have imported two modules foo and bar with
foo=PyImport_ImportModule("foo") and bar=PyImport_ImportModule("bar")
respectively.

Now suppose I have an artitrary python expression to evaluate.
Do I need to parse that thring and check for foo. and bar. before
jumping the usual
PyModule_GetDict,PyDict_GetItemString,PyObject_CallObject hoop hoop on
the PyObject for
the prefix or there is a better way?

btw PyRun_SimpleString("foo.baz()"); does not work:
 Traceback (most recent call last):
  File "", line 1, in ?
NameError: name 'foo' is not defined

and i potentially need a PyObject* back with the result of the
expression anyway.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: simple? embedding question

2007-10-30 Thread sndive
On Oct 30, 1:26 pm, Farshid Lashkari <[EMAIL PROTECTED]> wrote:
> [EMAIL PROTECTED] wrote:
> > suppose i have imported two modules foo and bar with
> > foo=PyImport_ImportModule("foo") and bar=PyImport_ImportModule("bar")
> > respectively.
>
> > Now suppose I have an artitrary python expression to evaluate.
> > Do I need to parse that thring and check for foo. and bar. before
> > jumping the usual
> > PyModule_GetDict,PyDict_GetItemString,PyObject_CallObject hoop hoop on
> > the PyObject for
> > the prefix or there is a better way?
>
> > btw PyRun_SimpleString("foo.baz()"); does not work:
> >  Traceback (most recent call last):
> >   File "", line 1, in ?
> > NameError: name 'foo' is not defined
>
> > and i potentially need a PyObject* back with the result of the
> > expression anyway.
>
> I believe the problem is that you are not importing the "foo" and "bar"
> modules into the __main__ scope. Try using PyImport_ImportModuleEx,
> which will allow you to specify the global scope to import the module
> into. For example, to import the modules into the __main__ scope you
> could do the following:
>
> PyObject* mainmod = PyImport_AddModule("__main__");
> PyObject* maindict = PyModule_GetDict(mainmod);
>
> foo = PyImport_ImportModuleEx("foo", maindict , maindict , NULL);
> bar = PyImport_ImportModuleEx("bar", maindict , maindict , NULL);
>
> Once the modules are imported into the __main__ scope, you should be
> able to use PyRun_SimpleString() to evaluate expressions.
>
> -Farshid

i switched to PyImport_ImportModuleEx per your suggestion but i still
get

Traceback (most recent call last):
  File "", line 1, in ?
NameError: name '__main__' is not defined

i tried PyRun_SimpleString("__main__.foo.baz()"); :
Traceback (most recent call last):
  File "", line 1, in ?
NameError: name '__main__' is not defined

i do not have if __name__ == '__main__':
statement anywhere in the module i'm importing,
but nevertheless i checked that PyImport_AddModule("__main__");
and subsequent getdict return non null pointers.
does non null dictionary indicate that PyImport_AddModule
succeeded as opposed to creating an empty module object?


-- 
http://mail.python.org/mailman/listinfo/python-list


Re: simple? embedding question

2007-10-30 Thread sndive
On Oct 30, 6:47 pm, Farshid Lashkari <[EMAIL PROTECTED]> wrote:
> [EMAIL PROTECTED] wrote:
> > i switched to PyImport_ImportModuleEx per your suggestion but i still
> > get
>
> > Traceback (most recent call last):
> >   File "", line 1, in ?
> > NameError: name '__main__' is not defined
>
> > i tried PyRun_SimpleString("__main__.foo.baz()"); :
> > Traceback (most recent call last):
> >   File "", line 1, in ?
> > NameError: name '__main__' is not defined
>
> > i do not have if __name__ == '__main__':
> > statement anywhere in the module i'm importing,
> > but nevertheless i checked that PyImport_AddModule("__main__");
> > and subsequent getdict return non null pointers.
> > does non null dictionary indicate that PyImport_AddModule
> > succeeded as opposed to creating an empty module object?
>
> Sorry, I misunderstood the behavior of the PyImport_ImportModuleEx()
> function. You can use the PyImport_AddModule() function as before, but
> you must explicitly add the module to the __main__ module. Here is some
> code:
>
> PyObject *mainmod = PyImport_AddModule("__main__");
> PyObject *foo = PyImport_ImportModule("foo");
> Py_INCREF(foo); //Increment foo module since PyModule_AddObject() steals
> reference
> PyModule_AddObject(mainmod, "foo", foo);
> PyRun_SimpleString("foo.baz()");
>
> -Farshid

it works!
could i cache maindict pointer in a global var or am i condemned to
call
PyObject* mainmod = PyImport_AddModule("__main__");
assert(mainmod);
PyObject* maindict = PyModule_GetDict(mainmod);
assert(maindict);
every time i want to PyRun_String?
i call Py_Finalize only atexit.

i also found boost library docs of some help:
http://service-spi.web.cern.ch/service-spi/external/Boost/1.30.0/rh73_gcc32/libs/python/doc/tutorial/doc/using_the_interpreter.html

btw what kind of object is returned in case i use Py_file_input
in PyRun_String?

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: why did these companies choose Tcl over Python

2007-10-30 Thread sndive
On Oct 30, 3:25 pm, chewie54 <[EMAIL PROTECTED]> wrote:
> Hello,
>
> As an electronics engineer I use some very expensive EDA CAD tool
> programs that are scriptable using Tcl.  I was wondering why these
> companies have choose to use Tcl instead of Python.   Some of these
> are:
>
>Mentor Graphics ModelTech VHDL and Verilog simulator
>Synopsys  Design Compiler and Primetime Static Timing Analyzer
>Actel FPGA tools.
>
> Tcl seems to very popular in my business as the scripting language of
> choice.
>
> I'm in the process of deciding to use Tcl or Python for a CAD tool
> program that I have been working on.Most of the core of the
> program,  the database,   will be done is C as an extension to either
> Tcl or Python,   but I intend to use Tk or wxPthon for the GUI.   I do
> need publishing quality outputs from drawings done on a graphics
> device that are scaled to standard printer paper sizes.
>
> I would prefer to use Python but can't deny how popular Tcl is,  as
> mentioned above,  so my question is why wasn't Python selected by
> these companies as the choice of scripting languages for their
> product?
>
> Are there any obvious advantages like:
>
> performance,
> memory footprint,
> better cross-platform support,
> ease of use,
>
> Thanks in advance for your thoughts about this.

tcl is somewhat simpler to extend but you'll pay the price later on in
the
form or the hideous tcl scripts floating around your system

-- 
http://mail.python.org/mailman/listinfo/python-list


PyDict_*() refcount assumptions

2007-10-31 Thread sndive
i checked this: 
http://svn.python.org/projects/python/trunk/Doc/data/refcounts.dat
and it seems that
PyDict_SetItem incref the value being added and the key
and PyDict_DelItem does not decrement any refcounts
so i have to do so manually for the key and for the data( by calling
PyDict_GetItem first).
Did i decipher the dat file correctly and is there a wrapper for
DelItem that reverses
the refcounts to what they were before the SetItem or do i have have
to do 2 calls for
deletion?

-- 
http://mail.python.org/mailman/listinfo/python-list


PyCheck for a classes defined in python and user data in PyObject_HEAD

2007-11-01 Thread sndive
q#1:
in C I want to check if a given PyObject is a xml.dom.minidom.Node (or
a derivative).
how do i extract a PyTypeObject for such a class?

issue #2
I'm in a situation when i don't really need to extend python with any
classes of my own but
i do have extra luggage for the python data structures such as tuples,
lists, dictionaries, etc
on the c++ side. I see no place in PyObject_HEAD where i can stick a
void* to my extra data.
If i enter a feature request into bugzilla for python do you think it
will be well received?
#ifdefed of course like the prev/next extras.
I'm surprised noone else is using evil twin approach.
The reason i'm asking is because while for dictionaries i can stick
weak pointer back to my c++
object in a key with an obscure name while in lists this would change
the list size and thus
is obviously not as easy to "hide" from the client code.
a placeholder in a pyobject would've been of help here

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: PyCheck for a classes defined in python and user data in PyObject_HEAD

2007-11-01 Thread sndive
On Nov 1, 4:14 pm, [EMAIL PROTECTED] wrote:
> q#1:
> in C I want to check if a given PyObject is a xml.dom.minidom.Node (or
> a derivative).
> how do i extract a PyTypeObject for such a class?

nevermind, i found an instance object that will work for me
as long as i figure out where is the documentation
beyond this:
http://docs.python.org/api/instanceObjects.html
on the data attrib access and extracting PyClassObject from it.

>
> issue #2
> I'm in a situation when i don't really need to extend python with any
> classes of my own but
> i do have extra luggage for the python data structures such as tuples,
> lists, dictionaries, etc
> on the c++ side. I see no place in PyObject_HEAD where i can stick a
> void* to my extra data.
> If i enter a feature request into bugzilla for python do you think it
> will be well received?
> #ifdefed of course like the prev/next extras.
> I'm surprised noone else is using evil twin approach.
> The reason i'm asking is because while for dictionaries i can stick
> weak pointer back to my c++
> object in a key with an obscure name while in lists this would change
> the list size and thus
> is obviously not as easy to "hide" from the client code.
> a placeholder in a pyobject would've been of help here


-- 
http://mail.python.org/mailman/listinfo/python-list


What's the reverse of Py_BuildValue("u#" ?

2007-01-23 Thread sndive
How could I get the pointer to and the length of ucs2 array out of a
PyObject representing
a string? Something that works whether PyObject string is in unicode or
not.

Also could I replace a sequence

if(PyBool_Check(obj)) {
...
}
if(PyString_Check(obj)) { // would this be true for any string
type?
...
}
if(PyFloat_Check(obj)) {
...

with a switch?

Thank you!

-- 
http://mail.python.org/mailman/listinfo/python-list


How do I build python with debug info? (and python module load questions)

2007-01-25 Thread sndive
I want to step into PyImport_ImportModule("my_foo") that's failing
despite the my_foo.py
being in the current directory. (PyRun_SimpleFileEx works on the exact
same file).
How would I run configure to build libpython with -g?

I wonder if I should just keep using PyRun_SimpleFileEx.

I want to be able to call arbitrary functions in the imported
file/module
using PyRun_SimpleString("import my_foo\nmy_foo.bar(whatever)")
after the file is loaded.

There is no '' in sys.path, I assume that should not affect
PyImport_ImportModule
to look in the current directory first. Does it?

Thank you!

-- 
http://mail.python.org/mailman/listinfo/python-list


Calling python function from C and import questions

2007-01-26 Thread sndive
Is there a better way to make a call from C than

PyRun_SimpleString("import
foo_in_python\nfoo_in_python.bar(whatever)\n");

?

I already imported the foo_in_python using PyImport_ImportModule
and wonder why do I need to keep importing it every time I'm
calling a python function in that module.

I stepped thru PyImport_ImportModule and it seems rather expensive.

I assume the module will end up in cache after PyImport_ImportModule
so that subsequent
PyRun_SimpleString("import foo_in_python\n...
is cheaper but I still have to wonder if I what I'm doing is the right
thing or there is a much simpler way of doing this.

Does anyone know why does the import tries to load .py files before
.pyc?
Thank you.

-- 
http://mail.python.org/mailman/listinfo/python-list


instance as a sequence

2007-11-05 Thread sndive
suppose i want to
make foo.childNodes[bar] available as foo[bar]
(while still providing access to the printxml/printprettyxml()
functions
and other functionality of dom/minidom instance).

What is a good way to accomplish that?

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: instance as a sequence

2007-11-05 Thread sndive
On Nov 5, 9:40 am, Paul McGuire <[EMAIL PROTECTED]> wrote:
> On Nov 5, 11:32 am, [EMAIL PROTECTED] wrote:
>
> > suppose i want to
> > make foo.childNodes[bar] available as foo[bar]
> > (while still providing access to the printxml/printprettyxml()
> > functions
> > and other functionality of dom/minidom instance).
>
> > What is a good way to accomplish that?
>
> define __getitem__(self, index) method on foo's class, and have the
> method return foo.childNodes[index].
>
> If you don't have access to foo's class, then just do this:
>
> foo.__getitem__ = delegatingGetItemMethod
>
> and define delegatingGetItemMethod as described above.
>
could i plug it into the (exiting) xml.dom.minidom class rather than
the instance?

-- 
http://mail.python.org/mailman/listinfo/python-list


is PyUnicode_Decode a part of public api

2007-11-05 Thread sndive
am i allowed to use PyUnicode_Decode or PyUnicode_DecodeUTF8 in my
code
or that is not advisable?

QString s("foo");
// QString::unicode returns garbage unusable for PyUnicode_FromUnicode
PyObject *uo =
PyUnicode_Decode(s.utf8(), s.length(), "utf8", 0);

-- 
http://mail.python.org/mailman/listinfo/python-list


supplemental data factory

2007-11-05 Thread sndive
i'm trying to import a module
typeinfo that looks like so:
import xml.dom import minidom

var mapper =
{
minidom.Element.__class__ : "Element",
minidom.Node.__class__ : "Node"
};

then i'd write a factory method that's look roughly like this:
addBaggage(PyObject *victim)
{
PyObject *typeinfo = PyImport_ImportModule("typeInfo");
PyObject *dict = PyObject_GetAttrString(pModule, "mapper");
   PyObject *pclass = PyObject_GetAttrString( victim, "__class__");

PyObject *baggagename = PyDict_GetItem(dict, pclass);

then i'll convert baggagename into a char * and key
into a dictionary mapping char * to factory methods
or painstakingly if PyObject_IsInstance() else PyObject_IsInstance()
the whole thing (i'll have more than two entries in the mapper
and, possibly, beyond the minidom module)

for my classes. Is there a simpler way to accomplish what i'm trying
to do, for example leverage the existing metadata for minidom module?
I'm thinking maybe i can just specify a tuple of modules in
typeinfo instead and iterate on all classes in those modules.
lookups in the dictionary seem quicker though... thoughts?

-- 
http://mail.python.org/mailman/listinfo/python-list


PyObject sanitizer (CPython 2.4.4)

2007-11-06 Thread sndive
I get this from valgrind (no suppression file but thgis probably is
not covered by the suppressor anyway):
==6108== Invalid read of size 4
==6108==at 0x48D19F4: lookdict_string (dictobject.c:359)
==6108==by 0x48D1B59: PyDict_GetItem (dictobject.c:554)
==6108==by 0x48B1657: instance_getattr2 (classobject.c:741)
==6108==by 0x48B701D: instance_getattr1 (classobject.c:725)
==6108==by 0x48B2670: instance_getattr (classobject.c:764)
==6108==by 0x48D6500: PyObject_GetAttr (object.c:1088)
==6108==by 0x48D6355: PyObject_GetAttrString (object.c:1031)

so i wonder if the PyObject i gave to PyObject_GetAttrString to gnaw
on is in good shape.
is assert(obj->ob_refcnt>0); a good way to check if i'm dealing with a
valid PyObject
or there is a helper that can do this for me?

-- 
http://mail.python.org/mailman/listinfo/python-list


private data stashed in local/global execution context of PyEval_EvalCode disappears down the execution stack

2007-11-06 Thread sndive
i naively created execution context:
PyObject *execcontext = PyDict_New();
stuffed a handle in it:
PyObject *ih = PyCObject_FromVoidPtr(handle, NULL);
int st= PyDict_SetItemString(res, "interp", ih);

and later on in a function for a module that i defined
expected to extract that handle:
PyObject *dict = PyEval_GetGlobals(); // or GetLocals
depending on where
// execcontext ended up in PyEval_EvalCode
PyObject *co = PyDict_GetItemString(dict, "interp");
assert(PyCObject_Check(co));
but i get null co either way and the dict does not match execcontext
that is passed into PyEval_EvalCode.

any ideas how to fix this?

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: private data stashed in local/global execution context of PyEval_EvalCode disappears down the execution stack

2007-11-13 Thread sndive
On Nov 9, 5:36 pm, "Gabriel Genellina" <[EMAIL PROTECTED]> wrote:
> En Tue, 06 Nov 2007 23:25:17 -0300, <[EMAIL PROTECTED]> escribió:
>
> > i naively created execution context:
> > PyObject *execcontext = PyDict_New();
> > stuffed a handle in it:
> > PyObject *ih = PyCObject_FromVoidPtr(handle, NULL);
> >int st= PyDict_SetItemString(res, "interp", ih);
>
> What's `res`?

duh!
it should be execcontext:
int st= PyDict_SetItemString(execcontext, "interp", ih);
then i'd
PyEval_EvalCode(somecallable, execcontext, g_maindict);
where g_maindict is a dictionary from __main__ module
...

later as part of PyEval_EvalCode
a method in a module extending python is executed by python runtime
and i want to get "interp" assuming it would be in the global context:
PyObject *dict = PyEval_GetGlobals();
PyObject *co = PyDict_GetItemString(dict, "interp");
but apparently PyEval_GetGlobals(); returns the dictionary
that has nothing to do with the execcontext passed into
PyEval_EvalCode higher up on the call stack
and i do not know how to get to the execcontext dictionary that
i passed in PyEval_EvalCode.
If each module has its own globals what the point of passing
global and local namespace into PyEval_EvalCode?

> One should make a lot of assumptions about your code because it's not
> complete. Please post a minimal complete example showing your problem.
>
It's a rather large program. My assumption was that just posting the
snippet around the call site and the callee pathetic attempt
to extract interp would be sufficient :(

-- 
http://mail.python.org/mailman/listinfo/python-list

Re: private data stashed in local/global execution context of PyEval_EvalCode disappears down the execution stack

2007-11-13 Thread sndive
On Nov 13, 3:31 pm, "Chris Mellon" <[EMAIL PROTECTED]> wrote:
> On Nov 13, 2007 3:18 PM,  <[EMAIL PROTECTED]> wrote:
>
> > On Nov 9, 5:36 pm, "Gabriel Genellina" <[EMAIL PROTECTED]> wrote:
> > > En Tue, 06 Nov 2007 23:25:17 -0300, <[EMAIL PROTECTED]> escribió:
>
> > > One should make a lot of assumptions about your code because it's not
> > > complete. Please post a minimal complete example showing your problem.
>
> > It's a rather large program. My assumption was that just posting the
> > snippet around the call site and the callee pathetic attempt
> > to extract interp would be sufficient :(
>
> The creation of a minimal runnable sample is a fantastic way to find
> any bugs in your code, and has the benefit of (if the bug is not in
> your code) giving other people a simple way to recreate the bug. If I
> were to check this (and I'm not, but I would if you'd posted runnable
> code) I'll have to write the code myself from your textual
> description. Then, if the code works, I'll have to post the code that
> I wrote as well as my negative response, and go through several back
> and forths trying to isolate any differences between what I wrote and
> what you wrote but didn't show. That's way more work than I'm willing
> to do to solve someone else's problem.
>
> In my experience, creating a minimal sample that demonstrates the bug
> will lead you to the actual bug more than half the time. That's a lot
> of time (yours and other peoples) that can be saved if you do it.

working on a smaller example. i could not get pyNode_root invoked yet
and
PyRun_String("import node\nprint node.root()\n",
 Py_file_input,
 exec, g_maindict);
triggers  Py_CompileString
failure with
Traceback (most recent call last):
  File "", line 1, in ?
ImportError: __import__ not found

without RyRun_String call program runs but the pyNode_root is not
invoked

#undef _POSIX_C_SOURCE
#include 

#ifndef PyMODINIT_FUNC  /* declarations for DLL import/export */
#define PyMODINIT_FUNC void
#endif

PyObject *g_mainmod;
PyObject *g_maindict;
bool worked = false;
static
PyObject *
pyNode_root(PyObject *self, PyObject *args)
{
PyObject *dict = PyEval_GetGlobals();
PyObject *co = PyDict_GetItemString(dict, "interp");
assert(PyCObject_Check(co));
void *interp = PyCObject_AsVoidPtr(co);
assert(interp);
// ...
printf("root() worked\n");
worked=true;
return 0;
}

static PyMethodDef module_methods[] = {
/* no need to create pyNode from python programs
{"new", pyNode_new, METH_VARARGS,
PyDoc_STR("new() -> new Node object")},
*/
{"root",pyNode_root,METH_VARARGS,
PyDoc_STR("root('dm') -> wrapper for the rootnode")},

{NULL}  /* Sentinel */
};

int
main()
{
Py_Initialize();

g_mainmod = PyImport_AddModule("__main__");
assert(g_mainmod);
g_maindict = PyModule_GetDict(g_mainmod);
assert(g_maindict);
Py_INCREF(g_maindict); // it was a borrowed reference

PyObject* m = Py_InitModule("node", module_methods);
if (m == NULL)
return 1;

PyObject *exec = PyDict_New();
void *handle = (void*)0xdeadc0ed;
PyObject *ih = PyCObject_FromVoidPtr(handle, NULL);
int st= PyDict_SetItemString(exec, "interp", ih);
assert(!st);


PyRun_String("import node\nprint node.root()\n",
 Py_file_input,
 exec, g_maindict);

PyObject *mScriptHandle=
Py_CompileString("import node\nprint node.root()\n",
 "comp", Py_file_input);
if(!mScriptHandle) {
PyErr_Print();
return 2;
}

PyObject *res = PyEval_EvalCode((PyCodeObject*)mScriptHandle,
exec, g_maindict);

assert(res=Py_None); // compiled as file_input
assert(worked);
}

-- 
http://mail.python.org/mailman/listinfo/python-list

Re: private data stashed in local/global execution context of PyEval_EvalCode disappears down the execution stack

2007-11-13 Thread sndive
On Nov 9, 7:36 pm, "Gabriel Genellina" <[EMAIL PROTECTED]> wrote:
> En Tue, 06 Nov 2007 23:25:17 -0300, <[EMAIL PROTECTED]> escribió:
>
> > i naively created execution context:
> > PyObject *execcontext = PyDict_New();
> > stuffed a handle in it:
> > PyObject *ih = PyCObject_FromVoidPtr(handle, NULL);
> > int st= PyDict_SetItemString(res, "interp", ih);
>
> What's `res`?
> One should make a lot of assumptions about your code because it's not
> complete. Please post a minimal complete example showing your problem.
>
> --
> Gabriel Genellina

for some reason i can't get compilestring/run_string to work in my
example.
compile the code below
and from the command line type

import node
print node.root()

you'd get:
Program received signal SIGSEGV, Segmentation fault.
[Switching to Thread -1218588544 (LWP 13539)]
0x08048e37 in pyNode_root (self=0x0, args=0xb759d02c) at main.cpp:17
17  assert(PyCObject_Check(co));
(gdb) p co
$1 = (PyObject *) 0x0

because interp is not on globals

#undef _POSIX_C_SOURCE
#include 

#ifndef PyMODINIT_FUNC  /* declarations for DLL import/export */
#define PyMODINIT_FUNC void
#endif

PyObject *g_mainmod;
PyObject *g_maindict;
bool worked = false;
static
PyObject *
pyNode_root(PyObject *self, PyObject *args)
{
PyObject *dict = PyEval_GetGlobals();
PyObject *co = PyDict_GetItemString(dict, "interp");
assert(PyCObject_Check(co));
void *interp = PyCObject_AsVoidPtr(co);
assert(interp);
// ...
printf("root() worked\n");
worked=true;
return 0;
}

static PyMethodDef module_methods[] = {
/* no need to create pyNode from python programs
{"new", pyNode_new, METH_VARARGS,
PyDoc_STR("new() -> new Node object")},
*/
{"root",pyNode_root,METH_VARARGS,
PyDoc_STR("root('dm') -> wrapper for the rootnode")},

{NULL}  /* Sentinel */
};

int
main()
{
Py_Initialize();

g_mainmod = PyImport_AddModule("__main__");
assert(g_mainmod);
g_maindict = PyModule_GetDict(g_mainmod);
assert(g_maindict);
Py_INCREF(g_maindict); // it was a borrowed reference

PyObject* m = Py_InitModule("node", module_methods);
if (m == NULL)
return 1;

PyObject *exec = PyDict_New();
void *handle = (void*)0xdeadc0ed;
PyObject *ih = PyCObject_FromVoidPtr(handle, NULL);
//Py_INCREF(ih);
int st= PyDict_SetItemString(exec, "interp", ih);
assert(!st);

PyRun_InteractiveLoop(stdin, "");
}

-- 
http://mail.python.org/mailman/listinfo/python-list

Re: private data stashed in local/global execution context of PyEval_EvalCode disappears down the execution stack

2007-11-13 Thread sndive
On Nov 13, 3:31 pm, "Chris Mellon" <[EMAIL PROTECTED]> wrote:
> On Nov 13, 2007 3:18 PM,  <[EMAIL PROTECTED]> wrote:
>
> > On Nov 9, 5:36 pm, "Gabriel Genellina" <[EMAIL PROTECTED]> wrote:
> > > En Tue, 06 Nov 2007 23:25:17 -0300, <[EMAIL PROTECTED]> escribió:
>
> > > One should make a lot of assumptions about your code because it's not
> > > complete. Please post a minimal complete example showing your problem.
>
> > It's a rather large program. My assumption was that just posting the
> > snippet around the call site and the callee pathetic attempt
> > to extract interp would be sufficient :(
>
> The creation of a minimal runnable sample is a fantastic way to find
> any bugs in your code, and has the benefit of (if the bug is not in
> your code) giving other people a simple way to recreate the bug. If I
> were to check this (and I'm not, but I would if you'd posted runnable
> code) I'll have to write the code myself from your textual
> description. Then, if the code works, I'll have to post the code that
> I wrote as well as my negative response, and go through several back
> and forths trying to isolate any differences between what I wrote and
> what you wrote but didn't show. That's way more work than I'm willing
> to do to solve someone else's problem.
>
> In my experience, creating a minimal sample that demonstrates the bug
> will lead you to the actual bug more than half the time. That's a lot
> of time (yours and other peoples) that can be saved if you do it.


save as testimp.py:
import node

print "import test worked"

def runtest():
print "runtest()!!!"
res = node.root()

the program reproducing the problem:
#undef _POSIX_C_SOURCE
#include 

#ifndef PyMODINIT_FUNC  /* declarations for DLL import/export */
#define PyMODINIT_FUNC void
#endif

PyObject *g_mainmod;
PyObject *g_maindict;
bool worked = false;
static
PyObject *
pyNode_root(PyObject *self, PyObject *args)
{
PyObject *dict = PyEval_GetGlobals();
PyObject *co = PyDict_GetItemString(dict, "interp");
assert(PyCObject_Check(co));
void *interp = PyCObject_AsVoidPtr(co);
assert(interp);
// ...
printf("root() worked\n");
worked=true;
return 0;
}

static PyMethodDef module_methods[] = {
/* no need to create pyNode from python programs
{"new", pyNode_new, METH_VARARGS,
PyDoc_STR("new() -> new Node object")},
*/
{"root",pyNode_root,METH_VARARGS,
PyDoc_STR("root('dm') -> wrapper for the rootnode")},

{NULL}  /* Sentinel */
};

int
main()
{
Py_Initialize();
PyRun_SimpleString("import sys\n"
   "sys.path.append('.')\n"
   "print ': ',sys.path\n"
   );

g_mainmod = PyImport_AddModule("__main__");
assert(g_mainmod);
g_maindict = PyModule_GetDict(g_mainmod);
assert(g_maindict);
Py_INCREF(g_maindict); // it was a borrowed reference

PyObject* m = Py_InitModule("node", module_methods);
if (m == NULL)
return 1;

PyObject *eglobal = PyDict_New();
void *handle = (void*)0xdeadc0ed;
PyObject *ih = PyCObject_FromVoidPtr(handle, NULL);
int st= PyDict_SetItemString(eglobal, "interp", ih);
assert(!st);

PyObject *import = PyImport_ImportModule("testimp");
if(!import) {
PyErr_Print();
return 3;
}
Py_INCREF(import);
PyModule_AddObject(g_mainmod, "testimp", import);

PyObject *mScriptHandle=
Py_CompileString("testimp.runtest()",
 "comp", Py_eval_input);
if(!mScriptHandle) {
if (PyErr_Occurred()) {
PyErr_Print();
}
return 2;
}

PyObject *res = PyEval_EvalCode((PyCodeObject*)mScriptHandle,
eglobal, g_maindict);
if (PyErr_Occurred()) {
PyErr_Print();
}

Py_DECREF(res);
assert(worked);
}

-- 
http://mail.python.org/mailman/listinfo/python-list

Re: private data stashed in local/global execution context of PyEval_EvalCode disappears down the execution stack

2007-11-14 Thread sndive
On Nov 14, 12:57 am, "Gabriel Genellina" <[EMAIL PROTECTED]>
wrote:
> En Tue, 13 Nov 2007 19:59:56 -0300, <[EMAIL PROTECTED]> escribió:
>
> > working on a smaller example. i could not get pyNode_root invoked yet
> > and
> > PyRun_String("import node\nprint node.root()\n",
> >  Py_file_input,
> >  exec, g_maindict);
>
> The globals argument should contain (at least) a key "__builtins__"
> pointing to the __builtin__ module; else, no builtin functions (like
> __import__) will be found. PyEval_Globals (you used it somewhere) returns
> a suitable globals argument.
>
thank you.
result is the same however:
pyt: main.cpp:17: PyObject* pyNode_root(PyObject*, PyObject*):
Assertion `co' failed.

Program received signal SIGABRT, Aborted.
[Switching to Thread -1218617216 (LWP 4807)]
0x00a54eff in raise () from /lib/tls/libc.so.6

with testimp.py:
import node

print "import test worked"

def runtest():
print "runtest()!!!"
res = node.root()

and the code
#undef _POSIX_C_SOURCE
#include 

#ifndef PyMODINIT_FUNC  /* declarations for DLL import/export */
#define PyMODINIT_FUNC void
#endif

PyObject *g_mainmod;
PyObject *g_maindict;
bool worked = false;
static
PyObject *
pyNode_root(PyObject *self, PyObject *args)
{
PyObject *dict = PyEval_GetGlobals();
PyObject *co = PyDict_GetItemString(dict, "interp");
assert(co);
assert(PyCObject_Check(co));
void *interp = PyCObject_AsVoidPtr(co);
assert(interp);
// ...
printf("root() worked\n");
worked=true;
return 0;
}

static PyMethodDef module_methods[] = {
/* no need to create pyNode from python programs
{"new", pyNode_new, METH_VARARGS,
PyDoc_STR("new() -> new Node object")},
*/
{"root",pyNode_root,METH_VARARGS,
PyDoc_STR("root('dm') -> wrapper for the rootnode")},

{NULL}  /* Sentinel */
};

int
main()
{
Py_Initialize();
PyRun_SimpleString("import sys\n"
   "sys.path.append('.')\n"
   "print ': ',sys.path\n"
   );

g_mainmod = PyImport_AddModule("__main__");
assert(g_mainmod);
g_maindict = PyModule_GetDict(g_mainmod);
assert(g_maindict);
Py_INCREF(g_maindict); // it was a borrowed reference

PyObject* m = Py_InitModule("node", module_methods);
if (m == NULL)
return 1;

PyObject *eglobal = PyDict_New();
void *handle = (void*)0xdeadc0ed;
PyObject *ih = PyCObject_FromVoidPtr(handle, NULL);
int st= PyDict_SetItemString(eglobal, "interp", ih);
assert(!st);
PyObject *builtins = PyEval_GetBuiltins();
st= PyDict_SetItemString(eglobal, "__builtins__", builtins);
assert(!st);

PyObject *import = PyImport_ImportModule("testimp");
if(!import) {
PyErr_Print();
return 3;
}
Py_INCREF(import);
PyModule_AddObject(g_mainmod, "testimp", import);

PyObject *mScriptHandle=
Py_CompileString("testimp.runtest()",
 "comp", Py_eval_input);
if(!mScriptHandle) {
if (PyErr_Occurred()) {
PyErr_Print();
}
return 2;
}

PyObject *res = PyEval_EvalCode((PyCodeObject*)mScriptHandle,
eglobal, g_maindict);
if (PyErr_Occurred()) {
PyErr_Print();
}

Py_DECREF(res);
assert(worked);
}


-- 
http://mail.python.org/mailman/listinfo/python-list

problem calling ElementTree._ElementInterface.find with PyObject_CallObject

2007-11-14 Thread sndive
I have a weid problem. If i do this:
import elementtree.ElementTree as ET
...
tree = ET.parse("whatever")
root = tree.getroot()
r = root.find('last')
print r
return root
where last is not an immediate child of root node i get back None.
However if i comment the r = root.find('last')
print r
part out and PyObject_CallObject on root from c++ space
i get

Traceback (most recent call last):
  File "/var/tmp/build/x86-ezx/lib/python2.4/site-packages/elementtree/
ElementTree.py", line 329, in find
return ElementPath.find(self, path)
  File "/var/tmp/build/x86-ezx/lib/python2.4/site-packages/elementtree/
ElementPath.py", line 188, in find
return _compile(path).find(element)
  File "/var/tmp/build/x86-ezx/lib/python2.4/site-packages/elementtree/
ElementPath.py", line 178, in _compile
p = Path(path)
  File "/var/tmp/build/x86-ezx/lib/python2.4/site-packages/elementtree/
ElementPath.py", line 70, in __init__
tokens = xpath_tokenizer(path)
AttributeError: _ElementInterface instance has no attribute 'last'

(the call to PyObject_CallObject works fine if root.find on the
requested attribute was performed first
from python space!!!)

this is with ElementTree 1.2.6 if that matters here at all
-- 
http://mail.python.org/mailman/listinfo/python-list


no __len__ attribute in NodeList or the list base?

2007-11-14 Thread sndive
i extract class object from an instance of NodeList (minicompat.py)
like so
PyObject *pclass = PyObject_GetAttrString( nodelistinsance,
"__class__");
but
 PyObject_GetAttrString(pclass, "__len__")
returns NULL.

i naively added
__len__ = _get_length
to NodeList
but that ends up in endless recursion.

to explain that endless loop i have to assume __len__ is defined
in the NodeList base(list that is)
but i don't see __len__ anywhere in listobject.c!!!

this is in cpython 2.4.4

__len__ is required to be present in NodeList per this:
http://www.python.org/dev/doc/maint24/lib/dom-nodelist-objects.html
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: no __len__ attribute in NodeList or the list base?

2007-11-15 Thread sndive
On Nov 15, 3:09 am, Hrvoje Niksic <[EMAIL PROTECTED]> wrote:
> [EMAIL PROTECTED] writes:
> > i extract class object from an instance of NodeList (minicompat.py)
> > like so
> > PyObject *pclass = PyObject_GetAttrString( nodelistinsance,
> > "__class__");
> > but
> >  PyObject_GetAttrString(pclass, "__len__")
> > returns NULL.
>
> Are you sure about that?

my apologies for the false alarm.
i was calling __len__ on Element rather than NodeList
thank you
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: PyCheck for a classes defined in python and user data in PyObject_HEAD

2007-11-15 Thread sndive
On Nov 1, 11:04 pm, "Gabriel Genellina" <[EMAIL PROTECTED]>
wrote:
> En Thu, 01 Nov 2007 22:13:35 -0300, <[EMAIL PROTECTED]> escribió:
>
> > On Nov 1, 4:14 pm, [EMAIL PROTECTED] wrote:
> >> q#1:
> >> in C I want to check if a given PyObject is a xml.dom.minidom.Node (or
> >> a derivative).
> >> how do i extract a PyTypeObject for such a class?
>
> > nevermind, i found an instance object that will work for me
> > as long as i figure out where is the documentation
> > beyond this:
> >http://docs.python.org/api/instanceObjects.html
> > on the data attrib access and extracting PyClassObject from it.
>
> classic_class = PyObject_GetAttrString( your_classic_instance, "__class__")
>
> >> issue #2
> >> I'm in a situation when i don't really need to extend python with any
> >> classes of my own but
> >> i do have extra luggage for the python data structures such as tuples,
> >> lists, dictionaries, etc
> >> on the c++ side. I see no place in PyObject_HEAD where i can stick a
> >> void* to my extra data.
>
> Assuming you can recompile all the extensions you use, you could insert
> your void* into _PyObject_HEAD_EXTRA and _PyObject_EXTRA_INIT

i added two void pointers to head_extra and 0,0, to the extra_init.
For some reason i get garbage in them from PyEval_EvalCode :-(
(that's after i blew the build dir and rebuilt 2.4.4 from scratch
so i don't think i have a bad build)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: PyCheck for a classes defined in python and user data in PyObject_HEAD

2007-11-16 Thread sndive
On Nov 15, 10:00 pm, "Gabriel Genellina" <[EMAIL PROTECTED]>
wrote:
> En Fri, 16 Nov 2007 00:27:42 -0300, <[EMAIL PROTECTED]> escribió:
>
>
>
> > On Nov 1, 11:04 pm, "Gabriel Genellina" <[EMAIL PROTECTED]>
> > wrote:
> >> En Thu, 01 Nov 2007 22:13:35 -0300, <[EMAIL PROTECTED]> escribió:
> >> >> I'm in a situation when i don't really need to extend python with any
> >> >> classes of my own but
> >> >> i do have extra luggage for the python data structures such as
> >> tuples,
> >> >> lists, dictionaries, etc
> >> >> on the c++ side. I see no place in PyObject_HEAD where i can stick a
> >> >> void* to my extra data.
>
> >> Assuming you can recompile all the extensions you use, you could insert
> >> your void* into _PyObject_HEAD_EXTRA and _PyObject_EXTRA_INIT
>
> > i added two void pointers to head_extra and 0,0, to the extra_init.
> > For some reason i get garbage in them from PyEval_EvalCode :-(
> > (that's after i blew the build dir and rebuilt 2.4.4 from scratch
> > so i don't think i have a bad build)
>
> Can't you use instead the other approaches suggested on this thread? Like
> keeping a weakkey dictionary holding your additional attributes? Seems a
> lot safer to do that way.
>
> I've never changed that headers myself so I can't guarantee it works.
> Perhaps there are some assumptions somewhere in the code about a specific
> layout. But since _PyObject_HEAD_EXTRA is used in the debug build I think
> you should be able to add your own fields, at least in principle...

i finally found that i need to reset my fields in _Py_NewReference(op)
also.
weakref approach won't work for me because i need extra data in lists/
dictionaries/tuples in addition to instances of classes
(all instances of all classes: can't derive and essentially
make my own bloated psl to maintain)

> Remember that this change invalidates ALL binaries for ANY library or
> extension you may be using. An additional check might be to change the

that's not a problem since i build with a prefix other than /usr/local
and the target is an embedded system so i have full control over
the environment (until someone cracks it :-)

i finally got most of the code working after i figured out that
HEAD_INIT is ignored by tuple allocation code, etc
and _Py_NewReference has to be modified.

thank all of you for all the help. you got me unstuck.
-- 
http://mail.python.org/mailman/listinfo/python-list


getElementsByTagName in ElementTree

2007-11-19 Thread sndive
what's the equivalent of minidom's getElementsByTagName in ElementTree?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: sockets: why doesn't my connect() block?

2007-11-19 Thread sndive
On Nov 17, 11:32 pm, 7stud <[EMAIL PROTECTED]> wrote:
> According to "Python in a Nutshell(2nd)", p. 523:
>
> connect:   s.connect((host, port))
> ...
> Blocks until the server accepts or rejects the connection attempt.
>
> However, my client program ends immediately after the call to
> connect()--even though my server program does not call accept():
>
> #server
> import socket
>
> s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
> port = 3200
> s.bind( ('', port) )
> s.listen(5)
>
> import time
> time.sleep(20)
>
> #client
> import socket
>
> s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
> host = 'localhost'
> port = 3200
>
> s.connect( (host, port) )
> print 'done'
>
> If I start my server program and then start my client program, the
> client ends immediately and displays: done.  I expected the client
> program to block indefinitely.


a better question is why you are not using higher level libraries,
such as twisted
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: PyObject_GetAttrString failing when getting a function pointer fr om PyObject* returned by Py_CompileString

2007-11-20 Thread sndive
On Nov 20, 3:27 am, "Borse, Ganesh" <[EMAIL PROTECTED]>
wrote:
> Hi,
>
> My following code is failing with an error of "isSizeSmall not function or 
> callable"
>
> //---
> char szExpr[2048];
> memset(szExpr,'\0',sizeof(szExpr));
> sprintf(szExpr,"def isSizeSmall(size,vol,ADV,prod):\n  if ( (size < 1000) & 
> (vol < (0.001 * ADV)) & (prod==\"Stock\")): return 100\n  else: return 
> 11\n\n\n");
>
> PyObject* pyco = Py_CompileString(szExpr,"", Py_file_input);
>
> // Get a pointer to this function
> char funcname[56];
> memset(funcname,'\0',sizeof(funcname));
> sprintf(funcname,"isSizeSmall");
>
> PyObject* func = PyObject_GetAttrString(pyco,funcname);
> if(!func || !PyCallable_Check(func))
>printf("%s not function or callable\n");
> //---
>
> In above case, Py_CompileString parses & compiles the code successfully.
>
> Can we not use the "PyObject*" returned by Py_CompileString as input to 
> PyObject_GetAttrString?
> What could be wrong?
>
per documentation
compile string returns None when you use Py_file_input

> I want to use this function pointer returned by PyObject_GetAttrString as 
> input to PyObject_CallObject.
> Can I use the "PyObject*" returned by Py_CompileString directly as is in call 
> to PyObject_CallObject?
>
not with Py_file_input
-- 
http://mail.python.org/mailman/listinfo/python-list


twisted client.getPage threading safety

2007-11-21 Thread sndive
I found nothing better to do than to start a new thread from c and run
twisted event loop there.

my question is: how can i safely call client.getPage() from another
thread? could i?
i have compiled python without threading support and am wondering what
the repercussions will be
(i don't want to enable threading for python space)
-- 
http://mail.python.org/mailman/listinfo/python-list


trouble selling twisted

2007-11-26 Thread sndive
i have a lot of trouble selling twisted a a client lib for network
access
(on embedded platform)
the group i'm a member of wants to write some
unmaintainable threaded blocking junk in c--.
does anyone can give me an idea how many kilobytes extra would it take
to have a pyqt linked in and a running qtreactor?
(qtreactor natirally depends on the qt bindings)

is there an exaample of going thru a passworded proxy
using twisted client classes?
i have trouble understanding how to adapt the proxy example on page 58
in the twisted book to my needs.
-- 
http://mail.python.org/mailman/listinfo/python-list


PyImport_ImportModule("foo.bar")

2007-12-06 Thread sndive
I'm trying to import foo.bar in order not to add every single
directory with python files to the search path.
i immediately follow the import with the PyModule_AddObject call to
put the imported module
to __main__ module
but later on  PyEval_EvalCode on Py_CompileString compiled code
"foo.bar.baz()" and dictionary of __main__
used for global and local context does not work (I get NameError).

is there an equivalent of import foo.bar as Bar; in c api
or should i call into submodules differently?
-- 
http://mail.python.org/mailman/listinfo/python-list


xpath and current python xml libraries

2007-12-10 Thread sndive
PyXML seems to be long gone. Is lxml the way to go if i want to have
xpath supported?
-- 
http://mail.python.org/mailman/listinfo/python-list