Re: C API: Getting PyObject by name

2010-10-02 Thread pbienst
For reference to posterity, this is how I got it to work in the end:

  PyObject* module = PyImport_ImportModule("__builtin__");
  PyObject* obj = PyRun_String("1", Py_eval_input,
PyModule_GetDict(module), NULL);
  Py_DECREF(module);
  long d = PyLong_AsLong(obj);
  printf("long:%ld\n", d);
  Py_DECREF(obj);

Can't say I really understand it, though, all rather esoteric...

Peter

On Oct 1, 12:16 pm, pbienst  wrote:
> Here is what I tried, but it prints out '-1':
>
> PyObject* obj = PyRun_String("1", Py_single_input,
> PyEval_GetGlobals(), PyEval_GetLocals());
> long d = PyLong_AsLong(obj);
> printf("long:%ld\n", d);
> Py_DECREF(obj);
>
> Peter
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: C API: Getting PyObject by name

2010-10-01 Thread pbienst
Here is what I tried, but it prints out '-1':

PyObject* obj = PyRun_String("1", Py_single_input,
PyEval_GetGlobals(), PyEval_GetLocals());
long d = PyLong_AsLong(obj);
printf("long:%ld\n", d);
Py_DECREF(obj);

Peter

On Sep 30, 9:24 pm, Thomas Jollans  wrote:
> On Thursday 30 September 2010, it occurred to pbienst to exclaim:
>
> > Hi,
>
> > I'm embedding Python in a C app.
>
> > Say I do the following:
>
> >   PyRun_SimpleString("a = 1")
>
> > Is there then a way to get access to the PyObject corresponding to a,
> > only making use in C of the fact that it's called "a"?
>
> > I've searched through the API docs, but I couldn't really find what I
> > was looking for.
>
> No. Not as such. But you could use PyRun_String, or PyRun_StringFlags. You
> could then access the globals object, but I ask you, why would you want to
> bind the object to a name in the first place? Use the fact that PyRun_String
> returns a reference to the result of an expression!

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


C API: Getting PyObject by name

2010-09-30 Thread pbienst
Hi,

I'm embedding Python in a C app.

Say I do the following:

  PyRun_SimpleString("a = 1")

Is there then a way to get access to the PyObject corresponding to a,
only making use in C of the fact that it's called "a"?

I've searched through the API docs, but I couldn't really find what I
was looking for.

Any help most appreciated!

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


Re: creating tar file and streaming it over HTTP?

2010-01-09 Thread pbienst
OK, thanks to the feedback from everyone I got the PUT from a client
to the WSGI server working.

I'm now trying to go the other way around: use a tar stream in one of
the functions in the WSGI server in order to send files to the client.
Problem is that the WSGI specs expects an iterator as return value for
streaming, whereas TarFile needs to write to a file obj.

Is there any way I can get these two to work together?

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


Re: creating tar file and streaming it over HTTP?

2010-01-06 Thread pbienst
Thanks for the tip! It doesn't change anything, though, so I've
debugged this a little bit further. The problem seems to be that the
receiving end (wsgi server) does not see the end of the data:

socket = environ["wsgi.input"]
while True:
sys.stderr.write("before")
chunk = socket.read(4096)
sys.stderr.write("after")
if not chunk:
sys.stderr.write("done")
break
sys.stderr.write(chunk)

There is data from the tar file being printed, but in the end it hangs
in the 'before' statement, and never gets to 'done'. I tried flushing
the fileobj created by makefile(), but that doesn't seem to help.
There is also no environ["CONTENT_LENGTH"] that I can use to detect
the end of the stream.

I'm probably missing something basic here...
-- 
http://mail.python.org/mailman/listinfo/python-list


creating tar file and streaming it over HTTP?

2010-01-06 Thread pbienst
I would like to bundle up a number of files in a tar file and send it
over a HTTP connection, but I would like to do this without creating
the tar file on disk first.

I know I can get tarfile to output to a stream by doing something like

tar_pipe = tarfile.open(mode="w|", fileobj=my_file_obj)

However, I can't figure out which file object to use to send this over
a HTTP connection.

I tried

conn = httplib.HTTPConnection(hostname, port)
conn.putrequest("PUT", "/client/files")
conn.endheaders()
tar_pipe = tarfile.open(mode="w|", fileobj=conn.sock.makefile())
for filename in filenames:
tar_pipe.add(filename)
tar_pipe.close()
conn.getresponse()

but that does not send any data...

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