calling Python script from another Python script (Windows, multiple installation problem)

2014-03-26 Thread Martin Landa
Hi all,

I am trying to fix a bug in the project which I am working for. The program 
starts on Windows via bat file which calls a Python script to set up the 
environment including GUI and allow to launch program-specific tools. Some of 
the tools are written in C, some in Python. These commands are run via 
subprocess.Popen class. The Windows installer comes with its own bundled Python 
which is usually different from possible system-wide Python. This is source of 
the problems, Python scripts run via subprocess.Popen are launched via 
system-wide Python version, not desired bundled Python. Except hacks like 
`Popen(sys.executable, ...)` I am looking for a better solution. I checked 
virtualenv [1] or Pylauncher [2] but without success, speaking about Python2 
not Python3 which comes from version 3.3 with intergrated pylauncher...

Thanks in advance for any ideas, suggestions or pointers! Martin

[1] http://www.virtualenv.org/en/latest/
[2] https://bitbucket.org/vinay.sajip/pylauncher
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: calling Python script from another Python script (Windows, multiple installation problem)

2014-03-26 Thread Martin Landa
Hi, 

> it should be possible to specify the path of the desired python
> 
> interpreter along with the executed script as an argument to
> 
> Popen(...). This should make the selection of the used python
> 
> explicit.
> 
> Or are there any other disadvantages of the current approach, which
> 
> you are solving in parallel?

not really, I am just searching for a better solution based on virtualenv or 
something similar...

Thanks, Martin
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: calling Python script from another Python script (Windows, multiple installation problem)

2014-03-26 Thread Martin Landa
Dne středa, 26. března 2014 13:29:47 UTC+1 Martin Landa napsal(a):

> not really, I am just searching for a better solution based on virtualenv or 
> something similar...

particularly I am using something like

if sys.platform == "win32":
# get full path including file extension for scripts
fcmd = get_real_command(args[0]) 
if fcmd.endswith('.py'):
args[0] = fcmd
args.insert(0, sys.executable)

where 'args' in subprocess.Popen's argument. I just wonder if there is a better 
or preferable solution over this kind of a hack. Martin

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


Re: calling Python script from another Python script (Windows, multiple installation problem)

2014-03-26 Thread Martin Landa
Dne středa, 26. března 2014 13:54:02 UTC+1 Chris Angelico napsal(a):
> On Wed, Mar 26, 2014 at 11:49 PM, Martin Landa  wrote:
> 
> > # get full path including file extension for scripts
> 
> > fcmd = get_real_command(args[0])

this function returns a full path including file extension for scripts. If 
'args[0]' is a binary file in the path, it returns 'args[0]'. If 'args[0]' is 
detected as a script (it finds in the search path file with such name and given 
extension, eg. `py`) it returns full path to the file, /path/to/file/args[0].py.

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


ctypes: catch system exit from C library

2011-11-03 Thread Martin Landa
Hi all,

in my python application I am calling functions from a C library via
`ctypes` interface. Some fns from that C library calls `exit()` on
error. It causes that the python application crashes even without any
notification. Is it possible to catch library system exit calls from
such python application?

Thanks in advance, Martin
-- 
http://mail.python.org/mailman/listinfo/python-list


string conversion latin2 to ascii

2007-11-27 Thread Martin Landa
Hi all,

sorry for a newbie question. I have unicode string (or better say
latin2 encoding) containing non-ascii characters, e.g.

s = "Ukázka_možnosti_využití_programu_OpenJUMP_v_SOA"

I would like to convert this string to plain ascii (using some lookup
table for latin2)

to get

-> Ukazka_moznosti_vyuziti_programu_OpenJUMP_v_SOA

Thanks for any hits! Regards, Martin Landa
-- 
http://mail.python.org/mailman/listinfo/python-list


python extension dynamic linking

2008-09-01 Thread Martin Landa
Hi,

I am writing Python extension in C++, in this extension I am using
methods from another Python extension. On Linux I am currently linking
my extension with used Python extension -- what is quite ugly.

gcc ... -lgdi

where gdi is a link to C++ extension imported by 'module'

I would like to avoid direct linking here.

import module # export some symbol used in my_module

import my_module

I realized that on Mac it is possible to dynamically link C++
extension using

-bundle -undefined dynamic_lookup

I wonder how to solve this problem on Linux or MS Window.

Thanks for any pointers... Martin
--
http://mail.python.org/mailman/listinfo/python-list


ctypes: pointer to method

2010-08-05 Thread Martin Landa
Hi,

is it possible to pass pointer to a method using ctypes. Sample code:

...
G_set_error_routine(byref(self._print_error))
...

def _print_error(self, msg, type):
"""!Redirect stderr"""
self.log.write(msg)

gives me

G_set_error_routine(byref(self._print_error))
TypeError: byref() argument must be a ctypes instance, not
'instancemethod'


C function G_set_error_routine is defined as

void G_set_error_routine(int (*error_routine) (const char *, int))

Thanks in advance for any pointers. Martin
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: ctypes: pointer to method

2010-08-06 Thread Martin Landa
Hi,

On Aug 5, 9:32 pm, Nobody  wrote:
> I don't know about methods, but it works for functions.
>
> > Sample code:
>
> >     ...
> >     G_set_error_routine(byref(self._print_error))
>
> This won't work; you have to be more explicit, e.g.:
>
>         errtype = CFUNCTYPE(c_int, POINTER(c_char), POINTER(c_int))
>         errfunc = errtype(print_error)
>         G_set_error_routine(errfunc)

the C function is defined as

G_set_error_routine = _libs['grass_gis.
7.0.svn'].G_set_error_routine
G_set_error_routine.restype = None
G_set_error_routine.argtypes = [CFUNCTYPE(UNCHECKED(c_int),
String, c_int)]

I defined in Python function print_error()

def print_error(self, msg, type):
print msg, type

and

errtype = CFUNCTYPE(UNCHECKED(c_int), String, c_int)
errfunc = errtype(print_error)
G_set_error_routine(errfunc)

unfortunately the application crashes when print_error() is called
from C library

static void vfprint_error(int type, const char *template, va_list ap)
{
char buffer[2000];  /* G_asprintf does not work */

vsprintf(buffer, template, ap);

G_debug(5, "print_error(): msg = \"%s\" type = %d", buffer, type);
print_error(buffer, type);
}

Any idea how to solve it. Thanks, Martin

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


Re: ctypes: pointer to method

2010-08-06 Thread Martin Landa
Hi,

On Aug 6, 10:10 pm, Martin Landa  wrote:

> Any idea how to solve it. Thanks, Martin

I overlooked note

"""
Make sure you keep references to CFUNCTYPE objects as long as they are
used from C code. ctypes doesn’t, and if you don’t, they may be
garbage collected, crashing your program when a callback is made.
"""

If I defined errtype and errfunc as global variable everything works.
Thanks again for your help. Martin
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: ctypes: pointer to method

2010-08-07 Thread Martin Landa
Hi,

On Aug 5, 9:32 pm, Nobody  wrote:
>         errtype = CFUNCTYPE(c_int, POINTER(c_char), POINTER(c_int))
>         errfunc = errtype(print_error)
>         G_set_error_routine(errfunc)

the problem occurs when restype is not None, but c_int. E.g.

if hasattr(_libs['grass_gis'], 'G_set_error_routine'):
G_set_error_routine = _libs['grass_gis'].G_set_error_routine
G_set_error_routine.restype = c_int
G_set_error_routine.argtypes = [CFUNCTYPE(UNCHECKED(c_int),
String, c_int)]

errtype = CFUNCTYPE(UNCHECKED(c_int), String, c_int)
errfunc = errtype(print_error)

or

errtype = CFUNCTYPE(c_int, String, c_int)
errfunc = errtype(print_error)

ends up with error

G_set_error_routine(errfunc)
TypeError: in method 'G_set_error_routine', argument 1 of type 'int (*)
(char const *,int)'

The first argument of CFUNCTYPE defines result type (restype), AFAIU
that should work.

Thanks in advance again, Martin
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: ctypes: pointer to method

2010-08-08 Thread Martin Landa
On Aug 7, 12:46 pm, Martin Landa  wrote:

> the problem occurs when restype is not None, but c_int. E.g.

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


send command to parent shell

2010-10-13 Thread Martin Landa
Hi,

is there a way how to send command from python script to the shell
(known id) from which the python script has been called? More
precisely, the goal is to exit running bash (on Linux) or cmd (on
Windows) directly from wxPython application, currently user needs to
quit wxPython application and then underlaying command prompt by
'exit' command.

Thanks in advance, Martin
-- 
http://mail.python.org/mailman/listinfo/python-list