如果您对使用io.TextIOWrapper包装一个开放流感兴趣,那么本文将是一篇不错的选择,我们将为您详在本文中,您将会了解到关于使用io.TextIOWrapper包装一个开放流的详细内容,并且为
如果您对使用io.TextIOWrapper包装一个开放流感兴趣,那么本文将是一篇不错的选择,我们将为您详在本文中,您将会了解到关于使用io.TextIOWrapper包装一个开放流的详细内容,并且为您提供关于android – ContextWrapper.getResources()中的NullPointerException、android – HTC设备中的InputConnectionWrapper.commitText StackOverflowError、android – 如何使用InputConnectionWrapper?、AttributeError: '_io.TextIOWrapper' 对象没有属性 'fp'的有价值信息。
本文目录一览:- 使用io.TextIOWrapper包装一个开放流
- android – ContextWrapper.getResources()中的NullPointerException
- android – HTC设备中的InputConnectionWrapper.commitText StackOverflowError
- android – 如何使用InputConnectionWrapper?
- AttributeError: '_io.TextIOWrapper' 对象没有属性 'fp'
使用io.TextIOWrapper包装一个开放流
如何将打开的二进制流(Python 2 file
,Python
3io.BufferedReader
和an)包装io.BytesIO
在中io.TextIOWrapper
?
我正在尝试编写将保持不变的代码:
- 在Python 2上运行。
- 在Python 3上运行。
- 使用从标准库生成的二进制流(即,我无法控制它们是什么类型)
- 将二进制流设为测试双倍(即没有文件句柄,无法重新打开)。
- 产生一个
io.TextIOWrapper
包装指定流的。
io.TextIOWrapper
之所以需要,是因为标准库的其他部分需要使用它的API。存在其他类似文件的类型,但是没有提供正确的API。
例
包装作为subprocess.Popen.stdout
属性显示的二进制流:
import subprocessimport iognupg_subprocess = subprocess.Popen( ["gpg", "--version"], stdout=subprocess.PIPE)gnupg_stdout = io.TextIOWrapper(gnupg_subprocess.stdout, encoding="utf-8")
在单元测试中,用io.BytesIO
实例替换流以控制其内容,而不用触摸任何子进程或文件系统。
gnupg_subprocess.stdout = io.BytesIO("Lorem ipsum".encode("utf-8"))
在Python 3的标准库创建的流上,效果很好。但是,相同的代码在Python 2生成的流上失败:
[Python 2]>>> type(gnupg_subprocess.stdout)<type ''file''>>>> gnupg_stdout = io.TextIOWrapper(gnupg_subprocess.stdout, encoding="utf-8")Traceback (most recent call last): File "<stdin>", line 1, in <module>AttributeError: ''file'' object has no attribute ''readable''
不是解决方案: file
一个明显的响应是在代码中具有一个分支,以测试流是否实际上是Python 2file
对象,并以与io.*
对象不同的方式处理该对象。
这不是经过良好测试的代码的选项,因为它会生成一个要进行单元测试的分支-为了尽快运行,该分支不得创建任何 真实的 文件系统对象-不能执行。
单元测试将提供测试加倍,而不是真实的file
对象。因此,创建那些测试双打不会使用的分支将破坏测试套件。
没有解决方案: io.open
一些受访者建议重新打开(例如使用io.open
)基础文件句柄:
gnupg_stdout = io.open( gnupg_subprocess.stdout.fileno(), mode=''r'', encoding="utf-8")
在Python 3和Python 2上均可使用
[Python 3]>>> type(gnupg_subprocess.stdout)<class ''_io.BufferedReader''>>>> gnupg_stdout = io.open(gnupg_subprocess.stdout.fileno(), mode=''r'', encoding="utf-8")>>> type(gnupg_stdout)<class ''_io.TextIOWrapper''>[Python 2]>>> type(gnupg_subprocess.stdout)<type ''file''>>>> gnupg_stdout = io.open(gnupg_subprocess.stdout.fileno(), mode=''r'', encoding="utf-8")>>> type(gnupg_stdout)<type ''_io.TextIOWrapper''>
但是,当然,它 依赖于 从文件句柄中 重新打开真实文件
。因此,当测试double是一个io.BytesIO
实例时,它在单元测试中将失败:
>>> gnupg_subprocess.stdout = io.BytesIO("Lorem ipsum".encode("utf-8"))>>> type(gnupg_subprocess.stdout)<type ''_io.BytesIO''>>>> gnupg_stdout = io.open(gnupg_subprocess.stdout.fileno(), mode=''r'', encoding="utf-8")Traceback (most recent call last): File "<stdin>", line 1, in <module>io.UnsupportedOperation: fileno
没有解决方案: codecs.getreader
标准库还具有该codecs
模块,该模块提供包装器功能:
import codecsgnupg_stdout = codecs.getreader("utf-8")(gnupg_subprocess.stdout)
很好,因为它不会尝试重新打开流。但是它无法提供io.TextIOWrapper
API。具体来说,它 不继承io.IOBase
并且
不具有encoding
属性:
>>> type(gnupg_subprocess.stdout)<type ''file''>>>> gnupg_stdout = codecs.getreader("utf-8")(gnupg_subprocess.stdout)>>> type(gnupg_stdout)<type ''instance''>>>> isinstance(gnupg_stdout, io.IOBase)False>>> gnupg_stdout.encodingTraceback (most recent call last): File "<stdin>", line 1, in <module> File "/usr/lib/python2.7/codecs.py", line 643, in __getattr__ return getattr(self.stream, name)AttributeError: ''_io.BytesIO'' object has no attribute ''encoding''
因此codecs
不提供替代的对象io.TextIOWrapper
。
该怎么办?
那么,如何通过测试倍数和真实对象编写适用于Python 2和Python 3的代码,这些对象 将已经打开的字节流
包裹起来io.TextIOWrapper
?
答案1
小编典典基于各种论坛上的多项建议,并尝试使用标准库来满足标准,我目前的结论是 这无法 像我们目前所拥有的 那样 使用库和类型 来完成 。
android – ContextWrapper.getResources()中的NullPointerException
07-05 21:13:30.063 7647-7647/root.fazerumsom E/AndroidRuntime﹕ FATAL EXCEPTION: main Process: root.fazerumsom,PID: 7647 java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{root.fazerumsom/root.fazerumsom.MainActivity}: java.lang.NullPointerException at android.app.ActivityThread.performlaunchActivity(ActivityThread.java:2110) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233) at android.app.ActivityThread.access$800(ActivityThread.java:135) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:136) at android.app.ActivityThread.main(ActivityThread.java:5001) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601) at dalvik.system.NativeStart.main(Native Method) Caused by: java.lang.NullPointerException at android.content.Contextwrapper.getResources(Contextwrapper.java:89) at android.view.ContextThemeWrapper.getResources(ContextThemeWrapper.java:78) at root.fazerumsom.MainActivity.<init>(MainActivity.java:31) at java.lang.class.newInstanceImpl(Native Method) at java.lang.class.newInstance(Class.java:1208) at android.app.Instrumentation.newActivity(Instrumentation.java:1061) at android.app.ActivityThread.performlaunchActivity(ActivityThread.java:2101) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233) at android.app.ActivityThread.access$800(ActivityThread.java:135) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:136) at android.app.ActivityThread.main(ActivityThread.java:5001) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601) at dalvik.system.NativeStart.main(Native Method)
解决方法
private String mStr = getResources().getString(...);
(或使用drawables或其他类型的资源).
这是无效的,因为尚未设置Context.在onCreate()方法中移动此赋值.
android – HTC设备中的InputConnectionWrapper.commitText StackOverflowError
在HTC D820U中调试,发现StackOverflowError.
设备:HTC D820u,Android 6.0,HTC版本:1.0.0.D820u
见日志:
01-11 10:55:33.101 11234-11234/com.xxx E/CrashReport:
java.lang.StackOverflowError: stack size 8MB
at
com.android.tools.profiler.support.event.InputConnectionWrapper.commitText(InputConnectionWrapper.java:42)
at
android.view.inputmethod.InputConnectionWrapper.commitText(InputConnectionWrapper.java:82)
at
com.android.tools.profiler.support.event.InputConnectionWrapper.commitText(InputConnectionWrapper.java:43)
at
android.view.inputmethod.InputConnectionWrapper.commitText(InputConnectionWrapper.java:82)
at
com.android.tools.profiler.support.event.InputConnectionWrapper.commitText(InputConnectionWrapper.java:43)
at
android.view.inputmethod.InputConnectionWrapper.commitText(InputConnectionWrapper.java:82)
at
com.android.tools.profiler.support.event.InputConnectionWrapper.commitText(InputConnectionWrapper.java:43)
at
android.view.inputmethod.InputConnectionWrapper.commitText(InputConnectionWrapper.java:82)
at
com.android.tools.profiler.support.event.InputConnectionWrapper.commitText(InputConnectionWrapper.java:43)
at
android.view.inputmethod.InputConnectionWrapper.commitText(InputConnectionWrapper.java:82)
at
com.android.tools.profiler.support.event.InputConnectionWrapper.commitText(InputConnectionWrapper.java:43)
at
android.view.inputmethod.InputConnectionWrapper.commitText(InputConnectionWrapper.java:82)
at
com.android.tools.profiler.support.event.InputConnectionWrapper.commitText(InputConnectionWrapper.java:43)
at
android.view.inputmethod.InputConnectionWrapper.commitText(InputConnectionWrapper.java:82)
at
com.android.tools.profiler.support.event.InputConnectionWrapper.commitText(InputConnectionWrapper.java:43)
at
android.view.inputmethod.InputConnectionWrapper.commitText(InputConnectionWrapper.java:82)
at
com.android.tools.profiler.support.event.InputConnectionWrapper.commitText(InputConnectionWrapper.java:43)
at
android.view.inputmethod.InputConnectionWrapper.commitText(InputConnectionWrapper.java:82)
at
com.android.tools.profiler.support.event.InputConnectionWrapper.commitText(InputConnectionWrapper.java:43)
at
android.view.inputmethod.InputConnectionWrapper.commitText(InputConnectionWrapper.java:82)
at
com.android.tools.profiler.support.event.InputConnectionWrapper.commitText(InputConnectionWrapper.java:43)
at
android.view.inputmethod.InputConnectionWrapper.commitText(InputConnectionWrapper.java:82)
at
com.android.tools.profiler.support.event.InputConnectionWrapper.commitText(InputConnectionWrapper.java:43)
at
android.view.inputmethod.InputConnectionWrapper.commitText(InputConnectionWrapper.java:82)
at
com.android.tools.profiler.support.event.InputConnectionWrapper.commitText(InputConnectionWrapper.java:43)
at
android.view.inputmethod.InputConnectionWrapper.commitText(InputConnectionWrapper.java:82)
at
com.android.tools.profiler.support.event.InputConnectionWrapper.commitText(InputConnectionWrapper.java:43)
at
android.view.inputmethod.InputConnectionWrapper.commitText(InputConnectionWrapper.java:82)
at
com.android.tools.profiler.support.event.InputConnectionWrapper.commitText(InputConnectionWrapper.java:43)
at
android.view.inputmethod.InputConnectionWrapper.commitText(InputConnectionWrapper.java:82)
at
com.android.tools.profiler.support.event.InputConnectionWrapper.commitText(InputConnectionWrapper.java:43)
at
android.view.inputmethod.InputConnectionWrapper.commitText(InputConnectionWrapper.java:82)
at
com.android.tools.profiler.support.event.InputConnectionWrapper.commitText(InputConnectionWrapper.java:43)
at
android.view.inputmethod.InputConnectionWrapper.commitText(InputConnectionWrapper.java:82)
at
com.android.tools.profiler.support.event.InputConnectionWrapper.commitText(InputConnectionWrapper.java:43)
at
android.view.inputmethod.InputConnectionWrapper.commitText(InputConnectionWrapper.java:82)
at
com.android.tools.profiler.support.event.InputConnectionWrapper.commitText(InputConnectionWrapper.java:43)
at
android.view.inputmethod.InputConnectionWrapper.commitText(InputConnectionWrapper.java:82)
at
com.android.tools.profiler.support.event.InputConnectionWrapper.commitText(InputConnectionWrapper.j
01-11 10:55:33.101 11234-11234/com.xxx E/CrashReport:++++++++++++++++++++++++++++++++++++++++++
关于日志,关于我的应用程序没有崩溃.
这是Android的错误吗?还是HTC Rom的bug?
解决方法:
我得到了相同的错误Nexus 5.看起来像是Android分析器错误.您可以通过在运行配置中禁用高级分析来修复它:
更新:根据issue,这应该在Android Studio 3.1 beta 1中修复.
更新2:@ilyamuromets确认要在AS 3.1.1中解决的问题
android – 如何使用InputConnectionWrapper?
我搜索了一下,发现了一个名为InputConnectionWrapper的有趣类.根据javadoc,它将作为给定InputConnection的代理.所以我将其子类化为:
private class EditTextInputConnection extends InputConnectionWrapper { public EditTextInputConnection(InputConnection target,boolean mutable) { super(target,mutable); } @Override public boolean commitText(CharSequence text,int newCursorPosition) { // some code which takes the input and manipulates it and calls editText.getText().replace() afterwards return true; } }
为了初始化包装器,我在EditText子类中覆盖了以下方法:
public InputConnection onCreateInputConnection(EditorInfo outAttrs) { InputConnection con = super.onCreateInputConnection(outAttrs); EditTextInputConnection connectionWrapper = new EditTextInputConnection(con,true); return connectionWrapper; }
但是,commitText()永远不会被调用.当我在字段中输入一些文本时,onCreateInputConnection()也被调用,并且EditTextInputConnection的构造函数也是,但从不是commitText(),尽管它应该是.至少,这就是我理解InputConnectionWrapper的用法.或者我错了?
编辑:似乎,commitText()只调用特殊字符,如“.”,“”等.据我了解所有其他字符的Android源代码应该调用InputConnectionWrapper.sendKeyEvent(),但事实并非如此.我完全陷入困境.我已经尝试过EditText.onKeyPreIme(),但这仅适用于硬件键盘.所以这是别无选择……我真的不明白,为什么Android会处理与硬件键盘不同的软键盘.
EditText.onTextChanged()也会在非用户输入上触发,所以这也不是,我正在寻找.
解决方法
AttributeError: '_io.TextIOWrapper' 对象没有属性 'fp'
如何解决AttributeError: ''_io.TextIOWrapper'' 对象没有属性 ''fp''?
当我尝试将 txt 文件发送到 discord 网络钩子时出现此错误。 (python3)
import os
import subprocess
import requests
import discord
import dhooks
from dhooks import Webhook,Embed
hook = Webhook("https://discord.com/api/webhooks/816226588005367849/C_qZ-zGf80vucvO6RBQbL9tQaECdhaCwSp8im5ZkCotwEPzdwqgkYm1jmrfhlcafBhPI")
discord_txt = open("data.txt","r+")
hook.send(file=discord_txt)
解决方法
根据 the docs,您应该使用 File
中的 dhooks
类:
from dhooks import Webhook,File
hook = Webhook("https://discord.com/api/webhooks/...")
Discord_txt = File("data.txt")
hook.send(file=Discord_txt)
今天关于使用io.TextIOWrapper包装一个开放流的介绍到此结束,谢谢您的阅读,有关android – ContextWrapper.getResources()中的NullPointerException、android – HTC设备中的InputConnectionWrapper.commitText StackOverflowError、android – 如何使用InputConnectionWrapper?、AttributeError: '_io.TextIOWrapper' 对象没有属性 'fp'等更多相关知识的信息可以在本站进行查询。
本文标签: