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
53756b10
Commit
53756b10
authored
Jan 03, 1997
by
Guido van Rossum
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added PyLong_FromUnsignedLong() and PyLong_AsUnsignedLong().
parent
461a1c17
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
58 additions
and
0 deletions
+58
-0
Include/longobject.h
Include/longobject.h
+2
-0
Objects/longobject.c
Objects/longobject.c
+56
-0
No files found.
Include/longobject.h
View file @
53756b10
...
...
@@ -44,8 +44,10 @@ extern DL_IMPORT(PyTypeObject) PyLong_Type;
#define PyLong_Check(op) ((op)->ob_type == &PyLong_Type)
extern
PyObject
*
PyLong_FromLong
Py_PROTO
((
long
));
extern
PyObject
*
PyLong_FromUnsignedLong
Py_PROTO
((
unsigned
long
));
extern
PyObject
*
PyLong_FromDouble
Py_PROTO
((
double
));
extern
long
PyLong_AsLong
Py_PROTO
((
PyObject
*
));
extern
unsigned
long
PyLong_AsUnsignedLong
Py_PROTO
((
PyObject
*
));
extern
double
PyLong_AsDouble
Py_PROTO
((
PyObject
*
));
PyObject
*
PyLong_FromString
Py_PROTO
((
char
*
,
char
**
,
int
));
...
...
Objects/longobject.c
View file @
53756b10
...
...
@@ -109,6 +109,27 @@ newlongobject(ival)
return
(
object
*
)
v
;
}
/* Create a new long int object from a C unsigned long int */
object
*
PyLong_FromUnsignedLong
(
ival
)
unsigned
long
ival
;
{
/* Assume a C long fits in at most 5 'digits' */
/* Works on both 32- and 64-bit machines */
longobject
*
v
=
alloclongobject
(
5
);
if
(
v
!=
NULL
)
{
unsigned
long
t
=
ival
;
int
i
;
for
(
i
=
0
;
i
<
5
;
i
++
)
{
v
->
ob_digit
[
i
]
=
t
&
MASK
;
t
>>=
SHIFT
;
}
v
=
long_normalize
(
v
);
}
return
(
object
*
)
v
;
}
/* Create a new long int object from a C double */
object
*
...
...
@@ -181,6 +202,41 @@ getlongvalue(vv)
return
x
*
sign
;
}
/* Get a C long int from a long int object.
Returns -1 and sets an error condition if overflow occurs. */
unsigned
long
PyLong_AsUnsignedLong
(
vv
)
object
*
vv
;
{
register
longobject
*
v
;
unsigned
long
x
,
prev
;
int
i
;
if
(
vv
==
NULL
||
!
is_longobject
(
vv
))
{
err_badcall
();
return
(
unsigned
long
)
-
1
;
}
v
=
(
longobject
*
)
vv
;
i
=
v
->
ob_size
;
x
=
0
;
if
(
i
<
0
)
{
err_setstr
(
OverflowError
,
"can't convert negative value to unsigned long"
);
return
(
unsigned
long
)
-
1
;
}
while
(
--
i
>=
0
)
{
prev
=
x
;
x
=
(
x
<<
SHIFT
)
+
v
->
ob_digit
[
i
];
if
((
x
>>
SHIFT
)
!=
prev
)
{
err_setstr
(
OverflowError
,
"long int too long to convert"
);
return
(
unsigned
long
)
-
1
;
}
}
return
x
;
}
/* Get a C double from a long int object. No overflow check. */
double
...
...
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