curses & disable readline & replace stdin
Hi ! I'd like to init curses and still have working Python interactive command line. I found that you can replace stdin/stdout/stderr like this: #!/usr/bin/python -i import curses import sys import atexit # this doesn't help, anyway #del sys.modules['readline'] # global variable (curses window) stdscr = None class FakeStdIO(object): def write(self, str): global stdscr stdscr.addstr(str) stdscr.refresh() def readline(self): print "$ ", return stdscr.getstr() stdscr = curses.initscr() atexit.register(curses.endwin) curses.echo() f = FakeStdIO() sys.stdin = f sys.stdout = f sys.stderr = f print 'Hello, curses !'# <-- this works l = sys.stdin.readline() # <-- this also works fine # <-- interactive Python prompt doesn't echo imput It looks that '>>>' prompt is using the readline module, which is not using sys.stdin at all. Q: Can you prevent python interpreter to use readline module (even when it is available), *without* recompiling python ? I think it should be possible to write a module in C to access Python internals. But can you do it "from python" ? The other option is to make readline somehow work with curses. This is however not what I intended. Platform: Linux Mandrake, Python 2.3.5 Thanks, BranoZ -- http://mail.python.org/mailman/listinfo/python-list
Re: looking for way to include many times some .py code from another python code
On Tuesday 08 March 2005 12:41, Martin MOKREJŠ wrote: > cat somefile.py > a = 222 > b = 111 > c = 9 > > cat anotherfile.py > > def a(): > include somefile > postprocess(a) What about : def a(): exec open('somefile.py') postprocess(a) You can even use the optional dictionary parameter: class klass(object): pass a = klass() exec open('somefile.py') in a.__dict__ print a.a, a.b, a.c 222 111 9 Cheers, BranoZ -- http://mail.python.org/mailman/listinfo/python-list
Embeding Py: failed to instantiate a class
Hi ! I need to import a module and create an instance of a class from that module (in C). import mod o = mod.klass() (mod.klass is a subclass of tuple) When I receive a class object from the module.. module = PyImport_ImportModule("mod") cls = PyObject_GetAttrString(module, "klass") ..it fails the PyClass_Check(cls) check. Some classes pass the check, like: class test1: pass class test3(test1): pass but most don't. It is enough to subclass a buildin type. E.g. this class fails the check: class test2(object): pass Q: Do I need to initialize the class object before using ? How ? I have found similar questions in this group, but no "do-it-this-way" answer. It looks that new-style classes have a different C API semantic than old ones. To rephrase my question: Q: Can you give an example code of creating an instance of new-style class ? Thanks, BranoZ PS: Below are some example code used to test it. --- mod.py --- class test1: pass class test2(object): pass class test3(test1): pass --- main.c --- #include static PyObject *module = NULL; void check(char *name) { PyObject *cls = NULL; printf("%s - ", name); if ((cls=PyObject_GetAttrString(module, name)) == NULL) { PyErr_Print(); return; } PyObject_Print(cls, stdout, 0); if (!PyClass_Check(cls)) printf(" - not a class!!\n"); else printf(" - is a class.\n"); } int main(int argc, char *argv[]) { Py_Initialize(); if ((module=PyImport_ImportModule("mod")) == NULL) { PyErr_Print(); return -1; } check("test1"); check("test2"); check("test3"); Py_Main(argc, argv); Py_Finalize(); } --- output --- $ ./main test1 - - is a class. test2 - - not a class!! test3 - - is a class. Python 2.3 (#2, Feb 9 2005, 13:41:20) [GCC 3.3.1 (Mandrake Linux 9.2 3.3.1-2mdk)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import mod >>> mod.test1 >>> mod.test2 >>> mod.test1() >>> mod.test2() >>> -- http://mail.python.org/mailman/listinfo/python-list
Embeding Py: failed to instantiate a class
Hi ! I need to import a module and create an instance of a class from that module (in C). import mod o = mod.klass() (mod.klass is a subclass of tuple) When I receive a class object from the module.. module = PyImport_ImportModule("mod") cls = PyObject_GetAttrString(module, "klass") ..it fails the PyClass_Check(cls) check. Some classes pass the check, like: class test1: pass class test3(test1): pass but most don't. It is enough to subclass a buildin type. E.g. this class fails the check: class test2(object): pass Q: Do I need to initialize the class object in some way ? How ? Thanks, BranoZ PS: Below are some example code used to test it. --- mod.py --- class test1: pass class test2(object): pass class test3(test1): pass --- main.c --- #include static PyObject *module = NULL; void check(char *name) { PyObject *cls = NULL; printf("%s - ", name); if ((cls=PyObject_GetAttrString(module, name)) == NULL) { PyErr_Print(); return; } PyObject_Print(cls, stdout, 0); if (!PyClass_Check(cls)) printf(" - not a class!!\n"); else printf(" - is a class.\n"); } int main(int argc, char *argv[]) { Py_Initialize(); if ((module=PyImport_ImportModule("mod")) == NULL) { PyErr_Print(); return -1; } check("test1"); check("test2"); check("test3"); Py_Main(argc, argv); Py_Finalize(); } --- output --- $ ./main test1 - - is a class. test2 - - not a class!! test3 - - is a class. Python 2.3 (#2, Feb 9 2005, 13:41:20) [GCC 3.3.1 (Mandrake Linux 9.2 3.3.1-2mdk)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import mod >>> mod.test1 >>> mod.test2 >>> mod.test1() >>> mod.test2() >>> -- http://mail.python.org/mailman/listinfo/python-list
execfile() on file subclass or string
Hi ! I have a python script represented by a string. I need to execute it in a context. 'exec' does the job, but doesn't display the filename in tracebacks. 'execfile' is displaying the filename but it can only exec a script in a filesystem. I have tried: - to give exec a filename, like: exec script_content in dict({'__file__':'bla.py'}) => didn't work (neighter with __name__) - create an in-memory file subclassing 'file' class memfile(file): .. def read(self).. => also pointless as 'execfile' doesn't accept a file only a filename Can you help me ? The only problem I need to resolve is that I don't see the script name in tracebacks. Thanks, BranoZ PS: The script didn't came from the filesystem, but I know it's content and a name. -- http://mail.python.org/mailman/listinfo/python-list
Re: embedded python example: PyString_FromString doesnt work?
Hi David ! I cannot see anything wrong on your code. So, I'm posting my working example. Hint: try to determine, why it is returning NULL (the PyErr_Print() call) BranoZ #include int main(int argc, char *argv[]) { PyObject *s; int ret; if (argc < 2) return -1; Py_Initialize(); s = PyString_FromString(argv[1]); if (s == NULL) { PyErr_Print(); return -1; } ret = PyObject_Print(s, stdout, 0); Py_XDECREF(s); if (ret < 0) { PyErr_Print(); return -1; } return 0; } $ cc test_String.c -o test_String -I /usr/include/python2.3 -lpython2.3 $ ./test_String $ ./test_String hello 'hello' -- http://mail.python.org/mailman/listinfo/python-list
Re: exec src in {}, {} strangeness
As Greg pointed.. g = {} exec open('t.py').read() in g, g is what you want. But you can write it also this way: exec open('t.py').read() in {} because if you specify only globals, the same dictionary is also used for locals. (locals() is used as a default only if you don't specify globals) OR explicitly move class Foo to globals t.py contains: globals Foo class Foo: pass class Bar: f = Foo (Should work. I haven't tried it, though) BranoZ -- http://mail.python.org/mailman/listinfo/python-list
Re: execfile() on file subclass or string
> exec compile("code", "filename", "exec") Thanks for the tip! Works great! BranoZ -- http://mail.python.org/mailman/listinfo/python-list
Re: embedded python example: PyString_FromString doesnt work?
> wierd. does: http://www.python.org/doc/2.3.2/ext/pure-embedding.html work > for you ? Yes. It does. > ./test_String script1.py multiply 4 5 Don't run it with the ".py" suffix. The argv[1] is a module name, not a filename.. Even if you do, it may not find the module. Depending of what you have in PYTHONPATH. Try also: export PYTHONPATH=. > I believe you have to call Py_SetProgramName before Py_Initialize, Yes. This is another way to affect the search path. BranoZ -- http://mail.python.org/mailman/listinfo/python-list
ignoring keywords on func. call
Hi ! If I define 'f' like this def f(a): print a then, the call with keywords f(1, optional=2) fails. I have to change 'f' to def f(a, **kwrds): print a to ignore optional parameters. BUT.. Q: Can you call 'f' with keywords that will be ignored, without changing 'f's definition ? I would like to avoid code like this: k = {} k['optional'] = 2 try: f(1, **k) except TypeError: f(1) Also, the problem is that I don't know if the TypeError was caused by calling 'f' with keywords or somewhere "inside" f. You can also say that I need to specify optional parameters on caller side (not called side). BranoZ -- http://mail.python.org/mailman/listinfo/python-list
Re: ignoring keywords on func. call
> > Q: Can you call 'f' with keywords that will be > > ignored, without changing 'f's definition ? > > no. OK. Thank you for an answer. > what's the use case? I have extended the dict class, so that my __getitem__ accepts an optional parameter class MyTree(dict): def __getitem__(self, key, **k): .. def lookup(self, seq): node = self for k in seq: node = node.__getitem__(k, someoption=True) lookup takes a sequence of keys to lookup in hierarchy of Tree nodes. But I would like lookup to work also on objects that are not subclasses of Tree. It has to work on any dict-like class. If a user defines a custom class, he will intuitively define __getitem__ like this: def MyNode(??): def __getitem__(self, key): .. I could check in 'lookup' if isinstance(node, Tree): node = node.__getitem__(k, someoption=True) else: node = node.__getitem__(k) But it still doesn't guarantee that __getitem__ accepts keywords. (What if somebody will extend the Tree class and overlook the definition of __getitem__ and define a "classic" one) BranoZ -- http://mail.python.org/mailman/listinfo/python-list