|
|
Subject: Re: [Python.NET] RunString does not work - msg#00009
List: python.dotnet
thanks for your reply. I had the necessary imports in place but was still seeing the error.
The following code from Michael Eddington resolved the issue (I think the current problem is because of the dictionary being used)
public static PyObject RunString(string code) { PyObject module = ImportModule("__main__"); PyDict globals = new PyDict(Runtime.PyModule_GetDict(module.Handle
)); if (locals == null) { locals = new PyDict(Runtime.PyDict_New()); } Runtime.Incref(globals.Handle); Runtime.Incref
(locals.Handle); IntPtr result = Runtime.PyRun_String(code, (IntPtr)257, globals.Handle, locals.Handle); if (result == IntPtr.Zero) { return null; } return new PyObject(result);
}
rgd
Sesh
On 3/5/07, Brian Lloyd < brian.lloyd@xxxxxxxxxxxxxx> wrote:
FYI, if you are getting a NameError using RunString that usually means that the string you run needs to do some imports — remember that RunString creates (essentially) a
totally new global local namespace, so you'll need to import any names you need in your code snippet. e.g. CLR.Food.SpamAndEggs.cook() ...will fail but import CLR import
CLR.Food CLR.Food.SpamAndEggs.cook() ...should work hope this helps, -Brian
On 3/4/07 4:37 AM, "Seshagiri Cherukuri" <
seshagiri.cherukuri-Re5JQEeQqe8AvxtiuMwx3w@xxxxxxxxxxxxxxxx> wrote:
hi All, I have embedded pythonnet into my C# application and this works great as far as creating .net objects from python, executing methods etc. Here I am using the RunSimpleString (the PyRun_SimpleString) wrapper to send commands from the C# application to the pythonnet.
However I am facing the following problems: 1. After RunSimpleString, PyErr_Occurred or PyErr_Fetch are not able to detect any exceptions even if they did occur - for example, consider the code - curErr =
PythonEngine.RunSimpleString("10/0\n" ); - this code does return a -1 value indicating error. - however after this none of PyErr_Occurred or PyErr_Fetch do not detect this error
- from python documentation it looks like there is no way to obtain the exception information 2. using RunString - because of above problem I want to send a multi-line code including a try-except: block
- I have tried using RunString with all the three modes - Py_eval_input, Py_single_input and Py_file_input - The RunString works only if the code does not have any .net objects or does not include any .net exceptions
- If the code sent to RunString includes a .net object e.g. obj, then RunString always returns in NameError, obj is not defined 3. Strangely, if I run the console application, it is able to work with .net exceptions - both .net and user defined exceptions
4. Is this because of the new dictionary created while in RunString? thanks Sesh Cherukuri
_________________________________________________ Python.NET mailing list -
PythonDotNet-+ZN9ApsXKcEdnm+yROfE0A@xxxxxxxxxxxxxxxxhttp://mail.python.org/mailman/listinfo/pythondotnet
-- Seshagiri Rao Cherukuri
_________________________________________________
Python.NET mailing list - PythonDotNet-+ZN9ApsXKcEdnm+yROfE0A@xxxxxxxxxxxxxxxx
http://mail.python.org/mailman/listinfo/pythondotnet
Was this page helpful?
Thread at a glance:
Previous Message by Date:
click to view message preview
Re: [Python.NET] RunString does not work
FYI, if you are getting a NameError using RunString that usually means that the string
you run needs to do some imports — remember that RunString creates (essentially) a
totally new global local namespace, so you’ll need to import any names you need in
your code snippet.
e.g.
CLR.Food.SpamAndEggs.cook()
...will fail
but
import CLR
import CLR.Food
CLR.Food.SpamAndEggs.cook()
...should work
hope this helps,
-Brian
On 3/4/07 4:37 AM, "Seshagiri Cherukuri" <seshagiri.cherukuri@xxxxxxxxx> wrote:
hi All,
I have embedded pythonnet into my C# application and this works great as far as creating .net objects from python, executing methods etc.
Here I am using the RunSimpleString (the PyRun_SimpleString) wrapper to send commands from the C# application to the pythonnet.
However I am facing the following problems:
1. After RunSimpleString, PyErr_Occurred or PyErr_Fetch are not able to detect any exceptions even if they did occur
- for example, consider the code - curErr = PythonEngine.RunSimpleString("10/0\n" );
- this code does return a -1 value indicating error.
- however after this none of PyErr_Occurred or PyErr_Fetch do not detect this error
- from python documentation it looks like there is no way to obtain the exception information
2. using RunString
- because of above problem I want to send a multi-line code including a try-except: block
- I have tried using RunString with all the three modes - Py_eval_input, Py_single_input and Py_file_input
- The RunString works only if the code does not have any .net objects or does not include any .net exceptions
- If the code sent to RunString includes a .net object e.g. obj, then RunString always returns in NameError, obj is not defined
3. Strangely, if I run the console application, it is able to work with .net exceptions - both .net and user defined exceptions
4. Is this because of the new dictionary created while in RunString?
thanks
Sesh Cherukuri
_________________________________________________
Python.NET mailing list - PythonDotNet-+ZN9ApsXKcEdnm+yROfE0A@xxxxxxxxxxxxxxxx
http://mail.python.org/mailman/listinfo/pythondotnet
_________________________________________________
Python.NET mailing list - PythonDotNet-+ZN9ApsXKcEdnm+yROfE0A@xxxxxxxxxxxxxxxx
http://mail.python.org/mailman/listinfo/pythondotnet
Next Message by Date:
click to view message preview
[Python.NET] Problem with DateTimePicker control
Hi,
I have problems setting the Value of a DateTimePicker control of Windows
Forms. The Value property *is* set (I can print it), but if displayed
only the current date is shown in the control, not the value I have set
the Value property set to before.
The python version is 2.3, the .net versions tested are 1.1 and 2.0.
Here are some code snippets:
#-----------------------------------------------------------------------
def dt2SysDt(dtFrom, incr = 0):
#-----------------------------------------------------------------------
"python date/dateTime in .net-DateTime(Date) umwandeln"
return (System.DateTime(dtFrom.year, dtFrom.month, dtFrom.day)
.AddDays(incr))
#-----------------------------------------------------------------------
def sysDt2Dt(dtFrom, incr = 0):
#-----------------------------------------------------------------------
".net-DateTime(Date) in python dateTime.date umwandeln"
dtFrom = dtFrom.AddDays(incr)
rv = datetime.date(dtFrom.Year, dtFrom.Month, dtFrom.Day)
#print "sysDt2Dt=", rv
return rv
....
# set the value
self.dtpErzeugtVon.Value = dt2SysDt(src.dtErzeugtAb)
# display the values of dtp's
for ctl in (self.dtpErzeugtVon, self.dtpErzeugtBis,
self.dtpVersendetVon, self.dtpVersendetBis):
print "dtp...=", ctl.Name, ctl.Value, sysDt2Dt(ctl.Value)
# the print statement shows the following
...
dtp...= dtpErzeugtBis 31.03.2007 00:00:00 2007-03-31
...
When the form is shown with ShowDialog(), only the current date is show
(march 7 instead of march 31).
Does anyone have an idea?
Thanks
Peter Schwalm
_________________________________________________
Python.NET mailing list - PythonDotNet-+ZN9ApsXKcEdnm+yROfE0A@xxxxxxxxxxxxxxxx
http://mail.python.org/mailman/listinfo/pythondotnet
Previous Message by Thread:
click to view message preview
Re: [Python.NET] RunString does not work
FYI, if you are getting a NameError using RunString that usually means that the string
you run needs to do some imports — remember that RunString creates (essentially) a
totally new global local namespace, so you’ll need to import any names you need in
your code snippet.
e.g.
CLR.Food.SpamAndEggs.cook()
...will fail
but
import CLR
import CLR.Food
CLR.Food.SpamAndEggs.cook()
...should work
hope this helps,
-Brian
On 3/4/07 4:37 AM, "Seshagiri Cherukuri" <seshagiri.cherukuri@xxxxxxxxx> wrote:
hi All,
I have embedded pythonnet into my C# application and this works great as far as creating .net objects from python, executing methods etc.
Here I am using the RunSimpleString (the PyRun_SimpleString) wrapper to send commands from the C# application to the pythonnet.
However I am facing the following problems:
1. After RunSimpleString, PyErr_Occurred or PyErr_Fetch are not able to detect any exceptions even if they did occur
- for example, consider the code - curErr = PythonEngine.RunSimpleString("10/0\n" );
- this code does return a -1 value indicating error.
- however after this none of PyErr_Occurred or PyErr_Fetch do not detect this error
- from python documentation it looks like there is no way to obtain the exception information
2. using RunString
- because of above problem I want to send a multi-line code including a try-except: block
- I have tried using RunString with all the three modes - Py_eval_input, Py_single_input and Py_file_input
- The RunString works only if the code does not have any .net objects or does not include any .net exceptions
- If the code sent to RunString includes a .net object e.g. obj, then RunString always returns in NameError, obj is not defined
3. Strangely, if I run the console application, it is able to work with .net exceptions - both .net and user defined exceptions
4. Is this because of the new dictionary created while in RunString?
thanks
Sesh Cherukuri
_________________________________________________
Python.NET mailing list - PythonDotNet-+ZN9ApsXKcEdnm+yROfE0A@xxxxxxxxxxxxxxxx
http://mail.python.org/mailman/listinfo/pythondotnet
_________________________________________________
Python.NET mailing list - PythonDotNet-+ZN9ApsXKcEdnm+yROfE0A@xxxxxxxxxxxxxxxx
http://mail.python.org/mailman/listinfo/pythondotnet
Next Message by Thread:
click to view message preview
[Python.NET] unsubscribe
Best Regards,
==============================
Kodak Health Group Global R&D Center (Shanghai)
No. 27 Xin Jinqiao Road
Building 4 Jinqiao Software Park,
Shanghai, P.R.China 201206
Tel :8621-50308810-5374
Mail : Andy.bu-3K1NbTuQvykAvxtiuMwx3w@xxxxxxxxxxxxxxxx
Building 4, Jinqiao Software Park, Shanghai 201206
==============================
pythondotnet-request-+ZN9ApsXKcEdnm+yROfE0A@xxxxxxxxxxxxxxxx
Sent by: pythondotnet-bounces-+ZN9ApsXKcEdnm+yROfE0A@xxxxxxxxxxxxxxxx
03/04/2007 07:00 PM
Please respond to
pythondotnet-+ZN9ApsXKcEdnm+yROfE0A@xxxxxxxxxxxxxxxx
To
pythondotnet-+ZN9ApsXKcEdnm+yROfE0A@xxxxxxxxxxxxxxxx
cc
Subject
PythonDotNet Digest, Vol 41, Issue 4
Send PythonDotNet mailing list submissions to
pythondotnet-+ZN9ApsXKcEdnm+yROfE0A@xxxxxxxxxxxxxxxx
To subscribe or unsubscribe via the World Wide Web, visit
http://mail.python.org/mailman/listinfo/pythondotnet
or, via email, send a message with subject or body 'help' to
pythondotnet-request-+ZN9ApsXKcEdnm+yROfE0A@xxxxxxxxxxxxxxxx
You can reach the person managing the list at
pythondotnet-owner-+ZN9ApsXKcEdnm+yROfE0A@xxxxxxxxxxxxxxxx
When replying, please edit your Subject line so it is more specific
than "Re: Contents of PythonDotNet digest..."
Today's Topics:
1. RunString does not work (Seshagiri Cherukuri)
----------------------------------------------------------------------
Message: 1
Date: Sun, 4 Mar 2007 01:37:52 -0800
From: "Seshagiri Cherukuri" <seshagiri.cherukuri-Re5JQEeQqe8AvxtiuMwx3w@xxxxxxxxxxxxxxxx>
Subject: [Python.NET] RunString does not work
To: pythondotnet-+ZN9ApsXKcEdnm+yROfE0A@xxxxxxxxxxxxxxxx
Message-ID:
<8d12fba0703040137s728f6848vb061a602472c9490-JsoAwUIsXosN+BqQ9rBEUg@xxxxxxxxxxxxxxxx>
Content-Type: text/plain; charset="iso-8859-1"
hi All,
I have embedded pythonnet into my C# application and this works great as
far
as creating .net objects from python, executing methods etc.
Here I am using the RunSimpleString (the PyRun_SimpleString) wrapper to
send
commands from the C# application to the pythonnet.
However I am facing the following problems:
1. After RunSimpleString, PyErr_Occurred or PyErr_Fetch are not able to
detect any exceptions even if they did occur
- for example, consider the code - curErr = PythonEngine.RunSimpleString(
"10/0\n");
- this code does return a -1 value indicating error.
- however after this none of PyErr_Occurred or PyErr_Fetch do not detect
this error
- from python documentation it looks like there is no way to obtain the
exception information
2. using RunString
- because of above problem I want to send a multi-line code including a
try-except: block
- I have tried using RunString with all the three modes - Py_eval_input,
Py_single_input and Py_file_input
- The RunString works only if the code does not have any .net objects or
does not include any .net exceptions
- If the code sent to RunString includes a .net object e.g. obj, then
RunString always returns in NameError, obj is not defined
3. Strangely, if I run the console application, it is able to work with
.net
exceptions - both .net and user defined exceptions
4. Is this because of the new dictionary created while in RunString?
thanks
Sesh Cherukuri
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/pythondotnet/attachments/20070304/b1b255fc/attachment-0001.htm
------------------------------
_______________________________________________
PythonDotNet mailing list
PythonDotNet-+ZN9ApsXKcEdnm+yROfE0A@xxxxxxxxxxxxxxxx
http://mail.python.org/mailman/listinfo/pythondotnet
End of PythonDotNet Digest, Vol 41, Issue 4
*******************************************
_________________________________________________
Python.NET mailing list - PythonDotNet-+ZN9ApsXKcEdnm+yROfE0A@xxxxxxxxxxxxxxxx
http://mail.python.org/mailman/listinfo/pythondotnet
|
|