From herron at ELLINGTON.com Fri Jan 26 16:20:31 2018 From: herron at ELLINGTON.com (Liam Herron) Date: Fri, 26 Jan 2018 21:20:31 +0000 Subject: [C++-sig] How can I get access to "argv/argc" command line in a boost python c++ extension? Message-ID: <96426055F003C542B3FB8A0928736DF9031D336907@EX2.ELLINGTON.com> So I have a boost python extension C++ library. I would like to access the "python commandline" in C++ without having to pass in the "sys.argv" into my C++ extension. Is there a way to do this? Include some boost python header and read it from some init variables or something? e.g. python someScript.py A B C 1 2 3 ////////////////////////////////////////////////////////////////////////// class_ ("DummyCppClass", "DummyCppClass obj", init<>()) .def("printCmdLine", &DummyCppClass::printCmdLine) ; ////////////////////////////////////////////////////////////////////////// #include #include std::string DummyCppClass::printCmdLine() const { std::cout << "Prog: " << HERE_argv[0] << ", Arg1: " << HERE_argv[1] << std::endl; } ////////////////////////////////////////////////////////////////////////// ============================================================================================= Email transmissions can not be guaranteed to be secure or error-free, as information could be intercepted, corrupted, lost, destroyed, arrive late or incomplete, or contain viruses. The sender therefore does not accept liability for any errors or omissions in the contents of this message which arise as a result of email transmission. In addition, the information contained in this email message is intended only for use of the individual or entity named above. If the reader of this message is not the intended recipient, or the employee or agent responsible to deliver it to the intended recipient, you are hereby notified that any dissemination, distribution,or copying of this communication, disclosure of the parties to it, or any action taken or omitted to be taken in reliance on it, is strictly prohibited, and may be unlawful. If you are not the intended recipient please delete this email message. ============================================================================================== -------------- next part -------------- An HTML attachment was scrubbed... URL: From stefan at seefeld.name Fri Jan 26 17:47:23 2018 From: stefan at seefeld.name (Stefan Seefeld) Date: Fri, 26 Jan 2018 17:47:23 -0500 Subject: [C++-sig] How can I get access to "argv/argc" command line in a boost python c++ extension? In-Reply-To: <96426055F003C542B3FB8A0928736DF9031D336907@EX2.ELLINGTON.com> References: <96426055F003C542B3FB8A0928736DF9031D336907@EX2.ELLINGTON.com> Message-ID: <72d6f314-5d63-1c09-e66b-ef35549dc0af@seefeld.name> On 26.01.2018 16:20, Liam Herron wrote: > > So I have a boost python extension C++ library.? I would like to > access the ?python commandline? in C++ without having to pass in the > ?sys.argv? into my C++ extension.??? > > Is there a way to do this??? Include some boost python header and read > it from some init variables or something? > You don't have to pass `sys.argv` as an argument. You can inspect it from wherever you need it. Just import `sys` and inspect the elements of `sys.argv`, just as you would do in Python. Stefan -- ...ich hab' noch einen Koffer in Berlin... -------------- next part -------------- An HTML attachment was scrubbed... URL: -------------- next part -------------- A non-text attachment was scrubbed... Name: signature.png Type: image/png Size: 1478 bytes Desc: not available URL: From charlie at charliedyson.net Fri Jan 26 10:27:34 2018 From: charlie at charliedyson.net (Charlie Dyson) Date: Fri, 26 Jan 2018 15:27:34 +0000 Subject: [C++-sig] Boost-Python: Inconsistent shared_ptr use-count since Boost 1.63 Message-ID: I believe the following behaviour changes in 1.63 judging by the ChangeLog, though unfortunately I can only test 1.60 vs 1.66 at the moment. Using Enterprise Linux 7 with gcc 7.2. Consider this module: #include #include #include namespace bp = boost::python; struct X { ~X () { std::cout << "Bye bye" << std::endl; } }; BOOST_PYTHON_MODULE (pymodule) { bp::class_> x_wrap ("X"); // Not needed in newer boost bp::register_ptr_to_python> (); auto x1 = std::make_shared (); auto x2 = bp::extract> (bp::object (x1)) (); // On a newer boost: use counts are not the same std::cout << x1.get () << ": "<< x1.use_count () << std::endl; // 2 std::cout << x2.get () << ": "<< x2.use_count () << std::endl; // 1 :( } For the older Boost, the reference counts are both two, but they are 2 and 1 respectively in the newer Boost. Is this expected? I wonder if there's some cleverness involving x2 somehow pointing to a BP object that in turn points to x1. Curiously X appears to only be destructed once, so it doesn't look like UB. Where it bites me is cleanup time. If I add the following to the end of the module, I get a segfault: static std::vector> v {x2}; Backtrace as follows: #0 subtype_dealloc (self=0x7ffff7e24050) at Objects/typeobject.c:955 #1 0x00007fffef9dfb59 in xdecref<_object> (p=) at ./boost/python/refcount.hpp:38 #2 reset (this=0x6c1f30) at ./boost/python/handle.hpp:222 #3 boost::python::converter::shared_ptr_deleter::operator() (this=0x6c1f30) at libs/python/src/converter/builtin_converters.cpp:35 #4 0x00007ffff7ff563a in _M_release (this=0x6c1f20) at .../bits/shared_ptr_base.h:154 #5 ~__shared_count (this=, __in_chrg=) at .../bits/shared_ptr_base.h:684 #6 ~__shared_ptr (this=, __in_chrg=) at .../bits/shared_ptr_base.h:1123 #7 ~shared_ptr (this=, __in_chrg=) at .../bits/shared_ptr.h:93 #8 _Destroy > (__pointer=) at .../bits/stl_construct.h:98 #9 __destroy*> (__last=, __first=0x678920) at .../bits/stl_construct.h:108 #10 _Destroy*> (__last=, __first=) at .../bits/stl_construct.h:137 #11 _Destroy*, std::shared_ptr > (__last=0x678930, __first=) at .../bits/stl_construct.h:206 #12 std::vector, std::allocator > >::~vector (this=0x7ffff7ff7a60 , __in_chrg=) at .../bits/stl_vector.h:434 #13 0x00007ffff6d24e69 in __run_exit_handlers () from /lib64/libc.so.6 #14 0x00007ffff6d24eb5 in exit () from /lib64/libc.so.6 #15 0x00007ffff6d0db1c in __libc_start_main () from /lib64/libc.so.6 #16 0x00000000004007a1 in _start () Is this a new behaviour change, or a bug, or was it always dangerous and I have just been lucky until now? Many thanks, Charlie From apap at coventor.com Mon Jan 29 09:53:57 2018 From: apap at coventor.com (apap at coventor.com) Date: Mon, 29 Jan 2018 14:53:57 +0000 Subject: [C++-sig] How can I get access to "argv/argc" command line in a boost python c++ extension? In-Reply-To: <72d6f314-5d63-1c09-e66b-ef35549dc0af@seefeld.name> References: <96426055F003C542B3FB8A0928736DF9031D336907@EX2.ELLINGTON.com> <72d6f314-5d63-1c09-e66b-ef35549dc0af@seefeld.name> Message-ID: unsubscribe From: Cplusplus-sig [mailto:cplusplus-sig-bounces+andras.pap=coventor.com at python.org] On Behalf Of Stefan Seefeld Sent: Friday, January 26, 2018 5:47 PM To: cplusplus-sig at python.org Subject: Re: [C++-sig] How can I get access to "argv/argc" command line in a boost python c++ extension? On 26.01.2018 16:20, Liam Herron wrote: So I have a boost python extension C++ library. I would like to access the "python commandline" in C++ without having to pass in the "sys.argv" into my C++ extension. Is there a way to do this? Include some boost python header and read it from some init variables or something? You don't have to pass `sys.argv` as an argument. You can inspect it from wherever you need it. Just import `sys` and inspect the elements of `sys.argv`, just as you would do in Python. [Stefan] -- ...ich hab' noch einen Koffer in Berlin... -------------- next part -------------- An HTML attachment was scrubbed... URL: -------------- next part -------------- A non-text attachment was scrubbed... Name: image001.png Type: image/png Size: 1478 bytes Desc: image001.png URL: From mohan.varma at gmail.com Wed Jan 31 10:07:03 2018 From: mohan.varma at gmail.com (Mohan Varma) Date: Wed, 31 Jan 2018 20:37:03 +0530 Subject: [C++-sig] Copying string values to ndarray using Boost.Python Message-ID: I have been trying to create an ndarray in C++ using Boost.Python while creating a Python module. The ndarray object is created using a composite dtype with an int value column, a 32-byte string column and a float value column. I am able to set the values for int and float columns and access them in Python. I am not sure on how to set the string column values in C++. Here is the synopsis of my code, I believe it explains the problem fairly well. Any help in this regard is greatly appreciated. struct Test { int id; std::string name; float value; }; std::vector test_values; // namespace bp = boost::python; namespace np = boost::python::numpy; // // bp::tuple shape = bp::make_tuple(test_values.size()); bp::list list_for_dtype; list_for_dtype.append(bp::make_tuple("id",np::dtype::get_builtin())); list_for_dtype.append(bp::make_tuple("name", np::dtype(bp::str("U32")))); list_for_dtype.append(bp::make_tuple("value",np::dtype::get_builtin())); np::dtype custom_dtype = np::dtype(list_for_dtype); int item_size = custom_dtype.get_itemsize(); np::ndarray values_array = np::zeros(shape, custom_dtype); char * dest_ptr = (char *) values_array .get_data(); for (size_t i = 0; i < test_values.size(); ++i) { memcpy((void *) dest_ptr, &test_values[i].id, sizeof(int)); dest_ptr += sizeof(int); // How do I copy the string value to ndarray buffer? //bp::str bpname(&test_values[i].name); //memcpy((void *) dest_ptr, bpname.ptr(), sizeof(float)); dest_ptr += 32 * 4; memcpy((void *) dest_ptr, &test_values[i].value, sizeof(float)); dest_ptr += sizeof(float); } Thank you, Mohan. -------------- next part -------------- An HTML attachment was scrubbed... URL: