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
fb0c7cad
Commit
fb0c7cad
authored
Feb 24, 2007
by
Georg Brandl
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Make bytes_repr return a string containing a b"" literal.
parent
1c08cd90
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
59 additions
and
59 deletions
+59
-59
Include/Python-ast.h
Include/Python-ast.h
+0
-1
Lib/test/test_bytes.py
Lib/test/test_bytes.py
+5
-3
Objects/bytesobject.c
Objects/bytesobject.c
+52
-54
Python/symtable.c
Python/symtable.c
+2
-1
No files found.
Include/Python-ast.h
View file @
fb0c7cad
...
...
@@ -516,4 +516,3 @@ keyword_ty _Py_keyword(identifier arg, expr_ty value, PyArena *arena);
alias_ty
_Py_alias
(
identifier
name
,
identifier
asname
,
PyArena
*
arena
);
PyObject
*
PyAST_mod2obj
(
mod_ty
t
);
Lib/test/test_bytes.py
View file @
fb0c7cad
...
...
@@ -69,9 +69,11 @@ class BytesTest(unittest.TestCase):
self
.
assertRaises
(
ValueError
,
bytes
,
[
10
**
100
])
def
test_repr
(
self
):
self
.
assertEqual
(
repr
(
bytes
()),
"bytes()"
)
self
.
assertEqual
(
repr
(
bytes
([
0
])),
"bytes([0x00])"
)
self
.
assertEqual
(
repr
(
bytes
([
0
,
1
,
254
,
255
])),
"bytes([0x00, 0x01, 0xfe, 0xff])"
)
self
.
assertEqual
(
repr
(
bytes
()),
"b''"
)
self
.
assertEqual
(
repr
(
bytes
([
0
])),
"b'
\
\
0'"
)
self
.
assertEqual
(
repr
(
bytes
([
0
,
1
,
254
,
255
])),
"b'
\
\
0
\
\
x01
\
\
xfe
\
\
xff'"
)
self
.
assertEqual
(
repr
(
bytes
(
'abc'
)),
"b'abc'"
)
self
.
assertEqual
(
repr
(
bytes
(
"'"
)),
"b'
\
\
''"
)
def
test_compare
(
self
):
b1
=
bytes
([
1
,
2
,
3
])
...
...
Objects/bytesobject.c
View file @
fb0c7cad
...
...
@@ -733,65 +733,63 @@ bytes_init(PyBytesObject *self, PyObject *args, PyObject *kwds)
return
-
1
;
}
/* Mostly copied from string_repr, but without the
"smart quote" functionality. */
static
PyObject
*
bytes_repr
(
PyBytesObject
*
self
)
{
PyObject
*
list
;
PyObject
*
str
;
PyObject
*
result
;
int
err
;
int
i
;
if
(
self
->
ob_size
==
0
)
return
PyString_FromString
(
"bytes()"
);
list
=
PyList_New
(
0
);
if
(
list
==
NULL
)
size_t
newsize
=
3
+
4
*
self
->
ob_size
;
PyObject
*
v
;
if
(
newsize
>
PY_SSIZE_T_MAX
||
newsize
/
4
!=
self
->
ob_size
)
{
PyErr_SetString
(
PyExc_OverflowError
,
"bytes object is too large to make repr"
);
return
NULL
;
str
=
PyString_FromString
(
"bytes(["
);
if
(
str
==
NULL
)
goto
error
;
err
=
PyList_Append
(
list
,
str
);
Py_DECREF
(
str
);
if
(
err
<
0
)
goto
error
;
for
(
i
=
0
;
i
<
self
->
ob_size
;
i
++
)
{
char
buffer
[
20
];
sprintf
(
buffer
,
", 0x%02x"
,
(
unsigned
char
)
(
self
->
ob_bytes
[
i
]));
str
=
PyString_FromString
((
i
==
0
)
?
buffer
+
2
:
buffer
);
if
(
str
==
NULL
)
goto
error
;
err
=
PyList_Append
(
list
,
str
);
Py_DECREF
(
str
);
if
(
err
<
0
)
goto
error
;
}
str
=
PyString_FromString
(
"])"
);
if
(
str
==
NULL
)
goto
error
;
err
=
PyList_Append
(
list
,
str
);
Py_DECREF
(
str
);
if
(
err
<
0
)
goto
error
;
str
=
PyString_FromString
(
""
);
if
(
str
==
NULL
)
goto
error
;
result
=
_PyString_Join
(
str
,
list
);
Py_DECREF
(
str
);
Py_DECREF
(
list
);
return
result
;
error:
/* Error handling when list != NULL */
Py_DECREF
(
list
);
return
NULL
;
v
=
PyString_FromStringAndSize
((
char
*
)
NULL
,
newsize
);
if
(
v
==
NULL
)
{
return
NULL
;
}
else
{
register
Py_ssize_t
i
;
register
char
c
;
register
char
*
p
;
int
quote
=
'\''
;
p
=
PyString_AS_STRING
(
v
);
*
p
++
=
'b'
;
*
p
++
=
quote
;
for
(
i
=
0
;
i
<
self
->
ob_size
;
i
++
)
{
/* There's at least enough room for a hex escape
and a closing quote. */
assert
(
newsize
-
(
p
-
PyString_AS_STRING
(
v
))
>=
5
);
c
=
self
->
ob_bytes
[
i
];
if
(
c
==
quote
||
c
==
'\\'
)
*
p
++
=
'\\'
,
*
p
++
=
c
;
else
if
(
c
==
'\t'
)
*
p
++
=
'\\'
,
*
p
++
=
't'
;
else
if
(
c
==
'\n'
)
*
p
++
=
'\\'
,
*
p
++
=
'n'
;
else
if
(
c
==
'\r'
)
*
p
++
=
'\\'
,
*
p
++
=
'r'
;
else
if
(
c
==
0
)
*
p
++
=
'\\'
,
*
p
++
=
'0'
;
else
if
(
c
<
' '
||
c
>=
0x7f
)
{
/* For performance, we don't want to call
PyOS_snprintf here (extra layers of
function call). */
sprintf
(
p
,
"
\\
x%02x"
,
c
&
0xff
);
p
+=
4
;
}
else
*
p
++
=
c
;
}
assert
(
newsize
-
(
p
-
PyString_AS_STRING
(
v
))
>=
1
);
*
p
++
=
quote
;
*
p
=
'\0'
;
_PyString_Resize
(
&
v
,
(
p
-
PyString_AS_STRING
(
v
)));
return
v
;
}
}
static
PyObject
*
...
...
Python/symtable.c
View file @
fb0c7cad
...
...
@@ -1177,7 +1177,8 @@ symtable_visit_expr(struct symtable *st, expr_ty e)
break
;
case
Num_kind
:
case
Str_kind
:
case
Ellipsis_kind
:
case
Bytes_kind
:
case
Ellipsis_kind
:
/* Nothing to do here. */
break
;
/* The following exprs can be assignment targets. */
...
...
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