Python测试类坚持'实例化测试'

时间:2016-08-13 02:53:49

标签: python matplotlib

我的测试类产生了所需的绘图,但我必须每次都手动停止执行 - 控制台继续显示'实例化测试&#39 ;;任何人都可以发现为什么执行永远不会停止?关于增加我的代码的任何提示' Pythonic-ness'也将不胜感激!

(在Mac OS X 10.11.6上的PyCharm CE 2016.2.1中运行的Python 3.5)

#define BR (__bridge id)
#define BRD (__bridge CFDictionaryRef)

+ (SecKeyRef)storePrivateKey:(NSString *)key inSystemKeychainWithTag:(NSString *)tag {
    NSRange spos;
    NSRange epos;
    spos = [key rangeOfString:@"-----BEGIN RSA PRIVATE KEY-----"];
    if(spos.length > 0) {
        epos = [key rangeOfString:@"-----END RSA PRIVATE KEY-----"];
    }
    else {
        spos = [key rangeOfString:@"-----BEGIN PRIVATE KEY-----"];
        epos = [key rangeOfString:@"-----END PRIVATE KEY-----"];
    }
    if(spos.location != NSNotFound && epos.location != NSNotFound){
        NSUInteger s = spos.location + spos.length;
        NSUInteger e = epos.location;
        NSRange range = NSMakeRange(s, e-s);
        key = [key substringWithRange:range];
    }
    key = [key stringByReplacingOccurrencesOfString:@"\r" withString:@""];
    key = [key stringByReplacingOccurrencesOfString:@"\n" withString:@""];
    key = [key stringByReplacingOccurrencesOfString:@"\t" withString:@""];
    key = [key stringByReplacingOccurrencesOfString:@" "  withString:@""];

    // This will be base64 encoded, decode it.
    NSData *data = [[NSData alloc] initWithBase64EncodedString:key options:0];

    if(data == nil){
        return nil;
    }

    //a tag to read/write keychain storage
    NSData *d_tag = [NSData dataWithBytes:[tag UTF8String] length:[tag length]];

    // Delete any old lingering key with the same tag
    NSMutableDictionary *options = [
    @{
      BR kSecClass: BR kSecClassKey,
      BR kSecAttrKeyType: BR kSecAttrKeyTypeRSA,
      BR kSecAttrApplicationTag: d_tag,
    }
    mutableCopy];

    SecItemDelete(BRD options);

    // Add persistent version of the key to system keychain
    [options addEntriesFromDictionary:
    @{
      BR kSecValueData:data,
      BR kSecAttrKeyClass: BR kSecAttrKeyClassPrivate,
      BR kSecReturnPersistentRef: @YES,
    }];

    CFTypeRef persistKey = nil;
    OSStatus status = SecItemAdd(BRD options, &persistKey);
    if (persistKey != nil){
        CFRelease(persistKey);
    }
    if ((status != noErr) && (status != errSecDuplicateItem)) {
        return nil;
    }

    [options removeObjectForKey:BR kSecValueData];
    [options removeObjectForKey:BR kSecReturnPersistentRef];

    [options addEntriesFromDictionary:
    @{
      BR kSecReturnRef:@YES,
      BR kSecAttrKeyType:BR kSecAttrKeyTypeRSA,
    }];

    // Now fetch the SecKeyRef version of the key
    SecKeyRef keyRef = nil;
    status = SecItemCopyMatching(BRD options, (CFTypeRef *)&keyRef);
    if(status != noErr){
        return nil;
    }
    return keyRef;
}

2 个答案:

答案 0 :(得分:1)

您需要关闭显示绘图的窗口,以便脚本(在本例中为您的测试)继续/完成/完成。 http://data-docs-samples.cfapps.io/docs-gemfire/latest/javadocs/japi/com/gemstone/gemfire/DataSerializer.html,强调我的:

  

如果要在显示器上查看绘图,用户界面后端将需要启动GUI主界面。这就是show()的作用。它告诉matplotlib提升到目前为止创建的所有数字窗口并启动mainloop。 因为默认情况下此主循环是阻塞的(即,脚本执行暂停),所以最后每个脚本只应调用一次。最后一个窗口关闭后,脚本执行将恢复。因此,如果您使用matplotlib仅生成图像而不想要用户界面窗口,则无需调用show(请参阅生成图像而不使用窗口出现什么是后端?)。

或者不要将show()用于测试,只需生成一张您可以稍后验证的图片。

答案 1 :(得分:0)

我在Django中有同样的错误。我发现其中一种迁移没有被应用。