From serge.weinstock at uk.bnpparibas.com Mon Sep 1 08:28:43 2014 From: serge.weinstock at uk.bnpparibas.com (Serge WEINSTOCK) Date: Mon, 1 Sep 2014 06:28:43 +0000 Subject: [Python.NET] Running an embedded interpreter In-Reply-To: References: Message-ID: It took me sometime to figure that I have to first fork, create a branch, apply the patch, commit and create a pull request but I think I have now understood how it works ;-) So, I?ve created the pull request. Could you have a look at it? Thanks, Serge From: pythondotnet-bounces+serge.weinstock=uk.bnpparibas.com at python.org [mailto:pythondotnet-bounces+serge.weinstock=uk.bnpparibas.com at python.org] Sent: 27 August 2014 17:34 To: pythondotnet at python.org Subject: Re: [Python.NET] Running an embedded interpreter Hi Serge, I don't see your pull request here https://github.com/renshawbay/pythonnet/pulls, and I can't see your fork. Could you send me the url of your fork and I'll take a look if I can pull the changes from there? If you want to have another go at creating the pull request this might help: On Wed, Aug 27, 2014 at 2:02 PM, Serge WEINSTOCK > wrote: Hi Tony, I?ve submitted the fix. As I?m new to git and github, I?m not sure I?ve done the right steps. Let me know if you can?t find my patch Thanks, Serge From: pythondotnet-bounces+serge.weinstock=uk.bnpparibas.com at python.org [mailto:pythondotnet-bounces+serge.weinstock=uk.bnpparibas.com at python.org] Sent: 26 August 2014 16:54 To: pythondotnet at python.org Subject: Re: [Python.NET] Running an embedded interpreter Hi Serge, ah great, good spot. Certainly, please send a pull request to the github repo and I'll merge it. thanks, Tony On Tue, Aug 26, 2014 at 1:07 AM, Serge WEINSTOCK > wrote: Hi Tony, I think I've found the issue: The signatures of Py_SetPythonHome, Py_GetPythonHome, Py_SetProgramName and Py_GetProgramName have changed from python 2.x to python 3.x: the strings are now Unicode strings. I've done the following patches (I've also added support for Py_SetPath and Py_GetPath as I needed to set up myself sys.path): * runtime.cs //================================================================== #if PYTHON32 || PYTHON33 || PYTHON34 [DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl, ExactSpelling=true, CharSet=CharSet.Ansi)] [return: MarshalAs(UnmanagedType.LPWStr)] internal unsafe static extern string Py_GetProgramName(); [DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl, ExactSpelling=true, CharSet=CharSet.Ansi)] internal unsafe static extern void Py_SetProgramName([MarshalAsAttribute(UnmanagedType.LPWStr)]string name); [DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl, ExactSpelling=true, CharSet=CharSet.Ansi)] [return: MarshalAs(UnmanagedType.LPWStr)] internal unsafe static extern string Py_GetPythonHome(); [DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl, ExactSpelling=true, CharSet=CharSet.Ansi)] internal unsafe static extern void Py_SetPythonHome([MarshalAsAttribute(UnmanagedType.LPWStr)]string home); [DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl, ExactSpelling=true, CharSet=CharSet.Ansi)] [return: MarshalAs(UnmanagedType.LPWStr)] internal unsafe static extern string Py_GetPath(); [DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl, ExactSpelling=true, CharSet=CharSet.Ansi)] internal unsafe static extern void Py_SetPath([MarshalAsAttribute(UnmanagedType.LPWStr)]string home); #else [DllImport(Runtime.dll, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true, CharSet = CharSet.Ansi)] internal unsafe static extern string Py_GetProgramName(); [DllImport(Runtime.dll, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true, CharSet = CharSet.Ansi)] internal unsafe static extern void Py_SetProgramName(string name); [DllImport(Runtime.dll, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true, CharSet = CharSet.Ansi)] internal unsafe static extern string Py_GetPythonHome(); [DllImport(Runtime.dll, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true, CharSet = CharSet.Ansi)] internal unsafe static extern void Py_SetPythonHome(string home); [DllImport(Runtime.dll, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true, CharSet = CharSet.Ansi)] internal unsafe static extern string Py_GetPath(); [DllImport(Runtime.dll, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true, CharSet = CharSet.Ansi)] internal unsafe static extern void Py_SetPath(string home); #endif //================================================================== PythonEngine.cs //================================================================== public static string PythonPath { get { string result = Runtime.Py_GetPath(); if (result == null) { return ""; } return result; } set { Runtime.Py_SetPath(value); } } //================================================================== Could you add these patches to the source repository? Thanks, Serge From: pythondotnet-bounces+serge.weinstock=uk.bnpparibas.com at python.org [mailto:pythondotnet-bounces+serge.weinstock=uk.bnpparibas.com at python.org] Sent: 21 August 2014 14:43 To: pythondotnet at python.org Subject: Re: [Python.NET] Running an embedded interpreter Hi Serge, sorry, not sure why one would work and not the other. For what it's worth, I've been using the 3.2 x64 version for some time now, both for calling .NET from python and for embedding Python into a .NET application without any problem like the ones you describe. What I suggest you try is grabbing the latest code from the renshawbay repo and build that using setupwin.py - you might want to edit that file to build the debug project. Then you will be able to step through and see exactly where it's going wrong. You can build it from visual studio if you prefer, but you will have to be careful to set some of the defines correctly; look at setupwin.py to see what needs setting. You can also download the pdb files and python source from python.org, which should allow you step into the python source code without having to build python yourself. Best regards, Tony On Thu, Aug 21, 2014 at 10:36 AM, Serge WEINSTOCK > wrote: Hi Tony, I?ve noticed that you are the main contributor for this branch of Python for .Net. Thanks a lot for that contribution. Maybe you can help me a little more with my issue. I think the main issue is due to the fact that I?m using Python 3.2. I?ve done the following tests: * Python 3.2 x86: * calling .Net libraries from standard python interpreter: works fine. * running embedded interpreter from .Net application: * from Visual Studio: works fine. Setting PYTHONHOME is enough. No need to set PYTHONPATH * from command line: doesn't work. Setting PYTHONPATH improves a little things. * Python 3.2 x64: * calling .Net libraries from standard python interpreter: works fine. * running embedded interpreter from .Net application: * from Visual Studio: doesn't work. Setting PYTHONPATH improves a little things. * from command line: doesn't work. Setting PYTHONPATH improves a little things. * Python 3.3 x86: * calling .Net libraries from standard python interpreter: works fine. * running embedded interpreter from .Net application: * from Visual Studio: works fine. * from command line: works fine. I've also compared for the VS run or for the command line run: * the paths given by 'sys.modules'. They are the same. * the paths of the loaded dlls as given by 'listdlls'. They are the same. Maybe you have a clue on why running an embedded interpreter works with 3.3 but not 3.3 Thanks, Serge From: Serge WEINSTOCK Sent: 19 August 2014 17:45 To: 'pythondotnet at python.org' Subject: RE: [Python.NET] Running an embedded interpreter Hi Tony, I?ve tried your suggestion but it doesn?t work. The issue seems to be more ?fundamental? as the import of the .Net System assembly doesn?t work. Serge From: pythondotnet-bounces+serge.weinstock=uk.bnpparibas.com at python.org [mailto:pythondotnet-bounces+serge.weinstock=uk.bnpparibas.com at python.org] Sent: 19 August 2014 17:16 To: pythondotnet at python.org Subject: Re: [Python.NET] Running an embedded interpreter Hi Serge, 'mbcs' is what python uses to mean the current configured encoding. I would guess that the encoding of sys.stdout is different when using visual studio output console than the console. You could try a different encoding method by setting the PYTHONIOENCODING environment variable before starting your exe, eg: SET PYTHONIOENCODING=utf-8:ignore Look for PYTHONIOENCODING here https://docs.python.org/3/using/cmdline.html for more details. Tony On Tue, Aug 19, 2014 at 2:22 PM, Serge WEINSTOCK > wrote: Hi, I?m trying to use Python3.2 using the Python.Net version found at: https://github.com/renshawbay/pythonnet I?m using the following simple test program: //======================================================================= using System; using System.IO; using Python.Runtime; namespace TestPythonNet { class Program { static void Main(string[] args) { string binDir = Path.GetDirectoryName(System.Reflection.Assembly.GetAssembly(typeof(Program)).Location); string pyHome = @"D:\src\scratch\TestPythonNet\TestPythonNet\PythonRuntime"; PythonEngine.PythonHome = @"D:\src\scratch\TestPythonNet\TestPythonNet\PythonRuntime"; PythonEngine.ProgramName = "PythonRuntime"; Environment.SetEnvironmentVariable("PYTHONPATH", Path.GetFullPath(Path.Combine(pyHome, "DLLs")) + ";" + Path.GetFullPath(Path.Combine(pyHome, "Lib")) + ";" + Path.GetFullPath(Path.Combine(pyHome, "Lib", "site-packages")) + ";" + binDir ); Environment.SetEnvironmentVariable("PYTHONVERBOSE", "1"); PythonEngine.Initialize(); PythonEngine.ImportModule("clr"); using (Py.GIL()) { PythonEngine.RunSimpleString( "import clr; " + "a = clr.AddReference('System'); " + "print(a.Location);" + "from System import Environment;" + "print(Environment.MachineName);"); } } } } //======================================================================= ?D:\src\scratch\TestPythonNet\TestPythonNet\PythonRuntime? is a folder where I?ve copied the DLLs and Lib folder from a python 3.2 x86 distribution. When I run it from Visual Studio it works fine (I guess it may be related to the fact that I?m using python tools for Visual Studio). But when I run it from the console, it fails with the output: //======================================================================= Traceback (most recent call last): File "D:\src\scratch\TestPythonNet\TestPythonNet\PythonRuntime\Lib\site.py", line 481, in execsitecustomize import sitecustomize UnicodeEncodeError: 'mbcs' codec can't encode characters in position 0--1: invalid character Traceback (most recent call last): File "D:\src\scratch\TestPythonNet\TestPythonNet\PythonRuntime\Lib\site.py", line 497, in execusercustomize import usercustomize UnicodeEncodeError: 'mbcs' codec can't encode characters in position 0--1: invalid character C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System\v4.0_4.0.0.0__b77a5c561934e089\System.dll Traceback (most recent call last): File "", line 1, in UnicodeEncodeError: 'mbcs' codec can't encode characters in position 0--1: invalid character //======================================================================= The ?print(a.Location);" works but not the ?from System import Environment?. There are also all these errors about mbcs. Any idea on what I?m doing wrong? Thanks, Serge Weinstock ___________________________________________________________ This e-mail may contain confidential and/or privileged information. If you are not the intended recipient (or have received this e-mail in error) please notify the sender immediately and delete this e-mail. Any unauthorised copying, disclosure or distribution of the material in this e-mail is prohibited. Please refer to http://www.bnpparibas.co.uk/en/email-disclaimer/ for additional disclosures. _________________________________________________ Python.NET mailing list - PythonDotNet at python.org https://mail.python.org/mailman/listinfo/pythondotnet ___________________________________________________________ This e-mail may contain confidential and/or privileged information. If you are not the intended recipient (or have received this e-mail in error) please notify the sender immediately and delete this e-mail. Any unauthorised copying, disclosure or distribution of the material in this e-mail is prohibited. Please refer to http://www.bnpparibas.co.uk/en/email-disclaimer/ for additional disclosures. _________________________________________________ Python.NET mailing list - PythonDotNet at python.org https://mail.python.org/mailman/listinfo/pythondotnet ___________________________________________________________ This e-mail may contain confidential and/or privileged information. If you are not the intended recipient (or have received this e-mail in error) please notify the sender immediately and delete this e-mail. Any unauthorised copying, disclosure or distribution of the material in this e-mail is prohibited. Please refer to http://www.bnpparibas.co.uk/en/email-disclaimer/ for additional disclosures. _________________________________________________ Python.NET mailing list - PythonDotNet at python.org https://mail.python.org/mailman/listinfo/pythondotnet ___________________________________________________________ This e-mail may contain confidential and/or privileged information. If you are not the intended recipient (or have received this e-mail in error) please notify the sender immediately and delete this e-mail. Any unauthorised copying, disclosure or distribution of the material in this e-mail is prohibited. Please refer to http://www.bnpparibas.co.uk/en/email-disclaimer/ for additional disclosures. _________________________________________________ Python.NET mailing list - PythonDotNet at python.org https://mail.python.org/mailman/listinfo/pythondotnet ___________________________________________________________ This e-mail may contain confidential and/or privileged information. If you are not the intended recipient (or have received this e-mail in error) please notify the sender immediately and delete this e-mail. Any unauthorised copying, disclosure or distribution of the material in this e-mail is prohibited. Please refer to http://www.bnpparibas.co.uk/en/email-disclaimer/ for additional disclosures. -------------- next part -------------- An HTML attachment was scrubbed... URL: From tony at pyxll.com Mon Sep 1 18:27:58 2014 From: tony at pyxll.com (Tony Roberts) Date: Mon, 1 Sep 2014 17:27:58 +0100 Subject: [Python.NET] Running an embedded interpreter In-Reply-To: References: Message-ID: Thanks Serge, that's done. cheers, Tony On Mon, Sep 1, 2014 at 7:28 AM, Serge WEINSTOCK < serge.weinstock at uk.bnpparibas.com> wrote: > It took me sometime to figure that I have to first fork, create a > branch, apply the patch, commit and create a pull request but I think I > have now understood how it works ;-) > > > > So, I?ve created the pull request. Could you have a look at it? > > > > Thanks, > > Serge > > > > *From:* pythondotnet-bounces+serge.weinstock=uk.bnpparibas.com at python.org > [mailto:pythondotnet-bounces+serge.weinstock=uk.bnpparibas.com at python.org] > > *Sent:* 27 August 2014 17:34 > > *To:* pythondotnet at python.org > *Subject:* Re: [Python.NET] Running an embedded interpreter > > > > Hi Serge, > > > > I don't see your pull request here > https://github.com/renshawbay/pythonnet/pulls, and I can't see your fork. > Could you send me the url of your fork and I'll take a look if I can pull > the changes from there? > > > > If you want to have another go at creating the pull request this might > help: > > > > > > > > On Wed, Aug 27, 2014 at 2:02 PM, Serge WEINSTOCK < > serge.weinstock at uk.bnpparibas.com> wrote: > > Hi Tony, > > > > I?ve submitted the fix. As I?m new to git and github, I?m not sure I?ve > done the right steps. Let me know if you can?t find my patch > > > > Thanks, > > Serge > > > > *From:* pythondotnet-bounces+serge.weinstock=uk.bnpparibas.com at python.org > [mailto:pythondotnet-bounces+serge.weinstock=uk.bnpparibas.com at python.org] > > *Sent:* 26 August 2014 16:54 > > > *To:* pythondotnet at python.org > *Subject:* Re: [Python.NET] Running an embedded interpreter > > > > Hi Serge, > > > > ah great, good spot. > > > > Certainly, please send a pull request to the github repo and I'll merge it. > > > > thanks, > > Tony > > > > On Tue, Aug 26, 2014 at 1:07 AM, Serge WEINSTOCK < > serge.weinstock at uk.bnpparibas.com> wrote: > > Hi Tony, > > I think I've found the issue: > The signatures of Py_SetPythonHome, Py_GetPythonHome, Py_SetProgramName > and Py_GetProgramName have changed from python 2.x to python 3.x: the > strings are now Unicode strings. > > I've done the following patches (I've also added support for Py_SetPath > and Py_GetPath as I needed to set up myself sys.path): > * runtime.cs > //================================================================== > #if PYTHON32 || PYTHON33 || PYTHON34 > [DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl, > ExactSpelling=true, CharSet=CharSet.Ansi)] > [return: MarshalAs(UnmanagedType.LPWStr)] > internal unsafe static extern string > Py_GetProgramName(); > > [DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl, > ExactSpelling=true, CharSet=CharSet.Ansi)] > internal unsafe static extern void > Py_SetProgramName([MarshalAsAttribute(UnmanagedType.LPWStr)]string > name); > > [DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl, > ExactSpelling=true, CharSet=CharSet.Ansi)] > [return: MarshalAs(UnmanagedType.LPWStr)] > internal unsafe static extern string > Py_GetPythonHome(); > > [DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl, > ExactSpelling=true, CharSet=CharSet.Ansi)] > internal unsafe static extern void > Py_SetPythonHome([MarshalAsAttribute(UnmanagedType.LPWStr)]string > home); > > [DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl, > ExactSpelling=true, CharSet=CharSet.Ansi)] > [return: MarshalAs(UnmanagedType.LPWStr)] > internal unsafe static extern string > Py_GetPath(); > > [DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl, > ExactSpelling=true, CharSet=CharSet.Ansi)] > internal unsafe static extern void > Py_SetPath([MarshalAsAttribute(UnmanagedType.LPWStr)]string home); > #else > [DllImport(Runtime.dll, CallingConvention = CallingConvention.Cdecl, > ExactSpelling = true, CharSet = CharSet.Ansi)] > internal unsafe static extern string > Py_GetProgramName(); > > [DllImport(Runtime.dll, CallingConvention = CallingConvention.Cdecl, > ExactSpelling = true, CharSet = CharSet.Ansi)] > internal unsafe static extern void > Py_SetProgramName(string name); > > [DllImport(Runtime.dll, CallingConvention = CallingConvention.Cdecl, > ExactSpelling = true, CharSet = CharSet.Ansi)] > internal unsafe static extern string > Py_GetPythonHome(); > > [DllImport(Runtime.dll, CallingConvention = CallingConvention.Cdecl, > ExactSpelling = true, CharSet = CharSet.Ansi)] > internal unsafe static extern void > Py_SetPythonHome(string home); > > [DllImport(Runtime.dll, CallingConvention = CallingConvention.Cdecl, > ExactSpelling = true, CharSet = CharSet.Ansi)] > internal unsafe static extern string > Py_GetPath(); > > [DllImport(Runtime.dll, CallingConvention = CallingConvention.Cdecl, > ExactSpelling = true, CharSet = CharSet.Ansi)] > internal unsafe static extern void > Py_SetPath(string home); > #endif > //================================================================== > PythonEngine.cs > //================================================================== > public static string PythonPath { > get > { > string result = Runtime.Py_GetPath(); > if (result == null) > { > return ""; > } > return result; > } > set > { > Runtime.Py_SetPath(value); > } > } > //================================================================== > > Could you add these patches to the source repository? > > Thanks, > > Serge > > > > From: pythondotnet-bounces+serge.weinstock=uk.bnpparibas.com at python.org > [mailto:pythondotnet-bounces+serge.weinstock=uk.bnpparibas.com at python.org] > > Sent: 21 August 2014 14:43 > > To: pythondotnet at python.org > Subject: Re: [Python.NET] Running an embedded interpreter > > Hi Serge, > > sorry, not sure why one would work and not the other. For what it's worth, > I've been using the 3.2 x64 version for some time now, both for calling > .NET from python and for embedding Python into a .NET application without > any problem like the ones you describe. > > What I suggest you try is grabbing the latest code from the renshawbay > repo and build that using setupwin.py - you might want to edit that file to > build the debug project. Then you will be able to step through and see > exactly where it's going wrong. You can build it from visual studio if you > prefer, but you will have to be careful to set some of the defines > correctly; look at setupwin.py to see what needs setting. > > You can also download the pdb files and python source from python.org, > which should allow you step into the python source code without having to > build python yourself. > > Best regards, > Tony > > > On Thu, Aug 21, 2014 at 10:36 AM, Serge WEINSTOCK < > serge.weinstock at uk.bnpparibas.com> wrote: > Hi Tony, > > I?ve noticed that you are the main contributor for this branch of Python > for .Net. Thanks a lot for that contribution. > > Maybe you can help me a little more with my issue. I think the main issue > is due to the fact that I?m using Python 3.2. > > I?ve done the following tests: > * Python 3.2 x86: > * calling .Net libraries from standard python interpreter: works fine. > * running embedded interpreter from .Net application: > * from Visual Studio: works fine. Setting PYTHONHOME is enough. > No need to set PYTHONPATH > * from command line: doesn't work. Setting PYTHONPATH improves a > little things. > * Python 3.2 x64: > * calling .Net libraries from standard python interpreter: works fine. > * running embedded interpreter from .Net application: > * from Visual Studio: doesn't work. Setting PYTHONPATH improves > a little things. > * from command line: doesn't work. Setting PYTHONPATH improves a > little things. > * Python 3.3 x86: > * calling .Net libraries from standard python interpreter: works fine. > * running embedded interpreter from .Net application: > * from Visual Studio: works fine. > * from command line: works fine. > > I've also compared for the VS run or for the command line run: > * the paths given by 'sys.modules'. They are the same. > * the paths of the loaded dlls as given by 'listdlls'. They are the same. > > Maybe you have a clue on why running an embedded interpreter works with > 3.3 but not 3.3 > > Thanks, > Serge > > > From: Serge WEINSTOCK > Sent: 19 August 2014 17:45 > To: 'pythondotnet at python.org' > Subject: RE: [Python.NET] Running an embedded interpreter > > Hi Tony, > > I?ve tried your suggestion but it doesn?t work. > > The issue seems to be more ?fundamental? as the import of the .Net System > assembly doesn?t work. > > Serge > > From: pythondotnet-bounces+serge.weinstock=uk.bnpparibas.com at python.org > [mailto:pythondotnet-bounces+serge.weinstock=uk.bnpparibas.com at python.org] > Sent: 19 August 2014 17:16 > To: pythondotnet at python.org > Subject: Re: [Python.NET] Running an embedded interpreter > Hi Serge, > > 'mbcs' is what python uses to mean the current configured encoding. I > would guess that the encoding of sys.stdout is different when using visual > studio output console than the console. > > You could try a different encoding method by setting the PYTHONIOENCODING > environment variable before starting your exe, eg: > SET PYTHONIOENCODING=utf-8:ignore > > Look for PYTHONIOENCODING here > https://docs.python.org/3/using/cmdline.html for more details. > > Tony > > > > On Tue, Aug 19, 2014 at 2:22 PM, Serge WEINSTOCK < > serge.weinstock at uk.bnpparibas.com> wrote: > Hi, > > I?m trying to use Python3.2 using the Python.Net version found at: > https://github.com/renshawbay/pythonnet > > I?m using the following simple test program: > > //======================================================================= > using System; > using System.IO; > using Python.Runtime; > > namespace TestPythonNet > { > class Program > { > static void Main(string[] args) > { > string binDir = > Path.GetDirectoryName(System.Reflection.Assembly.GetAssembly(typeof(Program)).Location); > string pyHome = > @"D:\src\scratch\TestPythonNet\TestPythonNet\PythonRuntime"; > PythonEngine.PythonHome = > @"D:\src\scratch\TestPythonNet\TestPythonNet\PythonRuntime"; > PythonEngine.ProgramName = "PythonRuntime"; > Environment.SetEnvironmentVariable("PYTHONPATH", > Path.GetFullPath(Path.Combine(pyHome, "DLLs")) + ";" + > Path.GetFullPath(Path.Combine(pyHome, "Lib")) + ";" + > Path.GetFullPath(Path.Combine(pyHome, "Lib", > "site-packages")) + ";" + > binDir > ); > Environment.SetEnvironmentVariable("PYTHONVERBOSE", "1"); > PythonEngine.Initialize(); > PythonEngine.ImportModule("clr"); > using (Py.GIL()) > { > PythonEngine.RunSimpleString( > "import clr; " + > "a = clr.AddReference('System'); " + > "print(a.Location);" + > "from System import Environment;" + > "print(Environment.MachineName);"); > } > } > } > } > //======================================================================= > > ?D:\src\scratch\TestPythonNet\TestPythonNet\PythonRuntime? is a folder > where I?ve copied the DLLs and Lib folder from a python 3.2 x86 > distribution. > > > When I run it from Visual Studio it works fine (I guess it may be related > to the fact that I?m using python tools for Visual Studio). > > But when I run it from the console, it fails with the output: > > //======================================================================= > Traceback (most recent call last): > File > "D:\src\scratch\TestPythonNet\TestPythonNet\PythonRuntime\Lib\site.py", > line 481, in execsitecustomize > import sitecustomize > UnicodeEncodeError: 'mbcs' codec can't encode characters in position 0--1: > invalid character > Traceback (most recent call last): > File > "D:\src\scratch\TestPythonNet\TestPythonNet\PythonRuntime\Lib\site.py", > line 497, in execusercustomize > import usercustomize > UnicodeEncodeError: 'mbcs' codec can't encode characters in position 0--1: > invalid character > > C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System\v4.0_4.0.0.0__b77a5c561934e089\System.dll > Traceback (most recent call last): > File "", line 1, in > UnicodeEncodeError: 'mbcs' codec can't encode characters in position 0--1: > invalid character > //======================================================================= > > The ?print(a.Location);" works but not the ?from System import > Environment?. There are also all these errors about mbcs. > > Any idea on what I?m doing wrong? > > Thanks, > Serge Weinstock > > > ___________________________________________________________ > This e-mail may contain confidential and/or privileged information. If you > are not the intended recipient (or have received this e-mail in error) > please notify the sender immediately and delete this e-mail. Any > unauthorised copying, disclosure or distribution of the material in this > e-mail is prohibited. > Please refer to http://www.bnpparibas.co.uk/en/email-disclaimer/ for > additional disclosures. > > _________________________________________________ > Python.NET mailing list - PythonDotNet at python.org > https://mail.python.org/mailman/listinfo/pythondotnet > > > ___________________________________________________________ > This e-mail may contain confidential and/or privileged information. If you > are not the intended recipient (or have received this e-mail in error) > please notify the sender immediately and delete this e-mail. Any > unauthorised copying, disclosure or distribution of the material in this > e-mail is prohibited. > > Please refer to http://www.bnpparibas.co.uk/en/email-disclaimer/ for > additional disclosures. > _________________________________________________ > Python.NET mailing list - PythonDotNet at python.org > https://mail.python.org/mailman/listinfo/pythondotnet > > > ___________________________________________________________ > This e-mail may contain confidential and/or privileged information. If you > are not the intended recipient (or have received this e-mail in error) > please notify the sender immediately and delete this e-mail. Any > unauthorised copying, disclosure or distribution of the material in this > e-mail is prohibited. > > Please refer to http://www.bnpparibas.co.uk/en/email-disclaimer/ for > additional disclosures. > _________________________________________________ > Python.NET mailing list - PythonDotNet at python.org > https://mail.python.org/mailman/listinfo/pythondotnet > > > > > ___________________________________________________________ > This e-mail may contain confidential and/or privileged information. If you > are not the intended recipient (or have received this e-mail in error) > please notify the sender immediately and delete this e-mail. Any > unauthorised copying, disclosure or distribution of the material in this > e-mail is prohibited. > > Please refer to http://www.bnpparibas.co.uk/en/email-disclaimer/ for > additional disclosures. > > > _________________________________________________ > Python.NET mailing list - PythonDotNet at python.org > https://mail.python.org/mailman/listinfo/pythondotnet > > > > > ___________________________________________________________ > This e-mail may contain confidential and/or privileged information. If you > are not the intended recipient (or have received this e-mail in error) > please notify the sender immediately and delete this e-mail. Any > unauthorised copying, disclosure or distribution of the material in this > e-mail is prohibited. > > Please refer to http://www.bnpparibas.co.uk/en/email-disclaimer/ for > additional disclosures. > > _________________________________________________ > Python.NET mailing list - PythonDotNet at python.org > https://mail.python.org/mailman/listinfo/pythondotnet > -------------- next part -------------- An HTML attachment was scrubbed... URL: From jgill at tokiomillennium.com Mon Sep 22 22:44:05 2014 From: jgill at tokiomillennium.com (John Gill) Date: Mon, 22 Sep 2014 20:44:05 +0000 Subject: [Python.NET] DB api module using pythondotnet and System.Data.SqlClient Message-ID: I just spend a frustrating afternoon trying to connect to SQL Server(s) with pymssql. The issue was I wanted to use windows authentication rather than supply username and password in the code. After getting no joy and lots of googling I realized I can just do: import clr clr.AddReference('System.Data') from System.Data.SqlClient import SqlConnection Sure enough this works a treat. I need to remember how powerful pythondotnet is and make sure I use it more. Meanwhile, I am wondering if anyone has tried to wrap this stuff up to produce a standard DB-API interface to SQL Server using pythondotnet and SqlConnection? If not, anyone else out there interested in this? John This communication and any attachments contain information which is confidential and may also be legally privileged. It is for the exclusive use of the intended recipient(s). If you are not the intended recipient(s) please note that any form of disclosure, distribution, copying, printing or use of this communication or the information in it or in any attachments is strictly prohibited and may be unlawful. If you have received this communication in error, please return it with the title "received in error" to postmaster at tokiomillennium.com and then permanently delete the email and any attachments from your system. E-mail communications cannot be guaranteed to be secure or error free, as information could be intercepted, corrupted, amended, lost, destroyed, arrive late or incomplete, or contain viruses. It is the recipient's responsibility to ensure that e-mail transmissions and any attachments are virus free. We do not accept liability for any damages or other consequences caused by information that is intercepted, corrupted, amended, lost, destroyed, arrives late or incomplete or contains viruses. ****************************************** -------------- next part -------------- An HTML attachment was scrubbed... URL: From serge.weinstock at uk.bnpparibas.com Fri Sep 26 17:52:40 2014 From: serge.weinstock at uk.bnpparibas.com (Serge WEINSTOCK) Date: Fri, 26 Sep 2014 15:52:40 +0000 Subject: [Python.NET] Python.Net for Python3: bug when converting a PyLong to a System.Int32 Message-ID: Hi Tony, I've found a bug when trying to use Python integer as .Net int32. For example: //======================================================================= using System; using Python.Runtime; namespace TestPythonNet { public class ATest { public long a64; public int a32; public void f32(int a) { Console.WriteLine("got int {0}", a);} public void f64(long a) { Console.WriteLine("got long {0}", a); } } class Program { static void Main(string[] args) { PythonEngine.PythonHome = @"D:\src\scratch\TestPythonNet\TestPythonNet\PythonRuntime"; PythonEngine.ProgramName = "PythonRuntime"; PythonEngine.Initialize(); PythonEngine.ImportModule("clr"); using (Py.GIL()) { try { PythonEngine.RunSimpleString( @" import clr clr.AddReference('TestPythonNet') from TestPythonNet import ATest f = ATest(); f.a64 = 10 # OK f.a32 = 10 # Fails! f.f64(-1) # OK f.f32(-1) # Fails! print('all fine!') "); } catch (PythonException e) { Console.WriteLine(e); } } } } } //======================================================================= All the call from python using integer as .Net int will fail. This is due to the fact that python3 has only one type of integer: int64. Furthermore, in the Python C API, all the conversion functions from or to int32 have disappeared. I think that the following patch fixes the issue: Src/runtime/converter.cs //======================================================================= static bool ToPrimitive(IntPtr value, Type obType, out Object result, bool setError) { IntPtr overflow = Exceptions.OverflowError; TypeCode tc = Type.GetTypeCode(obType); result = null; IntPtr op; int ival; switch(tc) { case TypeCode.String: string st = Runtime.GetManagedString(value); if (st == null) { goto type_error; } result = st; return true; case TypeCode.Int32: // Trickery to support 64-bit platforms. if (IntPtr.Size == 4) { //------ FIX START // convert it to an python int64 op = Runtime.PyNumber_Long(value); // if the conversion fails if (op == IntPtr.Zero) { if (Exceptions.ExceptionMatches(overflow)) { goto overflow; } goto type_error; } // convert it to an int64 long lval = (long)Runtime.PyLong_AsLongLong(op); // and try to convert it to an int32 if ((lval < int.MinValue) || (int.MaxValue < lval)) { Runtime.Decref(op); if (Exceptions.ExceptionMatches(overflow)) { goto overflow; } goto type_error; } Runtime.Decref(op); result = unchecked((int)lval); return true; //------ FIX END } else { op = Runtime.PyNumber_Long(value) //======================================================================= I will send a pull request to the github repo . Could you have a look at it? Thanks, Serge ___________________________________________________________ This e-mail may contain confidential and/or privileged information. If you are not the intended recipient (or have received this e-mail in error) please notify the sender immediately and delete this e-mail. Any unauthorised copying, disclosure or distribution of the material in this e-mail is prohibited. Please refer to http://www.bnpparibas.co.uk/en/email-disclaimer/ for additional disclosures. -------------- next part -------------- An HTML attachment was scrubbed... URL: From callum at notthesame.co.uk Tue Sep 30 01:43:56 2014 From: callum at notthesame.co.uk (Callum Noble) Date: Mon, 29 Sep 2014 16:43:56 -0700 Subject: [Python.NET] Python.Net for Python3: bug when converting a PyLong Message-ID: Hi Serge, Did you file a pull request for this one? I'm interested in the fix too... -------------- next part -------------- An HTML attachment was scrubbed... URL: From serge.weinstock at uk.bnpparibas.com Tue Sep 30 10:45:38 2014 From: serge.weinstock at uk.bnpparibas.com (Serge WEINSTOCK) Date: Tue, 30 Sep 2014 08:45:38 +0000 Subject: [Python.NET] Python.Net for Python3: bug when converting a PyLong In-Reply-To: References: Message-ID: Hi, I haven?t. I?m a little busy with work now. But I will do it today or tomorrow. I?ve found another issue when using an embedded Python interpreter. I will explain it in another thread Serge From: pythondotnet-bounces+serge.weinstock=uk.bnpparibas.com at python.org [mailto:pythondotnet-bounces+serge.weinstock=uk.bnpparibas.com at python.org] Sent: 30 September 2014 00:44 To: pythondotnet at python.org Subject: Re: [Python.NET] Python.Net for Python3: bug when converting a PyLong Hi Serge, Did you file a pull request for this one? I'm interested in the fix too... ___________________________________________________________ This e-mail may contain confidential and/or privileged information. If you are not the intended recipient (or have received this e-mail in error) please notify the sender immediately and delete this e-mail. Any unauthorised copying, disclosure or distribution of the material in this e-mail is prohibited. Please refer to http://www.bnpparibas.co.uk/en/email-disclaimer/ for additional disclosures. -------------- next part -------------- An HTML attachment was scrubbed... URL: From serge.weinstock at uk.bnpparibas.com Tue Sep 30 12:59:27 2014 From: serge.weinstock at uk.bnpparibas.com (Serge WEINSTOCK) Date: Tue, 30 Sep 2014 10:59:27 +0000 Subject: [Python.NET] Python.Net for Python3: bug when using an embedded interpreter Message-ID: Hi Tony, You recently made a change which prevents me using the library in an embedded Python interpreter. In pythonengine.cs, you "inject" decorators with the following code: //================================================================= public static void Initialize() { ..... IntPtr globals = Runtime.PyEval_GetGlobals(); PyDict locals = new PyDict(); try { IntPtr builtins = Runtime.PyEval_GetBuiltins(); Runtime.PyDict_SetItemString(locals.Handle, "__builtins__", builtins); var assembly = Assembly.GetExecutingAssembly(); using (Stream stream = assembly.GetManifestResourceStream("Python.Runtime.resources.clr.py")) using (StreamReader reader = new StreamReader(stream)) { // add the contents of clr.py to the module string clr_py = reader.ReadToEnd(); PyObject result = RunString(clr_py, globals, locals.Handle); if (null == result) throw new PythonException(); result.Dispose(); } //================================================================= When running from a Python script, it's fine because the Python interpreter has already been initialized and global variables have been defined. The issue is that when running from an embedded interpreter. As I must first initialize the Python engine using PythonEngine.Initialize(). So when we reach this piece of code, there is no script running, no "Python context", so "PyEval_GetGlobals" returns null and "RunString" fails. We could create an artificial global dictionary but it won't be available to the rest of the code. I think it should be best to move this code into a Python module. If run from a script (PyEval_GetGlobals() != null), you could inject the decorators with a simple "from decorators_pythonnet import *". If run from an embedded interpreter, we could add a line for running the same code once the engine has been initialized. What do you think? Serge Weinstock ___________________________________________________________ This e-mail may contain confidential and/or privileged information. If you are not the intended recipient (or have received this e-mail in error) please notify the sender immediately and delete this e-mail. Any unauthorised copying, disclosure or distribution of the material in this e-mail is prohibited. Please refer to http://www.bnpparibas.co.uk/en/email-disclaimer/ for additional disclosures. -------------- next part -------------- An HTML attachment was scrubbed... URL: From tony at pyxll.com Tue Sep 30 17:57:19 2014 From: tony at pyxll.com (Tony Roberts) Date: Tue, 30 Sep 2014 16:57:19 +0100 Subject: [Python.NET] Python.Net for Python3: bug when using an embedded interpreter In-Reply-To: References: Message-ID: Hi Serge, I see what you're saying. It would think that bit just needs to be moved until after we're sure python has been initialized so it works in both cases. I don't see any need to move it into a separate python module; having it compiled into one is cleaner IMHO. If you're not subclassing .NET classes in python and so don't need python methods/properties reflected to .net you won't need it anyway, so you could just remove that bit of code from your fork for now. I'll take a look, but if you get round to it before I do then please feel free to submit a pull request. I'll create an issue in github so it doesn't get forgotten. thanks, Tony On Tue, Sep 30, 2014 at 11:59 AM, Serge WEINSTOCK < serge.weinstock at uk.bnpparibas.com> wrote: > Hi Tony, > > > > You recently made a change which prevents me using the library in an > embedded Python interpreter. > > > > In pythonengine.cs, you ?inject? decorators with the following code: > > > > //================================================================= > > public static void Initialize() { > > ..... > > IntPtr globals = Runtime.PyEval_GetGlobals(); > > PyDict locals = new PyDict(); > > try > > { > > IntPtr builtins = Runtime.PyEval_GetBuiltins(); > > Runtime.PyDict_SetItemString(locals.Handle, "__builtins__", builtins); > > > > var assembly = Assembly.GetExecutingAssembly(); > > using (Stream stream = assembly.GetManifestResourceStream(" > Python.Runtime.resources.clr.py")) > > using (StreamReader reader = new StreamReader(stream)) > > { > > // add the contents of clr.py to the module > > string clr_py = reader.ReadToEnd(); > > PyObject result = RunString(clr_py, globals, locals.Handle); > > if (null == result) > > throw new PythonException(); > > result.Dispose(); > > } > > //================================================================= > > When running from a Python script, it?s fine because the Python > interpreter has already been initialized and global variables have been > defined. > > > > The issue is that when running from an embedded interpreter. As I must > first initialize the Python engine using PythonEngine.Initialize(). > > > > So when we reach this piece of code, there is no script running, no > ?Python context?, so ?PyEval_GetGlobals? returns null and ?RunString? fails. > > > > We could create an artificial global dictionary but it won?t be available > to the rest of the code. > > > > I think it should be best to move this code into a Python module. If run > from a script (PyEval_GetGlobals() != null), you could inject the > decorators with a simple ?from decorators_pythonnet import *?. If run from > an embedded interpreter, we could add a line for running the same code once > the engine has been initialized. > > > > What do you think? > > > > Serge Weinstock > > > > > ___________________________________________________________ > This e-mail may contain confidential and/or privileged information. If you > are not the intended recipient (or have received this e-mail in error) > please notify the sender immediately and delete this e-mail. Any > unauthorised copying, disclosure or distribution of the material in this > e-mail is prohibited. > > Please refer to http://www.bnpparibas.co.uk/en/email-disclaimer/ for > additional disclosures. > > _________________________________________________ > Python.NET mailing list - PythonDotNet at python.org > https://mail.python.org/mailman/listinfo/pythondotnet > -------------- next part -------------- An HTML attachment was scrubbed... URL: From tony at pyxll.com Tue Sep 30 20:15:36 2014 From: tony at pyxll.com (Tony Roberts) Date: Tue, 30 Sep 2014 19:15:36 +0100 Subject: [Python.NET] Python.Net for Python3: bug when using an embedded interpreter In-Reply-To: References: Message-ID: Should be fixed now. Please re-open if it's still a problem ( https://github.com/renshawbay/pythonnet/issues/13). cheers, Tony On Tue, Sep 30, 2014 at 4:57 PM, Tony Roberts wrote: > Hi Serge, > > I see what you're saying. It would think that bit just needs to be moved > until after we're sure python has been initialized so it works in both > cases. I don't see any need to move it into a separate python module; > having it compiled into one is cleaner IMHO. If you're not subclassing .NET > classes in python and so don't need python methods/properties reflected to > .net you won't need it anyway, so you could just remove that bit of code > from your fork for now. > > I'll take a look, but if you get round to it before I do then please feel > free to submit a pull request. I'll create an issue in github so it doesn't > get forgotten. > > thanks, > Tony > > > On Tue, Sep 30, 2014 at 11:59 AM, Serge WEINSTOCK < > serge.weinstock at uk.bnpparibas.com> wrote: > >> Hi Tony, >> >> >> >> You recently made a change which prevents me using the library in an >> embedded Python interpreter. >> >> >> >> In pythonengine.cs, you ?inject? decorators with the following code: >> >> >> >> //================================================================= >> >> public static void Initialize() { >> >> ..... >> >> IntPtr globals = Runtime.PyEval_GetGlobals(); >> >> PyDict locals = new PyDict(); >> >> try >> >> { >> >> IntPtr builtins = Runtime.PyEval_GetBuiltins(); >> >> Runtime.PyDict_SetItemString(locals.Handle, "__builtins__", builtins); >> >> >> >> var assembly = Assembly.GetExecutingAssembly(); >> >> using (Stream stream = assembly.GetManifestResourceStream(" >> Python.Runtime.resources.clr.py")) >> >> using (StreamReader reader = new StreamReader(stream)) >> >> { >> >> // add the contents of clr.py to the module >> >> string clr_py = reader.ReadToEnd(); >> >> PyObject result = RunString(clr_py, globals, locals.Handle); >> >> if (null == result) >> >> throw new PythonException(); >> >> result.Dispose(); >> >> } >> >> //================================================================= >> >> When running from a Python script, it?s fine because the Python >> interpreter has already been initialized and global variables have been >> defined. >> >> >> >> The issue is that when running from an embedded interpreter. As I must >> first initialize the Python engine using PythonEngine.Initialize(). >> >> >> >> So when we reach this piece of code, there is no script running, no >> ?Python context?, so ?PyEval_GetGlobals? returns null and ?RunString? fails. >> >> >> >> We could create an artificial global dictionary but it won?t be available >> to the rest of the code. >> >> >> >> I think it should be best to move this code into a Python module. If run >> from a script (PyEval_GetGlobals() != null), you could inject the >> decorators with a simple ?from decorators_pythonnet import *?. If run from >> an embedded interpreter, we could add a line for running the same code once >> the engine has been initialized. >> >> >> >> What do you think? >> >> >> >> Serge Weinstock >> >> >> >> >> ___________________________________________________________ >> This e-mail may contain confidential and/or privileged information. If >> you are not the intended recipient (or have received this e-mail in error) >> please notify the sender immediately and delete this e-mail. Any >> unauthorised copying, disclosure or distribution of the material in this >> e-mail is prohibited. >> >> Please refer to http://www.bnpparibas.co.uk/en/email-disclaimer/ for >> additional disclosures. >> >> _________________________________________________ >> Python.NET mailing list - PythonDotNet at python.org >> https://mail.python.org/mailman/listinfo/pythondotnet >> > > -------------- next part -------------- An HTML attachment was scrubbed... URL: