aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/deprecated/python/win-unicode-console/win_unicode_console/file_object.py
blob: 3c9b56ccb42d2e36734486a8c94dbf2362deed0d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
from .info import check_PY2
check_PY2()

import ctypes
from ctypes import (byref, pythonapi, 
	c_int, c_char_p, c_void_p, py_object, c_ssize_t)


class FileObject(ctypes.Structure):
	_fields_ = [
		#("_ob_next", c_void_p),
		#("_ob_prev", c_void_p),
		("ob_refcnt", c_ssize_t),
		("ob_type", c_void_p),
		
		("fp", c_void_p),
		("name", py_object),
		("mode", py_object),
		("close", c_void_p),
		("softspace", c_int),
		("binary", c_int),
		("buf", c_char_p),
		("bufend", c_char_p),
		("bufptr", c_char_p),
		("setbuf", c_char_p),
		("univ_newline", c_int),
		("newlinetypes", c_int),
		("skipnextlf", c_int),
		("encoding", py_object),
		("errors", py_object),
		("weakreflist", py_object),
		("unlocked_count", c_int),
		("readable", c_int),
		("writable", c_int),
	]
	
	@classmethod
	def from_file(cls, f):
		if not isinstance(f, file):
			raise TypeError("f has to be a file")
		
		return cls.from_address(id(f))
	
	def set_encoding(self, encoding):
		if not isinstance(encoding, str):
			raise TypeError("encoding has to be a str")
		
		pythonapi.PyFile_SetEncoding(byref(self), encoding)
	
	def copy_file_pointer(self, f):
		if not isinstance(f, file):
			raise TypeError("f has to be a file")
		
		self.fp = pythonapi.PyFile_AsFile(py_object(f))