Help: 64bit python call c and got OSError: exception: access violation writing 0xFFFFFFFF99222A60

2018-01-22 Thread Jason Qian via Python-list
Hello!

  I am using ctypes on Windows to interface with a dll  and it works fine
on Linux and windows 32-bit python.  But, when using 64-bit python, we got
error "exception: access violation writing 0x99222A60".

Checking our server, it seems work without any problem. but the python
gives an error and stop the application.

-- c --
class myPythonAPI
{
public:
myPythonAPI();
int createService(const char* serviceName){ /* run our application*/};
}
extern "C" {
__declspec(dllexport) myPythonAPI* loadInstance(){ return new
myPythonAPI(); }
__declspec(dllexport) int createService(myPythonAPI* obj, const char*
serviceName) { eturn obj->createService(serviceName);
};

-- python --
from ctypes import *
lib = cdll.LoadLibrary('xxx.dll')

lib.createService.argtypes=[c_int,ctypes.c_char_p]
lib.createService.restype=ctypes.c_int

class myDriver(object):
def init(self):
self.obj = lib.loadInstance()

def create_services(self,servicename):
result=lib.createService(self.obj,servicename)
return result

driver=myDriver()
driver.create_services(b"myExample")

Thanks for the help
Jason
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Help: 64bit python call c and got OSError: exception: access violation writing 0xFFFFFFFF99222A60

2018-01-22 Thread Jason Qian via Python-list
Thanks you very much, fixed the problem :)

On Mon, Jan 22, 2018 at 4:28 PM, Random832  wrote:

