Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
06051edc
Commit
06051edc
authored
Jul 13, 2000
by
Andrew M. Kuchling
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added PyObject_AsFileDescriptor, which checks for integer, long integer,
or .fileno() method
parent
9656abd9
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
59 additions
and
0 deletions
+59
-0
Include/fileobject.h
Include/fileobject.h
+1
-0
Objects/fileobject.c
Objects/fileobject.c
+58
-0
No files found.
Include/fileobject.h
View file @
06051edc
...
...
@@ -30,6 +30,7 @@ extern DL_IMPORT(PyObject *) PyFile_GetLine(PyObject *, int);
extern
DL_IMPORT
(
int
)
PyFile_WriteObject
(
PyObject
*
,
PyObject
*
,
int
);
extern
DL_IMPORT
(
int
)
PyFile_SoftSpace
(
PyObject
*
,
int
);
extern
DL_IMPORT
(
int
)
PyFile_WriteString
(
char
*
,
PyObject
*
);
extern
DL_IMPORT
(
int
)
PyObject_AsFileDescriptor
(
PyObject
*
);
#ifdef __cplusplus
}
...
...
Objects/fileobject.c
View file @
06051edc
...
...
@@ -1098,3 +1098,61 @@ PyFile_WriteString(char *s, PyObject *f)
else
return
-
1
;
}
/* Try to get a file-descriptor from a Python object. If the object
is an integer or long integer, its value is returned. If not, the
object's fileno() method is called if it exists; the method must return
an integer or long integer, which is returned as the file descriptor value.
-1 is returned on failure.
*/
int
PyObject_AsFileDescriptor
(
PyObject
*
o
)
{
int
fd
;
PyObject
*
meth
;
if
(
PyInt_Check
(
o
))
{
fd
=
PyInt_AsLong
(
o
);
}
else
if
(
PyLong_Check
(
o
))
{
fd
=
PyLong_AsLong
(
o
);
}
else
if
((
meth
=
PyObject_GetAttrString
(
o
,
"fileno"
))
!=
NULL
)
{
PyObject
*
fno
=
PyEval_CallObject
(
meth
,
NULL
);
Py_DECREF
(
meth
);
if
(
fno
==
NULL
)
return
-
1
;
if
(
PyInt_Check
(
fno
))
{
fd
=
PyInt_AsLong
(
fno
);
Py_DECREF
(
fno
);
}
else
if
(
PyLong_Check
(
fno
))
{
fd
=
PyLong_AsLong
(
fno
);
Py_DECREF
(
fno
);
}
else
{
PyErr_SetString
(
PyExc_TypeError
,
"fileno() returned a non-integer"
);
Py_DECREF
(
fno
);
return
-
1
;
}
}
else
{
PyErr_SetString
(
PyExc_TypeError
,
"argument must be an int, or have a fileno() method."
);
return
-
1
;
}
if
(
fd
<
0
)
{
PyErr_Format
(
PyExc_ValueError
,
"file descriptor cannot be a negative integer (%i)"
,
fd
);
return
-
1
;
}
return
fd
;
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment