Extension modules and common routines in Python/C API

2010-06-27 Thread ty ty
Hi, folks! 

I'm writing wrapper for C library. This library consist of several parts. And i 
want split my extension package into different extension modules. I think, this 
is the right way ;-) But, there are some common parts that exist in extension 
package, get_library_version, Error, and so on. I've decided to create 
additional module, named core, where these routines and variables are defined. 
And when get_library_version, for example, is used by programmer, Error, in 
opposite, is used by routines in another modules. As i mentioned in ( above 
mail), i use create_error for adding new exception class with neccessary 
fields. Now i call create_error in initcore function, which initialize core 
module. But, if i don't import package.core, and only import package.module, 
when package.module.function fails, python's runtime throws error:
SystemError: error return without exception set
because, Error is not properly initialize in that point. 

And, if i add:

if(!Error) 
  create_error(); 
in init functions of all my modules, create_error runs several times (for each 
imported module). I think, i get a different Error every time. I can't check 
this because of above issue.

So, my questions is: how can i organize this pattern? Should i add import 
package.core in __init__.py in package dir? Or should i create subclass 
exception of Error in every module?
What is the right and pythonic way for doing that? :) 

Thanks!

(crosspost from 
http://stackoverflow.com/questions/3119026/extension-modules-and-common-routines-in-python-c-api
 )
-- 
http://mail.python.org/mailman/listinfo/python-list


Creating exception class with custom fields in Python/C API

2010-06-28 Thread ty ty
Hello, list!

I'm writing a wrapper for C-library. When something goes wrong in that library, 
i can get error details. And i want to assign them to fields of my own 
exception class.

For this purpose, i looked throught Modules/_ctypes/_ctypes.c (in python source 
tree) and implemented same things. Briefly:

 define PyObject * Error in header file, 
 write init and other necessary functions 
 assign them to PyMethodDef array
 initialize class's dict with methods above (in function create_error)
 create new exception with no base class and with dict (in function 
create_error)
 call to create_error and assign Error class to module (PyModule_AddObject)
 throw error with PyErr_SetObject(Error, tpl);, where tpl is tuple with error 
details, which are assigned to Error's fields in init function
All of this is consistent with what i saw in Modules/_ctypes/_ctypes.c, i 
think. But it doesn't work: when i call PyErr_SetObject(Error, tpl);, Python's 
runtime raises error:
TypeError: unbound method __init__() must be called with Error instance as 
first argument (got str instance instead)

And PyObject_Print(Error, stdout, 0); returns:
{'__init__':  ...}

It's item for init function in PyMethodDef array:
{"__init__", myerror_init, METH_VARARGS, "initialize error"}

and it's function's signature:
static PyObject * myerror_init(PyObject * self, PyObject *args)

(python version -- 2.6.4)

Why methods are unbound? And what i've missed? Or what is the right and 
pythonic way to define exception with custom class attributes?

Thanks.

(crosspost from stackoverflow: 
http://stackoverflow.com/questions/3118617/creating-exception-class-with-custom-fields-in-python-c-api
 )
-- 
http://mail.python.org/mailman/listinfo/python-list


Simple (?) question about print statement

2005-12-14 Thread TY
Hi all,

I have this little simple script:

for i in range(10):
for j in range(500): pass # Timing-delay loop
print i

When you run it, it behaves as you would expect -- it prints 0 
on the next line prints 1  on the next line prints 2 
etc.

But if you add a comma at the end of print statement on the last line
like this:

for i in range(10):
for j in range(500): pass # Timing-delay loop
print i,

Now it does this:

 then prints 0 1 2 3 4 5 6 7 8 9 all at once.

Why?

How can I make it to print each numbers on the same line with pauses in
between them?

Thank you for any insight!

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


Re: Simple (?) question about print statement

2005-12-14 Thread TY
So I guess then my next question is why does adding comma to print
statement cause buffering, but not when you don't have comma?

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


Howto or Tutorial for tokenize module for a newbie?

2006-07-10 Thread TY
Hi,

Can someone point me to a Howto or Tutorial for tokenize module for a
newbie?  I read the documentation but it doesn't have a lot of info...
 Thanks!

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


How can I save command prompt screen

2008-07-15 Thread Ty hensons
how can i save my command prompt screen?


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