安装新的Windows系统后,我在其默认位置(c:\cygwin和
c:\Python27\python)安装了CygWin和64位Python(2.7.3),并将CygWin bin和Python目录添加到我的路径中(在用户变量PATH中).从正常的命令窗口,Python启动完美,但是当我bash在CygWin环境中调用它时,它会挂起,从不给我输入提示.
我以前在其他机器上完成了这个,但总是使用旧版本的Python(32位)和CygWin,以及Python在一个非标准的非标准位置.有没有其他人有这个问题,或者有人可以告诉我它可能是由于什么?
以下代码有什么问题(在Python 2.7.1下):
class TestFailed(BaseException):
def __new__(self, m):
self.message = m
def __str__(self):
return self.message
try:
raise TestFailed('Oops')
except TestFailed as x:
print x
Run Code Online (Sandbox Code Playgroud)
当我运行它时,我得到:
Traceback (most recent call last):
File "x.py", line 9, in <module>
raise TestFailed('Oops')
TypeError: exceptions must be old-style classes or derived from BaseException, not NoneType
Run Code Online (Sandbox Code Playgroud)
但它看起来TestFailed确实来自于BaseException.
给出以下代码:
#include <vector>
template<class C1, class C2, class Op>
std::vector<typename Op::result_type>
f(Op op, const C1& src1, const C2& src2)
{
}
template<class It, class Op>
std::vector<typename Op::result_type> g(Op op, It begin, It end)
{
}
template<class It1, class It2, class Op>
std::vector<typename Op::result_type> g(Op op, It1 left_begin, It1 left_end, It2 right_begin)
{
return std::vector<typename Op::result_type>();
}
struct ToS
{
typedef double result_type;
double operator() (long , double ) const { return 0.0; }
};
std::vector<double> h(std::vector<long> const& vl, std::vector<double> const& …Run Code Online (Sandbox Code Playgroud) 鉴于std::vector<std::unique_ptr<SomeType> >,使用remove_if它是否合法
?换句话说,给定此代码:
std::vector<std::unique_ptr<SomeType> > v;
// fill v, all entries point to a valid instance of SomeType...
v.erase( std::remove_if( v.begin(), v.end(), someCondition ), v.end() );
Run Code Online (Sandbox Code Playgroud)
在擦除后我保证所有指针仍然v有效.我知道,考虑到直观的实现
std::remove_if,并且考虑到我所看到的所有实现,它们将是.我想知道标准中是否有任何保证它的东西; 即,std::remove_if不允许复制任何有效条目而不将副本重新复制到其最终位置.
(当然,我认为条件不会复制.如果条件有如下签名:
struct Condition
{
bool operator()( std::unique_ptr<SomeType> ptr ) const;
};
Run Code Online (Sandbox Code Playgroud)
当然,所有的指针都会在之后无效
remove_if.)
如果我调用可移动对象会发生
std::set<>::insert什么,并且插入不会发生,因为已经有一个对象存在于该集合中.特别是,以下是有效的:
struct Object
{
std::string key;
// ...
struct OrderByKey
{
bool operator()( Object const& lhs, Object const& rhs) const
{
return lhs.key < rhs.key;
}
};
Object( Object&& other )
: key(std::move(other.key))
// ...
{
}
};
Run Code Online (Sandbox Code Playgroud)
和:
std::set<Object, Object::OrderByKey> registry;
void
register(Object&& o)
{
auto status = registry.insert(std::move(o));
if (!status.second)
throw std::runtime_error("Duplicate entry for " + o.key);
}
Run Code Online (Sandbox Code Playgroud)
之后registry.insert,当没有插入时,
o已移动或未移动.(如果它可能已被移动,我需要事先保存字符串的副本,以便在错误消息中使用它.(是的,我知道我总是可以写:
throw std::runtime_error( "Duplicate entry for " + status->first.key );
Run Code Online (Sandbox Code Playgroud)
这是我可能会做的.但我仍然想知道标准对此有何看法.)
标题或多或少都说明了一切.我有以下一点代码:
#include <vector>
#include <string>
#include <iterator>
#include <algorithm>
struct xloper12;
class Something
{
public:
std::string asString() const;
};
extern std::vector<Something> ourSomethings;
class ExcelOutputLoader
{
public:
void load( std::vector<std::string> const& value );
xloper12* asXloper() const;
};
extern xloper12* ProcessException( std::string const& functionName );
extern "C" __declspec(dllexport) xloper12*
getSomethingList()
{
try {
std::vector<std::string> results;
results.reserve( ourSomethings.size() );
std::transform(
ourSomethings.begin(),
ourSomethings.end(),
std::back_inserter(results),
[]( Something const& o ) { return o.asString(); } );
ExcelOutputLoader out;
out.load( results );
return out.asXloper();
} catch (...) …Run Code Online (Sandbox Code Playgroud) 我正在扩展我们的库(支持Python 2.7)中的类以支持PEP 3118,后者已经被反向移植到2.7.
从文档中,我需要初始化tp_as_buffer字段以指向a PyBufferProcs.但是,从2.7的文档中,此结构的描述仅包含旧缓冲区协议的条目.从来源,我收集的是
PyBufferProcs对新协议(一些其他的项目bf_getbuffer和bf_releasebuffer).
问题仍然存在:
我是否必须做一些特别的事情来告诉Python这些新条目是否有效?
我是否必须填写旧协议的条目?(例如,2.7的文档说明bf_getsegcount
可能不为空.但如果我支持PEP 3118,则不应使用此条目.)
考虑以下:
#include <iostream>
#define trace(name) std::cout << #name << " (" << this << "), i = " << i << std::endl
class C
{
C(C const&);
C& operator=(C const&);
public:
int i;
C() : i(42) { trace(CTOR); }
C(C&& other) : i(other.i) { trace(MOVE); other.i = 0; }
C& operator=(C&& other) { trace(ASGN); other.i = 0; return *this; }
~C() { trace(DTOR); }
};
C
func1(bool c)
{
C local;
if (c)
return local;
else
return C();
}
int …Run Code Online (Sandbox Code Playgroud) 我正在编写一个Excel插件,需要wchar_t
为Excel 生成输出(虽然在内部,我们是100%char,实际上限制char为纯ASCII).有一次,我正在使用
swprintf转换:
static wchar_t buffer[ 32369 ];
buffer[0] = swprintf( buffer + 1, sizeof(buffer) - 1, L"#%s!", message );
Run Code Online (Sandbox Code Playgroud)
Excel显示某种CJK字符,但message
(type char const*)是一个以空字符结尾的字符串,其中没有可打印ASCII字符(十六进制值0x20-0x7E).
我在一个小的测试程序中试过这个,用十六进制转换生成的字符串,看起来VC++就message
好像它是一个wchar_t const*(尽管它似乎'\0'正确识别,虽然它是在一个字节上); 这导致了wchar_t像0x6568
(而不是0x0068, 0x0065我期待的)的值.
根据C99标准,对于"%s"说明符,
swprintf应该将字符从char const*
"好像通过重复调用mbrtowc函数[...]"转换.我看到的行为是Visual C++库中的错误,还是全局语言环境中是否存在我必须更改的内容?
(FWIW:当我使用g ++编译和运行我的小测试程序时,我得到了我期望的行为.但是,G ++不是我们的Excel插件的选项,至少目前不是.)
我正在开发一个接口,以便 Python 程序可以调用现有的 C++ 库。如果我理解正确,这样的接口(和 C++ 库)应该作为模块导入,例如abc. 接口中的类型在接口的 C++ 端定义为静态变量,并使用以下顺序向 Python 注册:
void PythonRegistrationData::RegisterType(PyObject* module, PyTypeObject* type)\n{\n if (type->ob_refcnt < 2)\n {\n if (type->tp_base != NULL)\n RegisterType(module, type->tp_base);\n Py_INCREF(type);\n if (PyType_Ready(type) != 0)\n throw GPython::DummyError();\n PyModule_AddObject(module, type->tp_name, as<PyObject>(type));\n }\n}\nRun Code Online (Sandbox Code Playgroud)\n(递归是为了确保基类在派生类之前注册。模块已在较早创建。)根据C-API 文档,\xe2\x80\x9c 对于静态分配的类型对象,tp_name 字段应包含一个点.\xe2\x80\x9d; 因此,我tp_name使用进行初始化"abc.MyType"。当我这样做时,在加载模块后,\'module\' object has no attribute \'MyType\'当我尝试使用该类型时(例如x = abc.MyType(),之前已经做过import abc)。如果我只使用"MyType",它似乎可以工作,但这似乎与官方文档和教程中的示例相矛盾。
那么该字段的确切含义是什么tp_name?它真正应该包含什么?如果我不把模块名称放进去会有什么影响?