Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Need a way to make my own bytes #48838

Closed
lopgok mannequin opened this issue Dec 8, 2008 · 4 comments
Closed

Need a way to make my own bytes #48838

lopgok mannequin opened this issue Dec 8, 2008 · 4 comments
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) type-crash A hard crash of the interpreter, possibly with a core dump

Comments

@lopgok
Copy link
Mannequin

lopgok mannequin commented Dec 8, 2008

BPO 4588
Nosy @loewis, @benjaminp

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = <Date 2008-12-08.04:14:37.892>
created_at = <Date 2008-12-08.03:29:29.786>
labels = ['interpreter-core', 'invalid', 'type-crash']
title = 'Need a way to make my own bytes'
updated_at = <Date 2008-12-08.05:08:18.330>
user = 'https://bugs.python.org/lopgok'

bugs.python.org fields:

activity = <Date 2008-12-08.05:08:18.330>
actor = 'loewis'
assignee = 'none'
closed = True
closed_date = <Date 2008-12-08.04:14:37.892>
closer = 'benjamin.peterson'
components = ['Interpreter Core']
creation = <Date 2008-12-08.03:29:29.786>
creator = 'lopgok'
dependencies = []
files = []
hgrepos = []
issue_num = 4588
keywords = []
message_count = 4.0
messages = ['77286', '77287', '77288', '77289']
nosy_count = 3.0
nosy_names = ['loewis', 'benjamin.peterson', 'lopgok']
pr_nums = []
priority = 'normal'
resolution = 'not a bug'
stage = None
status = 'closed'
superseder = None
type = 'crash'
url = 'https://bugs.python.org/issue4588'
versions = ['Python 3.0']

@lopgok
Copy link
Mannequin Author

lopgok mannequin commented Dec 8, 2008

I want to make my own data of types bytes in order to write it out.
For example, I want to write out the bytes 0..9

#!/usr/bin/env python3.0
foo = b''
for i in range (0,10):
    foo += i
#sys.stdout.buffer.write(foo)
Here is the error:
Traceback (most recent call last):
  File "./x.py", line 4, in <module>
    foo += i
TypeError: can't concat bytes to int

I cannot find any function to convert the int i into something
that I can append to foo. I tried chr, which produced a string
typeerror. byte() was not defined. There must be a way to convert
an integral value to a bytes type.

@lopgok lopgok mannequin added interpreter-core (Objects, Python, Grammar, and Parser dirs) type-crash A hard crash of the interpreter, possibly with a core dump labels Dec 8, 2008
@benjaminp
Copy link
Contributor

bytes([some_number]) should do the trick.

@lopgok
Copy link
Mannequin Author

lopgok mannequin commented Dec 8, 2008

Doesn't work.
#!/usr/bin/env python3.0

import sys
foo = b''

for i in range (0,10):
    foo += bytes(i)

sys.stdout.buffer.write(foo)

produces a binary file of 45 bytes. Here is a hex dump (the '.'
represent unprintable characters):
+000 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
+016 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
+032 00 00 00 00 00 00 00 00 00 00 00 00 00 .............

that is 45 bytes of 0.

@loewis
Copy link
Mannequin

loewis mannequin commented Dec 8, 2008

Please don't use the bug tracker to obtain help, but only to report
bugs. Use python-list@python.org to get help.

In this specific case, also read Benjamin's answer more carefully.

@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) type-crash A hard crash of the interpreter, possibly with a core dump
Projects
None yet
Development

No branches or pull requests

1 participant