Re: [C++-sig] Why can't kill process by Ctrl-c in a boost.python module with an endless loop

2012-12-23 Thread Jaedyn K. Draper
Oh, my mistake. Not sure then, I've only embedded, never extended. Maybe 
someone else can help. :(


On 12/23/2012 1:59 AM, simon zhang wrote:
But I  don't call Py_Initialize().I call C++ code in 
Python.Don't  embed the Python to C++...


2012/12/23 Jaedyn K. Draper >


Instead of Py_Initialize() (wherever it is you call it), try
calling Py_InitializeEx(0). Py_Initialize() (or
Py_InitializeEx(1)) binds signal handlers (including SIGINT) to
send python exceptions instead of killing the process. This may be
what's hitting you.


On 12/23/2012 1:44 AM, simon zhang wrote:

I have make a boost.python module with an endless loop.But I
can't kill the process by ctrl-c.The following is an example.

C++

|#include  
#include  
#include  
#include  
usringnamespace  boost::python;

void  foo()  {
int  it=0;
while  (true)  {  //endless loop
++it;
std::cout<<  it
http://mail.python.org/mailman/listinfo/cplusplus-sig



___
Cplusplus-sig mailing list
[email protected] 
http://mail.python.org/mailman/listinfo/cplusplus-sig




___
Cplusplus-sig mailing list
[email protected]
http://mail.python.org/mailman/listinfo/cplusplus-sig


___
Cplusplus-sig mailing list
[email protected]
http://mail.python.org/mailman/listinfo/cplusplus-sig

Re: [C++-sig] Why can't kill process by Ctrl-c in a boost.python module with an endless loop

2012-12-23 Thread simon zhang
I do embedded and extended.Anyway, thank you for your reply. :)
This boost.python document is so complex. :(

2012/12/23 Jaedyn K. Draper 

>  Oh, my mistake. Not sure then, I've only embedded, never extended. Maybe
> someone else can help. :(
>
>
> On 12/23/2012 1:59 AM, simon zhang wrote:
>
> But I  don't call Py_Initialize().I call C++ code in Python.Don't  embed
> the Python to C++...
>
> 2012/12/23 Jaedyn K. Draper 
>
>>  Instead of Py_Initialize() (wherever it is you call it), try calling
>> Py_InitializeEx(0). Py_Initialize() (or Py_InitializeEx(1)) binds signal
>> handlers (including SIGINT) to send python exceptions instead of killing
>> the process. This may be what's hitting you.
>>
>>
>> On 12/23/2012 1:44 AM, simon zhang wrote:
>>
>>  I have make a boost.python module with an endless loop.But I can't kill
>> the process by ctrl-c.The following is an example.
>>
>>   C++
>>
>> #include #include > module.hpp>#include > def.hpp>#include 
>> usring namespace boost::python;
>> void foo() {
>>int it=0;
>>while (true) { //endless loop
>>++it;
>>std::cout<< it <>sleep(3);
>>}}
>>
>> BOOST_PYTHON_MODULE(ctopy){
>> def("foo",foo);}
>>
>>  python:
>>
>> import ctopy
>> ctopy.foo()
>>
>>  result:
>>
>> 1234.
>>
>>  I can't kill the foreground process by Ctrl-c.why the module don't
>> accept signal "SIGINT" that was sent by Ctrl-c.How to make it work.
>>
>>
>>  ___
>> Cplusplus-sig mailing 
>> [email protected]://mail.python.org/mailman/listinfo/cplusplus-sig
>>
>>
>>
>> ___
>> Cplusplus-sig mailing list
>> [email protected]
>> http://mail.python.org/mailman/listinfo/cplusplus-sig
>>
>
>
>
> ___
> Cplusplus-sig mailing 
> [email protected]://mail.python.org/mailman/listinfo/cplusplus-sig
>
>
>
> ___
> Cplusplus-sig mailing list
> [email protected]
> http://mail.python.org/mailman/listinfo/cplusplus-sig
>
___
Cplusplus-sig mailing list
[email protected]
http://mail.python.org/mailman/listinfo/cplusplus-sig

Re: [C++-sig] How to converter std::string* in boost.python?

2012-12-23 Thread simon zhang
..so,It seems I can only convert std::string and make a deep copy.I can
only avoid it as much as possible.Thank you.


2012/12/22 Jim Bosch 

> On 12/21/2012 03:52 AM, simon zhang wrote:
>
>> How to converter std::string* in boost.python?I have to handle some data
>> of c++ in python.The data may be big.So I return a pointer to python.
>> But there are some errors.
>>
>>
> If the data is big, and you really want to avoid a deep copy, the only way
> to use it is if you manually allocate the memory as a Python str object
> using the Python C API or return it as something else (like a NumPy array
> or buffer object, or a custom Boost.Python-wrapped class) that can hold
> C++-allocated memory.  A Python str object always owns its own memory, and
> so does std::string, so you can't convert from one to the other without a
> deep copy.
>
> Jim
>
> __**_
> Cplusplus-sig mailing list
> [email protected]
> http://mail.python.org/**mailman/listinfo/cplusplus-sig
>
___
Cplusplus-sig mailing list
[email protected]
http://mail.python.org/mailman/listinfo/cplusplus-sig

Re: [C++-sig] Why can't kill process by Ctrl-c in a boost.python module with an endless loop

2012-12-23 Thread simon zhang
This is the answer from the stackoverflow.

   while (true) { //endless loop
   ++it;
   std::cout<< it <

>  Oh, my mistake. Not sure then, I've only embedded, never extended. Maybe
> someone else can help. :(
>
>
> On 12/23/2012 1:59 AM, simon zhang wrote:
>
> But I  don't call Py_Initialize().I call C++ code in Python.Don't  embed
> the Python to C++...
>
> 2012/12/23 Jaedyn K. Draper 
>
>>  Instead of Py_Initialize() (wherever it is you call it), try calling
>> Py_InitializeEx(0). Py_Initialize() (or Py_InitializeEx(1)) binds signal
>> handlers (including SIGINT) to send python exceptions instead of killing
>> the process. This may be what's hitting you.
>>
>>
>> On 12/23/2012 1:44 AM, simon zhang wrote:
>>
>>  I have make a boost.python module with an endless loop.But I can't kill
>> the process by ctrl-c.The following is an example.
>>
>>   C++
>>
>> #include #include > module.hpp>#include > def.hpp>#include 
>> usring namespace boost::python;
>> void foo() {
>>int it=0;
>>while (true) { //endless loop
>>++it;
>>std::cout<< it <>sleep(3);
>>}}
>>
>> BOOST_PYTHON_MODULE(ctopy){
>> def("foo",foo);}
>>
>>  python:
>>
>> import ctopy
>> ctopy.foo()
>>
>>  result:
>>
>> 1234.
>>
>>  I can't kill the foreground process by Ctrl-c.why the module don't
>> accept signal "SIGINT" that was sent by Ctrl-c.How to make it work.
>>
>>
>>  ___
>> Cplusplus-sig mailing 
>> [email protected]://mail.python.org/mailman/listinfo/cplusplus-sig
>>
>>
>>
>> ___
>> Cplusplus-sig mailing list
>> [email protected]
>> http://mail.python.org/mailman/listinfo/cplusplus-sig
>>
>
>
>
> ___
> Cplusplus-sig mailing 
> [email protected]://mail.python.org/mailman/listinfo/cplusplus-sig
>
>
>
> ___
> Cplusplus-sig mailing list
> [email protected]
> http://mail.python.org/mailman/listinfo/cplusplus-sig
>
___
Cplusplus-sig mailing list
[email protected]
http://mail.python.org/mailman/listinfo/cplusplus-sig

Re: [C++-sig] Why can't kill process by Ctrl-c in a boost.python module with an endless loop

2012-12-23 Thread Jaedyn K. Draper

I was at least half right, then. Python was eating the signals. :)

As an alternative, to prevent yourself from having to do this in every 
loop in your code, you might try this on the python side:


import signal
signal.signal(signal.SIGINT, signal.SIG_DFL)

That'll stop Python from catching SIGINT globally and will always send 
the SIGINT down to the C++ and terminate.


Of course, that may or may not be an option depending on your specific 
needs.


On 12/23/2012 3:02 AM, simon zhang wrote:

This is the answer from the stackoverflow.

|while  (true)  {  //endless loop
++it;
std::cout<<  it<2012/12/23 Jaedyn K. Draper >


Oh, my mistake. Not sure then, I've only embedded, never extended.
Maybe someone else can help. :(


On 12/23/2012 1:59 AM, simon zhang wrote:

But I  don't call Py_Initialize().I call C++ code in
Python.Don't  embed the Python to C++...

2012/12/23 Jaedyn K. Draper mailto:[email protected]>>

Instead of Py_Initialize() (wherever it is you call it), try
calling Py_InitializeEx(0). Py_Initialize() (or
Py_InitializeEx(1)) binds signal handlers (including SIGINT)
to send python exceptions instead of killing the process.
This may be what's hitting you.


On 12/23/2012 1:44 AM, simon zhang wrote:

I have make a boost.python module with an endless loop.But I
can't kill the process by ctrl-c.The following is an example.

C++

|#include  
#include  
#include  
#include  
usringnamespace  boost::python;

void  foo()  {
int  it=0;
while  (true)  {  //endless loop
++it;
std::cout<<  it
http://mail.python.org/mailman/listinfo/cplusplus-sig



___
Cplusplus-sig mailing list
[email protected] 
http://mail.python.org/mailman/listinfo/cplusplus-sig




___
Cplusplus-sig mailing list
[email protected]  
http://mail.python.org/mailman/listinfo/cplusplus-sig



___
Cplusplus-sig mailing list
[email protected] 
http://mail.python.org/mailman/listinfo/cplusplus-sig




___
Cplusplus-sig mailing list
[email protected]
http://mail.python.org/mailman/listinfo/cplusplus-sig


___
Cplusplus-sig mailing list
[email protected]
http://mail.python.org/mailman/listinfo/cplusplus-sig

Re: [C++-sig] Why can't kill process by Ctrl-c in a boost.python module with an endless loop

2012-12-23 Thread simon zhang
Jaedyn.

I would like to discuss another issue, what is a good way to avoid deep
copy.There may be a few big data.From c++ to python and from python to
c++.Similar from c + + return std::string* to python and python pass the
std::string* parameter to c + + function.Maybe it should be similar to
smart pointers.Do you have any good suggestions in there?


2012/12/23 Jaedyn K. Draper 

>  I was at least half right, then. Python was eating the signals. :)
>
> As an alternative, to prevent yourself from having to do this in every
> loop in your code, you might try this on the python side:
>
> import signal
> signal.signal(signal.SIGINT, signal.SIG_DFL)
>
> That'll stop Python from catching SIGINT globally and will always send the
> SIGINT down to the C++ and terminate.
>
> Of course, that may or may not be an option depending on your specific
> needs.
>
>
> On 12/23/2012 3:02 AM, simon zhang wrote:
>
> This is the answer from the stackoverflow.
>
> while (true) { //endless loop
>++it;
>std::cout<< it if(PyErr_CheckSignals() == -1) {
>exit(1);
>}
>}
>
>
> 2012/12/23 Jaedyn K. Draper 
>
>>  Oh, my mistake. Not sure then, I've only embedded, never extended. Maybe
>> someone else can help. :(
>>
>>
>> On 12/23/2012 1:59 AM, simon zhang wrote:
>>
>> But I  don't call Py_Initialize().I call C++ code in Python.Don't  embed
>> the Python to C++...
>>
>> 2012/12/23 Jaedyn K. Draper 
>>
>>>  Instead of Py_Initialize() (wherever it is you call it), try calling
>>> Py_InitializeEx(0). Py_Initialize() (or Py_InitializeEx(1)) binds signal
>>> handlers (including SIGINT) to send python exceptions instead of killing
>>> the process. This may be what's hitting you.
>>>
>>>
>>> On 12/23/2012 1:44 AM, simon zhang wrote:
>>>
>>>  I have make a boost.python module with an endless loop.But I can't
>>> kill the process by ctrl-c.The following is an example.
>>>
>>>   C++
>>>
>>> #include #include >> module.hpp>#include >> def.hpp>#include 
>>> usring namespace boost::python;
>>> void foo() {
>>>int it=0;
>>>while (true) { //endless loop
>>>++it;
>>>std::cout<< it <>>sleep(3);
>>>}}
>>>
>>> BOOST_PYTHON_MODULE(ctopy){
>>> def("foo",foo);}
>>>
>>>  python:
>>>
>>> import ctopy
>>> ctopy.foo()
>>>
>>>  result:
>>>
>>> 1234.
>>>
>>>  I can't kill the foreground process by Ctrl-c.why the module don't
>>> accept signal "SIGINT" that was sent by Ctrl-c.How to make it work.
>>>
>>>
>>>  ___
>>> Cplusplus-sig mailing 
>>> [email protected]://mail.python.org/mailman/listinfo/cplusplus-sig
>>>
>>>
>>>
>>> ___
>>> Cplusplus-sig mailing list
>>> [email protected]
>>> http://mail.python.org/mailman/listinfo/cplusplus-sig
>>>
>>
>>
>>
>> ___
>> Cplusplus-sig mailing 
>> [email protected]://mail.python.org/mailman/listinfo/cplusplus-sig
>>
>>
>>
>> ___
>> Cplusplus-sig mailing list
>> [email protected]
>> http://mail.python.org/mailman/listinfo/cplusplus-sig
>>
>
>
>
> ___
> Cplusplus-sig mailing 
> [email protected]://mail.python.org/mailman/listinfo/cplusplus-sig
>
>
>
> ___
> Cplusplus-sig mailing list
> [email protected]
> http://mail.python.org/mailman/listinfo/cplusplus-sig
>
___
Cplusplus-sig mailing list
[email protected]
http://mail.python.org/mailman/listinfo/cplusplus-sig

Re: [C++-sig] Why can't kill process by Ctrl-c in a boost.python module with an endless loop

2012-12-23 Thread Jaedyn K. Draper
Yeah, I saw your other question about it. There's nothing in boost that 
I know of to achieve that. The only thing I'd know to suggest is what 
others have already suggested - encapsulate it in another class and pass 
that in. But it wouldn't be converted to a python string that way.


My question is whether or not you may be trying to optimize prematurely. 
If your performance needs are strict enough that you need to pass your 
string as a pointer, might it be better to write your application in C++ 
entirely, or prototype in python and then convert to C++?


You might try passing it as a reference instead of a pointer. I don't 
know if that'll work or not. But, try having a regular std::string 
instead of a std::string* and send it using ref(str) (the same way that 
I suggested you send things using ptr() in your earlier question). 
Again, I don't know if it'll work, but it might be worth a try.


If it's a return value, returning a ref() would cause a local variable 
to go out of scope. You could try passing the desired string as a 
reference parameter, rather than trying to return it. Again, I'm not 
sure if this will work or not, as I've never tried it.


If neither of those work, I'd have to play around with it some to see if 
I could come up with anything else.


On 12/23/2012 11:41 PM, simon zhang wrote:

Jaedyn.

I would like to discuss another issue, what is a good way to avoid 
deep copy.There may be a few big data.From c++ to python and from 
python to c++.Similar from c + + return std::string* to python and 
python pass the std::string* parameter to c + + function.Maybe it 
should be similar to smart pointers.Do you have any good suggestions 
in there?



2012/12/23 Jaedyn K. Draper >


I was at least half right, then. Python was eating the signals. :)

As an alternative, to prevent yourself from having to do this in
every loop in your code, you might try this on the python side:

import signal
signal.signal(signal.SIGINT, signal.SIG_DFL)

That'll stop Python from catching SIGINT globally and will always
send the SIGINT down to the C++ and terminate.

Of course, that may or may not be an option depending on your
specific needs.


On 12/23/2012 3:02 AM, simon zhang wrote:

This is the answer from the stackoverflow.

|while  (true)  {  //endless loop
++it;
std::cout<<  it>

Oh, my mistake. Not sure then, I've only embedded, never
extended. Maybe someone else can help. :(


On 12/23/2012 1:59 AM, simon zhang wrote:

But I  don't call Py_Initialize().I call C++ code in
Python.Don't  embed the Python to C++...

2012/12/23 Jaedyn K. Draper mailto:[email protected]>>

Instead of Py_Initialize() (wherever it is you call it),
try calling Py_InitializeEx(0). Py_Initialize() (or
Py_InitializeEx(1)) binds signal handlers (including
SIGINT) to send python exceptions instead of killing the
process. This may be what's hitting you.


On 12/23/2012 1:44 AM, simon zhang wrote:

I have make a boost.python module with an endless
loop.But I can't kill the process by ctrl-c.The
following is an example.

C++

|#include  
#include  
#include  
#include  
usringnamespace  boost::python;

void  foo()  {
int  it=0;
while  (true)  {  //endless loop
++it;
std::cout<<  it
http://mail.python.org/mailman/listinfo/cplusplus-sig



___
Cplusplus-sig mailing list
[email protected] 
http://mail.python.org/mailman/listinfo/cplusplus-sig




___
Cplusplus-sig mailing list
[email protected]  
http://mail.python.org/mailman/listinfo/cplusplus-sig



___
Cplusplus-sig mailing list
[email protected] 
http://mail.python.org/mailman/listinfo/cplusplus-sig




___
Cplusplus-sig mailing list
[email protected]  
http://mail.python.org/mailman/listinfo/cplusplus-sig



___
Cplusplus-sig mailing list
[email protected] 
http://mail.python.org/mailman/listinfo/cplusplus-sig




___
Cplusplus-sig mailing list
[email protected]
h

Re: [C++-sig] Why can't kill process by Ctrl-c in a boost.python module with an endless loop

2012-12-23 Thread simon zhang
Because string's memory allocated is different in python and c++.General
method should not suited.
I have reviewed the project.Maybe you're right, it is premature to optimize
the program.At least for now, it should not be a problem.
The program will have a c + + interface and a python interface, so I'm
doing some complicated.

I will use a simple method.The data will be copied.

In the future, I will be write a wrapped class. When the c++ interface,
it store the data by std :: string.When the python interface,it store the
data by  the Python str object. And perhaps this is a good way.
What do you think?


2012/12/24 Jaedyn K. Draper 

>  Yeah, I saw your other question about it. There's nothing in boost that I
> know of to achieve that. The only thing I'd know to suggest is what others
> have already suggested - encapsulate it in another class and pass that in.
> But it wouldn't be converted to a python string that way.
>
> My question is whether or not you may be trying to optimize prematurely.
> If your performance needs are strict enough that you need to pass your
> string as a pointer, might it be better to write your application in C++
> entirely, or prototype in python and then convert to C++?
>
> You might try passing it as a reference instead of a pointer. I don't know
> if that'll work or not. But, try having a regular std::string instead of a
> std::string* and send it using ref(str) (the same way that I suggested you
> send things using ptr() in your earlier question). Again, I don't know if
> it'll work, but it might be worth a try.
>
> If it's a return value, returning a ref() would cause a local variable to
> go out of scope. You could try passing the desired string as a reference
> parameter, rather than trying to return it. Again, I'm not sure if this
> will work or not, as I've never tried it.
>
> If neither of those work, I'd have to play around with it some to see if I
> could come up with anything else.
>
>
> On 12/23/2012 11:41 PM, simon zhang wrote:
>
> Jaedyn.
>
>  I would like to discuss another issue, what is a good way to avoid deep
> copy.There may be a few big data.From c++ to python and from python to
> c++.Similar from c + + return std::string* to python and python pass the
> std::string* parameter to c + + function.Maybe it should be similar to
> smart pointers.Do you have any good suggestions in there?
>
>
> 2012/12/23 Jaedyn K. Draper 
>
>>  I was at least half right, then. Python was eating the signals. :)
>>
>> As an alternative, to prevent yourself from having to do this in every
>> loop in your code, you might try this on the python side:
>>
>> import signal
>> signal.signal(signal.SIGINT, signal.SIG_DFL)
>>
>> That'll stop Python from catching SIGINT globally and will always send
>> the SIGINT down to the C++ and terminate.
>>
>> Of course, that may or may not be an option depending on your specific
>> needs.
>>
>>
>> On 12/23/2012 3:02 AM, simon zhang wrote:
>>
>> This is the answer from the stackoverflow.
>>
>> while (true) { //endless loop
>>++it;
>>std::cout<< it <>sleep(3);
>>if(PyErr_CheckSignals() == -1) {
>>exit(1);
>>}
>>}
>>
>>
>> 2012/12/23 Jaedyn K. Draper 
>>
>>>  Oh, my mistake. Not sure then, I've only embedded, never extended.
>>> Maybe someone else can help. :(
>>>
>>>
>>> On 12/23/2012 1:59 AM, simon zhang wrote:
>>>
>>> But I  don't call Py_Initialize().I call C++ code in Python.Don't  embed
>>> the Python to C++...
>>>
>>> 2012/12/23 Jaedyn K. Draper 
>>>
  Instead of Py_Initialize() (wherever it is you call it), try calling
 Py_InitializeEx(0). Py_Initialize() (or Py_InitializeEx(1)) binds signal
 handlers (including SIGINT) to send python exceptions instead of killing
 the process. This may be what's hitting you.


 On 12/23/2012 1:44 AM, simon zhang wrote:

  I have make a boost.python module with an endless loop.But I can't
 kill the process by ctrl-c.The following is an example.

   C++

 #include #include >>> module.hpp>#include >>> def.hpp>#include 
 usring namespace boost::python;
 void foo() {
int it=0;
while (true) { //endless loop
++it;
std::cout<< it <>>>sleep(3);
}}

 BOOST_PYTHON_MODULE(ctopy){
 def("foo",foo);}

  python:

 import ctopy
 ctopy.foo()

  result:

 1234.

  I can't kill the foreground process by Ctrl-c.why the module don't
 accept signal "SIGINT" that was sent by Ctrl-c.How to make it work.


  ___
 Cplusplus-sig mailing 
 [email protected]://mail.python.org/mailman/listinfo/cplusplus-sig



 ___
 Cplusplus-sig mailing list
 [email protected]
 http://mail.python.org/mailman/listinfo/cplu