> On Mon, Jan 22, 2018, at 16:00, Jason Qian via Python-list wrote:
> > Hello!
> >
> >   I am using ctypes on Windows to interface with a dll  and it works fine
> > on Linux and windows 32-bit python.  But, when using 64-bit python, we
> got
> > error "exception: access violation writing 0x99222A60".
>
> You are treating the obj type (myPythonAPI *) as c_int, which is only 32
> bits. You should be using a pointer type instead (ideally you should be
> using void * and c_void_p, so Python doesn't need the class definition.)
> Don't forget to set lib.loadInstance.restype as well.
>
> > __declspec(dllexport) myPythonAPI* loadInstance(){ return new
> > myPythonAPI(); }
> > __declspec(dllexport) int createService(myPythonAPI* obj, const char*
> > serviceName) { eturn obj->createService(serviceName);
>
> > lib = cdll.LoadLibrary('xxx.dll')
> >
> > lib.createService.argtypes=[c_int,ctypes.c_char_p]
> > lib.createService.restype=ctypes.c_int
> >
> > class myDriver(object):
> > def init(self):
> > self.obj = lib.loadInstance()
> --
> https://mail.python.org/mailman/listinfo/python-list
>
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Help: 64bit python call c and got OSError: exception: access violation writing 0xFFFFFFFF99222A60

2018-01-22 Thread Jason Qian via Python-list
Thanks for the help,

Jason

On Mon, Jan 22, 2018 at 5:41 PM, eryk sun  wrote:

> On Mon, Jan 22, 2018 at 9:00 PM, Jason Qian via Python-list
>  wrote:
> >
> > I am using ctypes on Windows to interface with a dll  and it works fine
> > on Linux and windows 32-bit python.  But, when using 64-bit python, we
> got
> > error "exception: access violation writing 0x99222A60".
> >
> > from ctypes import *
>
> Try to avoid * imports if it's a public module.
>
> > lib = cdll.LoadLibrary('xxx.dll')
>
> Just use CDLL directly. cdll.LoadLibrary is pointless, and
> `cdll.xxx` is problematic in some cases because it caches CDLL
> instances, which cache function pointers. Also, the ".dll" filename
> extension isn't required in Windows.
>
> > lib.createService.argtypes=[c_int,ctypes.c_char_p]
> > lib.createService.restype=ctypes.c_int
> >
> > class myDriver(object):
> > def init(self):
> > self.obj = lib.loadInstance()
>
> Since you didn't set loadInstance.restype, the result gets truncated
> as a C int, the default result type.
>
> I recommend defining an opaque ctypes struct (i.e. no defined fields)
> for the C++ class. This provides type safety. Staying strict and
> literal on types is more work than using a `void *` everywhere, but it
> pays off in terms of easier debugging and more resilient code. A
> simple example is that ctypes returns a `void *` result (or gets a
> struct field or array item) as a Python integer. Then for any FFI call
> that you may have forgotten to define argtypes, ctypes will default to
> truncating this integer value as a C int. At best that causes an
> immediate crash. At worst it's a subtle bug that corrupts data.
>
> Here's an example implementation with an opaque struct:
>
> import ctypes
>
> lib = ctypes.CDLL('xxx')
>
> class myPythonAPI(ctypes.Structure):
> pass
>
> PmyPythonAPI = ctypes.POINTER(myPythonAPI)
>
> lib.loadInstance.restype = PmyPythonAPI
> lib.loadInstance.argtypes = ()
>
> lib.createService.restype = ctypes.c_int
> lib.createService.argtypes = (PmyPythonAPI, ctypes.c_char_p)
>
> class myDriver(object):
>
> def init(self):
> self.obj = lib.loadInstance()
>
> def create_services(self, servicename):
> return lib.createService(self.obj, servicename)
>
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Help: 64bit python call c and got OSError: exception: access violation writing 0xFFFFFFFF99222A60

2018-01-24 Thread Jason Qian via Python-list
Again, thanks for the help. Everything is working fine after the changes.

Here is one more new issue needs some help.

On c side,

   The createService function can pass a  callback handler as second
parameter.
   Without callback handler, it works fine. But if we add the callback
handler,  the application will give a exception due to the pointer of
callback handler = NULL;

   Not sure, why the callback handler missed up, when the app calling from
python.

Thanks

-- python

lib.createService.argtypes=[ctypes.c_void_p,ctypes.c_char_p]
lib.createService.restype=ctypes.c_int

def create_services(self,servicename):
result=lib.createService(self.obj,servicename)
return result

--c --

__declspec(dllexport) int createService(void* obj, const char* serviceName)
{
 return ((myPythonAPI*)obj)->createService(serviceName);
}

int myPythonAPI::createService(const char* serviceName)
{
 //case 1 :
 //This works fine
 createService(methodname);

//case 2
//This will not working,
 InvocationCallback serviceCallback;
createService(methodname, &serviceCallback);
}











On Mon, Jan 22, 2018 at 5:58 PM, Jason Qian  wrote:

> Thanks you very much, fixed the problem :)
>
> On Mon, Jan 22, 2018 at 4:28 PM, Random832  wrote:
>
>> On Mon, Jan 22, 2018, at 16:00, Jason Qian via Python-list wrote:
>> > Hello!
>> >
>> >   I am using ctypes on Windows to interface with a dll  and it works
>> fine
>> > on Linux and windows 32-bit python.  But, when using 64-bit python, we
>> got
>> > error "exception: access violation writing 0x99222A60".
>>
>> You are treating the obj type (myPythonAPI *) as c_int, which is only 32
>> bits. You should be using a pointer type instead (ideally you should be
>> using void * and c_void_p, so Python doesn't need the class definition.)
>> Don't forget to set lib.loadInstance.restype as well.
>>
>> > __declspec(dllexport) myPythonAPI* loadInstance(){ return new
>> > myPythonAPI(); }
>> > __declspec(dllexport) int createService(myPythonAPI* obj, const char*
>> > serviceName) { eturn obj->createService(serviceName);
>>
>> > lib = cdll.LoadLibrary('xxx.dll')
>> >
>> > lib.createService.argtypes=[c_int,ctypes.c_char_p]
>> > lib.createService.restype=ctypes.c_int
>> >
>> > class myDriver(object):
>> > def init(self):
>> > self.obj = lib.loadInstance()
>> --
>> https://mail.python.org/mailman/listinfo/python-list
>>
>
>
-- 
https://mail.python.org/mailman/listinfo/python-list


Python call c pass a callback function on Linux

2018-01-24 Thread Jason Qian via Python-list
Hi,

  I have following code that works fine on windows.

InvocationCB=WINFUNCTYPE(None, c_char_p, c_int)
submit = lib.submit
submit.argtypes = [ctypes.c_void_p, c_void_p,InvocationCB]
submit.restype = ctypes.c_int

def handleResponse(message, code):
  print('--- handleResponse ---')
  print(message)
  print(code)

class GSPythonDriver(object):
  def submif(self,methodname)
invm_fn = InvocationCB(handleResponse)
lib.submit(self.obj,methodname,invm_fn)

 How can I do this on the Linux ?


Thanks for the help
Jason
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python call c pass a callback function on Linux

2018-01-24 Thread Jason Qian via Python-list
HI Dennis,

Thanks for the help, After changing  WINFUNCTYPE to CFUNCTYPE, the call
back function  works on the Linux :)

Thanks again,

Jason






On Wed, Jan 24, 2018 at 6:15 PM, Dennis Lee Bieber 
wrote:

> On Wed, 24 Jan 2018 17:16:22 -0500, Jason Qian via Python-list
>  declaimed the following:
>
> >Hi,
> >
> >  I have following code that works fine on windows.
> >
> You have not provided a minimal functional example...
>
> >InvocationCB=WINFUNCTYPE(None, c_char_p, c_int)
> >submit = lib.submit
>
> Where did lib.submit come from? No import statements are shown.
>
> >submit.argtypes = [ctypes.c_void_p, c_void_p,InvocationCB]
> >submit.restype = ctypes.c_int
> >
>
> You are setting things on the name submit yet...
>
> >def handleResponse(message, code):
> >  print('--- handleResponse ---')
> >  print(message)
> >  print(code)
> >
> >class GSPythonDriver(object):
> >  def submif(self,methodname)
>
> Is that a typo for submit?
>
> >invm_fn = InvocationCB(handleResponse)
> >lib.submit(self.obj,methodname,invm_fn)
>
> ... down here you are referring back to the full lib.submit (which may be
> the same object)
>
>
> No example instance of GSPythonDriver is created, and thereby
> nothing
> defined within it is invoked...
>
> However, the one thing that stands out is that WINFUNCTYPE is
> Windows
> "stdcall" specific, and ctypes defines CFUNCTYPE for the more global C
> calling conventions. But from there? You have to specify the proper library
> containing the functions you are invoking... Is that library (or
> equivalent) even available on your proposed target OS?
>
>
> --
> Wulfraed Dennis Lee Bieber AF6VN
> wlfr...@ix.netcom.comHTTP://wlfraed.home.netcom.com/
>
> --
> https://mail.python.org/mailman/listinfo/python-list
>
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Help: 64bit python call c and got OSError: exception: access violation writing 0xFFFFFFFF99222A60

2018-01-24 Thread Jason Qian via Python-list
Figured it out,

Thanks

On Wed, Jan 24, 2018 at 4:25 PM, Jason Qian  wrote:

> Again, thanks for the help. Everything is working fine after the changes.
>
> Here is one more new issue needs some help.
>
> On c side,
>
>The createService function can pass a  callback handler as second
> parameter.
>Without callback handler, it works fine. But if we add the callback
> handler,  the application will give a exception due to the pointer of
> callback handler = NULL;
>
>Not sure, why the callback handler missed up, when the app calling from
> python.
>
> Thanks
>
> -- python
>
> lib.createService.argtypes=[ctypes.c_void_p,ctypes.c_char_p]
> lib.createService.restype=ctypes.c_int
>
> def create_services(self,servicename):
> result=lib.createService(self.obj,servicename)
> return result
>
> --c --
>
> __declspec(dllexport) int createService(void* obj, const char*
> serviceName)
> {
>  return ((myPythonAPI*)obj)->createService(serviceName);
> }
>
> int myPythonAPI::createService(const char* serviceName)
> {
>  //case 1 :
>  //This works fine
>  createService(methodname);
>
> //case 2
> //This will not working,
>  InvocationCallback serviceCallback;
> createService(methodname, &serviceCallback);
> }
>
>
>
>
>
>
>
>
>
>
>
> On Mon, Jan 22, 2018 at 5:58 PM, Jason Qian  wrote:
>
>> Thanks you very much, fixed the problem :)
>>
>> On Mon, Jan 22, 2018 at 4:28 PM, Random832 
>> wrote:
>>
>>> On Mon, Jan 22, 2018, at 16:00, Jason Qian via Python-list wrote:
>>> > Hello!
>>> >
>>> >   I am using ctypes on Windows to interface with a dll  and it works
>>> fine
>>> > on Linux and windows 32-bit python.  But, when using 64-bit python, we
>>> got
>>> > error "exception: access violation writing 0x99222A60".
>>>
>>> You are treating the obj type (myPythonAPI *) as c_int, which is only 32
>>> bits. You should be using a pointer type instead (ideally you should be
>>> using void * and c_void_p, so Python doesn't need the class definition.)
>>> Don't forget to set lib.loadInstance.restype as well.
>>>
>>> > __declspec(dllexport) myPythonAPI* loadInstance(){ return new
>>> > myPythonAPI(); }
>>> > __declspec(dllexport) int createService(myPythonAPI* obj, const char*
>>> > serviceName) { eturn obj->createService(serviceName);
>>>
>>> > lib = cdll.LoadLibrary('xxx.dll')
>>> >
>>> > lib.createService.argtypes=[c_int,ctypes.c_char_p]
>>> > lib.createService.restype=ctypes.c_int
>>> >
>>> > class myDriver(object):
>>> > def init(self):
>>> > self.obj = lib.loadInstance()
>>> --
>>> https://mail.python.org/mailman/listinfo/python-list
>>>
>>
>>
>
-- 
https://mail.python.org/mailman/listinfo/python-list


Please help on print string that contains 'tab' and 'newline'

2018-01-27 Thread Jason Qian via Python-list
HI

   I am a string that contains \r\n\t

   [Ljava.lang.Object; does not exist*\r\n\t*at com.livecluster.core.tasklet


   I would like it print as :

[Ljava.lang.Object; does not exist
  tat com.livecluster.core.tasklet
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Please help on print string that contains 'tab' and 'newline'

2018-01-27 Thread Jason Qian via Python-list
HI

   I have a string that contains \r\n\t

   [Ljava.lang.Object; does not exist*\r\n\t*at com.livecluster.core.tasklet


   I would like to  print it as :

[Ljava.lang.Object; does not exist
 tat com.livecluster.core.tasklet

  How can I do this in python print ?


Thanks

On Sat, Jan 27, 2018 at 3:15 PM, Jason Qian  wrote:

> HI
>
>I am a string that contains \r\n\t
>
>[Ljava.lang.Object; does not exist*\r\n\t*at
> com.livecluster.core.tasklet
>
>
>I would like it print as :
>
> [Ljava.lang.Object; does not exist
>   tat com.livecluster.core.tasklet
>
>
>
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Please help on print string that contains 'tab' and 'newline'

2018-01-27 Thread Jason Qian via Python-list
Thanks for taking look this.

1. Python pass a function to c side as callback, and print out the message.

def handleError(message, code):
 print('** handleError **')
* print('exception ' + str(message))*

2. On c side : send stack trace back to python by calling the callback
function

Callback::Callback(InvocationER rcb)
:
_rcb(rcb)
{
}
void Callback::handleError(Exception &e, int taskId) {
 *(_rcb)((char*)e.getStackTrace().c_str(), taskId);*
}


So, the source of the string is std::string. On the python side is byte
array.

  Ljava.lang.Object; does not exist*\r\n\t*at com

Thanks




On Sat, Jan 27, 2018 at 4:20 PM, Ned Batchelder 
wrote:

> On 1/27/18 3:15 PM, Jason Qian via Python-list wrote:
>
>> HI
>>
>> I am a string that contains \r\n\t
>>
>> [Ljava.lang.Object; does not exist*\r\n\t*at
>> com.livecluster.core.tasklet
>>
>>
>> I would like it print as :
>>
>> [Ljava.lang.Object; does not exist
>>tat com.livecluster.core.tasklet
>>
>
> It looks like you are doing something that is a little bit, and perhaps a
> lot, more complicated than printing a string.  Can you share the code that
> is trying to produce that output?
>
> --Ned.
> --
> https://mail.python.org/mailman/listinfo/python-list
>
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Please help on print string that contains 'tab' and 'newline'

2018-01-27 Thread Jason Qian via Python-list
Thanks for taking look this.


The source of the string is std::string from our c code as callback .
On the python side is shows as bytes.

Is there way we can reformat the string that replace \r\n with newline, so
python can correctly print it ?

Thanks

On Sat, Jan 27, 2018 at 5:39 PM, Terry Reedy  wrote:

> On 1/27/2018 3:15 PM, Jason Qian via Python-list wrote:
>
>> HI
>>
>> I am a string that contains \r\n\t
>>
>> [Ljava.lang.Object; does not exist*\r\n\t*at
>> com.livecluster.core.tasklet
>>
>>
>> I would like it print as :
>>
>> [Ljava.lang.Object; does not exist
>>tat com.livecluster.core.tasklet
>>
>
> Your output does not match the input.  Don't add, or else remove, the *s
> if you don't want to see them.  Having '\t' print as '  t' makes no sense.
>
> In IDLE
>
> >>> print('[Ljava.lang.Object; does not exist*\r\n\t*at
> com.livecluster.core.tasklet')
> [Ljava.lang.Object; does not exist*
> *at com.livecluster.core.tasklet
>
> IDLE ignores \r, other display mechanisms may not.  You generally should
> not use it.
>
> Pasting the text, with the literal newline embedded, does not work in
> Windows interactive interpreter, so not testing this there.
>
>
> --
> Terry Jan Reedy
>
> --
> https://mail.python.org/mailman/listinfo/python-list
>
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Please help on print string that contains 'tab' and 'newline'

2018-01-27 Thread Jason Qian via Python-list
there are 0D 0A 09

%c %d  116


*%c %d  13%c %d  10%c %d
 9*
%c %d  97


On Sat, Jan 27, 2018 at 9:05 PM, Dennis Lee Bieber 
wrote:

> On Sat, 27 Jan 2018 20:33:58 -0500, Jason Qian via Python-list
>  declaimed the following:
>
> >  Ljava.lang.Object; does not exist*\r\n\t*at com
> >
>
> Does that source contain
>
> 0x0d 0x0a 0x09
>   
>
> or is it really
>
> 0x5c 0x72 0x5c 0x61 0x5c 0x74
> \r\n\t
>
> as individual characters?
>
>
> >>> bks = chr(0x5c)
> >>> ar = "r"
> >>> en = "n"
> >>> te = "t"
> >>>
> >>> strn = "".join([bks, ar, bks, en, bks, te])
> >>> strn
> '\\r\\n\\t'
> >>> print strn
> \r\n\t
> >>> cstr = "\r\n\t"
> >>> cstr
> '\r\n\t'
> >>> print cstr
>
>
> >>>
>
> --
> Wulfraed Dennis Lee Bieber AF6VN
> wlfr...@ix.netcom.comHTTP://wlfraed.home.netcom.com/
>
> --
> https://mail.python.org/mailman/listinfo/python-list
>
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Please help on print string that contains 'tab' and 'newline'

2018-01-28 Thread Jason Qian via Python-list
print(repr(message))  out :

*does not exist\r\n\tat com.*


for ch in message:
  printf("%d %c",ch, chr(ch))


%d %c 110 n
%d %c 111 o
%d %c 116 t
%d %c 32
%d %c 101 e
%d %c 120 x
%d %c 105 i
%d %c 115 s
%d %c 116 t

*%d %c 13%d %c 10*
*%d %c 9*
%d %c 97 a
%d %c 116 t
%d %c 32
%d %c 99 c
%d %c 111 o
%d %c 109 m





On Sun, Jan 28, 2018 at 9:50 AM, Steven D'Aprano <
steve+comp.lang.pyt...@pearwood.info> wrote:

> On Sat, 27 Jan 2018 21:23:02 -0500, Jason Qian via Python-list wrote:
>
> > there are 0D 0A 09
>
> If your string actually contains CARRIAGE RETURN (OD) NEWLINE (OA), and
> TAB (09) characters, then you don't need to do anything. Just call print,
> and they will be printed correctly.
>
> If that doesn't work, then your input string doesn't contain what you
> think it contains. Please call this:
>
> print(repr(the_string))
>
> and COPY AND PASTE the output here so we can see it.
>
>
>
> --
> Steve
>
> --
> https://mail.python.org/mailman/listinfo/python-list
>
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Please help on print string that contains 'tab' and 'newline'

2018-01-28 Thread Jason Qian via Python-list
Thanks a lot :)

os.write(1, message)  works !

On Sun, Jan 28, 2018 at 8:04 PM, Dan Stromberg  wrote:

> How about:
> >>> os.write(1, message)
>
> On Sun, Jan 28, 2018 at 4:51 PM, Jason Qian via Python-list
>  wrote:
> > print(repr(message))  out :
> >
> > *does not exist\r\n\tat com.*
> >
> >
> > for ch in message:
> >   printf("%d %c",ch, chr(ch))
> >
> >
> > %d %c 110 n
> > %d %c 111 o
> > %d %c 116 t
> > %d %c 32
> > %d %c 101 e
> > %d %c 120 x
> > %d %c 105 i
> > %d %c 115 s
> > %d %c 116 t
> >
> > *%d %c 13%d %c 10*
> > *%d %c 9*
> > %d %c 97 a
> > %d %c 116 t
> > %d %c 32
> > %d %c 99 c
> > %d %c 111 o
> > %d %c 109 m
>
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Please help on print string that contains 'tab' and 'newline'

2018-01-28 Thread Jason Qian via Python-list
Thanks Peter,

  replace print with os.write  fixed the problem.



On Sun, Jan 28, 2018 at 3:57 AM, Peter Otten <__pete...@web.de> wrote:

> Jason Qian via Python-list wrote:
>
> > HI
> >
> >I have a string that contains \r\n\t
> >
> >[Ljava.lang.Object; does not exist*\r\n\t*at
> >[com.livecluster.core.tasklet
> >
> >
> >I would like to  print it as :
> >
> > [Ljava.lang.Object; does not exist
> >  tat com.livecluster.core.tasklet
> >
> >   How can I do this in python print ?
>
> Assuming the string contains the escape sequences rather than an actual
> TAB, CR or NL you can apply codecs.decode():
>
> >>> s = r"[Ljava.lang.Object; does not exist\r\n\tat
> com.livecluster.core.tasklet"
> >>> print(s)
> [Ljava.lang.Object; does not exist\r\n\tat com.livecluster.core.tasklet
> >>> import codecs
> >>> print(codecs.decode(s, "unicode-escape"))
> [Ljava.lang.Object; does not exist
> at com.livecluster.core.tasklet
>
> Note that this will decode all escape sequences that may occur in a string
> literal
> in Python, e. g.
>
> >>> codecs.decode(r"\x09 \u03c0 \N{soft ice cream}", "unicode-escape")
> '\t π 🍦'
>
> and will complain when the string is not a valid Python string literal:
>
> >>> codecs.decode(r"\x", "unicode-escape")
> Traceback (most recent call last):
>   File "", line 1, in 
> UnicodeDecodeError: 'unicodeescape' codec can't decode bytes in position
> 0-1: truncated \xXX escape
>
> If you need more control you can build your own conversion routine:
>
> import re
>
> lookup = {
> "r": "\r",
> "n": "\n",
> "t": "\t",
> }
>
> def substitute(match):
> group = match.group(1)
> return lookup.get(group, group)
>
> def decode(s):
> return re.sub(r"\\(.)", substitute, s)
>
> s = decode("alpha\\n \\xomega")
> print(s)
> print(repr(s))
>
>
> --
> https://mail.python.org/mailman/listinfo/python-list
>
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Please help on print string that contains 'tab' and 'newline'

2018-01-28 Thread Jason Qian via Python-list
The message type is bytes,  this may make different ?

 print(type(message))



On Sun, Jan 28, 2018 at 8:41 PM, Steven D'Aprano <
steve+comp.lang.pyt...@pearwood.info> wrote:

> On Sun, 28 Jan 2018 20:31:39 -0500, Jason Qian via Python-list wrote:
>
> > Thanks a lot :)
> >
> > os.write(1, message)  works !
>
> I still do not believe that print(message) doesn't work. I can see no
> reason why you need to use os.write().
>
>
>
>
> --
> Steve
>
> --
> https://mail.python.org/mailman/listinfo/python-list
>
-- 
https://mail.python.org/mailman/listinfo/python-list


Help on convert PyObject to string (c) Python 3.6

2018-02-04 Thread Jason Qian via Python-list
Hi,

   This is the case of calling python from c and the python function  will
return a string.

   It seems python been called correctly, but got error when convert the
python string to c string.

-- c --

   PyObject* pValue = PyObject_CallObject(pFunc, pArgs);


-- python --

import string, random
def StringGen(argv):
out_string_size=int(argv);
output_data=''.join(random.choice(string.ascii_lowercase) for x in
range(out_string_size))
return output_data


I try:

 PyObject* pValue = PyObject_CallObject(pFunc, pArgs);
const char* sp = 0;
if (!PyArg_ParseTuple(pValue, "s", &sp)) {
}

and got

"SystemError: new style getargs format but argument is not a tuple"



Thanks for the help
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Help on convert PyObject to string (c) Python 3.6

2018-02-04 Thread Jason Qian via Python-list
Hi Chris,

   Thanks a lot ! Using PyUnicode_DecodeUTF8 fix the problem.



On Sun, Feb 4, 2018 at 12:02 PM, Chris Angelico  wrote:

> On Mon, Feb 5, 2018 at 3:52 AM, Jason Qian via Python-list
>  wrote:
> > Hi,
> >
> >This is the case of calling python from c and the python function
> will
> > return a string.
> >
> >It seems python been called correctly, but got error when convert the
> > python string to c string.
> >
> > -- c --
> >
> >PyObject* pValue = PyObject_CallObject(pFunc, pArgs);
> >
> >
> > -- python --
> >
> > import string, random
> > def StringGen(argv):
> > out_string_size=int(argv);
> > output_data=''.join(random.choice(string.ascii_lowercase) for x in
> > range(out_string_size))
> > return output_data
> >
> >
> > I try:
> >
> >  PyObject* pValue = PyObject_CallObject(pFunc, pArgs);
> > const char* sp = 0;
> > if (!PyArg_ParseTuple(pValue, "s", &sp)) {
> > }
> >
> > and got
> >
> > "SystemError: new style getargs format but argument is not a tuple"
> >
>
> You're using something that is specifically for parsing *arguments*
> (as indicated by the "PyArg" prefix). If you want to get a 'const char
> *' from a Python string, you probably want to encode it UTF-8. There's
> a convenience function for that:
>
> https://docs.python.org/3/c-api/unicode.html#c.PyUnicode_AsUTF8AndSize
>
> And if you know for sure that there won't be any \0 in the string, you
> can use the next function in the docs, which doesn't bother returning
> the size. (It'll always be null-terminated.)
>
> ChrisA
> --
> https://mail.python.org/mailman/listinfo/python-list
>
-- 
https://mail.python.org/mailman/listinfo/python-list


Help on PyImport_Import(pNAme)

2018-02-04 Thread Jason Qian via Python-list
Hi,

  This only works when loading  modules from the current directory.
  Is there a way I can load from somewhere else ?

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


Re: Help on convert PyObject to string (c) Python 3.6

2018-02-19 Thread Jason Qian via Python-list
Thanks a lot and I will take a look Cython,

On Mon, Feb 19, 2018 at 3:23 PM, Stefan Behnel  wrote:

> Jason Qian via Python-list schrieb am 04.02.2018 um 17:52:
> >This is the case of calling python from c and the python function
> will
> > return a string.
>
> Hi Jason,
>
> I noticed that you ran into a couple of problems using the C-API, so I
> suggest looking into Cython instead. It basically translates Python to C
> and supports optional static usage of C and C++ data types, so you get all
> the native code interaction *and* all the Python interaction and features,
> without having to go through the hassle of learning how the Python
> internals work (and why they don't work for you). The code it generates is
> probably also faster and safer than what you are currently writing (no
> offence, just experience from reading and writing a lot of such code).
>
> http://cython.org/
>
> Stefan
>
> --
> https://mail.python.org/mailman/listinfo/python-list
>
-- 
https://mail.python.org/mailman/listinfo/python-list


How to link to python 3.6.4 library on linux ?

2018-02-19 Thread Jason Qian via Python-list
 Hi,

  I am calling python from a  c application.
  It compiles and works fine on the windows. How do I compile and link
it on the linux for Python 3.6.4  ?

  Under python dir, it only have a static library,

   /opt/Python-3.6.4*/lib*/*libpython3.6m.a*

* If I link to it, I got:*

*/opt/Python-3.6.4/lib/libpython3.6m.a(abstract.o): relocation R_X86_64_32S
against `_Py_NotImplementedStruct' can not be used when making a shared
object; recompile with -fPIC*
*/opt/Python-3.6.4/lib/libpython3.6m.a: could not read symbols: Bad value*


*Thanks for the help,*
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to link to python 3.6.4 library on linux ?

2018-02-19 Thread Jason Qian via Python-list
Thanks  Chris,

I think I figured it out that when build python on Linux, we need to
enable-shared.

Thanks again,

On Mon, Feb 19, 2018 at 5:04 PM, Chris Angelico  wrote:

> On Tue, Feb 20, 2018 at 8:07 AM, Jason Qian via Python-list
>  wrote:
> >  Hi,
> >
> >   I am calling python from a  c application.
> >   It compiles and works fine on the windows. How do I compile and
> link
> > it on the linux for Python 3.6.4  ?
> >
> >   Under python dir, it only have a static library,
> >
> >/opt/Python-3.6.4*/lib*/*libpython3.6m.a*
> >
> > * If I link to it, I got:*
> >
> > */opt/Python-3.6.4/lib/libpython3.6m.a(abstract.o): relocation
> R_X86_64_32S
> > against `_Py_NotImplementedStruct' can not be used when making a shared
> > object; recompile with -fPIC*
> > */opt/Python-3.6.4/lib/libpython3.6m.a: could not read symbols: Bad
> value*
> >
>
> By "calling Python from C", do you mean what the docs call "embedding"?
>
> https://docs.python.org/3/extending/embedding.html
>
> I would recommend following the tutorial there, if you haven't
> already. If you have, and the tutorial code doesn't work for you, post
> your code and what you did, and we'll try to help with that.
>
> ChrisA
> --
> https://mail.python.org/mailman/listinfo/python-list
>
-- 
https://mail.python.org/mailman/listinfo/python-list


Help on ctypes.POINTER for Python array

2023-05-11 Thread Jason Qian via Python-list
Hi,

Need some help,

in the Python, I have a array of string

 var_array=["Opt1=DG","Opt1=DG2"]

I need to call c library and  pass var_array as parameter

In the   argtypes,  how do I set up ctypes.POINTER(???)  for var_array?

func.argtypes=[ctypes.c_void_p,ctypes.c_int, ctypes.POINTER()]

In the c code:

int  func (void* obj, int index,  char** opt)

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


Re: Help on ctypes.POINTER for Python array

2023-05-11 Thread Jason Qian via Python-list
Awesome, thanks!

On Thu, May 11, 2023 at 1:47 PM Eryk Sun  wrote:

> On 5/11/23, Jason Qian via Python-list  wrote:
> >
> > in the Python, I have a array of string
> > var_array=["Opt1=DG","Opt1=DG2"]
> > I need to call c library and  pass var_array as parameter
> > In the   argtypes,  how do I set up ctypes.POINTER(???)  for var_array?
> >
> > func.argtypes=[ctypes.c_void_p,ctypes.c_int, ctypes.POINTER()]
> >
> > In the c code:
> > int  func (void* obj, int index,  char** opt)
>
> The argument type is ctypes.POINTER(ctypes.c_char_p), but that's not
> sufficient. It doesn't implement converting a list of str objects into
> an array of c_char_p pointers that reference byte strings. You could
> write a wrapper function that implements the conversion before calling
> func(), or you could set the argument type to a custom subclass of
> ctypes.POINTER(ctypes.c_char_p) that implements the conversion via the
> from_param() class method.
>
> https://docs.python.org/3/library/ctypes.html#ctypes._CData.from_param
>
> Here's an example of the latter.
>
> C library:
>
> #include 
>
> int
> func(void *obj, int index, char **opt)
> {
> int length;
> for (length=0; opt[length]; length++);
> if (index < 0 || index >= length) {
> return -1;
> }
> return printf("%s\n", opt[index]);
> }
>
>
> Python:
>
> import os
> import ctypes
>
> lib = ctypes.CDLL('./lib.so')
> BaseOptions = ctypes.POINTER(ctypes.c_char_p)
>
> class Options(BaseOptions):
> @classmethod
> def from_param(cls, param):
> if isinstance(param, list):
> new_param = (ctypes.c_char_p * (len(param) + 1))()
> for i, p in enumerate(param):
> new_param[i] = os.fsencode(p)
> param = new_param
> return BaseOptions.from_param(param)
>
> lib.func.argtypes = (ctypes.c_void_p, ctypes.c_int, Options)
>
>
> demo:
>
> >>> opts = ['Opt1=DG', 'Opt1=DG2']
> >>> lib.func(None, 0, opts)
> Opt1=DG
> 8
> >>> lib.func(None, 1, opts)
> Opt1=DG2
> 9
>
-- 
https://mail.python.org/mailman/listinfo/python-list


Help on ImportError('Error: Reinit is forbidden')

2023-05-17 Thread Jason Qian via Python-list
 Hi,

   I Need some of your help.

 I have the following C code to import *Import python.*   It works 99% of
the time, but sometimes  receives  "*ImportError('Error: Reinit is
forbidden')*". error.
 **We run multiple instances of the app parallelly.

*** Python version(3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:59:51) [MSC
v.1914 64 bit (AMD64)]

PyObject * importPythonModule(const char* pmodName)
{
const char* errors = NULL;
 int nlen = strlen(pmodName);
 PyObject *pName = PyUnicode_DecodeUTF8(pmodName, nlen, errors);
 PyObject *pModule = *PyImport_Import*(pName);
 Py_DECREF(pName);
 if (pModule == NULL) {
 if (*PyErr_Occurred*()) {
handleError("PyImport_Import()");
  }
   }
}
void handleError(const char* msg)
{
  ...
  "PyImport_Import() - ImportError('Error: Reinit is forbidden')"
}


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


Re: Help on ImportError('Error: Reinit is forbidden')

2023-05-18 Thread Jason Qian via Python-list
Hi Barry,

void handleError(const char* msg)
{
...
PyErr_Fetch(&pyExcType, &pyExcValue, &pyExcTraceback);
PyErr_NormalizeException(&pyExcType, &pyExcValue, &pyExcTraceback);

PyObject* str_value = PyObject_Repr(pyExcValue);
PyObject* pyExcValueStr = PyUnicode_AsEncodedString(str_value, "utf-8",
"Error ~");
const char **strErrValue* = PyBytes_AS_STRING(pyExcValueStr);

//where   *strErrValue*   = "ImportError('Error: Reinit is forbidden')"
...
}

What we imported is a Python file which import some pyd libraries.


Thanks
Jason


On Thu, May 18, 2023 at 3:53 AM Barry  wrote:

>
>
> > On 17 May 2023, at 20:35, Jason Qian via Python-list <
> python-list@python.org> wrote:
> >
> >  Hi,
> >
> >   I Need some of your help.
> >
> > I have the following C code to import *Import python.*   It works 99% of
> > the time, but sometimes  receives  "*ImportError('Error: Reinit is
> > forbidden')*". error.
> > **We run multiple instances of the app parallelly.
> >
> > *** Python version(3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:59:51) [MSC
> > v.1914 64 bit (AMD64)]
> >
> > PyObject * importPythonModule(const char* pmodName)
> > {
> >const char* errors = NULL;
> > int nlen = strlen(pmodName);
> > PyObject *pName = PyUnicode_DecodeUTF8(pmodName, nlen, errors);
> > PyObject *pModule = *PyImport_Import*(pName);
> > Py_DECREF(pName);
> > if (pModule == NULL) {
> > if (*PyErr_Occurred*()) {
> >handleError("PyImport_Import()");
> >  }
> >   }
> > }
> > void handleError(const char* msg)
> > {
> >  ...
> >  "PyImport_Import() - ImportError('Error: Reinit is forbidden')"
> > }
>
> You do not seem to printing out msg, you have assumed it means reinit it
> seems.
> What does msg contain when it fails?
>
> Barry
> >
> >
> > Thanks
> > Jason
> > --
> > https://mail.python.org/mailman/listinfo/python-list
> >
>
>
-- 
https://mail.python.org/mailman/listinfo/python-list


ModuleNotFoundError: No module named 'encodings'

2018-09-06 Thread Jason Qian via Python-list
Hi

Need some help.

I have a C++ application that invokes Python.

...
Py_SetPythonHome("python_path");
Py_Initialize();

This works fine on Python 3.6.4 version, but got errors on Python 3.7.0
when calling Py_Initialize(),

Fatal Python error: initfsencoding: unable to load the file system codec
ModuleNotFoundError: No module named 'encodings'


Thanks for the help
Jason
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: ModuleNotFoundError: No module named 'encodings'

2018-09-07 Thread Jason Qian via Python-list
Thanks Thomas,

You are right, this seems the Python home configuration issue.

One more question.

Is there a way I can catch the error ( Fatal Python error: initfsencoding:
..) as exception in the c code ?

try{
Py_Initialize();
}catch(xxx)
{

}


Thanks



On Thu, Sep 6, 2018 at 5:29 PM, Thomas Jollans  wrote:

> On 09/06/2018 09:46 PM, Jason Qian via Python-list wrote:
>
>> Hi
>>
>> Need some help.
>>
>> I have a C++ application that invokes Python.
>>
>> ...
>> Py_SetPythonHome("python_path");
>>
>
> This isn't actually a line in your code, is it? For one thing,
> Py_SetPythonHome expects a wchar_t*...
>
> Py_Initialize();
>>
>> This works fine on Python 3.6.4 version, but got errors on Python 3.7.0
>> when calling Py_Initialize(),
>>
>> Fatal Python error: initfsencoding: unable to load the file system codec
>> ModuleNotFoundError: No module named 'encodings'
>>
>
> So, Python can't find a core module. This either means your Python 3.7
> build is broken, or it doesn't know where to look. Perhaps whatever it is
> you're actually passing to Py_SetPythonHome needs to be changed to point to
> the right place? (i.e. maybe you're giving it the location of the Python
> 3.6 library rather than the Python 3.7 one)
>
> -- Thomas
> --
> https://mail.python.org/mailman/listinfo/python-list
>
-- 
https://mail.python.org/mailman/listinfo/python-list


Help on PyList 3.7.0

2018-09-13 Thread Jason Qian via Python-list
Hey,

Need some help on PyList.


#get path
PyObject *path = PyObject_GetAttrString(sys, "path");

#new user path
PyObject* newPath = PyUnicode_DecodeUTF8(userPath, strlen( userPath ),
errors);

#append  newPath to path
PyList_Append(path, newPath);

How to check if the newPath is already in the path ?

So, If the path contains the newPath, I will not append the newpath.


Thanks for help
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Help on PyList 3.7.0

2018-09-14 Thread Jason Qian via Python-list
Thanks a lot.

On Thu, Sep 13, 2018 at 5:24 PM, MRAB  wrote:

> On 2018-09-13 21:50, Jason Qian via Python-list wrote:
>
>> Hey,
>>
>> Need some help on PyList.
>>
>>
>> #get path
>> PyObject *path = PyObject_GetAttrString(sys, "path");
>>
>> #new user path
>> PyObject* newPath = PyUnicode_DecodeUTF8(userPath, strlen( userPath ),
>> errors);
>>
>> #append  newPath to path
>> PyList_Append(path, newPath);
>>
>> How to check if the newPath is already in the path ?
>>
>> So, If the path contains the newPath, I will not append the newpath.
>>
>>
>> Thanks for help
>>
>> A list is a sequence. Use PySequence_Contains.
> --
> https://mail.python.org/mailman/listinfo/python-list
>
-- 
https://mail.python.org/mailman/listinfo/python-list


how to get string printed by PyErr_Print( )

2018-09-17 Thread Jason Qian via Python-list
Hey,

Someone has discussed this issue before. Other than redirect stderr, does
the new version python 3.7.0 has other way to retrieve the string
whichPyErr_Print( ) ?


if (PyErr_Occurred())
PyErr_Print(); //need to retrieve the error to string

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